1 // This file is part of Moodle - http://moodle.org/
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.
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.
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 /* jshint node: true, browser: false */
18 * @copyright 2014 Andrew Nicols
19 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 module.exports = function(grunt) {
27 var path = require('path'),
30 cwd = process.env.PWD || process.cwd(),
31 inAMD = path.basename(cwd) == 'amd';
33 // Globbing pattern for matching all AMD JS source files.
34 var amdSrc = [inAMD ? cwd + '/src/*.js' : '**/amd/src/*.js'];
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
42 * @param {String} destPath the current destination
43 * @param {String} srcPath the matched src path
44 * @return {String} The rewritten destination path.
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);
53 // Project configuration.
56 options: {jshintrc: '.jshintrc'},
71 "theme/bootstrapbase/style/moodle.css": "theme/bootstrapbase/less/moodle.less",
72 "theme/bootstrapbase/style/editor.css": "theme/bootstrapbase/less/editor.less",
81 nospawn: true // We need not to spawn so config can be changed dynamically.
84 files: ['**/amd/src/**/*.js'],
88 files: ["theme/bootstrapbase/less/**/*.less"],
89 tasks: ["less:bootstrapbase"]
92 files: ['**/yui/src/**/*.js'],
98 tasks.shifter = function() {
99 var exec = require('child_process').spawn,
110 grunt.log.ok("Running shifter on " + cwd);
111 args.push( path.normalize(__dirname + '/node_modules/shifter/bin/shifter'));
113 // Determine the most appropriate options to run with based upon the current location.
114 if (path.basename(cwd) === 'src') {
115 // Detect whether we're in a src directory.
116 grunt.log.debug('In a src directory');
119 } else if (path.basename(path.dirname(cwd)) === 'src') {
120 // Detect whether we're in a module directory.
121 grunt.log.debug('In a module directory');
122 options.module = true;
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');
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');
137 if (options.recursive) {
138 args.push('--recursive');
141 // Always ignore the node_modules directory.
142 args.push('--excludes', 'node_modules');
144 // Add the stderr option if appropriate
145 if (grunt.option('verbose')) {
146 args.push('--lint-stderr');
149 if (grunt.option('no-color')) {
150 args.push('--color=false');
153 var execShifter = function() {
155 shifter = exec("node", args, {
161 // Tidy up after exec.
162 shifter.on('exit', function (code) {
164 grunt.fail.fatal('Shifter failed with code: ' + code);
166 grunt.log.ok('Shifter build complete.');
172 // Actually run shifter.
173 if (!options.recursive) {
176 // Check that there are yui modules otherwise shifter ends with exit code 1.
178 var hasYuiModules = function(directory, callback) {
179 fs.readdir(directory, function(err, files) {
181 return callback(err, null);
184 // If we already found a match there is no need to continue scanning.
185 if (found === true) {
189 // We need to track the number of files to know when we return a result.
190 var pending = files.length;
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);
200 // Iterate through subdirs if there were no matches.
201 files.forEach(function (file) {
203 var p = path.join(directory, file);
204 var stat = fs.statSync(p);
205 if (!stat.isDirectory()) {
209 // We defer the pending-1 until we scan the whole dir and subdirs.
210 hasYuiModules(p, function(err, result) {
212 return callback(err);
215 if (result === true) {
216 // Once we get a true we notify the caller.
218 return callback(null, true);
223 // Notify the caller that the whole dir has been scaned and there are no matches.
224 return callback(null, false);
229 // No subdirs here, otherwise the return would be deferred until all subdirs are scanned.
231 return callback(null, false);
237 hasYuiModules(cwd, function(err, result) {
239 grunt.fail.fatal(err.message);
242 if (result === true) {
245 grunt.log.ok('No YUI modules to build.');
252 tasks.startup = function() {
253 // Are we in a YUI directory?
254 if (path.basename(path.resolve(cwd, '../../')) == 'yui') {
255 grunt.task.run('shifter');
256 // Are we in an AMD directory?
258 grunt.task.run('amd');
261 grunt.task.run('css');
262 grunt.task.run('js');
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 }]);
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();
275 cwd = process.env.PWD || process.cwd();
279 // Register NPM tasks.
280 grunt.loadNpmTasks('grunt-contrib-uglify');
281 grunt.loadNpmTasks('grunt-contrib-jshint');
282 grunt.loadNpmTasks('grunt-contrib-less');
283 grunt.loadNpmTasks('grunt-contrib-watch');
285 // Register JS tasks.
286 grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
287 grunt.registerTask('amd', ['jshint', 'uglify']);
288 grunt.registerTask('js', ['amd', 'shifter']);
290 // Register CSS taks.
291 grunt.registerTask('css', ['less:bootstrapbase']);
293 // Register the startup task.
294 grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
296 // Register the default task.
297 grunt.registerTask('default', ['startup']);