Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathbuild_cli.py
More file actions
Latest commit
221 lines (172 loc) · 6.45 KB
/
Copy pathbuild_cli.py
File metadata and controls
221 lines (172 loc) · 6.45 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# -*- coding: utf-8 -*-
"""This python program converts various parts of glowscript from the most
convenient format for modification into the most convenient format for
deployment.
* Take shaders from shaders/*.shader and combine them into lib/glow/shaders.gen.js
* Extract glowscript libraries list from ``untrusted/run.js``.
In the implementation, we need ``slimit`` as our dependency::
$ pip install slimit
TODO
* Come up with a less painful model for development than running this after every change
* Combine and minify lib/*.js into ide.min.js, run.min.js, and embed.min.js
"""
from __future__ importdivision
from __future__ importprint_function
importargparse
importos
importsubprocess
fromfunctoolsimportpartial
fromcollectionsimportnamedtuple
frompprintimportpprint
fromslimitimportast
fromslimit.parserimportParserasJSParser
fromslimit.visitorsimportnodevisitor
version="2.2dev"
src_dir=os.path.dirname(__file__)
defextract_glow_lib():
runjs=norm_path('untrusted/run.js')
parser=JSParser()
withopen(runjs) asf:
tree=parser.parse(f.read())
fornodeinnodevisitor.visit(tree):
if (isinstance(node, ast.Assign) and
isinstance(node.left, ast.DotAccessor) and
node.left.identifier.value=='glowscript_libraries'and
isinstance(node.right, ast.Object)):
break
else:
print('Parsing {} failed'.format(runjs))
exit(-1)
returnpreproc_lib_path({
prop.left.value:
[
eval(lib.value)
forlibinprop.right.items
ifisinstance(lib, ast.String)
]
forpropinnode.right.properties
})
defpreproc_lib_path(libs):
pjoin=partial(os.path.join, src_dir, 'untrusted')
return {pkg: map(os.path.normpath, (map(pjoin, paths)))
forpkg, pathsinlibs.items()}
defbuild_shader():
shader_file= ["Export({shaders: {"]
shaders_dir=os.path.join(src_dir, 'shaders')
output_js=os.path.join(src_dir, 'lib', 'glow', 'shaders.gen.js')
forfninos.listdir(shaders_dir):
ifnotfn.endswith('.shader'):
continue
name=fn.rpartition('.shader')[0]
withopen(os.path.join(shaders_dir, fn), 'rt') asf:
shader_file.append('"{name}":{src!r},'.format(
name=name, src=f.read()))
shader_file.append('}});')
withopen(output_js, 'w') asf:
f.writelines('\n'.join(shader_file))
print("Shader {!r} built successfully.".format(output_js))
defnorm_path(p):
'''
:param p: path related to source dir
>>> norm_path('lib/glow/graph.js')
'path/to/src/dir/lib/glow/graph.js'
'''
returnos.path.normpath(os.path.join(src_dir, p))
defcombine(inlibs):
defgen():
yield (
"/*This is a combined, compressed file. "
"Look at https://github.com/BruceSherwood/glowscript "
"for source code and copyright information.*/"
)
yield";(function(){})();"
forfnininlibs:
withopen(fn, 'r') asf:
yieldf.read()
return"\n".join(gen())
defminify(inlibs, inlibs_nomin, outlib, no_min=False):
'''
Do unglify for ``inlibs``
:param inlibs: a list of paths which want to be minify
:param inlibs_nomin: a list of paths which do *not* want to be minify
:param no_min: if True, we build no minified libraries only.
Available environment variable:
:NODE_PATH: the path of nodejs exetuable
'''
node_cmd=os.environ.get('NODE_PATH', 'node')
uglifyjs=norm_path('build-tools/UglifyJS/bin/uglifyjs')
withopen(outlib, 'w') asoutf:
ifnotno_min:
uglify=subprocess.Popen(
[node_cmd, uglifyjs],
stdin=subprocess.PIPE,
stdout=outf,
)
uglify.communicate(combine(inlibs))
rc=uglify.wait()
ifrc!=0:
print("Something went wrong on {}".format(outlib))
else:
print("Uglify {} successfully".format(outlib))
ifinlibs_nomin:
outf.write(combine(inlibs_nomin))
defbuild_package(libs, no_min=False):
'''
:param libs: the dictionary contain all glowscript libraries::
{
"package_1": [
'lib 1'
...
],
"package_2": [
...
],
...
}
:param no_min: if True, we build no minified libraries only.
'''
Package=namedtuple('Package',
('inlibs', 'inlibs_nomin', 'outlib', 'comment'))
pkgs= (
Package(inlibs='run',
inlibs_nomin=[],
outlib='glow.{}.min.js'.format(version),
comment='glow run-time package'),
Package(inlibs='compile',
inlibs_nomin=[],
outlib='compiler.{}.min.js'.format(version),
comment='compiler package'),
Package(inlibs='RSrun',
inlibs_nomin=[],
outlib='RSrun.{}.min.js'.format(version),
comment='RapydScript run-time package'),
Package(inlibs='RScompile',
inlibs_nomin=[],
outlib='RScompiler.{}.min.js'.format(version),
comment='GlowScript package'),
)
forpkginpkgs:
minify(libs[pkg.inlibs],
pkg.inlibs_nomin,
norm_path('package/{}'.format(pkg.outlib)),
no_min=no_min)
print('Finished {}'.format(pkg.comment))
defcmd_args():
parser=argparse.ArgumentParser()
parser.add_argument('-s', '--shader', action='store_true', default=False,
help="Build shader file 'lib/glow/shaders.gen.js' only")
parser.add_argument('--no-min', dest='no_min', action='store_true',
default=False, help="Build non-minified libraries only")
parser.add_argument('-l', '--libs', action='store_true', default=False,
help='Show glowscript libraries and exit')
returnparser.parse_args()
if__name__=='__main__':
glowscript_libraries=extract_glow_lib()
args=cmd_args()
ifargs.libs:
pprint(glowscript_libraries)
elifargs.shader:
build_shader(glowscript_libraries)
else: # default: build all
build_shader()
build_package(glowscript_libraries, no_min=args.no_min)