forked from fluid-project/infusion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Gruntfile.js
538 lines (500 loc) · 20 KB
/
Gruntfile.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
/*
Copyright 2013-2016 OCAD University
Copyright 2014-2016 Raising the Floor - International
Licensed under the Educational Community License (ECL), Version 2.0 or the New
BSD license. You may not use this file except in compliance with one these
Licenses.
You may obtain a copy of the ECL 2.0 License and BSD License at
https://github.com/fluid-project/infusion/raw/master/Infusion-LICENSE.txt
*/
/* eslint-env node */
"use strict";
var _ = require("lodash");
var execSync = require("child_process").execSync;
/**
* Returns a string result from executing a supplied command.
* The command is executed synchronosly.
*
* @param {String} command - the command to execute
* @param {Object} options - optional arguments "verbose" will output a full stack trace when an error occurs,
* "defaultValue" will set the default return value, useful in case of errors or when a result may be an empty string.
* @returns {String} - returns a string representation of the result of the command or the defaultValue.
*/
var getFromExec = function (command, options) {
var result = options.defaultValue;
var stderr = options.verbose ? "pipe" : "ignore";
try {
result = execSync(command, {stdio: ["pipe", "pipe", stderr]});
} catch (e) {
if (options.verbose) {
console.log("Error executing command: " + command);
console.log(e.stack);
}
}
return result;
};
/**
* Adds '.min' convention in front of the first period of a filename string
* Example results:
* infusion-all.js -> infusion-all.min.js
* infusion-all.js.map -> infusion-all.min.js.map
* @param {String} fileName - filename string to add '.min' to
* @returns the modified filename string
*/
var addMin = function (fileName) {
var segs = fileName.split(".");
var min = "min";
if (segs[0] && segs.indexOf(min) < 0 ) {
segs.splice(1, 0, min);
}
return segs.join(".");
};
/**
* Rename function for grunt file tasks for adding ".min" convention
* to filename string; won't do anything to strings that already
* include ".min"
* @param {String} dest - supplied by Grunt task, see http://gruntjs.com/configuring-tasks#the-rename-property
* @param {String} src - supplied by Grunt task, see http://gruntjs.com/configuring-tasks#the-rename-property
*/
var addMinifyToFilename = function (dest, src) {
return dest + addMin(src);
};
module.exports = function (grunt) {
var setBuildSettings = function (settings) {
grunt.config.set("buildSettings", {}); // delete previous settings
_.forEach(settings, function (value, setting) {
var settingPath = ["buildSettings", setting].join(".");
grunt.config.set(settingPath, value);
});
};
// Project configuration.
grunt.initConfig({
pkg: grunt.file.readJSON("package.json"),
revision: getFromExec("git rev-parse --verify --short HEAD", {defaultValue: "Unknown revision, not within a git repository"}),
branch: getFromExec("git rev-parse --abbrev-ref HEAD", {defaultValue: "Unknown branch, not within a git repository"}),
allBuildName: "<%= pkg.name %>-all",
buildSettings: {}, // set by the build tasks
customBuildName: "<%= pkg.name %>-<%= buildSettings.name %>",
banner: "/*!\n <%= pkg.name %> - v<%= pkg.version %>\n <%= grunt.template.today('dddd, mmmm dS, yyyy, h:MM:ss TT') %>\n branch: <%= branch %> revision: <%= revision %>*/\n",
clean: {
build: "build",
products: "products",
stylus: "src/framework/preferences/css/*.css",
stylusDist: "dist/assets/**/stylus", // removes the empty stylus directory from the distribution
ciArtifacts: ["*.tap"],
dist: "dist",
postBuild: {
files: [{}]
}
},
copy: {
all: {
files: [{
expand: true,
src: ["src/**", "tests/**", "demos/**", "examples/**"],
dest: "build/"
}]
},
custom: {
files: [{
expand: true,
src: "<%= modulefiles.custom.output.dirs %>",
dest: "build/"
}]
},
necessities: {
files: [{
src: ["README.*", "ReleaseNotes.*", "Infusion-LICENSE.*"],
dest: "build/"
}, {
// The jQuery license file needs to be copied explicitly since
// "src/lib/jQuery" directory contains several jQuery modules
// that have individual dependencies.json files.
src: "src/lib/jQuery/jQuery-LICENSE.txt",
dest: "build/lib/jQuery/jQuery-LICENSE.txt",
filter: function () {
return grunt.file.exists("build/lib/jQuery/");
}
}]
},
distJS: {
files: [{
expand: true,
cwd: "build/",
src: "<%= allBuildName %>.*",
dest: "dist/",
rename: function (dest, src) {
return grunt.config.get("buildSettings.compress") ? addMinifyToFilename(dest, src) : dest + src;
}
}, {
expand: true,
cwd: "build/",
src: "<%= customBuildName %>.*",
dest: "dist/",
rename: function (dest, src) {
return grunt.config.get("buildSettings.compress") ? addMinifyToFilename(dest, src, "js") : dest + src;
}
}]
},
distAssets: {
files: [{
expand: true,
cwd: "build/",
src: ["src/lib/fonts/**", "src/framework/preferences/fonts/**", "src/framework/preferences/images/**"],
dest: "dist/assets/"
}]
}
},
uglify: {
options: {
banner: "<%= banner %>",
mangle: false,
sourceMap: true,
sourceMapIncludeSources: true
},
all: {
files: [{
src: "<%= modulefiles.all.output.files %>",
dest: "./build/<%= allBuildName %>.js"
}]
},
custom: {
files: [{
src: "<%= modulefiles.custom.output.files %>",
dest: "./build/<%= customBuildName %>.js"
}]
}
},
modulefiles: {
all: {
src: ["src/**/*Dependencies.json"]
},
custom: {
options: {
exclude: "<%= buildSettings.exclude %>",
include: "<%= buildSettings.include %>"
},
src: ["src/**/*Dependencies.json"]
}
},
map: {
// append "/**" to the end of all of all of
// directory paths for copy:custom to ensure that
// all of the subdirectories and files are copied over
copyDirs: {
files: "<%= copy.custom.files %>",
prop: "copy.custom.files.0.src",
fn: function (str) {
return str + "/**";
}
},
postBuildClean: {
files: "<%= clean.postBuild.files %>",
prop: "clean.postBuild.files.0.src",
fn: function (str) {
var buildPath = "build/";
return str.startsWith(buildPath) ? str : buildPath + str;
}
}
},
// Still need the concat task as uglify does not honour the {compress: false} option
// see: https://github.com/mishoo/UglifyJS2/issues/696
concat: {
options: {
separator: ";\n",
banner: "<%= banner %>",
sourceMap: true
},
all: {
nonull: true,
cwd: "./build/", // Src matches are relative to this path.
src: "<%= modulefiles.all.output.files %>",
dest: "./build/<%= allBuildName %>.js"
},
custom: {
nonull: true,
cwd: "./build/", // Src matches are relative to this path.
src: "<%= modulefiles.custom.output.files %>",
dest: "./build/<%= customBuildName %>.js"
}
},
compress: {
all: {
options: {
archive: "products/<%= allBuildName %>-<%= pkg.version %>.zip"
},
files: [{
expand: true, // Enable dynamic expansion.
cwd: "./build/", // Src matches are relative to this path.
src: ["**/*"], // Actual pattern(s) to match.
dest: "./infusion" // Destination path prefix in the zip package
}]
},
custom: {
options: {
archive: "products/<%= customBuildName %>-<%= pkg.version %>.zip"
},
files: "<%= compress.all.files %>"
}
},
eslint: {
all: ["src/**/*.js", "tests/**/*.js", "demos/**/*.js", "examples/**/*.js", "*.js"]
},
jsonlint: {
all: ["src/**/*.json", "tests/**/*.json", "demos/**/*.json", "examples/**/*.json"]
},
stylus: {
compile: {
options: {
compress: "<%= buildSettings.compress %>",
relativeDest: ".."
},
files: [{
expand: true,
src: ["src/**/css/stylus/*.styl"],
ext: ".css"
}]
},
dist: {
options: {
compress: "<%= buildSettings.compress %>",
relativeDest: ".."
},
files: [{
expand: true,
src: ["src/**/css/stylus/*.styl"],
ext: "<% buildSettings.compress ? print('.min.css') : print('.css') %>",
dest: "dist/assets/"
}]
}
},
// grunt-contrib-watch task to watch and rebuild stylus files
// automatically when doing stylus development
watch: {
buildStylus: {
files: ["src/**/css/stylus/*.styl", "src/**/css/stylus/utils/*.styl"],
tasks: "buildStylus"
}
},
shell: {
runTests: {
command: "vagrant ssh -c 'cd /home/vagrant/sync/; DISPLAY=:0 testem ci --file tests/testem.json'"
}
},
distributions:
{
"all": {},
"all.min": {
options: {
compress: true
}
},
"all-no-jquery": {
options: {
exclude: "jQuery, jQueryUI"
}
},
"all-no-jquery.min": {
options: {
exclude: "jQuery, jQueryUI",
compress: true
}
},
"framework": {
options: {
include: "framework"
}
},
"framework.min": {
options: {
include: "framework",
compress: true
}
},
"framework-no-jquery": {
options: {
include: "framework",
exclude: "jQuery, jQueryUI"
}
},
"framework-no-jquery.min": {
options: {
include: "framework",
exclude: "jQuery, jQueryUI",
compress: true
}
}
}
});
// Load the plugins:
grunt.loadNpmTasks("grunt-contrib-uglify");
grunt.loadNpmTasks("grunt-contrib-clean");
grunt.loadNpmTasks("grunt-contrib-copy");
grunt.loadNpmTasks("grunt-contrib-concat");
grunt.loadNpmTasks("grunt-contrib-compress");
grunt.loadNpmTasks("fluid-grunt-eslint");
grunt.loadNpmTasks("grunt-jsonlint");
grunt.loadNpmTasks("grunt-modulefiles");
grunt.loadNpmTasks("grunt-contrib-stylus");
grunt.loadNpmTasks("grunt-shell");
grunt.loadNpmTasks("grunt-contrib-watch");
// Custom tasks:
// Simple task for transforming a property
grunt.registerMultiTask("map", "a task wrapper around the map function from lodash", function () {
var transformed = _.map(this.filesSrc, this.data.fn);
grunt.config.set(this.data.prop, transformed);
});
grunt.registerTask("pathMap", "Triggers the map task for the specified build target", function (target) {
grunt.task.run("map:postBuildClean");
if (target === "custom") {
grunt.task.run("map:copyDirs");
}
});
grunt.registerTask("setPostBuildCleanUp", "Sets the file source for post build cleanup", function (target) {
grunt.config.set("clean.postBuild.files.0.src", "<%= modulefiles." + target + ".output.files %>");
});
// Task for organizing the build
grunt.registerTask("build", "Generates a minified or source distribution for the specified build target", function (target) {
target = target || "all";
setBuildSettings({
name: grunt.option("name") || "custom",
exclude: grunt.option("exclude"),
include: grunt.option("include"),
compress: !grunt.option("source"),
target: target
});
var concatTask = grunt.config.get("buildSettings.compress") ? "uglify:" : "concat:";
var tasks = [
"clean",
"lint",
"stylus:compile",
"modulefiles:" + target,
"setPostBuildCleanUp:" + target,
"pathMap:" + target,
"copy:" + target,
"copy:necessities",
concatTask + target,
"compress:" + target,
"clean:postBuild"
];
grunt.task.run(tasks);
});
grunt.registerMultiTask("distributions", "Enables a project to split its files into a set of modules. A module's information is stored in a json file containing a name for the module, the files it contains, and other modules it depends on. The module files can then be accumulated into various configurations of included and excluded modules, which can be fed into other plugins (e.g. grunt-contrib-concat) for packaging.", function () {
// Merge task-specific and/or target-specific options with these defaults.
var options = this.options({
name: this.target,
source: true,
target: "all",
compress: false
});
if (options.exclude || options.include) {
options.target = "custom";
}
setBuildSettings(options);
var concatTask = options.compress ? "uglify:" : "concat:";
var tasks = [
"cleanForDist",
"stylus:dist",
"modulefiles:" + options.target,
"pathMap:" + options.target,
"copy:" + options.target,
"copy:necessities",
concatTask + options.target,
"copy:distJS",
"copy:distAssets"
];
grunt.task.run(tasks);
});
grunt.registerTask("buildDists", "Tasks to run before publishing to NPM", function (target) {
var tasks = [
"clean",
"lint",
"distributions" + ( target ? ":" + target : "" ),
"cleanForDist",
"verifyDistJS",
"verifyDistCSS"
];
grunt.task.run(tasks);
});
/** Verifies that directory "contains the files in fileList
* logs a report
* @param {String} dir - base directory expected to contain files
* @param {Array} fileList - array of string filenames to check; may include
* full paths and thereby search subdirectories of dir
* @returns a report structure for further processing
*/
var verifyFiles = function (dir, fileList) {
var report = {
fileList: {},
missingFiles: 0,
expectedFiles: fileList.length
};
_.forEach(fileList, function (fileName) {
var fileExists = grunt.file.exists(dir, fileName);
if (!fileExists) {
report.missingFiles = report.missingFiles + 1;
}
report.fileList[dir + "/" + fileName] = {"present": fileExists};
});
return report;
};
/** Displays a file verification report generated by verifyFiles
* @param {Object} report - the report to display
*/
var displayVerifyFilesReport = function (report) {
_.forEach(report.fileList, function (value, fileName) {
var fileExists = value.present;
if (fileExists) {
grunt.log.oklns(fileName + " - ✓ Present".green);
} else {
grunt.log.errorlns(fileName + " - ✗ Missing".red);
}
});
};
/** Processes a file verification report, and fails the build if
* any files are missing
* @param {Object} report - the report to process
*/
var processVerifyFilesReport = function (report) {
if (report.missingFiles > 0) {
grunt.log.subhead("Verification failed".red);
grunt.fail.fatal(report.missingFiles + " out of " + report.expectedFiles + " expected files not found");
} else {
grunt.log.subhead("Verification passed".green);
grunt.log.oklns("All expected files were present");
}
};
grunt.registerTask("verifyDistJS", "Verifies that the expected /dist/*.js files and their source maps were created", function () {
grunt.log.subhead("Verifying that expected distribution JS files are present in /dist directory");
var expectedFilenames = [];
var distributions = grunt.config.get("distributions");
_.forEach(distributions, function (value, distribution) {
var jsFilename = "infusion-" + distribution + ".js";
var mapFilename = jsFilename + ".map";
expectedFilenames.push(jsFilename, mapFilename);
});
var report = verifyFiles("dist", expectedFilenames);
displayVerifyFilesReport(report);
processVerifyFilesReport(report);
});
grunt.registerTask("verifyDistCSS", "Verifies that the expected /dist/ CSS files were created", function () {
grunt.log.subhead("Verifying that expected distribution CSS files are present in /dist/assets directory");
var expectedFilenames = [];
var preferencesStylusFiles = grunt.file.expand("src/framework/preferences/css/stylus/*.styl");
_.forEach(preferencesStylusFiles, function (stylusFile) {
var cssFilename = stylusFile.replace(".styl", ".css");
var minifiedCSSFilename = stylusFile.replace(".styl", ".min.css");
// Remove /stylus from the path, since dist/assets won't have it
expectedFilenames.push(cssFilename.replace("/stylus", ""), minifiedCSSFilename.replace("/stylus", ""));
});
var report = verifyFiles("dist/assets", expectedFilenames);
displayVerifyFilesReport(report);
processVerifyFilesReport(report);
});
grunt.registerTask("cleanForDist", ["clean:build", "clean:products", "clean:stylus", "clean:stylusDist", "clean:ciArtifacts"]);
grunt.registerTask("buildStylus", ["clean:stylus", "stylus:compile"]);
grunt.registerTask("default", ["build:all"]);
grunt.registerTask("custom", ["build:custom"]);
grunt.registerTask("lint", "Apply eslint and jsonlint", ["eslint", "jsonlint"]);
grunt.registerTask("tests", "Run tests", ["shell:runTests"]);
};