forked from after12am/boids.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.py
101 lines (86 loc) · 2.71 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
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
#!/usr/bin/python
"""
We use closure-compiler in build process.
There is no hindrance in develop. but closure-compiler
is necessary for building release version.
1. download latest from https://github.com/google/closure-compiler
2. install closure-compiler.
```
sudo mv compiler.jar /usr/local/bin/closure
```
"""
import re, os, sys, time, tempfile, yaml
version = yaml.load(open('package.json').read()).get('version')
module = 'boids'
input_path = 'src/'
output_path = ['build/boids.js', 'example/scripts/boids.js']
header = '''/*
* boids.js
* https://github.com/after12am/boids.js
*
* Copyright 2012-2016 Satoshi Okami
* Released under the MIT license
*/
'''
def sources():
filePaths = [
'src/core/math/Vector3.js',
'src/boids.js',
'src/core/Vehicle.js',
'src/core/SteeredVehicle.js',
'src/core/BiologicalVehicle.js',
'src/object/MeshObject.js',
'src/object/Bird.js'
]
return filePaths
def compile(sources):
return '\n'.join('// %s\n%s' % (path, open(path).read()) for path in sources)
def compress_source(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() {\nvar exports = {VERSION: \'%s\'};\n\nexports.THREE = {};\n\n' % (module, version) + compile(sources()) + '\nreturn exports;\n})();\n'
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('java -jar /usr/local/bin/closure --js %s --js_output_file %s' % (temp1_path, temp2_path))
os.remove(temp1_path)
data = open(temp2_path).read()
os.remove(temp2_path)
data = compress_source(data)
data = header + data
for path in output_path:
open(path, 'w').write(data)
print 'built %s (%u lines)' % (path, len(data.split('\n')))
def gen_docs():
os.system('npm run docs')
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
if 'debug' in sys.argv:
build()
if 'docs' in sys.argv:
gen_docs()
if __name__ == '__main__':
if 'docs' in sys.argv:
monitor()
sys.exit(0)
build()
if 'debug' in sys.argv:
monitor()