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