forked from cookiecutter-flask/cookiecutter-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
102 lines (77 loc) · 2.63 KB
/
tasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""Invoke tasks."""
import os
import shutil
from typing import Iterator
# isort: off
# Temporary monkeypatch; see https://github.com/pyinvoke/invoke/issues/833
import inspect
if not hasattr(inspect, "getargspec"):
inspect.getargspec = inspect.getfullargspec
from invoke import task
# isort: on
HERE = os.path.abspath(os.path.dirname(__file__))
DEFAULT_APP_NAME = "my_flask_app"
COOKIE = os.path.join(HERE, DEFAULT_APP_NAME)
REQUIREMENTS = os.path.join(COOKIE, "requirements", "dev.txt")
def _run_npm_command(ctx, command):
os.chdir(COOKIE)
ctx.run(f"npm {command}", echo=True)
os.chdir(HERE)
def _run_flask_command(ctx, command, *args):
os.chdir(COOKIE)
flask_command = f"flask {command}"
if args:
flask_command += f" {' '.join(args)}"
ctx.run(flask_command, echo=True)
@task
def build(ctx):
"""Build the cookiecutter."""
ctx.run(f"python cookiecutter_spec.py {HERE} --no-input")
@task(pre=[build])
def build_install(ctx):
"""Build the cookiecutter."""
_run_npm_command(ctx, "install")
ctx.run(f"pip install -r {REQUIREMENTS} --ignore-installed", echo=True)
@task
def clean(ctx):
"""Clean out generated cookiecutter."""
if os.path.exists(COOKIE):
shutil.rmtree(COOKIE)
@task(pre=[clean, build_install])
def lint(ctx):
"""Run lint commands."""
_run_npm_command(ctx, "run lint")
os.chdir(COOKIE)
os.environ["FLASK_ENV"] = "production"
os.environ["FLASK_DEBUG"] = "0"
_run_flask_command(ctx, "lint", "--check")
@task(pre=[clean, build_install])
def test(ctx):
"""Run tests."""
os.chdir(COOKIE)
os.environ["FLASK_ENV"] = "production"
os.environ["FLASK_DEBUG"] = "0"
_run_flask_command(ctx, "test")
def _walk_template_files() -> Iterator[str]:
template_dir = os.path.join(HERE, "{{cookiecutter.app_name}}")
for root, _, template_files in os.walk(template_dir):
for template_file in template_files:
yield os.path.join(root, template_file)
@task
def no_placeholders(ctx):
"""Check that default project name hasn't been committed to template dir"""
for template_file in _walk_template_files():
try:
with open(template_file, "r") as f:
if DEFAULT_APP_NAME in f.read():
raise ValueError(
f"Template cannot contain default app name, but {DEFAULT_APP_NAME} found in {f.name}"
)
except UnicodeDecodeError:
pass
@task(pre=[clean, build])
def test_image_build(ctx):
"""Run tests."""
os.chdir(COOKIE)
os.environ["DOCKER_BUILDKIT"] = "1"
ctx.run("docker compose build flask-dev", echo=True)