forked from jsxgraph/jsxgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
make.py
497 lines (405 loc) · 16.2 KB
/
make.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
#!/usr/bin/env python
# -*- coding: utf-8 -*-
license = """/*
Copyright 2008-2012
Matthias Ehmann,
Michael Gerhaeuser,
Carsten Miller,
Bianca Valentin,
Alfred Wassermann,
Peter Wilfahrt
This file is part of JSXGraph.
JSXGraph is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
JSXGraph is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with JSXGraph. If not, see <http://www.gnu.org/licenses/>.
*/
"""
duallicense = """/*
Copyright 2008-2012
Matthias Ehmann,
Michael Gerhaeuser,
Carsten Miller,
Bianca Valentin,
Alfred Wassermann,
Peter Wilfahrt
Dual licensed under the Apache License Version 2.0, or LGPL Version 3 licenses.
You should have received a copy of the GNU Lesser General Public License
along with JSXCompressor. If not, see <http://www.gnu.org/licenses/>.
You should have received a copy of the Apache License along with JSXCompressor.
If not, see <http://www.apache.org/licenses/>.
*/
"""
import sys;
# Parse command line options
import getopt;
# Used for makeRelease & makeCompressor
import os
import re
import tempfile
import shutil
# Used for JSHint
import urllib
# Default values for options. May be overridden via command line options
yui = "~/Tools/yuicompressor"
jsdoc = "~/Tools/jsdoc-toolkit"
jstest = "~/Tools/JsTestDriver/JsTestDriver-1.3.4-a.jar"
output = "distrib"
version = None
hint = None
reset = ""
port = 4224
server = ""
'''
Prints some instructions on how to use this script
'''
def usage():
print
print "Usage: python", sys.argv[0], "[OPTIONS]... TARGET"
print "Compile and minify the JSXGraph source code, run unit tests, and compile the docs."
print
print "Options:"
print " -h, --help Display this help and exit."
print " -l, --hint=FILE Set the file you want to check with JSHint."
print " -j, --jsdoc=PATH Search for jsdoc-toolkit in PATH."
print " -o, --output=PATH Override the default output path distrib/ by PATH."
print " -p, --port=PORT Set server port for the JsTestDriver server. Default is 4224."
print " --reset Force the test server to reload the browsers."
print " -s, --server=URL Overrides the server option in the JsTestDriver config."
print " -v, --version=VERSION Use VERSION as release version for proper zip archive and"
print " folder names."
print " -y, --yui=PATH Search for YUI Compressor in PATH."
print
print "Targets:"
print " Core Concatenates and minifies JSXGraph source files into"
print " distrib/jsxgraphcore.js ."
print " Release Makes Core, Readers and Docs and creates distribution ready zip archives"
print " in distrib/ ."
print " Readers Makes Readers and copies them to distrib/ ."
print " Docs Generate documentation from source code comments. Uses"
print " jsdoc-toolkit."
print " Hint Run JSHint on the file given with -l or --hint."
print " Plot Make a slim JSXGraph core just for function plotting."
print " Test Run Unit Tests with JsTestDriver."
print " TestServer Run JsTestDriver server."
print " Compressor Minify and create a zip archive for JSXCompressor."
print " All Makes JSXGraph and Compressor."
'''
Search for line
baseFile = 'AbstractRenderer,...,gunzip';
and return list of filenames
'''
def findFilenames(filename):
lines = open(filename).readlines()
expr = re.compile("baseFiles\s*=\s*('|\")([\w,\s]+)('|\")")
for el in lines:
el = re.compile("\s+").sub("",el) # Replace whitespace
r = expr.search(el)
if r and r.groups()[1]!='gxt':
files = r.groups()[1].split(',')
return files # return all files in loadjsxgraph.js
return []
'''
Generate jsxgraphcore.js and place it in <output>
'''
def makeCore():
global yui, jsdoc, version, output, license
print "Making Core..."
jstxt = ''
license = ("/* Version %s */\n" % version) + license
# Take the source files and write them into jstxt
loader = ['loadjsxgraphInOneFile']
for f in loader:
print 'take ', f
jstxt += open('src/'+f+'.js','r').read()
jstxt += '\n';
files = findFilenames('src/loadjsxgraph.js')
for f in files:
print 'take ', f
jstxt += open('src/'+f+'.js','r').read()
jstxt += '\n';
renderer = ['SVGRenderer','VMLRenderer','CanvasRenderer']
for f in renderer:
print 'take ', f
jstxt += open('src/'+f+'.js','r').read()
jstxt += '\n';
# tmpfilename = tempfile.mktemp()
srcFilename = output + '/jsxgraphsrc.js'
fout = open(srcFilename,'w')
fout.write(jstxt)
fout.close()
# Prepend license text
coreFilename = output + "/jsxgraphcore.js"
fout = open(coreFilename,'w')
fout.write(license)
fout.close()
# Minify; YUI compressor from Yahoo
s = "java -jar " + yui + "/build/yuicompressor*.jar --type js " + srcFilename + " >>" + coreFilename
print s
os.system(s)
# os.remove(tmpfilename)
shutil.copy("src/themes/dark.js", output + "/themes/dark.js")
shutil.copy("src/themes/gui.js", output + "/themes/gui.js")
'''
Generate slim jsxplotcore.js and place it in <output>
'''
def makePlot():
global yui, jsdoc, version, output, license
print "Making Plot..."
jstxt = ''
license = ("/* Version %s */\n" % version) + license
# Take the source files and write them into jstxt
loader = ['loadjsxgraphInOneFile']
for f in loader:
print 'take ', f
jstxt += open('src/'+f+'.js','r').read()
jstxt += '\n';
files = 'JXG,Math,MathNumerics,MathStatistics,MathSymbolic,MathGeometry,AbstractRenderer,GeonextParser,Board,Options,jsxgraph,GeometryElement,Coords,Point,Line,Curve,Text,Composition,Util,Transformation,RGBColor,Wrappers,Ticks'.split(',')
for f in files:
print 'take ', f
jstxt += open('src/'+f+'.js','r').read()
jstxt += '\n';
renderer = ['SVGRenderer','CanvasRenderer']
for f in renderer:
print 'take ', f
jstxt += open('src/'+f+'.js','r').read()
jstxt += '\n';
tmpfilename = tempfile.mktemp()
fout = open(tmpfilename,'w')
fout.write(jstxt)
fout.close()
# Prepend license text
coreFilename = output + "/jsxplotcore.js"
fout = open(coreFilename,'w')
fout.write(license)
fout.close()
# Minify; YUI compressor from Yahoo
s = "java -jar " + yui + "/build/yuicompressor*.jar --type js " + tmpfilename + " >>" + coreFilename
print s
os.system(s)
os.remove(tmpfilename)
'''
Generate JSXGraph HTML reference, zip it and place the archive in <output>
'''
def makeDocs(afterCore = False):
global yui, jsdoc, version, output
jsd = os.path.expanduser(jsdoc)
if afterCore:
out = output
else:
out = "distrib"
print "Making Docs"
print "Updating jsdoc-template and plugin"
try:
# cp ../doc/jsdoc-tk/plugins/* $ROOT/app/plugins/
shutil.copy("doc/jsdoc-tk/plugins/jsxPseudoClass.js", jsd + "/app/plugins/jsxPseudoClass.js")
# cp -r ../doc/jsdoc-tk/template/* $ROOT/templates/jsdoc
shutil.rmtree(jsd + "/templates/jsx/", True)
shutil.copytree("doc/jsdoc-tk/template", jsd + "/templates/jsx")
# mkdir $ROOT/templates/jsdoc/static
# cp ../distrib/jquery.min.js $ROOT/templates/jsdoc/static
# cp ../distrib/jsxgraphcore.js $ROOT/templates/jsdoc/static
# cp ../distrib/jsxgraph.css $ROOT/templates/jsdoc/static
shutil.copy("distrib/jquery.min.js", jsd + "/templates/jsx/static")
shutil.copy(out + "/jsxgraphcore.js", jsd + "/templates/jsx/static")
shutil.copy("distrib/jsxgraph.css", jsd + "/templates/jsx/static")
except IOError as (errno, strerror):
print "Error: Can't update jsdoc-toolkit template and/or plugin:",strerror
sys.exit(2)
#FILELIST=$(cat ../src/loadjsxgraph.js | grep "baseFiles\s*=\s*'\(\w*,\)\+" | awk -F \' '{ print $2 }' | sed 's/,/.js ..\/src\//g')
files = findFilenames('src/loadjsxgraph.js')
filesStr = "src/loadjsxgraph.js src/" + ".js src/".join(files) + ".js src/SVGRenderer.js src/VMLRenderer.js src/CanvasRenderer.js"
#java -jar $ROOT/jsrun.jar $ROOT/app/run.js -a -v -t=$ROOT/templates/jsdoc -d=docs ../src/loadjsxgraph.js ../src/$FILELIST.js ../src/SVGRenderer.js ../src/VMLRenderer.js
os.system("java -jar " + jsd + "/jsrun.jar " + jsd + "/app/run.js -v -p -t=" + jsd + "/templates/jsx -d=tmp/docs " + filesStr)
#zip -r tmp/docs.zip tmp/docs/
os.system("cd tmp && zip -r docs-" + version + ".zip docs/ && cd ..")
shutil.move("tmp/docs-" + version + ".zip", output + "/docs-" + version + ".zip")
'''
Make targets Readers and place them in <output>
'''
def makeReaders():
global yui, output, version, license
print "Making Readers..."
lic = ("/* Version %s */\n" % version) + license
reader = ['Geonext', 'Geogebra', 'Intergeo', 'Cinderella']
for f in reader:
fname = f + "Reader"
shutil.copy("src/" + fname + ".js", "tmp/")
shutil.copy("src/" + fname + ".js", output)
# Minify; YUI compressor from Yahoo
srcFilename = "tmp/" + fname + ".js"
# Prepend license text
coreFilename = output + "/" + fname + ".min.js"
fout = open(coreFilename,'w')
fout.write(lic)
fout.close()
s = "java -jar " + yui + "/build/yuicompressor*.jar --type js " + srcFilename + " >>" + coreFilename
print s
os.system(s)
'''
Make targets Core and Docs and create distribution-ready zip archives in <output>
'''
def makeRelease():
print "Make Release"
makeCore()
makeDocs(True)
shutil.copy(output + "/jsxgraphcore.js", "tmp/jsxgraphcore.js")
shutil.copy(output + "/jsxgraphsrc.js", "tmp/jsxgraphsrc.js")
shutil.copy("README.md", "tmp/README.md")
shutil.copy("CHANGELOG.md", "tmp/CHANGELOG.md")
shutil.copy("LICENSE", "tmp/LICENSE")
shutil.copy("distrib/jsxgraph.css", "tmp/jsxgraph.css")
makeReaders()
shutil.copy("src/themes/dark.js", "tmp/themes/dark.js")
shutil.copy("src/themes/gui.js", "tmp/themes/gui.js")
os.system("cd tmp && zip -r jsxgraph-" + version + ".zip docs/ jsxgraphcore.js jsxgraphsrc.js jsxgraph.css themes/ README LICENSE && cd ..")
shutil.move("tmp/jsxgraph-" + version + ".zip", output + "/jsxgraph-" + version + ".zip")
'''
Make JSXCompressor, a JSXGraph subproject
'''
def makeCompressor(afterCore = False):
global yui, jsdoc, version, output
print "Make JSXCompressor"
if afterCore:
out = output
else:
out = "distrib"
jstxt = 'JXG = {exists: (function(undefined){return function(v){return !(v===undefined || v===null);}})()};\n'
jstxt += 'JXG.decompress = function(str) {return unescape((new JXG.Util.Unzip(JXG.Util.Base64.decodeAsArray(str))).unzip()[0][0]);};\n'
# Take the source files and write them into jstxt
loader = ['Util']
for f in loader:
print 'take ', f
jstxt += open('src/'+f+'.js','r').read()
jstxt += '\n';
srcFilename = output + "/jsxcompressor.js"
fout = open(srcFilename,'w')
fout.write(jstxt)
fout.close()
# Prepend license text
coreFilename = output + "/jsxcompressor.min.js"
fout = open(coreFilename, 'w')
fout.write(duallicense)
fout.close()
# Minify
# YUI compressor from Yahoo
s = 'java -jar ' + yui + '/build/yuicompressor*.jar --type js ' + srcFilename + ' >>' + coreFilename
print s
os.system(s)
os.system("cp %s %s" % (srcFilename, 'JSXCompressor/'))
os.system("cp %s %s" % (coreFilename, 'JSXCompressor/'))
# If makeCore has been called just befure, make sure you grab the newest version
os.system("cp %s %s" % (out + '/jsxgraphcore.js', 'JSXCompressor/'))
os.system("cp %s %s" % ('distrib/prototype.js', 'JSXCompressor/'))
os.system("cp %s %s" % ('distrib/jsxgraph.css', 'JSXCompressor/'))
os.system("rm JSXCompressor/*~")
os.system("zip -r " + output + "/jsxcompressor.zip JSXCompressor/*")
'''
Fetch a file from the web
'''
def fetch(url, local):
webFile = urllib.urlopen(url)
localFile = open(local, 'w')
localFile.write(webFile.read())
webFile.close()
localFile.close()
'''
Check a file with JSHint
'''
def makeHint():
global hint
# TODO: If hint is None use all files in src/*
if hint is None:
print "No file given. Please provide a file with the -l or --hint option."
return
# Fetch program files
fetch('https://github.com/jshint/jshint/raw/master/env/rhino.js', '/tmp/rhino.js')
fetch('http://jshint.com/jshint.js', '/tmp/jshint.js')
abshint = os.path.abspath(hint)
os.system('cd /tmp && rhino /tmp/rhino.js ' + abshint)
'''
Run Unit Tests
'''
def makeTest():
global jstest, reset, server
os.system('java -jar ' + jstest + ' ' + reset + ' ' + server + ' --tests all --basePath ./ --config test/jsTestDriver.conf --captureConsole');
'''
Run Unit Tests Server
'''
def makeTestServer():
global jstest, reset, port
os.system('java -jar ' + jstest + ' --port ' + str(port));
'''
Make targets Release and Compressor
'''
def makeAll():
makeRelease()
makeCompressor(True)
def main(argv):
global yui, jsdoc, version, output, hint, jstest, reset, port, server
try:
opts, args = getopt.getopt(argv, "hy:j:v:o:l:t:p:s:", ["help", "yui=", "jsdoc=", "version=", "output=", "hint=", "test=", "reset", "port=", "server="])
except getopt.GetoptError as (errono, strerror):
usage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
usage()
sys.exit(2)
elif opt in ("-j", "--jsdoc"):
jsdoc = arg
elif opt in ("-o", "--output"):
output = os.path.expanduser(arg)
elif opt in ("-v", "--version"):
version = arg
elif opt in ("-y", "--yui"):
yui = arg
elif opt in ("-l", "--hint"):
hint = arg
elif opt in ("-t", "--test"):
jstest = arg
elif opt in ("-p", "--port"):
port = arg
elif opt in ("--reset"):
reset = '--reset'
elif opt in ("-s", "--server"):
if arg == 'btmdxe':
server = "--server http://btmdxe.mat.uni-bayreuth.de:4224"
else:
server = "--server " + arg
target = "".join(args)
# Search for the version and print it before the license text.
if not version:
expr = re.compile("JSXGraph v(.*) Copyright")
r = expr.search(open("src/jsxgraph.js").read())
version = r.group(1)
try:
# Create tmp directory and output directory
if not os.path.exists(output):
os.mkdir(output)
if not os.path.exists(output + "/themes"):
os.mkdir(output + "/themes")
if not os.path.exists("tmp"):
os.mkdir("tmp")
if not os.path.exists("tmp/themes"):
os.mkdir("tmp/themes")
# Call the target make function
globals()["make" + target]()
shutil.rmtree("tmp/")
except KeyError:
# Oooops, target doesn't exist.
print "Error: Target", target, "does not exist."
usage()
shutil.rmtree("tmp/")
sys.exit(1)
except IOError:
print "Error: Can't create tmp directories."
sys.exit(1)
if __name__ == "__main__":
main(sys.argv[1:])