-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
85 lines (62 loc) · 1.76 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
import subprocess
from pathlib import Path
from invoke import task
from common import site_dir, build_dir, site_root
from render import render_template
from app import app, get_build_urls
@task
def serve(ctx):
"""
Serve the site at localhost:8000 so that you can see the results of your
changes without building.
"""
app.run(port=8000, debug=True)
@task
def serve_build(ctx):
"""
Serve the contents of the build/ directory.
"""
run('cd {} && python -m http.server'.format(build_dir))
@task
def clean(ctx):
"""
Delete all files inside the build directory.
"""
if build_dir.exists():
run('rm -rf build/*')
@task
def build(ctx):
"""
Build the static files for the web site and put them inside the build
directory.
"""
import shutil
clean(ctx)
client = app.test_client()
# Generate HTML files using Flask.
for url in get_build_urls():
dest = build_dir / Path(url).relative_to(site_root) / 'index.html'
print('{} -> {}'.format(url, dest))
if not dest.exists():
dest.parent.mkdir(parents=True, exist_ok=True)
with dest.open('wb') as fp:
data = client.get(url).data
fp.write(data)
# Copy static files.
for src in site_dir.rglob('*?.*'):
dest = build_dir / src.relative_to(site_dir)
if dest.name.startswith('.'):
continue
print(dest)
if not dest.exists():
dest.parent.mkdir(parents=True, exist_ok=True)
shutil.copy(str(src), str(dest))
@task
def publish(ctx):
"""
Publish the web site to GitHub Pages.
"""
build(ctx)
run('ghp-import -n -p {}'.format(build_dir))
def run(cmd):
subprocess.call(cmd, shell=isinstance(cmd, str))