-
Notifications
You must be signed in to change notification settings - Fork 3
/
tasks.py
102 lines (75 loc) · 2.49 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
import glob
import json
import os
import shutil
import delegator
import invoke
import livereload
import sass
from livereload.watcher import Watcher
# https://github.com/lepture/python-livereload/issues/156
class GlobWatcher(Watcher):
def is_glob_changed(self, path, ignore=None):
for f in glob.glob(path, recursive=True):
if self.is_file_changed(f, ignore):
return True
return False
def path(*paths):
if not paths[0].startswith(os.sep):
paths = (os.getcwd(),) + paths
return os.path.abspath(os.path.join(*paths))
def mkdir(dirname):
dirname = path(dirname)
if not os.path.exists(dirname):
os.makedirs(dirname)
return dirname
def assets(*paths):
return path('assets', *paths)
def import_node_module(ctx, target, destination=None):
destination = path('dist', destination or os.path.basename(target))
target = path('node_modules', target, 'dist')
ctx.run(f'rm -rf {destination}')
ctx.run(f'cp -r {target} {destination}')
@invoke.task()
def optimize_images(ctx):
ctx.run('optimize-images ./public/static/')
@invoke.task()
def styles(ctx):
compiled_css = sass.compile(
filename=assets('sass/style.sass'),
include_paths=(assets('sass'), path('node_modules')),
output_style='compressed',
)
mkdir('dist/css')
with open(path('dist/css/style.css'), 'w') as f:
f.write(compiled_css)
@invoke.task
def collectstatic(ctx):
ctx.run('python manage.py collectstatic --noinput', pty=True)
@invoke.task()
def import_npm_assets(ctx):
import_node_module(ctx, '@sjvair/monitor-map')
import_node_module(ctx, '@sjvair/web-widget', 'widget')
import_node_module(ctx, '@sjvair/sensor-scatterplot')
@invoke.task()
def build(ctx):
# Directory prep
ctx.run('rm -rf ./public/static')
mkdir(path('dist'))
import_npm_assets(ctx)
styles(ctx)
collectstatic(ctx)
optimize_images(ctx)
@invoke.task
def release(ctx):
ctx.run('python manage.py migrate --no-input', pty=True)
ctx.run('python manage.py sync_staticfiles_s3', pty=True)
# Some steps in here are no longer needed
@invoke.task()
def watch(ctx):
server = livereload.Server(watcher=GlobWatcher())
server.watch(path('./assets/img/'), lambda: collectstatic(ctx))
server.watch(assets('js/**'), lambda: collectstatic(ctx))
server.watch(assets('sass/**'), lambda: [styles(ctx), collectstatic(ctx)])
server.watch(path('./dist/lib/'), lambda: collectstatic(ctx))
server.serve()