MDL-52127 grunt: be aware of third party paths
[moodle.git] / Gruntfile.js
CommitLineData
adeb96d2
DW
1// This file is part of Moodle - http://moodle.org/
2//
3// Moodle is free software: you can redistribute it and/or modify
4// it under the terms of the GNU General Public License as published by
5// the Free Software Foundation, either version 3 of the License, or
6// (at your option) any later version.
7//
8// Moodle is distributed in the hope that it will be useful,
9// but WITHOUT ANY WARRANTY; without even the implied warranty of
10// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11// GNU General Public License for more details.
12//
13// You should have received a copy of the GNU General Public License
14// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
0b777a06 15/* jshint node: true, browser: false */
3adb62b7 16/* eslint-env node */
adeb96d2
DW
17
18/**
19 * @copyright 2014 Andrew Nicols
20 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21 */
22
23/**
24 * Grunt configuration
25 */
26
27module.exports = function(grunt) {
28 var path = require('path'),
e67585f8 29 tasks = {},
30db70ab
DP
30 cwd = process.env.PWD || process.cwd(),
31 async = require('async'),
32 DOMParser = require('xmldom').DOMParser,
33 xpath = require('xpath');
00cceb7f
DP
34
35 // Windows users can't run grunt in a subdirectory, so allow them to set
36 // the root by passing --root=path/to/dir.
37 if (grunt.option('root')) {
38 var root = grunt.option('root');
39 if (grunt.file.exists(__dirname, root)) {
40 cwd = path.join(__dirname, root);
41 grunt.log.ok('Setting root to '+cwd);
42 } else {
43 grunt.fail.fatal('Setting root to '+root+' failed - path does not exist');
44 }
45 }
46
47 var inAMD = path.basename(cwd) == 'amd';
adeb96d2 48
5cc5f311
DP
49 // Globbing pattern for matching all AMD JS source files.
50 var amdSrc = [inAMD ? cwd + '/src/*.js' : '**/amd/src/*.js'];
51
52 /**
53 * Function to generate the destination for the uglify task
54 * (e.g. build/file.min.js). This function will be passed to
55 * the rename property of files array when building dynamically:
56 * http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically
57 *
58 * @param {String} destPath the current destination
59 * @param {String} srcPath the matched src path
60 * @return {String} The rewritten destination path.
61 */
62 var uglify_rename = function (destPath, srcPath) {
63 destPath = srcPath.replace('src', 'build');
64 destPath = destPath.replace('.js', '.min.js');
65 destPath = path.resolve(cwd, destPath);
66 return destPath;
67 };
68
30db70ab
DP
69 /**
70 * Find thirdpartylibs.xml and generate an array of paths contained within
71 * them (used to generate ignore files and so on).
72 *
73 * @return {array} The list of thirdparty paths.
74 */
75 var getThirdPartyPathsFromXML = function() {
76 var thirdpartyfiles = grunt.file.expand('*/**/thirdpartylibs.xml');
77 var libs = ['node_modules/', 'vendor/'];
78
79 thirdpartyfiles.forEach( function(file) {
80 var dirname = path.dirname(file);
81
82 var doc = new DOMParser().parseFromString(grunt.file.read(file));
83 var nodes = xpath.select("/libraries/library/location/text()", doc);
84
85 nodes.forEach(function(node) {
86 var lib = path.join(dirname, node.toString());
87 if (grunt.file.isDir(lib)) {
88 // Ensure trailing slash on dirs.
89 lib = lib.replace(/\/?$/, '/');
90 }
91
92 // Look for duplicate paths before adding to array.
93 if (libs.indexOf(lib) === -1) {
94 libs.push(lib);
95 }
96 });
97 });
98 return libs;
99 };
100
101 // An array of paths to third party directories.
102 var thirdPartyPaths = getThirdPartyPathsFromXML();
103
104 /**
105 * Determine if the file is a Moodle file, or its listed in
106 * the thirdpartylibs.xml file paths as a third party file.
107 *
108 * @param {string} file The file path to determine if thirdparty
109 * @return {bool} false If thid party file.
110 */
111 var isMoodleFile = function(file) {
112 if (grunt.file.isMatch(thirdPartyPaths, file)) {
113 return false;
114 }
115 return true;
116 };
117
adeb96d2
DW
118 // Project configuration.
119 grunt.initConfig({
120 jshint: {
121 options: {jshintrc: '.jshintrc'},
5cc5f311 122 amd: { src: amdSrc }
adeb96d2 123 },
3adb62b7 124 eslint: {
30db70ab
DP
125 // Even though warnings dont stop the build we don't display warnings by default because
126 // at this moment we've got too many core warnings.
3adb62b7 127 options: { quiet: !grunt.option('show-lint-warnings') },
be4b3cc6
DP
128 // Check AMD files. We add some stricter rules which we can't apply to the default configuration due
129 // to YUI rollups.
130 amd: {
131 src: amdSrc,
30db70ab 132 filter: isMoodleFile,
be4b3cc6
DP
133 options: { rules: {'no-undef': 'error', 'no-unused-vars': 'error', 'no-empty': 'error', 'no-unused-expressions': 'error'} }
134 },
135 // Check YUI module source files.
a1587268 136 yui: {
30db70ab
DP
137 src: ['**/yui/src/**/*.js', '!*/**/yui/src/*/meta/*.js'],
138 filter: isMoodleFile
a1587268 139 }
3adb62b7 140 },
adeb96d2 141 uglify: {
5cc5f311
DP
142 amd: {
143 files: [{
144 expand: true,
145 src: amdSrc,
146 rename: uglify_rename
147 }]
adeb96d2 148 }
a4a52e56
DP
149 },
150 less: {
151 bootstrapbase: {
152 files: {
153 "theme/bootstrapbase/style/moodle.css": "theme/bootstrapbase/less/moodle.less",
154 "theme/bootstrapbase/style/editor.css": "theme/bootstrapbase/less/editor.less",
155 },
156 options: {
157 compress: true
158 }
159 }
8efbb7b1
DP
160 },
161 watch: {
162 options: {
163 nospawn: true // We need not to spawn so config can be changed dynamically.
164 },
165 amd: {
166 files: ['**/amd/src/**/*.js'],
167 tasks: ['amd']
168 },
169 bootstrapbase: {
170 files: ["theme/bootstrapbase/less/**/*.less"],
171 tasks: ["less:bootstrapbase"]
172 },
173 yui: {
174 files: ['**/yui/src/**/*.js'],
a1587268 175 tasks: ['yui']
8efbb7b1 176 },
1aa454ed
DP
177 },
178 shifter: {
179 options: {
180 recursive: true,
181 paths: [cwd]
182 }
adeb96d2
DW
183 }
184 });
185
30db70ab
DP
186 /**
187 * Generate ignore files (utilising thirdpartylibs.xml data)
188 */
189 tasks.ignorefiles = function() {
190 // Generate .eslintignore.
191 var eslintIgnores = ['# Generated by "grunt ignorefiles"', '*/**/yui/src/*/meta/', '*/**/build/'].concat(thirdPartyPaths);
192 grunt.file.write('.eslintignore', eslintIgnores.join('\n'));
193 };
194
1aa454ed
DP
195 /**
196 * Shifter task. Is configured with a path to a specific file or a directory,
197 * in the case of a specific file it will work out the right module to be built.
198 *
199 * Note that this task runs the invidiaul shifter jobs async (becase it spawns
200 * so be careful to to call done().
201 */
adeb96d2 202 tasks.shifter = function() {
30db70ab 203 var done = this.async(),
1aa454ed 204 options = grunt.config('shifter.options');
adeb96d2 205
1aa454ed
DP
206 // Run the shifter processes one at a time to avoid confusing output.
207 async.eachSeries(options.paths, function (src, filedone) {
208 var args = [];
e67585f8
DW
209 args.push( path.normalize(__dirname + '/node_modules/shifter/bin/shifter'));
210
1aa454ed
DP
211 // Always ignore the node_modules directory.
212 args.push('--excludes', 'node_modules');
213
adeb96d2 214 // Determine the most appropriate options to run with based upon the current location.
1aa454ed
DP
215 if (grunt.file.isMatch('**/yui/**/*.js', src)) {
216 // When passed a JS file, build our containing module (this happen with
217 // watch).
218 grunt.log.debug('Shifter passed a specific JS file');
219 src = path.dirname(path.dirname(src));
220 options.recursive = false;
221 } else if (grunt.file.isMatch('**/yui/src', src)) {
222 // When in a src directory --walk all modules.
adeb96d2
DW
223 grunt.log.debug('In a src directory');
224 args.push('--walk');
1aa454ed
DP
225 options.recursive = false;
226 } else if (grunt.file.isMatch('**/yui/src/*', src)) {
227 // When in module, only build our module.
adeb96d2 228 grunt.log.debug('In a module directory');
adeb96d2 229 options.recursive = false;
1aa454ed
DP
230 } else if (grunt.file.isMatch('**/yui/src/*/js', src)) {
231 // When in module src, only build our module.
232 grunt.log.debug('In a source directory');
233 src = path.dirname(src);
234 options.recursive = false;
adeb96d2
DW
235 }
236
1aa454ed
DP
237 if (grunt.option('watch')) {
238 grunt.fail.fatal('The --watch option has been removed, please use `grunt watch` instead');
adeb96d2
DW
239 }
240
adeb96d2
DW
241 // Add the stderr option if appropriate
242 if (grunt.option('verbose')) {
243 args.push('--lint-stderr');
244 }
245
a07afffc
DP
246 if (grunt.option('no-color')) {
247 args.push('--color=false');
248 }
249
8f76bfb6
DM
250 var execShifter = function() {
251
1aa454ed
DP
252 grunt.log.ok("Running shifter on " + src);
253 grunt.util.spawn({
254 cmd: "node",
255 args: args,
256 opts: {cwd: src, stdio: 'inherit', env: process.env}
257 }, function (error, result, code) {
8f76bfb6
DM
258 if (code) {
259 grunt.fail.fatal('Shifter failed with code: ' + code);
260 } else {
261 grunt.log.ok('Shifter build complete.');
1aa454ed 262 filedone();
8f76bfb6
DM
263 }
264 });
265 };
266
adeb96d2 267 // Actually run shifter.
8f76bfb6
DM
268 if (!options.recursive) {
269 execShifter();
270 } else {
271 // Check that there are yui modules otherwise shifter ends with exit code 1.
1aa454ed
DP
272 if (grunt.file.expand({cwd: src}, '**/yui/src/**/*.js').length > 0) {
273 args.push('--recursive');
274 execShifter();
275 } else {
276 grunt.log.ok('No YUI modules to build.');
277 filedone();
278 }
8f76bfb6 279 }
1aa454ed 280 }, done);
adeb96d2
DW
281 };
282
283 tasks.startup = function() {
284 // Are we in a YUI directory?
e67585f8 285 if (path.basename(path.resolve(cwd, '../../')) == 'yui') {
a1587268 286 grunt.task.run('yui');
adeb96d2 287 // Are we in an AMD directory?
c9b6feea 288 } else if (inAMD) {
65d070ae 289 grunt.task.run('amd');
adeb96d2
DW
290 } else {
291 // Run them all!.
65d070ae
DP
292 grunt.task.run('css');
293 grunt.task.run('js');
adeb96d2
DW
294 }
295 };
296
1aa454ed
DP
297 // On watch, we dynamically modify config to build only affected files. This
298 // method is slightly complicated to deal with multiple changed files at once (copied
299 // from the grunt-contrib-watch readme).
300 var changedFiles = Object.create(null);
301 var onChange = grunt.util._.debounce(function() {
302 var files = Object.keys(changedFiles);
303 grunt.config('jshint.amd.src', files);
304 grunt.config('uglify.amd.files', [{ expand: true, src: files, rename: uglify_rename }]);
305 grunt.config('shifter.options.paths', files);
306 changedFiles = Object.create(null);
307 }, 200);
adeb96d2 308
8efbb7b1 309 grunt.event.on('watch', function(action, filepath) {
1aa454ed
DP
310 changedFiles[filepath] = action;
311 onChange();
8efbb7b1
DP
312 });
313
adeb96d2
DW
314 // Register NPM tasks.
315 grunt.loadNpmTasks('grunt-contrib-uglify');
316 grunt.loadNpmTasks('grunt-contrib-jshint');
a4a52e56 317 grunt.loadNpmTasks('grunt-contrib-less');
8efbb7b1 318 grunt.loadNpmTasks('grunt-contrib-watch');
3adb62b7 319 grunt.loadNpmTasks('grunt-eslint');
adeb96d2 320
65d070ae 321 // Register JS tasks.
adeb96d2 322 grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
30db70ab 323 grunt.registerTask('ignorefiles', 'Generate ignore files for linters', tasks.ignorefiles);
a1587268 324 grunt.registerTask('yui', ['eslint:yui', 'shifter']);
3adb62b7 325 grunt.registerTask('amd', ['eslint:amd', 'jshint', 'uglify']);
a1587268 326 grunt.registerTask('js', ['amd', 'yui']);
65d070ae
DP
327
328 // Register CSS taks.
329 grunt.registerTask('css', ['less:bootstrapbase']);
adeb96d2
DW
330
331 // Register the startup task.
332 grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
333
334 // Register the default task.
335 grunt.registerTask('default', ['startup']);
336};