-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks.py
165 lines (137 loc) · 4.68 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# -*- coding: utf-8 -*-
"""
Base version of package/tasks.py, created by version 0.3.1 of
package/root/dir> dk-tasklib install
(it should reside in the root directory of your package)
This file defines tasks for the Invoke tool: http://www.pyinvoke.org
Basic usage::
inv -l # list all available tasks
inv build -f # build everything, forcefully
inv build --docs # only build the docs
dk-tasklib is a library of basic tasks that tries to automate common tasks.
dk-tasklib will attempt to install any tools/libraries/etc. that are required,
e.g. when running the task to compile x.less to x.css, it will check that
the lessc compiler is installed (and if not it will attempt to install it).
This file is an initial skeleton, you are supposed to edit and add to it so it
will fit your use case.
"""
# pragma: nocover
from __future__ import print_function
import os
import warnings
from dkfileutils.changed import changed
from dkfileutils.path import Path
from dktasklib.wintask import task
from invoke import Collection
from dktasklib import docs as doctools
from dktasklib import jstools
from dktasklib import lessc
from dktasklib import version, upversion
from dktasklib.manage import collectstatic
from dktasklib.package import Package, package
from dktasklib.watch import Watcher
# from dktasklib.publish import publish
#: where tasks.py is located (root of package)
DIRNAME = Path(os.path.dirname(__file__))
# collectstatic
# --------------
# Specify which settings file should be used when running
# `python manage.py collectstatic` (must be on the path or package root
# directory).
DJANGO_SETTINGS_MODULE = ''
# .less
# ------
# there should be a mypkg/mypkg/less/mypkg.less file that imports any other
# needed sources
# .jsx (es6 source)
# ------------------
# list any .jsx files here. Only filename.jsx (don't include the path).
# The files should reside in mypkg/mypkg/js/ directory.
JSX_FILENAMES = []
# ============================================================================
# autodoc is in a separate process, so can't use settings.configure().
HAVE_SETTINGS = bool(DJANGO_SETTINGS_MODULE)
if not HAVE_SETTINGS and (DIRNAME / 'settings.py').exists():
# look for a dummy settings.py module in the root of the package.
DJANGO_SETTINGS_MODULE = 'settings'
if DJANGO_SETTINGS_MODULE:
os.environ['DJANGO_SETTINGS_MODULE'] = DJANGO_SETTINGS_MODULE
WARN_ABOUT_SETTINGS = not bool(DJANGO_SETTINGS_MODULE)
@task
def build_js(ctx, force=False):
"""Build all javascript files.
"""
for fname in JSX_FILENAMES:
jstools.babel(
ctx,
'{pkg.source_js}/' + fname,
'{pkg.django_static}/{pkg.name}/js/' + fname + '.js',
force=force
)
@task
def build(ctx, less=False, docs=False, js=False, force=False):
"""Build everything and collectstatic.
"""
specified = any([less, docs, js])
buildall = not specified
if buildall or less:
less_fname = ctx.pkg.source_less / ctx.pkg.name + '.less'
if less_fname.exists():
lessc.LessRule(
ctx,
src='{pkg.source_less}/{pkg.name}.less',
dst='{pkg.django_static}/{pkg.name}/css/{pkg.name}-{version}.min.css',
force=force
)
elif less:
warnings.warn(
"WARNING: build --less specified, but no file at: " + less_fname
)
if buildall or docs:
if WARN_ABOUT_SETTINGS:
warnings.warn(
"autodoc might need a dummy settings file in the root of "
"your package. Since it runs in a separate process you cannot"
"use settings.configure()"
)
doctools.build(ctx, force=force)
if buildall or js:
build_js(ctx, force)
if HAVE_SETTINGS and (force or changed(ctx.pkg.django_static)):
collectstatic(ctx, DJANGO_SETTINGS_MODULE, force=force)
@task
def watch(ctx):
"""Automatically run build whenever a relevant file changes.
"""
watcher = Watcher(ctx)
watcher.watch_directory(
path='{pkg.source_less}', ext='.less',
action=lambda e: build(ctx, less=True)
)
watcher.watch_directory(
path='{pkg.source_js}', ext='.jsx',
action=lambda e: build(ctx, js=True)
)
watcher.watch_directory(
path='{pkg.docs}', ext='.rst',
action=lambda e: build(ctx, docs=True)
)
watcher.start()
# individual tasks that can be run from this project
ns = Collection(
build,
watch,
build_js,
lessc,
doctools,
version, upversion,
package,
collectstatic,
# publish,
)
ns.configure({
'pkg': Package(),
'run': {
'echo': True
}
})