forked from cookiecutter-flask/cookiecutter-flask
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
69 lines (55 loc) · 1.92 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""Invoke tasks."""
import json
import os
import shutil
import webbrowser
from invoke import task
HERE = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(HERE, "cookiecutter.json"), "r") as fp:
COOKIECUTTER_SETTINGS = json.load(fp)
# Match default value of app_name from cookiecutter.json
COOKIECUTTER_SETTINGS["app_name"] = "my_flask_app"
COOKIE = os.path.join(HERE, COOKIECUTTER_SETTINGS["app_name"])
REQUIREMENTS = os.path.join(COOKIE, "requirements", "dev.txt")
def _run_npm_command(ctx, command):
os.chdir(COOKIE)
ctx.run("npm {0}".format(command), echo=True)
os.chdir(HERE)
@task
def build(ctx):
"""Build the cookiecutter."""
ctx.run("cookiecutter {0} --no-input".format(HERE))
_run_npm_command(ctx, "install")
_run_npm_command(ctx, "run build")
@task
def clean(ctx):
"""Clean out generated cookiecutter."""
if os.path.exists(COOKIE):
shutil.rmtree(COOKIE)
print("Removed {0}".format(COOKIE))
else:
print("App directory does not exist. Skipping.")
def _run_flask_command(ctx, command, *args):
os.chdir(COOKIE)
flask_command = "flask {0}".format(command)
if args:
flask_command = "{0} {1}".format(flask_command, " ".join(args))
ctx.run(flask_command, echo=True)
@task(pre=[clean, build])
def test(ctx):
"""Run lint commands and tests."""
ctx.run("pip install -r {0} --ignore-installed".format(REQUIREMENTS), echo=True)
_run_npm_command(ctx, "run lint")
os.chdir(COOKIE)
shutil.copyfile(os.path.join(COOKIE, ".env.example"), os.path.join(COOKIE, ".env"))
os.environ["FLASK_ENV"] = "production"
os.environ["FLASK_DEBUG"] = "0"
_run_flask_command(ctx, "lint", "--check")
_run_flask_command(ctx, "test")
@task
def readme(ctx, browse=False):
ctx.run("rst2html.py README.rst > README.html")
if browse:
webbrowser.open_new_tab("README.html")