forked from chromakode/xkcdfools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
executable file
·73 lines (61 loc) · 2.41 KB
/
build.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
#!/usr/bin/env python
from __future__ import with_statement
import sys
from os import mkdir, remove, listdir
from os.path import isfile, isdir, join, normpath
from shutil import copy, rmtree
import subprocess
import re
from itertools import chain
"""Minifies JS and copies files to build/ directory"""
YUI = "tools/yuicompressor-2.4.2.jar"
MINIFY_RE = re.compile(r'<!--\s*MINIFY:\s*-->((?:<script.+</script>|\s)+)<!--\s*TO:\s*(.+)-->')
SCRIPT_RE = re.compile(r'<script type="text/javascript" src="([^"]+)"></script>')
def touch_dir(path):
if not isdir(path):
print "Creating directory (%s)..." % path
mkdir(path)
def clean(build_dir):
if isdir(build_dir):
print "Removing existing directory (%s)." % build_dir
rmtree(build_dir)
def build(src_dir, build_dir):
to_minify = {}
touch_dir(build_dir)
print "Writing index.html..."
index_data = open(join(src_dir, "index.html")).read()
def sub_minify(match):
scripts, to_script = match.groups()
to_src = SCRIPT_RE.match(to_script).group(1)
for src in SCRIPT_RE.findall(scripts):
# Minify only JS with relative paths
if not src.startswith("http"):
to_minify.setdefault(to_src, []).append(normpath(join(src_dir, src)))
return to_script
with open(join(build_dir, "index.html"), "w") as f:
f.write(MINIFY_RE.sub(sub_minify, index_data))
print "Minifying..."
for mini_name, mini_scripts in to_minify.iteritems():
mini_path = join(build_dir, mini_name)
if isfile(mini_path):
remove(mini_path)
mini_file = open(mini_path, "a")
for mini_script in mini_scripts:
if isfile(mini_script):
print " + %s" % mini_script
subprocess.call(["java", "-jar", YUI, mini_script],
stdout=mini_file)
print "--> %s" % mini_path
print "Copying data files..."
minified_paths = set(chain(*to_minify.values()))
for filename in listdir(src_dir):
filepath = join(src_dir, filename)
if not filepath == "src/index.html" and not filepath in minified_paths:
print " + %s" % filepath
copy(filepath, build_dir)
print "Build complete."
if __name__=="__main__":
if len(sys.argv) == 2 and sys.argv[1] == "clean":
clean("build")
else:
build("src", "build")