-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathgulpfile.js
executable file
·584 lines (458 loc) · 19.2 KB
/
gulpfile.js
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
/* LICENSE
_This file is Copyright 2018 by the Image Processing and Analysis Group (BioImage Suite Team). Dept. of Radiology & Biomedical Imaging, Yale School of Medicine._
BioImage Suite Web is licensed under the Apache License, Version 2.0 (the "License");
- you may not use this software except in compliance with the License.
- You may obtain a copy of the License at [http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)
__Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.__
ENDLICENSE */
/* jshint node:true */
"use strict";
require('./config/bis_checknodeversion');
const gulp = require('gulp'),
program=require('commander'),
os = require('os'),
fs = require('fs'),
path=require('path'),
colors=require('colors/safe'),
bis_gutil=require('./config/bis_gulputils'),
rimraf=require('rimraf'),
es = require('event-stream'),
glob = require('glob');
const getTime=bis_gutil.getTime;
const getFileSize=bis_gutil.getFileSize;
// ------------------------------------ Utility Functions ---------------------------------------------
// -----------------------------------------------------------------------------------------
// Command Line stuff
// -----------------------------------------------------------------------------------------
program
.option('-i, --input <s>','mainscript to build')
.option('-m, --minify','flag to minify')
.option('-l, --platform <s>','platform')
.option('-p, --dopack <s>','dopackage 0=electron-packager, 1=run inno or zip in addition',parseInt)
.option('-w, --worker','if present build the webworker as well')
.option('-s, --sworker','if present build the service worker and index.js as well')
.option('--nomain','if present do not build the main bislib.js bundle')
.option('--localhost','only local access')
.option('--portno <s>','port for server (8080 is default)')
.option('--internal <n>','if 1 use internal code, if 2 serve the internal directory as well',parseInt)
.option('--external <n>','if 1 use extra external code (in ../external)',parseInt)
.option('--verbose','verbose')
.option('--tasks','show all tasks')
.parse(process.argv);
if (program.dopack === undefined)
program.dopack=2;
//console.log('Workers=',program.worker,program.sworker);
let options = {
inpfilename : program.input || "all",
outdir : "build/web",
distdir : "build/dist",
verbose : program.verbose || false,
platform : program.platform || os.platform(),
dopack : program.dopack || 0,
webworker : program.worker || false,
eslint : program.eslint,
sworker : program.sworker || false,
nomain : program.nomain || false,
internal : program.internal,
external : program.external || 0 ,
portno : parseInt(program.portno) || 8080,
hostname : '0.0.0.0',
};
if (program.localhost)
options.hostname='localhost';
if (program.minify)
options.minify=1;
else
options.minify=0;
if (program.internal === undefined)
options.internal=1;
if (program.eslint === undefined)
options.eslint=1;
const mainoption=program.rawArgs[2];
// -----------------------------------------------------------------------------------------
// Install and Zip Issues
// Second pass to help with gulp zip and gulp package
// -----------------------------------------------------------------------------------------
if (mainoption=="fullpackage")
options.dopack=2;
options.baseoutput=".";
options.outdir+="/";
let plat=options.platform;
if (plat==='all') {
plat=[ 'win32','linux'];
if (os.platform()!=='win32')
plat.push("darwin");
} else {
plat=options.platform.split(",");
}
options.platform=plat;
if (options.verbose) {
console.log(getTime()+' Full options ='+JSON.stringify(options,null,2)+'.\n');
}
// -----------------------------------------------------------------------------------------
// M A I N P R O G R A M Set internal structure with all parameters
// -----------------------------------------------------------------------------------------
let internal = {
// JS Bundles
bislib : 'bislib.js',
indexlib : 'index.js',
serviceworkerlib : 'bisweb-sw.js',
webworkerlib : 'webworkermain.js',
// CSS Bundles
biscss : 'bislib.css', // Dark Mode
dependcss : [
"./lib/css/bootstrap_dark_edited.css",
"./node_modules/jstree/dist/themes/default/style.css",
"./node_modules/bootstrap-slider/dist/css/bootstrap-slider.min.css",
"./web/biscommon.css"
],
biscss2 : 'bislib_bright.css', // Bright Mode
dependcss2 : [
"./lib/css/bootstrap_bright_edited.css",
"./node_modules/jstree/dist/themes/default/style.css",
"./node_modules/bootstrap-slider/dist/css/bootstrap-slider.min.css",
"./web/biscommon.css"
], // Bright mode
lintscripts : ['js/**/*.js','config/*.js','compiletools/*.js','*.js','web/*.js','test/**/*.js'],
toolarray : [ 'index'],
serveroptions : { },
setwebpackwatch : 0,
};
// Define server options
internal.serveroptions = {
"root" : path.normalize(__dirname),
"host" : options.hostname,
"port" : `${options.portno}`,
'directoryListing': false,
};
if (options.external>0) {
options.external=1;
}
if (options.internal>=2 || options.external>0) {
internal.serveroptions = {
"root" : path.normalize(path.resolve(__dirname,'..'))
};
}
if (options.internal) {
internal.lintscripts.push('../internal/js/*/*.js');
internal.lintscripts.push('../internal/js/*.js');
}
if (options.external) {
internal.lintscripts.push('../external/js/*/*.js');
internal.lintscripts.push('../external/js/*.js');
}
const oldlst=internal.lintscripts;
internal.lintscripts=[];
oldlst.forEach( (nm) => {
const nmlist=glob.sync(nm);
nmlist.forEach( (fname) => {
if (fname.indexOf(".#")<0) {
internal.lintscripts.push(fname);
}
});
});
// ---------------------------
// Get Tool List
// ---------------------------
internal.appinfo=require('./package.json');
internal.setup=require('./web/images/tools.json');
// Let example tools
internal.extra=require('./web/images/examples.json');
let keys2=Object.keys(internal.extra.tools);
for (let i=0;i<keys2.length;i++) {
internal.setup.tools[keys2[i]]=internal.extra.tools[keys2[i]];
}
let keys=Object.keys(internal.setup.tools);
console.log(getTime()+colors.cyan(' Config versiontag='+bis_gutil.getVersionTag(internal.appinfo.version)+' tools='+keys));
if (options.inpfilename === "" || options.inpfilename === "all") {
let obj=internal.setup.tools;
let keys=Object.keys(obj);
let names=['index'];
for (let i=0;i<keys.length;i++) {
let donotbuild=obj[keys[i]]['donotbuild'] || false;
if (!donotbuild)
names.push(keys[i]);
}
options.inpfilename=names.join(",");
}
// ------------------------
// Define webpack jobs
// ------------------------
if (mainoption==="build") {
options.sworker=1;
options.webworker=0;
}
internal.webpackjobs = [ { path: './js/webcomponents/' , name: internal.bislib } ];
if (options.inpfilename === 'index' || options.nomain) {
internal.webpackjobs=[];
}
if (options.sworker) {
internal.webpackjobs.push({ path: './web/' , name : internal.indexlib });
internal.webpackjobs.push({ path: './web/' , name : internal.serviceworkerlib });
}
if (options.webworker) {
internal.webpackjobs.push(
{ path : "./js/webworker/",
name : internal.webworkerlib,
});
}
// -------------------------------
// Verbose Info
// -------------------------------
if (options.verbose) {
console.log(getTime()+' Scripts to process are '+colors.cyan(options.inpfilename));
}
// ------------------------------- ------------------------------- -------------------------------
//
// Preliminaries Over
//
// Define Tasks
//
// ------------------------------- ------------------------------- -------------------------------
function createDate() {
return new Promise( (resolve) => {
const git = require('git-rev');
git.long( (str) => {
bis_gutil.createDateFile(path.resolve(options.outdir,'bisdate.json'),str,internal.appinfo.version);
bis_gutil.createDateFile(path.resolve(options.outdir,'bisdate.js'),str,internal.appinfo.version);
resolve();
});
});
}
gulp.task('eslint', () => {
// ESLint ignores files with "node_modules" paths.
// So, it's best to have gulp ignore the directory as well.
// Also, Be sure to return the stream from the task;
// Otherwise, the task may end before the stream has finished.
const eslint = require('gulp-eslint');
console.log(colors.yellow(getTime(),"Scannng scripts ",internal.lintscripts.join(',')));
return gulp.src(internal.lintscripts)
// eslint() attaches the lint output to the "eslint" property
// of the file object so it can be used by other modules.
.pipe(eslint({
"env": {
"browser": true,
"node": true,
"commonjs": true,
"es6": true
},
"extends": "eslint:recommended",
"parserOptions": {
"sourceType": "module",
"ecmaVersion": 2017
},
"rules": {
'no-console': 'off',
'no-prototype-builtins': 'off',
'indent' : 'off',
'require-atomic-updates' : 'off',
"semi": [
"error",
"always"
]
},
'globals' : [
'BigInt64Array' ,
'BigUint64Array'
]
})).pipe(eslint.format());
});
gulp.task('watch', () => {
return gulp.watch(internal.lintscripts, gulp.series('eslint'));
});
// ------------------------------------------------------------------------
gulp.task('webpack', function (done) {
createDate().then( () => {
bis_gutil.runWebpack(internal.webpackjobs,
options.internal,
options.external,
__dirname,
options.minify,
options.outdir,
options.verbose,
internal.setwebpackwatch).then( () => {
console.log(getTime()+' webpack done num jobs=',internal.webpackjobs.length);
done();
});
});
});
gulp.task('testdata', ((done) => {
const gulpzip = require('gulp-zip');
let outdir=path.resolve(path.join(options.distdir,'test'));
let outfile=path.resolve(path.join(options.distdir,'testdata.zip'));
rimraf.sync(outdir);
rimraf.sync(outfile);
console.log(getTime()+' Creating zip file '+outfile);
es.concat(
gulp.src(['./test/testdata/**/*']).pipe(gulp.dest(outdir+'/testdata')),
gulp.src(['./test/webtestdata/**/*']).pipe(gulp.dest(outdir+'/webtestdata')),
gulp.src(['./test/module_tests.json']).pipe(gulp.dest(outdir)),
).on('end', () => {
console.log(getTime()+' Files copied to '+outdir+', creating '+outfile);
gulp.src([outdir+'/**/*']).pipe(gulpzip(path.basename(outfile))).pipe(gulp.dest(options.distdir)).on('end', () => {
console.log(getTime()+ ' Done zipping '+outfile);
let sz=getFileSize(outfile);
if (sz>0)
console.log(colors.green(getTime()+' ____ zip file created in '+outfile+' (size='+sz+' MB )'));
else
console.log(colors.red(getTime()+' ____ failed to create zip file '+outfile));
done();
});
});
}));
gulp.task('buildtest', ((done) => {
let maincss = './web/biswebtest.css';
let maincss2 = './web/biswebdisplaytest.css';
Promise.all([
bis_gutil.createHTML('biswebtest',options.outdir,'bislib.js',internal.biscss2),
bis_gutil.createCSSCommon([maincss],'biswebtest.css',options.outdir),
bis_gutil.createHTML('biswebdisplaytest',options.outdir,'bislib.js',internal.biscss2),
bis_gutil.createHTML('biswebdisplaytest2',options.outdir,'bislib.js',internal.biscss2),
bis_gutil.createCSSCommon([maincss2],'biswebdisplaytest.css',options.outdir),
]).then( () => { done();});
}));
gulp.task('setwebpackwatch', (done) => {
internal.setwebpackwatch=1;
done();
});
gulp.task('webserver', ()=> {
const webserver = require('gulp-webserver');
console.log(colors.red(getTime()+' server options=',JSON.stringify(internal.serveroptions)));
return gulp.src('.').pipe(webserver(internal.serveroptions));
});
gulp.task('commonfiles', (done) => {
console.log(getTime()+' Copying css,fonts,images');
es.concat(
gulp.src([ 'web/manifest.json']).pipe(gulp.dest(options.outdir)),
gulp.src([ 'web/images/**/*']).pipe(gulp.dest(options.outdir+'/images/')),
gulp.src(["./lib/css/bootstrap_*_edited.css" ]).pipe(gulp.dest(options.outdir+'/css/')),
gulp.src([ 'lib/fonts/*']).pipe(gulp.dest(options.outdir+'/fonts/')),
gulp.src([ 'web/dcm2nii_binaries/**/*']).pipe(gulp.dest(options.outdir+'/dcm2nii_binaries')),
gulp.src('./lib/js/webcomponents-lite.js').pipe(gulp.dest(options.outdir)),
gulp.src('./node_modules/jquery/dist/jquery.min.js').pipe(gulp.dest(options.outdir)),
gulp.src('./node_modules/three/build/three.min.js').pipe(gulp.dest(options.outdir)),
gulp.src('./node_modules/d3/d3.min.js').pipe(gulp.dest(options.outdir)),
gulp.src('./node_modules/bootstrap/dist/js/bootstrap.min.js').pipe(gulp.dest(options.outdir)),
gulp.src('./web/aws/biswebaws.html').pipe(gulp.dest(options.outdir)),
gulp.src('./web/aws/awsparameters.js').pipe(gulp.dest(options.outdir)),
).on('end', () => {
bis_gutil.createHTML('console',options.outdir,'',internal.biscss);
done();
});
});
gulp.task('commonfileselectron', (done) => {
console.log(getTime()+' Copying extra common files for electron.');
es.concat(
gulp.src('./web/bispreload.js').pipe(gulp.dest(options.outdir)),
gulp.src('./web/biselectron.js').pipe(gulp.dest(options.outdir)),
gulp.src('./web/package.json').pipe(gulp.dest(options.outdir)),
).on('end', () => {
done();
});
});
gulp.task('tools', ( (cb) => {
internal.toolarray = options.inpfilename.split(",");
console.log(getTime()+colors.green(' Building tools ['+internal.toolarray+']'));
console.log(getTime()+colors.green(' Building tool : common css'));
let promises=[];
promises.push(bis_gutil.createCSSCommon(internal.dependcss,internal.biscss,options.outdir));
promises.push(bis_gutil.createCSSCommon(internal.dependcss2,internal.biscss2,options.outdir));
for (let index=0;index<internal.toolarray.length;index++) {
let toolname=internal.toolarray[index];
let gpl=true;
let maincss=internal.biscss;
if (toolname!=='index') {
if (internal.setup.tools[toolname].nogpl)
gpl=false;
if (internal.setup.tools[toolname].bright) {
maincss=internal.biscss2;
}
}
console.log(getTime()+colors.green(' Building tool '+(index+1)+'/'+internal.toolarray.length+' : '+toolname+' using '+maincss));
let jsname =internal.bislib;
if (index===0)
jsname=internal.indexlib;
promises.push(bis_gutil.createHTML(toolname,options.outdir,jsname,maincss,gpl));
let customcss='./web/'+toolname+'.css';
if (fs.existsSync(customcss))
promises.push(bis_gutil.createCSSCommon([customcss],toolname+'.css',options.outdir));
let customjs='web/'+toolname+'.js';
if (fs.existsSync(customjs) && toolname!=='index') {
console.log(getTime()+'\tCopying JS '+customjs);
promises.push( new Promise((resolve) => {
gulp.src([ customjs ]).pipe(gulp.dest(options.outdir)).on('end', () => { resolve();});
}));
}
}
Promise.all(promises).then( () => { cb();});
}));
gulp.task('zip', ((done) => {
bis_gutil.createZIPFile(options.baseoutput,options.outdir,internal.appinfo.version,options.distdir,done);
}));
gulp.task('packageint', (done) => {
bis_gutil.createPackage(options.dopack,
internal.setup.tools,
__dirname,options.outdir,internal.appinfo.version,options.platform,options.distdir,done);
});
gulp.task('package', gulp.series('commonfiles','commonfileselectron','packageint'));
gulp.task('clean', (done) => {
rimraf.sync(options.outdir+'tmp');
gulp.src([ options.outdir+'libbiswasm*.js']).
pipe(gulp.dest(options.outdir+'/tmp')).on('end', () => {
const del = require('del');
let arr = [options.outdir+'#*',
options.outdir+'*~',
options.outdir+'*.txt',
options.outdir+'*.js*',
options.outdir+'*.zip',
options.outdir+'*.wasm',
options.outdir+'*.png',
options.outdir+'*.html*',
options.outdir+'*.css*',
options.outdir+'css',
options.outdir+'fonts',
options.outdir+'images',
options.outdir+'doc/*',
options.outdir+'node_modules',
options.outdir+'test',
options.distdir+"/*",
];
console.log(getTime()+' Cleaning files ***** .');
del(arr).then( () => {
gulp.src([ options.outdir+'tmp/*']).pipe(gulp.dest(options.outdir)).on('end', () => {
rimraf.sync(options.outdir+'tmp');
done();
});
});
});
});
gulp.task('jsdoc', (done) => {
bis_gutil.jsDOC(__dirname,'config/jsdoc_conf.json',done);
});
gulp.task('cdoc', (done) => {
bis_gutil.doxygen(__dirname,'config/Doxyfile',done);
});
gulp.task('npmpack', (done) => {
bis_gutil.createnpmpackage(__dirname,internal.appinfo.version,'build/dist',done);
});
// -------------------- Straight compound tasks
gulp.task('buildint', gulp.series('commonfiles','tools','buildtest'));
gulp.task('build',
gulp.series('clean',
gulp.parallel('webpack',
gulp.series('commonfiles','tools','buildtest'))));
gulp.task('buildpackage', gulp.series('buildint',
'package'));
gulp.task('doc', gulp.parallel('jsdoc','cdoc'));
gulp.task('serve',
gulp.series(
'setwebpackwatch',
'webserver',
gulp.parallel(
'webpack')
));
gulp.task('default', gulp.series('serve'));