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();
32 // Windows users can't run grunt in a subdirectory, so allow them to set
33 // the root by passing --root=path/to/dir.
34 if (grunt.option('root')) {
35 var root = grunt.option('root');
36 if (grunt.file.exists(__dirname, root)) {
37 cwd = path.join(__dirname, root);
38 grunt.log.ok('Setting root to '+cwd);
40 grunt.fail.fatal('Setting root to '+root+' failed - path does not exist');
44 var inAMD = path.basename(cwd) == 'amd';
46 // Project configuration.
49 options: {jshintrc: '.jshintrc'},
50 files: [inAMD ? cwd + '/src/*.js' : '**/amd/src/*.js']
54 files: grunt.file.expandMapping(
55 ['**/src/*.js', '!**/node_modules/**'],
59 rename: function(destBase, destPath) {
60 destPath = destPath.replace('src', 'build');
61 destPath = destPath.replace('.js', '.min.js');
62 destPath = path.resolve(cwd, destPath);
72 "theme/bootstrapbase/style/moodle.css": "theme/bootstrapbase/less/moodle.less",
73 "theme/bootstrapbase/style/editor.css": "theme/bootstrapbase/less/editor.less",
82 tasks.shifter = function() {
83 var exec = require('child_process').spawn,
94 args.push( path.normalize(__dirname + '/node_modules/shifter/bin/shifter'));
96 // Determine the most appropriate options to run with based upon the current location.
97 if (path.basename(cwd) === 'src') {
98 // Detect whether we're in a src directory.
99 grunt.log.debug('In a src directory');
102 } else if (path.basename(path.dirname(cwd)) === 'src') {
103 // Detect whether we're in a module directory.
104 grunt.log.debug('In a module directory');
105 options.module = true;
108 if (grunt.option('watch')) {
109 if (!options.walk && !options.module) {
110 grunt.fail.fatal('Unable to watch unless in a src or module directory');
113 // It is not advisable to run with recursivity and watch - this
114 // leads to building the build directory in a race-like fashion.
115 grunt.log.debug('Detected a watch - disabling recursivity');
116 options.recursive = false;
117 args.push('--watch');
120 if (options.recursive) {
121 args.push('--recursive');
124 // Always ignore the node_modules directory.
125 args.push('--excludes', 'node_modules');
127 // Add the stderr option if appropriate
128 if (grunt.option('verbose')) {
129 args.push('--lint-stderr');
132 if (grunt.option('no-color')) {
133 args.push('--color=false');
136 var execShifter = function() {
138 shifter = exec("node", args, {
144 // Tidy up after exec.
145 shifter.on('exit', function (code) {
147 grunt.fail.fatal('Shifter failed with code: ' + code);
149 grunt.log.ok('Shifter build complete.');
155 // Actually run shifter.
156 if (!options.recursive) {
159 // Check that there are yui modules otherwise shifter ends with exit code 1.
161 var hasYuiModules = function(directory, callback) {
162 fs.readdir(directory, function(err, files) {
164 return callback(err, null);
167 // If we already found a match there is no need to continue scanning.
168 if (found === true) {
172 // We need to track the number of files to know when we return a result.
173 var pending = files.length;
175 // We first check files, so if there is a match we don't need further
176 // async calls and we just return a true.
177 for (var i = 0; i < files.length; i++) {
178 if (files[i] === 'yui') {
179 return callback(null, true);
183 // Iterate through subdirs if there were no matches.
184 files.forEach(function (file) {
186 var p = path.join(directory, file);
187 stat = fs.statSync(p);
188 if (!stat.isDirectory()) {
192 // We defer the pending-1 until we scan the whole dir and subdirs.
193 hasYuiModules(p, function(err, result) {
195 return callback(err);
198 if (result === true) {
199 // Once we get a true we notify the caller.
201 return callback(null, true);
206 // Notify the caller that the whole dir has been scaned and there are no matches.
207 return callback(null, false);
212 // No subdirs here, otherwise the return would be deferred until all subdirs are scanned.
214 return callback(null, false);
220 hasYuiModules(cwd, function(err, result) {
222 grunt.fail.fatal(err.message);
225 if (result === true) {
228 grunt.log.ok('No YUI modules to build.');
235 tasks.startup = function() {
236 // Are we in a YUI directory?
237 if (path.basename(path.resolve(cwd, '../../')) == 'yui') {
238 grunt.task.run('shifter');
239 // Are we in an AMD directory?
241 grunt.task.run('amd');
244 grunt.task.run('css');
245 grunt.task.run('js');
250 // Register NPM tasks.
251 grunt.loadNpmTasks('grunt-contrib-uglify');
252 grunt.loadNpmTasks('grunt-contrib-jshint');
253 grunt.loadNpmTasks('grunt-contrib-less');
255 // Register JS tasks.
256 grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
257 grunt.registerTask('amd', ['jshint', 'uglify']);
258 grunt.registerTask('js', ['amd', 'shifter']);
260 // Register CSS taks.
261 grunt.registerTask('css', ['less:bootstrapbase']);
263 // Register the startup task.
264 grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
266 // Register the default task.
267 grunt.registerTask('default', ['startup']);