-
Notifications
You must be signed in to change notification settings - Fork 1
/
Cakefile
376 lines (328 loc) · 11.9 KB
/
Cakefile
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
fs = require 'fs'
path = require 'path'
{exec} = require 'child_process'
###
Files
###
morpheusModules = ['morpheus','morpheus-api', 'morpheus-generator', 'morpheus-editor', 'morpheus-renderer', 'morpheus-gui']
morpheusFiles = [
'morpheus-generator'
'morpheus-editor'
'morpheus-renderer'
'morpheus-gui'
]
apiFiles = [
'api/api.csm'
'api/exports'
]
generatorFiles = [
'common/directives'
'common/array'
'common/math'
'common/morpheus.log'
'generator/core'
'generator/util.tostring'
'generator/translate.csm'
'generator/compile.asm.api'
'generator/compile.asm.generics'
'generator/compile.asm.optimize'
'generator/compile.asm.bounds'
'generator/compile.asm'
'generator/compile.glsl.api'
'generator/compile.glsl.library'
'generator/compile.glsl.compiler'
'generator/compile.glsl.compilerDistance'
'generator/compile.glsl.sceneDistance'
'generator/compile.glsl.sceneId'
'generator/compile.glsl'
'generator/exports'
]
guiFiles = [
'common/directives'
'common/math'
'common/morpheus.log'
'gui/core'
'gui/constants'
'gui/state'
'gui/mouse'
'gui/scene'
'gui/events.window'
'gui/events.mouse'
'gui/events.keyboard'
'gui/events.controls'
'gui/events.register'
'gui/events.idle'
'gui/events.init'
'gui/create'
'gui/models'
'gui/exports'
]
rendererFiles = [
'common/directives'
'common/math'
'common/morpheus.log'
'renderer/core'
'renderer/state'
'renderer/model'
'renderer/scene'
'renderer/exports'
]
editorFiles = [
'common/directives'
'common/morpheus.log'
'editor/translate.sugaredjs'
'editor/create'
'editor/sourcecode'
'editor/exports'
]
completeFiles = [
'glquery/glquery'
'glquery/glquery.math.module'
'adt/adt'
'adt/adt-html'
'parameterize/parameterize-form'
'jsandbox/jsandbox'
'morpheus'
]
###
Generic functions
###
Function.prototype.partial = (args0...) ->
fn = this
(args1...) -> this.fn (args0.concat args1)...
###
Build helpers
###
# (String, String, Maybe (() -> IO)) -> IO
concatHeader = (filename, module, callback) ->
fs.readFile "static/lib/#{filename}.js", 'utf8', (err, fileContents) ->
throw err if err
fs.readFile "src/common/header.js", 'utf8', (err, commonHeaderContents) ->
throw err if err
if module?
fs.readFile "src/#{module}/header.js", 'utf8', (err, headerContents) ->
throw err if err
callback?(commonHeaderContents + headerContents + fileContents)
else
callback?(commonHeaderContents + fileContents)
# (String, String) -> Maybe (() -> IO) -> String -> IO
buildText = (filename, module) -> (callback) -> (text) ->
fs.writeFile "build/#{filename}.coffee", text.join('\n\n'), 'utf8', (err) ->
throw err if err
exec "coffee -o static/lib -c build/#{filename}.coffee", (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr
fs.unlink "build/#{filename}.coffee", (err) ->
throw err if err
# Concatenate the header file
concatHeader filename, module, (text) ->
# Write out the result
fs.writeFile "static/lib/#{filename}.js", text, 'utf8', (err) ->
throw err if err
console.log "...Done (#{filename}.js)"
callback?()
# String -> Maybe (String -> IO) -> String -> IO
prependText = (preText) -> (callback) -> (text) ->
console.log "Concatinating debug flag..."
callback?(preText + text)
# [String] -> Maybe (String -> IO) -> () -> IO
concatSrcFiles = (files) -> (callback) ->
contents = new Array files.length
remaining = files.length
for file, index in files then do (file, index) ->
fs.readFile "src/#{file}.coffee", 'utf8', (err, fileContents) ->
throw err if err
contents[index] = fileContents
(callback contents) if --remaining is 0 and callback?
# String -> Maybe (() -> IO) -> String -> IO
writeJSFile = (filename) -> (callback) -> (text) ->
fs.writeFile "static/lib/#{filename}.js", text.join('\n\n'), 'utf8', (err) ->
throw err if err
console.log "...Done (#{filename}.js)"
callback?()
# [String] -> (String -> IO) -> IO
concatLibFiles = (files) -> (callback) ->
contents = new Array files.length
remaining = files.length
for file, index in files then do (file, index) ->
fs.readFile "static/lib/#{file}.js", 'utf8', (err, fileContents) ->
throw err if err
contents[index] = fileContents
(callback contents) if --remaining is 0 and callback?
###
Build scripts
###
# Maybe (() -> IO) -> IO
buildMorpheus = (callback) ->
(concatLibFiles morpheusFiles) (writeJSFile 'morpheus') callback
# Maybe (String -> IO) -> String -> IO
prependDebug = prependText "morpheusDebug = true\n"
# Maybe (() -> IO) -> IO
buildApi = (callback, debug) ->
if debug
(concatSrcFiles apiFiles) (prependDebug (buildText 'morpheus-api', 'api') callback)
else
(concatSrcFiles apiFiles) (buildText 'morpheus-api', 'api') callback
buildGenerator = (callback, debug) ->
if debug
(concatSrcFiles generatorFiles) (prependDebug (buildText 'morpheus-generator', 'generator') callback)
else
(concatSrcFiles generatorFiles) (buildText 'morpheus-generator', 'generator') callback
buildEditor = (callback, debug) ->
if debug
(concatSrcFiles editorFiles) (prependDebug (buildText 'morpheus-editor', 'editor') callback)
else
(concatSrcFiles editorFiles) (buildText 'morpheus-editor', 'editor') callback
buildRenderer = (callback, debug) ->
if debug
(concatSrcFiles rendererFiles) (prependDebug (buildText 'morpheus-renderer', 'renderer') callback)
else
(concatSrcFiles rendererFiles) (buildText 'morpheus-renderer', 'renderer') callback
buildGui = (callback, debug) ->
if debug
(concatSrcFiles guiFiles) (prependDebug (buildText 'morpheus-gui', 'gui') callback)
else
(concatSrcFiles guiFiles) (buildText 'morpheus-gui', 'gui') callback
# Maybe (() -> IO) -> IO
minify = (callback) ->
path.exists 'node_modules/.bin/uglifyjs', (exists) ->
tool = if exists then 'node_modules/.bin/uglifyjs' else 'uglifyjs'
remaining = morpheusModules.length
for file in morpheusModules then do (file) ->
path.exists "static/lib/#{file}.js", (exists) ->
if exists
exec "#{tool} static/lib/#{file}.js > static/lib/#{file}.min.js", (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr
console.log "...Done (#{file}.min.js)"
callback?() if --remaining is 0
# Maybe (() -> IO) -> IO
packComplete = (callback) ->
(concatLibFiles completeFiles) (writeJSFile 'morpheus.complete') (-> ->
console.log "...Done (morpheus.complete.js)"
callback?())
###
Tasks
###
option '-g', '--global', 'Use with fetch to install supporting libraries and tools globally'
#task 'build', "Build the entire morpheus module", ->
# exec "mkdir -p 'build'", (err, stdout, stderr) -> return
# buildMorpheus()
task 'build-api', "Build the API module", ->
exec "mkdir -p 'build'", (err, stdout, stderr) -> return
buildApi()
task 'build-generator', "Build the generator module", ->
exec "mkdir -p 'build'", (err, stdout, stderr) -> return
buildGenerator()
task 'build-editor', "Build the editor module", ->
exec "mkdir -p 'build'", (err, stdout, stderr) -> return
buildEditor()
task 'build-renderer', "Build the renderer module", ->
exec "mkdir -p 'build'", (err, stdout, stderr) -> return
buildRenderer()
task 'build-gui', "Build the gui module", ->
exec "mkdir -p 'build'", (err, stdout, stderr) -> return
buildGui()
task 'pack-complete', "Pack morpheus with all its dependencies into a single .js file", ->
packComplete()
task 'all', "Build all distribution files", ->
exec "mkdir -p 'build'", (err, stdout, stderr) -> return
buildApi -> buildGenerator -> buildEditor -> buildRenderer -> buildGui -> buildMorpheus -> minify -> packComplete()
task 'debug', "Build all distribution files in debug (development) mode", ->
exec "mkdir -p 'build'", (err, stdout, stderr) -> return
buildApi -> buildGenerator -> buildEditor -> buildRenderer -> buildGui -> buildMorpheus -> minify -> packComplete()
task 'fetch:tools', "Fetch all supporting tools", (options) ->
invoke 'fetch:npm'
invoke 'fetch:uglifyjs'
invoke 'fetch:express'
task 'fetch:npm', "Fetch the npm package manager (always global)", ->
if options.global
console.warn "npm is always installed globally"
exec "curl http://npmjs.org/install.sh | sudo sh", (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr
console.log "Done."
task 'fetch:uglifyjs', "Fetch the UglifyJS minification tool", (options) ->
exec "#{if options.global then 'sudo ' else ''}npm install uglify-js #{if options.global then '-g' else ''}", (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr
console.log "Done."
task 'fetch:express', "Fetch the express server (for running a local server)", (options) ->
exec "#{if options.global then 'sudo ' else ''}npm install express #{if options.global then '-g' else ''}", (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr
console.log "Done."
task 'fetch:libraries', "Update all supporting libraries", (options) ->
invoke 'fetch:glquery'
#invoke 'fetch:jquery'
#invoke 'fetch:jsandbox'
#invoke 'fetch:uglifyjs-parser'
invoke 'fetch:adt.js'
invoke 'fetch:adt-html.js'
invoke 'fetch:parameterize-form'
task 'fetch:glquery', "Update the glQuery library (always local)", (options) ->
if options.global
console.warn "glquery is always installed locally"
urls = [
'https://raw.github.com/glQuery/glQuery/master/dist/glquery.js'
'https://raw.github.com/glQuery/glQuery/master/dist/extra/glquery.math.module.js'
]
remaining = urls.length
downloadCallback = (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr
--remaining
console.log "Done." if remaining == 0
for url in urls
exec "wget -nv -O static/lib/glquery/#{url.substr url.lastIndexOf('/') + 1} #{url}", downloadCallback
task 'fetch:adt.js', "Update the adt.js library (always local)", (options) ->
if options.global
console.warn "adt.js is always installed locally"
urls = [
'https://raw.github.com/rehno-lindeque/adt.js/master/dist/adt.js'
]
remaining = urls.length
downloadCallback = (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr
--remaining
console.log "Done." if remaining == 0
for url in urls
exec "wget -nv -O static/lib/adt/#{url.substr url.lastIndexOf('/') + 1} #{url}", downloadCallback
task 'fetch:adt-html.js', "Update the adt-html.js library (always local)", (options) ->
if options.global
console.warn "adt-html.js is always installed locally"
urls = [
'https://raw.github.com/rehno-lindeque/adt-html.js/master/dist/adt-html.js'
]
remaining = urls.length
downloadCallback = (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr
--remaining
console.log "Done." if remaining == 0
for url in urls
exec "wget -nv -O static/lib/adt/#{url.substr url.lastIndexOf('/') + 1} #{url}", downloadCallback
task 'fetch:parameterize-form', "Update the parameterize-form library (always local)", (options) ->
if options.global
console.warn "parameterize-form is always installed locally"
urls = [
'https://raw.github.com/circuithub/parameterize-form/master/dist/parameterize-form.js'
]
remaining = urls.length
downloadCallback = (err, stdout, stderr) ->
throw err if err
console.log stdout + stderr
--remaining
console.log "Done." if remaining == 0
for url in urls
exec "wget -nv -O static/lib/parameterize/#{url.substr url.lastIndexOf('/') + 1} #{url}", downloadCallback
task 'minify', "Minify the resulting application file after build", ->
minify()
task 'clean', "Cleanup all build files and distribution files", ->
exec "rm -rf build"
remaining = morpheusModules.length
for file in morpheusModules
exec "rm static/lib/#{file}.js", (err, stdout, stderr) ->
#console.log stdout + stderr
console.log "...Done (clean)" if --remaining is 0