MDL-49817 grunt: make shifter work watch on right place
[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 */
adeb96d2
DW
16
17/**
18 * @copyright 2014 Andrew Nicols
19 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
20 */
21
22/**
23 * Grunt configuration
24 */
25
26module.exports = function(grunt) {
27 var path = require('path'),
8f76bfb6 28 fs = require('fs'),
e67585f8 29 tasks = {},
c9b6feea
JLB
30 cwd = process.env.PWD || process.cwd(),
31 inAMD = path.basename(cwd) == 'amd';
adeb96d2 32
5cc5f311
DP
33 // Globbing pattern for matching all AMD JS source files.
34 var amdSrc = [inAMD ? cwd + '/src/*.js' : '**/amd/src/*.js'];
35
36 /**
37 * Function to generate the destination for the uglify task
38 * (e.g. build/file.min.js). This function will be passed to
39 * the rename property of files array when building dynamically:
40 * http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically
41 *
42 * @param {String} destPath the current destination
43 * @param {String} srcPath the matched src path
44 * @return {String} The rewritten destination path.
45 */
46 var uglify_rename = function (destPath, srcPath) {
47 destPath = srcPath.replace('src', 'build');
48 destPath = destPath.replace('.js', '.min.js');
49 destPath = path.resolve(cwd, destPath);
50 return destPath;
51 };
52
adeb96d2
DW
53 // Project configuration.
54 grunt.initConfig({
55 jshint: {
56 options: {jshintrc: '.jshintrc'},
5cc5f311 57 amd: { src: amdSrc }
adeb96d2
DW
58 },
59 uglify: {
5cc5f311
DP
60 amd: {
61 files: [{
62 expand: true,
63 src: amdSrc,
64 rename: uglify_rename
65 }]
adeb96d2 66 }
a4a52e56
DP
67 },
68 less: {
69 bootstrapbase: {
70 files: {
71 "theme/bootstrapbase/style/moodle.css": "theme/bootstrapbase/less/moodle.less",
72 "theme/bootstrapbase/style/editor.css": "theme/bootstrapbase/less/editor.less",
73 },
74 options: {
75 compress: true
76 }
77 }
8efbb7b1
DP
78 },
79 watch: {
80 options: {
81 nospawn: true // We need not to spawn so config can be changed dynamically.
82 },
83 amd: {
84 files: ['**/amd/src/**/*.js'],
85 tasks: ['amd']
86 },
87 bootstrapbase: {
88 files: ["theme/bootstrapbase/less/**/*.less"],
89 tasks: ["less:bootstrapbase"]
90 },
91 yui: {
92 files: ['**/yui/src/**/*.js'],
93 tasks: ['shifter']
94 },
adeb96d2
DW
95 }
96 });
97
98 tasks.shifter = function() {
99 var exec = require('child_process').spawn,
100 done = this.async(),
101 args = [],
102 options = {
103 recursive: true,
104 watch: false,
105 walk: false,
106 module: false
107 },
108 shifter;
109
0b777a06 110 grunt.log.ok("Running shifter on " + cwd);
e67585f8
DW
111 args.push( path.normalize(__dirname + '/node_modules/shifter/bin/shifter'));
112
adeb96d2 113 // Determine the most appropriate options to run with based upon the current location.
e67585f8 114 if (path.basename(cwd) === 'src') {
adeb96d2
DW
115 // Detect whether we're in a src directory.
116 grunt.log.debug('In a src directory');
117 args.push('--walk');
118 options.walk = true;
e67585f8 119 } else if (path.basename(path.dirname(cwd)) === 'src') {
adeb96d2
DW
120 // Detect whether we're in a module directory.
121 grunt.log.debug('In a module directory');
122 options.module = true;
123 }
124
125 if (grunt.option('watch')) {
126 if (!options.walk && !options.module) {
127 grunt.fail.fatal('Unable to watch unless in a src or module directory');
128 }
129
130 // It is not advisable to run with recursivity and watch - this
131 // leads to building the build directory in a race-like fashion.
132 grunt.log.debug('Detected a watch - disabling recursivity');
133 options.recursive = false;
134 args.push('--watch');
135 }
136
137 if (options.recursive) {
138 args.push('--recursive');
139 }
140
141 // Always ignore the node_modules directory.
142 args.push('--excludes', 'node_modules');
143
144 // Add the stderr option if appropriate
145 if (grunt.option('verbose')) {
146 args.push('--lint-stderr');
147 }
148
a07afffc
DP
149 if (grunt.option('no-color')) {
150 args.push('--color=false');
151 }
152
8f76bfb6
DM
153 var execShifter = function() {
154
155 shifter = exec("node", args, {
156 cwd: cwd,
157 stdio: 'inherit',
158 env: process.env
159 });
160
161 // Tidy up after exec.
162 shifter.on('exit', function (code) {
163 if (code) {
164 grunt.fail.fatal('Shifter failed with code: ' + code);
165 } else {
166 grunt.log.ok('Shifter build complete.');
167 done();
168 }
169 });
170 };
171
adeb96d2 172 // Actually run shifter.
8f76bfb6
DM
173 if (!options.recursive) {
174 execShifter();
175 } else {
176 // Check that there are yui modules otherwise shifter ends with exit code 1.
177 var found = false;
178 var hasYuiModules = function(directory, callback) {
179 fs.readdir(directory, function(err, files) {
180 if (err) {
181 return callback(err, null);
182 }
183
184 // If we already found a match there is no need to continue scanning.
185 if (found === true) {
186 return;
187 }
188
189 // We need to track the number of files to know when we return a result.
190 var pending = files.length;
191
192 // We first check files, so if there is a match we don't need further
193 // async calls and we just return a true.
194 for (var i = 0; i < files.length; i++) {
195 if (files[i] === 'yui') {
196 return callback(null, true);
197 }
198 }
199
200 // Iterate through subdirs if there were no matches.
201 files.forEach(function (file) {
202
203 var p = path.join(directory, file);
0b777a06 204 var stat = fs.statSync(p);
8f76bfb6
DM
205 if (!stat.isDirectory()) {
206 pending--;
207 } else {
208
209 // We defer the pending-1 until we scan the whole dir and subdirs.
210 hasYuiModules(p, function(err, result) {
211 if (err) {
212 return callback(err);
213 }
214
215 if (result === true) {
216 // Once we get a true we notify the caller.
217 found = true;
218 return callback(null, true);
219 }
220
221 pending--;
222 if (pending === 0) {
223 // Notify the caller that the whole dir has been scaned and there are no matches.
224 return callback(null, false);
225 }
226 });
227 }
228
229 // No subdirs here, otherwise the return would be deferred until all subdirs are scanned.
230 if (pending === 0) {
231 return callback(null, false);
232 }
233 });
234 });
235 };
236
237 hasYuiModules(cwd, function(err, result) {
238 if (err) {
239 grunt.fail.fatal(err.message);
240 }
241
242 if (result === true) {
243 execShifter();
244 } else {
245 grunt.log.ok('No YUI modules to build.');
246 done();
247 }
248 });
249 }
adeb96d2
DW
250 };
251
252 tasks.startup = function() {
253 // Are we in a YUI directory?
e67585f8 254 if (path.basename(path.resolve(cwd, '../../')) == 'yui') {
adeb96d2
DW
255 grunt.task.run('shifter');
256 // Are we in an AMD directory?
c9b6feea 257 } else if (inAMD) {
65d070ae 258 grunt.task.run('amd');
adeb96d2
DW
259 } else {
260 // Run them all!.
65d070ae
DP
261 grunt.task.run('css');
262 grunt.task.run('js');
adeb96d2
DW
263 }
264 };
265
266
8efbb7b1
DP
267 // On watch, we dynamically modify config to build only affected files.
268 grunt.event.on('watch', function(action, filepath) {
269 grunt.config('jshint.amd.src', filepath);
270 grunt.config('uglify.amd.files', [{ expand: true, src: filepath, rename: uglify_rename }]);
0b777a06
DP
271 if (filepath.match('yui')) {
272 // Set the cwd to the base directory for yui modules which have changed.
273 cwd = filepath.split(path.sep + 'yui' + path.sep + 'src').shift();
274 } else {
275 cwd = process.env.PWD || process.cwd();
276 }
8efbb7b1
DP
277 });
278
adeb96d2
DW
279 // Register NPM tasks.
280 grunt.loadNpmTasks('grunt-contrib-uglify');
281 grunt.loadNpmTasks('grunt-contrib-jshint');
a4a52e56 282 grunt.loadNpmTasks('grunt-contrib-less');
8efbb7b1 283 grunt.loadNpmTasks('grunt-contrib-watch');
adeb96d2 284
65d070ae 285 // Register JS tasks.
adeb96d2 286 grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
65d070ae
DP
287 grunt.registerTask('amd', ['jshint', 'uglify']);
288 grunt.registerTask('js', ['amd', 'shifter']);
289
290 // Register CSS taks.
291 grunt.registerTask('css', ['less:bootstrapbase']);
adeb96d2
DW
292
293 // Register the startup task.
294 grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
295
296 // Register the default task.
297 grunt.registerTask('default', ['startup']);
298};