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/>.
17 * @copyright 2014 Andrew Nicols
18 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 module.exports = function(grunt) {
26 var path = require('path'),
29 cwd = process.env.PWD || process.cwd(),
30 inAMD = path.basename(cwd) == 'amd';
32 // Globbing pattern for matching all AMD JS source files.
33 var amdSrc = [inAMD ? cwd + '/src/*.js' : '**/amd/src/*.js'];
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
41 * @param {String} destPath the current destination
42 * @param {String} srcPath the matched src path
43 * @return {String} The rewritten destination path.
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);
52 // Project configuration.
55 options: {jshintrc: '.jshintrc'},
70 "theme/bootstrapbase/style/moodle.css": "theme/bootstrapbase/less/moodle.less",
71 "theme/bootstrapbase/style/editor.css": "theme/bootstrapbase/less/editor.less",
80 nospawn: true // We need not to spawn so config can be changed dynamically.
83 files: ['**/amd/src/**/*.js'],
87 files: ["theme/bootstrapbase/less/**/*.less"],
88 tasks: ["less:bootstrapbase"]
91 files: ['**/yui/src/**/*.js'],
97 tasks.shifter = function() {
98 var exec = require('child_process').spawn,
109 args.push( path.normalize(__dirname + '/node_modules/shifter/bin/shifter'));
111 // Determine the most appropriate options to run with based upon the current location.
112 if (path.basename(cwd) === 'src') {
113 // Detect whether we're in a src directory.
114 grunt.log.debug('In a src directory');
117 } else if (path.basename(path.dirname(cwd)) === 'src') {
118 // Detect whether we're in a module directory.
119 grunt.log.debug('In a module directory');
120 options.module = true;
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');
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');
135 if (options.recursive) {
136 args.push('--recursive');
139 // Always ignore the node_modules directory.
140 args.push('--excludes', 'node_modules');
142 // Add the stderr option if appropriate
143 if (grunt.option('verbose')) {
144 args.push('--lint-stderr');
147 if (grunt.option('no-color')) {
148 args.push('--color=false');
151 var execShifter = function() {
153 shifter = exec("node", args, {
159 // Tidy up after exec.
160 shifter.on('exit', function (code) {
162 grunt.fail.fatal('Shifter failed with code: ' + code);
164 grunt.log.ok('Shifter build complete.');
170 // Actually run shifter.
171 if (!options.recursive) {
174 // Check that there are yui modules otherwise shifter ends with exit code 1.
176 var hasYuiModules = function(directory, callback) {
177 fs.readdir(directory, function(err, files) {
179 return callback(err, null);
182 // If we already found a match there is no need to continue scanning.
183 if (found === true) {
187 // We need to track the number of files to know when we return a result.
188 var pending = files.length;
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);
198 // Iterate through subdirs if there were no matches.
199 files.forEach(function (file) {
201 var p = path.join(directory, file);
202 stat = fs.statSync(p);
203 if (!stat.isDirectory()) {
207 // We defer the pending-1 until we scan the whole dir and subdirs.
208 hasYuiModules(p, function(err, result) {
210 return callback(err);
213 if (result === true) {
214 // Once we get a true we notify the caller.
216 return callback(null, true);
221 // Notify the caller that the whole dir has been scaned and there are no matches.
222 return callback(null, false);
227 // No subdirs here, otherwise the return would be deferred until all subdirs are scanned.
229 return callback(null, false);
235 hasYuiModules(cwd, function(err, result) {
237 grunt.fail.fatal(err.message);
240 if (result === true) {
243 grunt.log.ok('No YUI modules to build.');
250 tasks.startup = function() {
251 // Are we in a YUI directory?
252 if (path.basename(path.resolve(cwd, '../../')) == 'yui') {
253 grunt.task.run('shifter');
254 // Are we in an AMD directory?
256 grunt.task.run('amd');
259 grunt.task.run('css');
260 grunt.task.run('js');
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 }]);
271 // Register NPM tasks.
272 grunt.loadNpmTasks('grunt-contrib-uglify');
273 grunt.loadNpmTasks('grunt-contrib-jshint');
274 grunt.loadNpmTasks('grunt-contrib-less');
275 grunt.loadNpmTasks('grunt-contrib-watch');
277 // Register JS tasks.
278 grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
279 grunt.registerTask('amd', ['jshint', 'uglify']);
280 grunt.registerTask('js', ['amd', 'shifter']);
282 // Register CSS taks.
283 grunt.registerTask('css', ['less:bootstrapbase']);
285 // Register the startup task.
286 grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
288 // Register the default task.
289 grunt.registerTask('default', ['startup']);