This repository has been archived by the owner on Aug 10, 2018. It is now read-only.
forked from oaeproject/3akai-ux
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Gruntfile.js
345 lines (319 loc) · 14.3 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
/*!
* Copyright 2013 Apereo Foundation (AF) Licensed under the
* Educational Community License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may
* obtain a copy of the License at
*
* http://opensource.org/licenses/ECL-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.
*/
var _ = require('underscore');
var util = require('util');
var vm = require('vm');
module.exports = function(grunt) {
// Project configuration.
grunt.initConfig({
'pkg': '<json:package.json>',
'meta': {
'banner': '/*! <%= pkg.name %> - v<%= pkg.version %> - ' +
'<%= grunt.template.today("yyyy-mm-dd") %>\n' +
'<%= pkg.homepage ? "* " + pkg.homepage + "\n" : "" %>' +
'* Copyright (c) <%= grunt.template.today("yyyy") %> <%= pkg.author.name %>;' +
' Licensed <%= _.pluck(pkg.licenses, "type").join(", ") %> */'
},
'target': process.env['DESTDIR'] || 'target',
'qunit': {
'files': ['tests/qunit/tests/*.html']
},
'lint': {
'files': [
'grunt.js',
'admin/**/*.js',
'shared/**/*.js',
'ui/**/*.js',
'node_modules/oae-*/**/*.js',
'!node_modules/oae-release-tools/**'
]
},
'watch': {
'files': '<config:lint.files>',
'tasks': 'lint test'
},
'jshint': {
'options': {
'sub': true
},
'globals': {
'exports': true,
'module': false
}
},
'clean': {
'folder': '<%= target %>/'
},
'copy': {
'main': {
'files': [
{
'expand': true,
'src': [
'**',
'!<%= target %>/**',
'!tests/**',
'!tools/**',
'!node_modules/.*/**',
'!node_modules/grunt*/**',
'!node_modules/oae-release-tools/**',
'!node_modules/optimist/**',
'!node_modules/properties-parser/**',
'!node_modules/readdirp/**',
'!node_modules/underscore/**'
],
'dest': '<%= target %>/original'
}
]
}
},
'requirejs': {
'optimize': {
'options': {
'appDir': './',
'baseUrl': './shared',
'mainConfigFile': './shared/oae/api/oae.bootstrap.js',
'dir': '<%= target %>/optimized',
'optimize': 'uglify',
'preserveLicenseComments': false,
'optimizeCss': 'standard',
'cssImportIgnore': null,
'inlineText': true,
'useStrict': false,
'pragmas': {},
'skipPragmas': false,
'skipModuleInsertion': false,
'modules': [{
'name': 'oae.core',
'exclude': ['jquery']
}],
'fileExclusionRegExp': /^(\.|<%= target %>|tests|tools|grunt|optimist|properties-parser|readdirp|underscore$|oae-release-tools)/,
'logLevel': 2
}
}
},
'ver': {
'oae': {
'basedir': '<%= target %>/optimized',
'phases': [
{
// Rename and hash these folders
'folders': [
'<%= target %>/optimized/shared/bundles',
'<%= target %>/optimized/ui/bundles',
'<%= target %>/optimized/admin/bundles',
'<%= target %>/optimized/shared/vendor/js/l10n/cultures'
],
// Rename and hash these files
'files': _hashFiles([
'<%= target %>/optimized/shared',
'<%= target %>/optimized/ui',
'<%= target %>/optimized/admin',
'<%= target %>/optimized/docs'
], ['html', 'json', 'ico', 'less'], [
'!<%= target %>/optimized/shared/vendor/js/l10n/cultures.*/**',
'!<%= target %>/optimized/ui/bundles.*/**',
'<%= target %>/optimized/shared/oae/macros/*.html'
]),
// Look for and replace references to the above (non-excluded) files and folders in these files
'references': [
'<%= target %>/optimized/shared/**/*.html',
'<%= target %>/optimized/shared/**/*.js',
'<%= target %>/optimized/shared/**/*.css',
'<%= target %>/optimized/ui/**/*.html',
'<%= target %>/optimized/ui/**/*.js',
'<%= target %>/optimized/ui/**/*.css',
'<%= target %>/optimized/admin/**/*.html',
'<%= target %>/optimized/admin/**/*.js',
'<%= target %>/optimized/admin/**/*.css',
'<%= target %>/optimized/docs/**/*.html',
'<%= target %>/optimized/docs/**/*.js',
'<%= target %>/optimized/docs/**/*.css',
'<%= target %>/optimized/node_modules/oae-*/**/*.html',
'<%= target %>/optimized/node_modules/oae-*/**/*.js',
'<%= target %>/optimized/node_modules/oae-*/**/*.css',
'<%= target %>/optimized/node_modules/oae-*/**/*.json'
]
}
],
'version': '<%= target %>/optimized/hashes.json'
}
},
'git-describe': {
'oae': {}
},
'casperjs': {
options: {},
files: ['tests/casperjs/suites/*.js']
}
});
// Load tasks from npm modules
grunt.loadNpmTasks('grunt-contrib-clean');
grunt.loadNpmTasks('grunt-contrib-copy');
grunt.loadNpmTasks('grunt-contrib-qunit');
grunt.loadNpmTasks('grunt-git-describe');
grunt.loadNpmTasks('grunt-ver');
grunt.loadNpmTasks('grunt-contrib-requirejs');
grunt.loadNpmTasks('grunt-casperjs');
// Task to write the version to a file
grunt.registerTask('writeVersion', function() {
this.requires('git-describe');
var json = grunt.template.process('{"oae:ux-version":"<%= meta.version %>"}');
grunt.file.write(grunt.config('target') + '/optimized/ui/version.json', json);
});
// Task to fill out the nginx config template
grunt.registerTask('configNginx', function() {
var infile = './nginx/nginx.json';
if (grunt.file.exists(infile)) {
var nginxConfig = require(infile);
var template = grunt.file.read('./nginx/nginx.conf');
grunt.config.set('nginxConf', nginxConfig);
var config = grunt.template.process(template);
var outfile = grunt.config('target') + '/optimized/nginx/nginx.conf';
grunt.file.write(outfile, config);
grunt.log.writeln('nginx.conf rendered at '.green + outfile.green);
} else {
var msg = 'No ' + infile + ' found, not rendering nginx.conf template';
grunt.log.writeln(msg.yellow);
}
});
// Task to hash files
grunt.registerTask('hashFiles', function() {
this.requires('requirejs');
// Add the modules as phases to ver:oae
var oaeModules = grunt.file.expand({filter:'isDirectory'}, grunt.config('target') + '/optimized/node_modules/oae-*/*');
oaeModules.forEach(function(module) {
grunt.log.writeln(module);
var config = {
'folders': [ module + '/bundles' ],
'files': _hashFiles([module], ['json']),
'references': [
module + '/**/*.html',
module + '/**/*.js',
module + '/**/*.css',
module + '/*.json'
]
};
grunt.config.set('ver.' + module + '.basedir', module);
grunt.config.set('ver.' + module + '.phases', [config]);
grunt.task.run('ver:' + module);
});
grunt.task.run('ver:oae');
grunt.task.run('updateBootstrapPaths');
});
// Task to update the paths in oae.bootstrap to the hashed versions
grunt.registerTask('updateBootstrapPaths', function() {
this.requires('ver:oae');
var basedir = grunt.config('target') + '/optimized/';
var hashedPaths = require('./' + grunt.config.get('ver.oae.version'));
var bootstrapPath = basedir + hashedPaths['/shared/oae/api/oae.bootstrap.js'];
var bootstrap = grunt.file.read(bootstrapPath);
var regex = /("|')?paths("|')?: ?\{[^}]*\}/;
var scriptPaths = 'paths = {' + bootstrap.match(regex)[0] + '}';
var paths = vm.runInThisContext(scriptPaths).paths;
// Update the bootstrap file with the hashed paths
Object.keys(paths).forEach(function(key) {
var prefix = '/shared/';
var path = prefix + paths[key] + '.js';
var hashedPath = '';
if (hashedPaths[path]) {
hashedPath = hashedPaths[path];
// trim off prefix and .js
paths[key] = hashedPath.substring(prefix.length, hashedPath.length - 3);
}
});
bootstrap = bootstrap.replace(regex, 'paths:' + JSON.stringify(paths));
grunt.file.write(bootstrapPath, bootstrap);
grunt.log.writeln('Boots strapped'.green);
});
// A task that will copy the release files to a directory of your choosing
grunt.registerTask('copyReleaseArtifacts', function(outputDir) {
if (!outputDir) {
return grunt.log.writeln('Please provide a path where the release files should be copied to'.red);
}
var config = {
'files': [
{
'expand': true,
'src': ['./<%= grunt.config("target") %>/*', './README.md', './LICENSE', './COMMITTERS.txt'],
'dest': outputDir
}
]
};
grunt.config.set('copy.release', config);
grunt.task.run('copy:release');
});
// Release task.
// This essentially runs the default task and then copies the target directory to the `outputDir`
//
// Example:
// grunt release:/tmp/release
//
// This will run the entire UI build (minification, hashing, nginx config, etc, ..) and create the following folders:
// /tmp/release/optimized - contains the minified UI sources
// /tmp/release/original - contains the original UI files
grunt.registerTask('release', function(outputDir) {
if (!outputDir) {
return grunt.log.writeln('Please provide a path where the release files should be copied to'.red);
}
// Run the default task that will minify and hash all the UI files.
grunt.task.run('default');
// Copy the minified and original files to the output directory
// and give them the proper names so no config changes in Hilary need to occur
grunt.task.run('copyReleaseArtifacts:' + outputDir);
});
// Wrap the QUnit task
grunt.renameTask('qunit', 'contrib-qunit');
grunt.registerTask('qunit', function(host) {
// Fall back to the `qunit-host` option
host = host || grunt.option('qunit-host');
if (!host) {
return grunt.fail.fatal('Please provide a link to a running OAE instance. e.g. `grunt qunit:tenant1.oae.com` or `grunt qunit --qunit-host tenant1.oae.com`');
}
var urls = _.map(grunt.file.expand(grunt.config.get('qunit.files')), function(file) {
return 'http://' + host + '/' + file;
});
var config = {'options': {'urls': urls}};
grunt.config.set('contrib-qunit.all', config);
grunt.task.run('contrib-qunit');
});
// Run the CasperJS and QUnit tests
grunt.registerTask('test', ['casperjs', 'qunit']);
// Default task for production build
grunt.registerTask('default', ['clean', 'copy', 'git-describe', 'requirejs', 'hashFiles', 'writeVersion', 'configNginx']);
};
/**
* Generate the glob expressions to match all files that have extensions that are supposed to be hashed (as defined
* by `HASHED_EXTENSIONS`). You can optionally exclude extensions for special cases.
*
* @param {String[]} directories The list of directories whose files to hash
* @param {String[]} [excludeExts] The extensions to exclude from the list of `HASHED_EXTENSIONS`, if any
* @param {String[]} [extra] Extra glob patterns to append, in addition to the ones added for the extensions
* @return {String[]} An array of glob expressions that match the files to hash in the directories
* @api private
*/
var _hashFiles = function(directories, excludeExts, extra) {
excludeExts = excludeExts || [];
var globs = [];
directories.forEach(function(directory) {
globs.push(util.format('%s/**', directory));
excludeExts.forEach(function(ext) {
// Exclude both direct children of the exlucded extensions, and all grandchildren
globs.push(util.format('!%s/*.%s', directory, ext));
globs.push(util.format('!%s/**/*.%s', directory, ext));
});
});
return (extra) ? _.union(globs, extra) : globs;
};