-
Notifications
You must be signed in to change notification settings - Fork 147
/
build.py
executable file
·69 lines (58 loc) · 2 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
#!/usr/bin/python
module = 'GL'
input_path = 'src/'
output_path = 'lightgl.js'
import re, os, sys, time, tempfile
header = '''/*
* lightgl.js
* http://github.com/evanw/lightgl.js/
*
* Copyright 2011 Evan Wallace
* Released under the MIT license
*/
'''
def sources():
return [os.path.join(base, f) for base, folders, files in \
os.walk(input_path) for f in files if f.endswith('.js')]
def compile(sources):
return '\n'.join('// %s\n%s' % (path, open(path).read()) for path in sources)
def compress_glsl(text):
def compress(match):
text = match.group(0)
if ' ' in text: # assume all strings with two consecutive spaces are glsl
text = re.sub('/\*.*?\*/', '', text) # remove all comments
text = re.sub(' +', ' ', text) # replace consecutive spaces with one space
text = re.sub(r' ?(\+|\-|\*|/|,|=|{|}|;|\(|\)|<|>|!|\'|\") ?', r'\1', text) # tighten spaces around some tokens
return text
text = re.sub(r"('([^'\\]|\\(.|\n))*'|\"([^\"\\]|\\(.|\n))*\")", compress, text) # replace all strings
return text
def build():
data = 'var %s = (function() {\n\n%s\nreturn %s;\n})();\n' % (module, compile(sources()), module)
if 'release' in sys.argv:
f1, temp1_path = tempfile.mkstemp()
f2, temp2_path = tempfile.mkstemp()
os.write(f1, data)
os.close(f1)
os.close(f2)
os.system('uglifyjs "%s" -mo "%s"' % (temp1_path, temp2_path))
os.remove(temp1_path)
data = open(temp2_path).read()
os.remove(temp2_path)
data = compress_glsl(data)
data = header + data
open(output_path, 'w').write(data)
print 'built %s (%u lines)' % (output_path, len(data.split('\n')))
def stat():
return [os.stat(file).st_mtime for file in sources()]
def monitor():
a = stat()
while True:
time.sleep(0.5)
b = stat()
if a != b:
a = b
build()
if __name__ == '__main__':
build()
if 'debug' in sys.argv:
monitor()