Completed
Push — master ( 46f648...f02689 )
by Bjorn
24s
created

build_js()   A

Complexity

Conditions 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
dl 0
loc 10
rs 9.4285
1
# -*- coding: utf-8 -*-
2
"""
3
Base version of package/tasks.py, created by
4
5
    package/root/dir> dk-tasklib install
6
7
(it should reside in the root directory of your package)
8
9
This file defines tasks for the Invoke tool: http://www.pyinvoke.org
10
11
Basic usage::
12
13
    inv -l               # list all available tasks
14
    inv build -f         # build everything, forcefully
15
    inv build --docs     # only build the docs
16
17
dk-tasklib is a library of basic tasks that tries to automate common tasks.
18
dk-tasklib will attempt to install any tools/libraries/etc. that are required,
19
e.g. when running the task to compile x.less to x.css, it will check that
20
the lessc compiler is installed (and if not it will attempt to install it).
21
22
This file is an initial skeleton, you are supposed to edit and add to it so it
23
will fit your use case.
24
25
26
"""
27
# pragma: nocover
28
from __future__ import print_function
29
import os
30
import warnings
31
32
from dkfileutils.changed import changed
33
from dkfileutils.path import Path
34
from dktasklib.wintask import task
35
from invoke import Collection
36
37
from dktasklib import docs as doctools
38
from dktasklib import jstools
39
from dktasklib import lessc
40
from dktasklib import version, upversion
41
from dktasklib.manage import collectstatic
42
from dktasklib.package import Package, package
43
from dktasklib.watch import Watcher
44
# from dktasklib.publish import publish
45
46
#: where tasks.py is located (root of package)
47
DIRNAME = Path(os.path.dirname(__file__))
48
49
# collectstatic
50
# --------------
51
# Specify which settings file should be used when running
52
# `python manage.py collectstatic` (must be on the path or package root
53
# directory).
54
DJANGO_SETTINGS_MODULE = ''
55
56
# .less
57
# ------
58
# there should be a mypkg/mypkg/less/mypkg.less file that imports any other
59
# needed sources
60
61
# .jsx (es6 source)
62
# ------------------
63
# list any .jsx files here. Only filename.jsx (don't include the path).
64
# The files should reside in mypkg/mypkg/js/ directory.
65
JSX_FILENAMES = []
66
67
# ============================================================================
68
# autodoc is in a separate process, so can't use settings.configure().
69
HAVE_SETTINGS = bool(DJANGO_SETTINGS_MODULE)
70
if not HAVE_SETTINGS and (DIRNAME / 'settings.py').exists():
71
    # look for a dummy settings.py module in the root of the package.
72
    DJANGO_SETTINGS_MODULE = 'settings'
73
if DJANGO_SETTINGS_MODULE:
74
    os.environ['DJANGO_SETTINGS_MODULE'] = DJANGO_SETTINGS_MODULE
75
WARN_ABOUT_SETTINGS = not bool(DJANGO_SETTINGS_MODULE)
76
77
78
@task
79
def build_js(ctx, force=False):
80
    """Build all javascript files.
81
    """
82
    for fname in JSX_FILENAMES:
83
        jstools.babel(
84
            ctx,
85
            '{pkg.source_js}/' + fname,
86
            '{pkg.django_static}/{pkg.name}/js/' + fname + '.js',
87
            force=force
88
        )
89
90
91
@task
92
def build(ctx, less=False, docs=False, js=False, force=False):
93
    """Build everything and collectstatic.
94
    """
95
    specified = any([less, docs, js])
96
    buildall = not specified
97
98
    if buildall or less:
99
        less_fname = ctx.pkg.source_less / ctx.pkg.name + '.less'
100
        if less_fname.exists():
101
            lessc.LessRule(
102
                ctx,
103
                src='{pkg.source_less}/{pkg.name}.less',
104
                dst='{pkg.django_static}/{pkg.name}/css/{pkg.name}-{version}.min.css',
105
                force=force
106
            )
107
        elif less:
108
            print("WARNING: build --less specified, but no file at:", less_fname)
109
110
    if buildall or docs:
111
        if WARN_ABOUT_SETTINGS:
112
            warnings.warn(
113
                "autodoc might need a dummy settings file in the root of "
114
                "your package. Since it runs in a separate process you cannot"
115
                "use settings.configure()"
116
            )
117
        doctools.build(ctx, force=force)
118
119
    if buildall or js:
120
        build_js(ctx, force)
121
122
    if HAVE_SETTINGS and (force or changed(ctx.pkg.django_static)):
123
        collectstatic(ctx, DJANGO_SETTINGS_MODULE)
124
125
126
@task
127
def watch(ctx):
128
    """Automatically run build whenever a relevant file changes.
129
    """
130
    watcher = Watcher(ctx)
131
    watcher.watch_directory(
132
        path='{pkg.source_less}', ext='.less',
133
        action=lambda e: build(ctx, less=True)
134
    )
135
    watcher.watch_directory(
136
        path='{pkg.source_js}', ext='.jsx',
137
        action=lambda e: build(ctx, js=True)
138
    )
139
    watcher.watch_directory(
140
        path='{pkg.docs}', ext='.rst',
141
        action=lambda e: build(ctx, docs=True)
142
    )
143
    watcher.start()
144
145
146
# individual tasks that can be run from this project
147
ns = Collection(
148
    build,
149
    watch,
150
    build_js,
151
    lessc,
152
    doctools,
153
    version, upversion,
154
    package,
155
    collectstatic,
156
    # publish,
157
)
158
ns.configure({
159
    'pkg': Package(),
160
    'run': {
161
        'echo': True
162
    }
163
})
164