from invoke import task


@task
def runserver(ctx, pty=True):
    """Run django dev server."""
    ctx.run('python manage.py runserver', pty=pty)


@task
def build(ctx):
    """Build docs, static-files, etc."""
    ctx.run("sphinx-build docs docs/_build")
    ctx.run("npm run build-static")


@task
def test(ctx, coverage=True, pty=True):
    """Run automated tests and code quality checks."""
    test_cmd = 'pytest'
    test_cmd += ' --cov=scrappyr --cov-report term-missing' if coverage else ''

    print_header('Automated tests')
    ctx.run(test_cmd, pty=pty)

    print_header('Django check')
    ctx.run('python manage.py check', pty=pty)

    print_header('Flake8')
    ctx.run('flake8', pty=pty)


@task
def createapp(ctx, app):
    ctx.run('mkdir scrappyr/{}'.format(app))
    ctx.run('python manage.py startapp {0} scrappyr/{0}'.format(app))


@task
def clean(ctx, docs=True, bytecode=True, static_files=True, extra=''):
    """Remove build files.

    This command removes:
        - Documentation built by Sphinx
        - *.pyc files
    """
    patterns = ['build']
    if docs:
        patterns.append('docs/_build')
    if bytecode:
        patterns.append('**/*.pyc')
        patterns.append('**/__pycache__/*')
    if static_files:
        patterns.append('scrappyr/static/webpack_bundles/')
    if extra:
        patterns.append(extra)
    for pattern in patterns:
        ctx.run('rm -rf {0}'.format(pattern))


@task
def serve_static(ctx, pty=True):
    """Run webpack server to build static files when files are changed."""
    ctx.run('npm run watch', pty=pty)


@task
def graph_models(ctx):
    """Create ./scrappyr-models.pdf file displaying visualization of Django models"""
    ctx.run('python manage.py graph_models -ag -o scrappyr-models.pdf')


def print_header(title):
    print()
    print(title)
    print('-' * len(title))

from invoke import task


@task
def runserver(ctx, pty=True):
    """Run django dev server."""
    ctx.run('python manage.py runserver', pty=pty)


@task
def build(ctx):
    """Build docs, static-files, etc."""
    ctx.run("sphinx-build docs docs/_build")
    ctx.run("npm run build-static")


@task
def test(ctx, coverage=True, pty=True):
    """Run automated tests and code quality checks."""
    test_cmd = 'pytest'
    test_cmd += ' --cov=scrappyr --cov-report term-missing' if coverage else ''

    print_header('Automated tests')
    ctx.run(test_cmd, pty=pty)

    print_header('Django check')
    ctx.run('python manage.py check', pty=pty)

    print_header('Flake8')
    ctx.run('flake8', pty=pty)


@task
def createapp(ctx, app):
    ctx.run('mkdir scrappyr/{}'.format(app))
    ctx.run('python manage.py startapp {0} scrappyr/{0}'.format(app))


@task
def clean(ctx, docs=True, bytecode=True, static_files=True, extra=''):
    """Remove build files.

    This command removes:
        - Documentation built by Sphinx
        - *.pyc files
    """
    patterns = ['build']
    if docs:
        patterns.append('docs/_build')
    if bytecode:
        patterns.append('**/*.pyc')
        patterns.append('`find . -name __pycache__ -type d`')
    if static_files:
        patterns.append('scrappyr/static/webpack_bundles/')
    if extra:
        patterns.append(extra)
    for pattern in patterns:
        ctx.run('rm -rf {0}'.format(pattern))


@task
def serve_static(ctx, pty=True):
    """Run webpack server to build static files when files are changed."""
    ctx.run('npm run watch', pty=pty)


@task
def graph_models(ctx):
    """Create ./scrappyr-models.pdf file displaying visualization of Django models"""
    ctx.run('python manage.py graph_models -ag -o scrappyr-models.pdf')


def print_header(title):
    print()
    print(title)
    print('-' * len(title))

Fix deletion of __pycache__ in clean up task