-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgulpfile.coffee
567 lines (522 loc) · 19.1 KB
/
gulpfile.coffee
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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
# ---------------------------------------------- Requires
gulp = require('gulp')
coffee = require('gulp-coffee')
flatten = require('gulp-flatten')
rename = require('gulp-rename')
plumber = require('gulp-plumber')
jeditor = require('gulp-json-editor')
argv = require('yargs').argv
path = require('path')
gulpif = require('gulp-if')
_ = require('underscore')
notify = require("gulp-notify")
coffeeify = require('gulp-coffeeify')
through = require('through2')
node = undefined
os = require('os');
# ---------------------------------------------- Functions
getGlob = (paths) ->
args = Array::slice.call(arguments)
answer = sources.map((i) ->
args.map (p) ->
if p[0] == '!'
'!' + i + '/' + p.substring(1)
else
i + '/' + p
)
[].concat.apply [], answer
getFirstFolderName = (path) ->
path.dirname = path.dirname.split('/')[0]
return path
getRPath = (path) ->
module = path.dirname.split('/')[0]
if path.basename == 'r' and path.extname == ''
path.basename = ''
path.dirname = ''
outputDirname = module + '/' + path.dirname.replace(module + '/src/server/r', '')
path.dirname = outputDirname
return path
getLegacyRPath = (path) ->
# console.warn "Warning: All R code in modules path 'modules/**/src/server/**/*.{R,r}' should be moved to 'modules/**/src/server/r/*.{R,r}'"
module = path.dirname.split('/')[0]
additionalPath = path.dirname.replace(module + '/src/server/', '')
# console.log "Warning: Please move '#{path.dirname}/#{path.basename}#{path.extname}' to '#{module}/src/server/r/#{additionalPath}/#{path.basename}#{path.extname}'"
if path.basename == 'r' and path.extname == ''
path.basename = ''
path.dirname = ''
outputDirname = module + '/' + path.dirname.replace(module + '/src/server/', '')
path.dirname = outputDirname
return path
editPackageJson = () ->
func = jeditor((json) ->
delete json.scripts.postinstall
json
)
return func
getTestFixuresPath = (path) ->
path = getFirstFolderName(path)
path.dirname += '/testFixtures'
return path
getServiceTestsPath = (path) ->
path = getFirstFolderName(path)
path.dirname += '/serviceTests'
return path
getPythonPath = (path) ->
module = path.dirname.split('/')[0]
if path.basename == 'python' and path.extname == ''
path.basename = ''
path.dirname = ''
outputDirname = module + '/' + path.dirname.replace(module + '/src/server/python', '')
path.dirname = outputDirname
return
getAssetsPath = (path) ->
module = path.dirname.split('/')[0]
if path.basename == 'assets' and path.extname == ''
path.basename = ''
path.dirname = ''
outputDirname = module + '/' + path.dirname.replace(module + '/src/server/assets', '')
path.dirname = outputDirname
return
addREnvironmentCleanUp = (file,contents) ->
# file contents are handed
# over as buffers
if path.extname(file.path) in [".R",".r"] && contents.match('# ROUTE:.*') && !contents.match('# MEMORY_LIMIT_EXEMPT')
cleanFunction = "DONE;racas::checkMemoryLimitAndKillSelf();racas::cleanEnvironment();"
contents = "#{contents}\r\n#{cleanFunction}\r\n"
return contents
modify = (options = {}) ->
through.obj (file, enc, next) ->
error = null
if file.isBuffer()
if fileModifier = options.fileModifier
try
content = fileModifier file, file.contents.toString 'utf8'
file.contents = Buffer.from(content)
catch _error
console.log _error
next error, file
# ------------------------------------------------- Read Inputs
build = argv.buildPath or process.env.BUILD_PATH or ''
if build == ''
build = 'build'
if argv.sourceDirectories? | process.env.SOURCE_DIRECTORIES?
sources = (argv.sourceDirectories or process.env.SOURCE_DIRECTORIES).split(',')
else
sources = []
if !argv.customonly or true
acas_base = path.relative('.', argv.acasBase or process.env.ACAS_BASE or '')
if acas_base == ''
acas_base = '.'
sources.push acas_base
if !argv.baseonly or true
acas_custom = path.relative('.', argv.acasCustom or process.env.ACAS_CUSTOM or '')
if acas_custom == ''
acas_custom = 'acas_custom'
sources.push acas_custom
acas_shared = path.relative('.', argv.acasShared or process.env.ACAS_SHARED or '')
if acas_shared == ''
acas_shared = 'acas_shared'
sources.push acas_shared
startupArgs = []
if argv.debugbrk
startupArgs.push "--inspect-brk=5858"
startupArgs.push "app.js"
if argv.stubsMode
startupArgs.push "stubsMode"
console.log 'working directory \'' + __dirname + '\''
console.log 'setting build to: ' + build
console.log 'setting source directories to: ' + JSON.stringify(sources)
# ------------------------------------------------- Setup Configs
globalCoffeeOptions = {sourcemaps:true}
globalCopyOptions = {allowEmpty:true}
globalExecuteOptions = {cwd: build, env: process.env}
globalWatchOptions =
interval: 1000
debounceDelay: 500
usePolling: true
# mode: 'poll'
taskConfigs =
coffee: [
taskName: "root"
src: getGlob('*.coffee')
dest: build
options: _.extend _.clone(globalCoffeeOptions), {}
,
taskName: "publicConf"
src: getGlob('public_conf/*.coffee')
dest: build + '/src/javascripts/ServerAPI'
options: _.extend _.clone(globalCoffeeOptions), {}
,
taskName: "buildUtilities"
src: getGlob('modules/BuildUtilities/src/server/*.coffee')
dest: build + '/src/javascripts/BuildUtilities'
options: _.extend _.clone(globalCoffeeOptions), {}
renameFunction: getFirstFolderName
,
taskName: "serverAPI"
src: getGlob('modules/ServerAPI/src/server/*.coffee')
dest: build + '/src/javascripts/ServerAPI'
options: _.extend _.clone(globalCoffeeOptions), {}
renameFunction: getFirstFolderName
,
taskName: "serverUtilityFunctions"
src: getGlob('modules/**/src/server/routes/ServerUtilityFunctions.coffee')
dest: build + '/routes'
options: _.extend _.clone(globalCoffeeOptions), {}
flatten: true
,
taskName: "rootConf"
src: getGlob('conf/*.coffee')
dest: build + '/conf'
options: _.extend _.clone(globalCoffeeOptions), {}
,
taskName: "conf"
src: getGlob('modules/**/conf/*.coffee')
dest: build + '/public/javascripts/conf'
options: _.extend _.clone(globalCoffeeOptions), {}
renameFunction: getFirstFolderName
,
taskName: "client"
src: getGlob('modules/**/src/client/*.coffee')
dest: build + '/public/javascripts/src'
options: _.extend _.clone(globalCoffeeOptions), {}
renameFunction: getFirstFolderName
,
taskName: "routes"
src: getGlob('modules/**/src/server/routes/*.coffee')
dest: build + '/routes'
options: _.extend _.clone(globalCoffeeOptions), {}
flatten: true
,
taskName: "server"
src: getGlob('modules/**/src/server/*.coffee')
dest: build + '/src/javascripts'
options: _.extend _.clone(globalCoffeeOptions), {}
renameFunction: getFirstFolderName
,
taskName: "spec"
src: getGlob('modules/**/spec/*.coffee')
dest: build + '/public/javascripts/spec'
options: _.extend _.clone(globalCoffeeOptions), {}
renameFunction: getFirstFolderName
,
taskName: "testFixtures"
src: getGlob('modules/**/spec/testFixtures/*.coffee')
dest: build + '/public/javascripts/spec'
options: _.extend _.clone(globalCoffeeOptions), {}
renameFunction: getTestFixuresPath
,
taskName: "serviceTests"
src: getGlob('modules/**/spec/serviceTests/*.coffee', 'modules/public/conf/serviceTests/*.coffee')
dest: build + '/src/spec'
options: _.extend _.clone(globalCoffeeOptions), {}
renameFunction: getServiceTestsPath
],
execute: [
taskName: "npmInstall"
command: 'npm'
args: [ 'install' ]
options: _.extend _.clone(globalExecuteOptions), cwd: build
src: [
build + '/package.json'
]
,
taskName: "prepare_config_files"
command: 'node'
args: ['PrepareConfigFiles.js']
options: _.extend _.clone(globalExecuteOptions), cwd: build + '/src/javascripts/BuildUtilities'
src: [
build + '/conf/*.properties'
build + '/conf/*.properties.example'
build + '/conf/*.env'
build + '/src/r/*'
build + '/src/javascripts/BuildUtilities/PrepareConfigFiles.js'
]
,
taskName: "prepareTestJSON"
command: 'node'
args: ['PrepareTestJSON.js']
options: _.extend _.clone(globalExecuteOptions), cwd: build + '/src/javascripts/BuildUtilities'
src: [ build + '/public/javascripts/spec/testFixtures/*.js' ]
,
taskName: "prepareModuleIncludes"
command: 'node'
args: ['PrepareModuleIncludes.js']
options: _.extend _.clone(globalExecuteOptions), cwd: build + '/src/javascripts/BuildUtilities'
src: [
build + '/src/javascripts/BuildUtilities/PrepareModuleIncludes.js'
build + '/app_template.js'
build + '/public/stylesheets/**.css'
build + '/public/html/**.html'
build + '/public/javascripts/**.js'
build + '/public/javascripts/spec/testFixtures/**.js'
build + '/public/javascripts/spec/**.js'
]
,
taskName: "prepareModuleConfJSON"
command: 'node'
args: ['PrepareModuleConfJSON.js']
options: _.extend _.clone(globalExecuteOptions), cwd: build + '/src/javascripts/BuildUtilities'
src: [ build + '/public/javascripts/conf/**/*.js' ]
],
copy: [
taskName: "bin"
src: getGlob('bin/**')
dest: build + '/bin'
options: _.extend _.clone(globalCopyOptions), {}
,
taskName: "envFiles"
src: getGlob('*.env', '.env', 'conf/**.env')
dest: build + '/conf'
options: _.extend _.clone(globalCopyOptions), {}
,
taskName: "public"
src: getGlob('public/**')
dest: build + '/public'
options: _.extend _.clone(globalCopyOptions), {}
,
taskName: "conf"
src: getGlob('conf/**', '!conf/*.coffee')
dest: build + '/conf'
options: _.extend _.clone(globalCopyOptions), {}
,
taskName: "moduleConf"
src: getGlob('modules/**/conf/**', '!modules/**/conf/*.coffee')
dest: build + '/public/conf'
options: _.extend _.clone(globalCopyOptions), {}
renameFunction: getFirstFolderName
,
taskName: "nodeModulesCustomized"
src: getGlob('node_modules_customized/**')
dest: build+"/node_modules_customized"
options: _.extend _.clone(globalCopyOptions), {}
,
taskName: "jade"
src: getGlob('modules/**/src/client/*.jade*')
dest: build + '/views'
options: _.extend _.clone(globalCopyOptions), {}
flatten: true
,
taskName: "r"
src: getGlob('modules/**/src/server/r/**')
dest: build + '/src/r'
options: _.extend _.clone(globalCopyOptions), {}
renameFunction: getRPath
,
taskName: "python"
src: getGlob('modules/**/src/server/python/**', '!modules/**/src/server/python/**/venv/**')
dest: build + '/src/python'
options: _.extend _.clone(globalCopyOptions), {}
renameFunction: getPythonPath
,
taskName: "serverR"
src: getGlob('modules/**/src/server/*.{R,r}')
dest: build + '/src/r'
options: _.extend _.clone(globalCopyOptions), {}
renameFunction: getFirstFolderName
modifyFunction: addREnvironmentCleanUp
,
taskName: "legacyServerR"
src: getGlob('modules/**/src/server/**/*.{R,r}', '!modules/**/src/server/*.{R,r}','!modules/**/src/server/r/*.{R,r}')
dest: build + '/src/r'
options: _.extend _.clone(globalCopyOptions), {}
renameFunction: getLegacyRPath
,
taskName: "html"
src: getGlob('modules/**/src/client/*.html')
dest: build + '/public/html'
options: _.extend _.clone(globalCopyOptions), {}
renameFunction: getFirstFolderName
,
taskName: "css"
src: getGlob('modules/**/src/client/*.css')
dest: build + '/public/stylesheets'
options: _.extend _.clone(globalCopyOptions), {}
renameFunction: getFirstFolderName
,
taskName: "serviceTestsR"
src: getGlob('modules/**/spec/serviceTests/*.{R,r}')
dest: build + '/src/r/spec'
options: _.extend _.clone(globalCopyOptions), {}
renameFunction: getFirstFolderName
,
taskName: "routesJS"
src: getGlob('modules/**/src/server/routes/*.js')
dest: build + '/routes'
options: _.extend _.clone(globalCopyOptions), {}
flatten: true
,
taskName: "CmpdReg"
src: getGlob('modules/CmpdReg/src/**')
dest: build + '/public/CmpdReg'
options: _.extend _.clone(globalCopyOptions), {}
,
taskName: "spec"
src: getGlob("modules/**/spec/**", "!modules/**/spec/**/*.coffee", "!modules/**/spec/testFixtures/*.coffee")
dest: build + '/src/spec'
options: _.extend _.clone(globalCopyOptions), {}
renameFunction: getTestFixuresPath
,
taskName: "serverAssets"
src: getGlob('modules/**/src/server/assets/**')
dest: build + '/src/assets'
options: _.extend _.clone(globalCopyOptions), {}
renameFunction: getAssetsPath
,
taskName: "clientAssets"
src: getGlob('modules/**/src/client/assets/**')
dest: build + '/public/assets'
options: _.extend _.clone(globalCopyOptions), {}
renameFunction: getFirstFolderName
],
others:
packageJSON:
taskName: "packageJSON"
src: getGlob('*package.json')
dest: build
app:
taskName: "app"
command: 'node'
args: startupArgs
options: _.extend _.clone(globalExecuteOptions), cwd: build
src: [
build + '/conf/*.env'
build + '/conf/compiled/*'
build + '/app.js'
build + '/views/*'
build + '/routes/*'
build + '/src/javascripts/**'
build + '/spec/javascripts/**'
]
createExecuteTask = (options) =>
taskName = "execute:#{options.taskName}"
watch = options.watch
gulp.task taskName, (cb) ->
spawn = require('child_process').spawn
command = spawn(options.command, options.args, options.options)
process.on 'exit', ->
command.kill()
return
command.stdout.on 'data', (data) ->
process.stdout.write "#{data}"
return
command.stderr.on 'data', (data) ->
process.stdout.write "#{data}"
return
command.on 'exit', (code) ->
cb code
unless watch == false || !options.src?
watchTaskName = "watch:#{taskName}"
watchOptions = watch?.options ? {}
gulp.task watchTaskName, ->
gulp.watch options.src, watchOptions, gulp.series(taskName)
.on('error', console.error)
return
watchTasks.push watchTaskName
return taskName
onError = (err) ->
@emit 'end'
process.stdout.write '\x07'
if os.platform() == 'darwin'
notify.onError(
title: '<%= error.message %>'
# subtitle: 'Failure!'
message: 'Error: <%= error.stack %>'
sound: false) err
else
notify.onError((options, callback) ->
console.log "\x1b[34m[#{options.message}]\x1b[0m \x1b[31mError: #{options.stack}\x1b[0m"
return
) err
createTask = (options, type) ->
taskName = "#{type}:#{options.taskName}"
taskOptions = options.options
src = options.src
dest = options.dest
watch = options.watch
shouldFlatten = options.flatten ? false
renameFunction = options.renameFunction
modifyFunction = options.modifyFunction
shouldCoffeify = (file) ->
if type=="coffee" && (file.path.indexOf("client/ExcelApp") > -1)
return true
else
return false
shouldCoffee = (file) ->
if type=="coffee" && !(file.path.indexOf("client/ExcelApp") > -1)
return true
else
return false
gulp.task taskName, ->
gulp.src(src, _.extend(taskOptions, {since: gulp.lastRun(taskName)}))
.pipe(plumber({errorHandler: onError}))
.pipe(gulpif(shouldFlatten,flatten()))
.pipe(gulpif(modifyFunction?, modify(fileModifier: modifyFunction)))
.pipe(gulpif(shouldCoffeify, coffeeify({options:{paths:[build+ '/node_modules']}})))
.pipe(gulpif(shouldCoffee,coffee(bare: true)))
.pipe(gulpif(renameFunction?,rename(renameFunction)))
.pipe gulp.dest(dest, {mode: 0o0777})
unless watch == false
watchTaskName = "watch:#{taskName}"
watchOptions = watch?.options ? {}
gulp.task watchTaskName, ->
gulp.watch src, watchOptions, gulp.series(taskName)
return
watchTasks.push watchTaskName
return taskName
# ---------------------------------------------- Create Tasks
watchTasks = []
# --------- Coffee/Watch:Coffee Tasks
coffeeTasks = (createTask(taskConfig,'coffee') for taskConfig in taskConfigs.coffee)
coffeeTasks = _.filter coffeeTasks, (item) -> item != "coffee:publicConf"
gulp.task 'coffee', gulp.parallel coffeeTasks
# --------- Copy/Watch:Copy Tasks
copyTasks = (createTask(taskConfig,'copy') for taskConfig in taskConfigs.copy)
# --------- Execute/Watch:Execute Tasks
executeTasks = (createExecuteTask(taskConfig) for taskConfig in taskConfigs.execute)
# --------- If --conf not passed in then remove prepare_config_files from the execute task
if !argv.conf
executeTasks = _.filter executeTasks, (item) -> item != "execute:prepare_config_files"
# --------- Package JSON Copy/Watch Task
gulp.task "copy:#{taskConfigs.others.packageJSON.taskName}", (done) ->
gulp.src(taskConfigs.others.packageJSON.src).pipe(jeditor((json) ->
delete json.scripts.postinstall
json
)).pipe gulp.dest(taskConfigs.others.packageJSON.dest)
done()
return
copyTasks.push "copy:#{taskConfigs.others.packageJSON.taskName}"
gulp.task "watch:#{taskConfigs.others.packageJSON.taskName}", ->
gulp.watch taskConfigs.others.packageJSON.src, _.clone(globalWatchOptions), gulp.series('copy:packageJSON')
watchTasks.push "watch:#{taskConfigs.others.packageJSON.taskName}"
# --------- App start, watch and restart tasks
gulp.task 'app', (done) =>
if node?
node.kill()
spawn = require('child_process').spawn
node = spawn('node', [ 'app.js' ], stdio: 'inherit', cwd: build)
node.on 'close', (code) ->
if code == 8
gulp.log 'Error detected, waiting for changes...'
return
done()
return
gulp.task "watch:#{taskConfigs.others.app.taskName}", ->
gulp.watch taskConfigs.others.app.src, _.clone(globalWatchOptions), gulp.series('app')
# --------- Remove module conf json unless running dev
unless argv._[0] == "dev"
executeTasks = _.filter executeTasks, (item) -> item != "execute:prepareModuleConfJSON"
# --------- Copy Task
gulp.task 'copy-execute-configs', gulp.series(gulp.parallel('coffee:serverUtilityFunctions','coffee:buildUtilities','coffee:serverAPI', 'copy:conf', 'copy:envFiles'), 'execute:prepare_config_files')
# --------- Copy Task
gulp.task 'copy', gulp.parallel copyTasks
# --------- Execute Task
gulp.task 'execute', gulp.series executeTasks
# --------- Watch Task
gulp.task 'watch', gulp.parallel watchTasks
# --------- Build Task
gulp.task('build', gulp.series('copy-execute-configs', gulp.parallel('copy','coffee'), 'coffee:publicConf', 'execute'));
# --------- Dev Task
gulp.task('dev', gulp.series(gulp.series('build'), gulp.parallel('watch', 'watch:app', 'app')));
# --------- Default Task
gulp.task 'default', gulp.series('build', 'watch')