# django_zero (see github.com/python-medikit)

from medikit import listen, require
from medikit.feature.make import which

PACKAGE = "django_zero"

require("git")
require("format")

COMMON_REQUIREMENTS = [
    "django ~=3.2,<3.3",
]

with require("python") as python:
    python.setup(
        name=PACKAGE,
        description="Zero-configuration django projects.",
        license="Apache License, Version 2.0",
        url="https://github.com/hartym/django-zero",
        download_url="https://github.com/hartym/django-zero/archive/{version}.tar.gz",
        author="Romain Dorgueil",
        author_email="romain@dorgueil.net",
        entry_points={"console_scripts": ["django-zero = django_zero.commands:main"]},
    )

    python.add_requirements(
        *COMMON_REQUIREMENTS,
        "brotli ~=1.0.9",
        "django-allauth ~=0.47.0",
        "jinja2 ~=3.0.3",
        "mondrian ~=0.8",
        "whitenoise ~=5.3.0",
        # Celery
        celery=[
            *COMMON_REQUIREMENTS,
            "celery ~=5.0",  # waiting for django_celery_results version bump to allow upgrade to celery 5
            "django_celery_beat ~=2.2.1",
            "django_celery_results ~=2.2.0",  # see https://github.com/celery/django-celery-results/issues/173
        ],
        # Channels
        channels=[
            *COMMON_REQUIREMENTS,
            "channels ~=3.0.0",
            "daphne ~=3.0.0",
        ],
        # Dev-only tooling
        dev=[
            *COMMON_REQUIREMENTS,
            "cookiecutter ~=1.7",
            "django-extensions ~=3.1",
            "django_debug_toolbar ~=3.2",
            "honcho ~=1.0",
            "medikit ~=0.7",
            "werkzeug ~=2.0",
        ],
        # Production tools
        prod=[*COMMON_REQUIREMENTS, "gunicorn ~=20.0"],
    )

with require("nodejs") as nodejs:
    nodejs.setup(base_dir=PACKAGE).add_dependencies(
        {
            "@babel/cli": "^7.0.0",
            "@babel/core": "^7.0.0",
            "@babel/polyfill": "^7.0.0",
            "@babel/preset-env": "^7.0.0",
            "assets-webpack-plugin": "^3.5.1",
            "autoprefixer": "^9.1.5",
            "babel-loader": "^8.0.0",
            "bootstrap": "^4.1.0",
            "css-loader": "3.4.0",
            "file-loader": "5.0.2",
            "jquery": "1.9.1 - 3",
            "mini-css-extract-plugin": "0.9.0",
            "node-sass": "^4.0.0",
            "popper.js": "^1.12.9",
            "postcss-loader": "^3.0.0",
            "precss": "4.0.0",
            "resolve-url-loader": "3.1.1",
            "sass-loader": "8.0.0",
            "style-loader": "1.1.2",
            "webpack": "^4.0.0",
        }
    )

with require("pytest") as pytest:
    pytest.set_version(">=5.4.0")
    pytest.addons = {
        "coverage": "~=6.2",
        "pytest-cov": "~=3.0.0",
        "pyquery": "~=1.4",
        "pytest-django": "~=4.0",
    }

with require("make") as make:

    @listen(make.on_generate)
    def on_make_generate(event):
        event.makefile.add_target(
            "testrun",
            """
                $(eval TMPDIR := $(shell mktemp -d))
                (cd $(TMPDIR); $(PYTHON) -m virtualenv -p $(PYTHON) env)
                $(TMPDIR)/env/bin/pip install .[dev]
                $(eval ZERO := $(TMPDIR)/env/bin/django-zero)
                (cd $(TMPDIR); $(ZERO) create --no-input project acme)
                (cd $(TMPDIR)/acme; $(ZERO) install)
                (cd $(TMPDIR)/acme; $(ZERO) manage migrate)
                (cd $(TMPDIR)/acme; make start)
                trap 'rm -rf "$(TMPDIR)"' EXIT; (cd $(TMPDIR)/acme; $(ZERO) start)
            """,
            phony=True,
        )

    # Pipelines
    @listen(make.on_generate)
    def on_make_generate_pipelines(event):
        # Releases
        event.makefile.add_target(
            "release",
            """
                python -c 'import medikit; print(medikit.__version__)' || pip install medikit;
                $(PYTHON) -m medikit pipeline release start
            """,
            phony=True,
            doc="Releases django-zero.",
        )

    # Documentation
    @listen(make.on_generate)
    def on_make_generate_documentation(event):
        makefile = event.makefile

        makefile["VUEPRESS"] = which("vuepress")
        makefile["VUEPRESS_SOURCEDIR"] = "docs"

        makefile.add_target("docs", "$(VUEPRESS) dev docs", phony=True)
        makefile.add_target("docs-dist", "$(VUEPRESS) build docs", phony=True)
        makefile.add_target(
            "docs-deploy",
            " && ".join(
                [
                    "cd docs/.vuepress/dist",
                    "git init",
                    "git add -A",
                    'git commit -m "Build vuepress documentation."',
                    "git push -f git@github.com:django-zero/django-zero.github.io.git master",
                ]
            ),
            deps=["docs-dist"],
            phony=True,
        )

    # Formatting
    @listen(make.on_generate)
    def on_make_generate_format(event):
        makefile = event.makefile
        makefile[
            "BLACK_OPTIONS"
        ] += " --exclude '/(\.git|\.hg|\.mypy_cache|\.tox|\.venv|\.release|_build|buck-out|build|dist|node_modules|{{\"{{cookiecutter.name}}\"}})/'"

        makefile["ISORT_OPTIONS"] += " -s node_modules"


# vim: ft=python:
