Merge branch 'MDL-51582-30' of git://github.com/danpoltawski/moodle into MOODLE_30_STABLE
[moodle.git] / Gruntfile.js
blobdcc22a8f34fb5fe993f8e1449ee3b3fd6d2ef3a4
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.
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/>.
16 /**
17  * @copyright  2014 Andrew Nicols
18  * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
19  */
21 /**
22  * Grunt configuration
23  */
25 module.exports = function(grunt) {
26     var path = require('path'),
27         fs = require('fs'),
28         tasks = {},
29         cwd = process.env.PWD || process.cwd();
31     // Project configuration.
32     grunt.initConfig({
33         jshint: {
34             options: {jshintrc: '.jshintrc'},
35             files: ['**/amd/src/*.js']
36         },
37         uglify: {
38             dynamic_mappings: {
39                 files: grunt.file.expandMapping(
40                     ['**/src/*.js', '!**/node_modules/**'],
41                     '',
42                     {
43                         cwd: cwd,
44                         rename: function(destBase, destPath) {
45                             destPath = destPath.replace('src', 'build');
46                             destPath = destPath.replace('.js', '.min.js');
47                             destPath = path.resolve(cwd, destPath);
48                             return destPath;
49                         }
50                     }
51                 )
52             }
53         },
54         less: {
55             bootstrapbase: {
56                 files: {
57                     "theme/bootstrapbase/style/moodle.css": "theme/bootstrapbase/less/moodle.less",
58                     "theme/bootstrapbase/style/editor.css": "theme/bootstrapbase/less/editor.less",
59                 },
60                 options: {
61                     compress: true
62                 }
63            }
64         }
65     });
67     tasks.shifter = function() {
68        var  exec = require('child_process').spawn,
69             done = this.async(),
70             args = [],
71             options = {
72                 recursive: true,
73                 watch: false,
74                 walk: false,
75                 module: false
76             },
77             shifter;
79             args.push( path.normalize(__dirname + '/node_modules/shifter/bin/shifter'));
81             // Determine the most appropriate options to run with based upon the current location.
82             if (path.basename(cwd) === 'src') {
83                 // Detect whether we're in a src directory.
84                 grunt.log.debug('In a src directory');
85                 args.push('--walk');
86                 options.walk = true;
87             } else if (path.basename(path.dirname(cwd)) === 'src') {
88                 // Detect whether we're in a module directory.
89                 grunt.log.debug('In a module directory');
90                 options.module = true;
91             }
93             if (grunt.option('watch')) {
94                 if (!options.walk && !options.module) {
95                     grunt.fail.fatal('Unable to watch unless in a src or module directory');
96                 }
98                 // It is not advisable to run with recursivity and watch - this
99                 // leads to building the build directory in a race-like fashion.
100                 grunt.log.debug('Detected a watch - disabling recursivity');
101                 options.recursive = false;
102                 args.push('--watch');
103             }
105             if (options.recursive) {
106                 args.push('--recursive');
107             }
109             // Always ignore the node_modules directory.
110             args.push('--excludes', 'node_modules');
112             // Add the stderr option if appropriate
113             if (grunt.option('verbose')) {
114                 args.push('--lint-stderr');
115             }
117             var execShifter = function() {
119                 shifter = exec("node", args, {
120                     cwd: cwd,
121                     stdio: 'inherit',
122                     env: process.env
123                 });
125                 // Tidy up after exec.
126                 shifter.on('exit', function (code) {
127                     if (code) {
128                         grunt.fail.fatal('Shifter failed with code: ' + code);
129                     } else {
130                         grunt.log.ok('Shifter build complete.');
131                         done();
132                     }
133                 });
134             };
136             // Actually run shifter.
137             if (!options.recursive) {
138                 execShifter();
139             } else {
140                 // Check that there are yui modules otherwise shifter ends with exit code 1.
141                 var found = false;
142                 var hasYuiModules = function(directory, callback) {
143                     fs.readdir(directory, function(err, files) {
144                         if (err) {
145                             return callback(err, null);
146                         }
148                         // If we already found a match there is no need to continue scanning.
149                         if (found === true) {
150                             return;
151                         }
153                         // We need to track the number of files to know when we return a result.
154                         var pending = files.length;
156                         // We first check files, so if there is a match we don't need further
157                         // async calls and we just return a true.
158                         for (var i = 0; i < files.length; i++) {
159                             if (files[i] === 'yui') {
160                                 return callback(null, true);
161                             }
162                         }
164                         // Iterate through subdirs if there were no matches.
165                         files.forEach(function (file) {
167                             var p = path.join(directory, file);
168                             stat = fs.statSync(p);
169                             if (!stat.isDirectory()) {
170                                 pending--;
171                             } else {
173                                 // We defer the pending-1 until we scan the whole dir and subdirs.
174                                 hasYuiModules(p, function(err, result) {
175                                     if (err) {
176                                         return callback(err);
177                                     }
179                                     if (result === true) {
180                                         // Once we get a true we notify the caller.
181                                         found = true;
182                                         return callback(null, true);
183                                     }
185                                     pending--;
186                                     if (pending === 0) {
187                                         // Notify the caller that the whole dir has been scaned and there are no matches.
188                                         return callback(null, false);
189                                     }
190                                 });
191                             }
193                             // No subdirs here, otherwise the return would be deferred until all subdirs are scanned.
194                             if (pending === 0) {
195                                 return callback(null, false);
196                             }
197                         });
198                     });
199                 };
201                 hasYuiModules(cwd, function(err, result) {
202                     if (err) {
203                         grunt.fail.fatal(err.message);
204                     }
206                     if (result === true) {
207                         execShifter();
208                     } else {
209                         grunt.log.ok('No YUI modules to build.');
210                         done();
211                     }
212                 });
213             }
214     };
216     tasks.startup = function() {
217         // Are we in a YUI directory?
218         if (path.basename(path.resolve(cwd, '../../')) == 'yui') {
219             grunt.task.run('shifter');
220         // Are we in an AMD directory?
221         } else if (path.basename(cwd) == 'amd') {
222             grunt.task.run('amd');
223         } else {
224             // Run them all!.
225             grunt.task.run('css');
226             grunt.task.run('js');
227         }
228     };
231     // Register NPM tasks.
232     grunt.loadNpmTasks('grunt-contrib-uglify');
233     grunt.loadNpmTasks('grunt-contrib-jshint');
234     grunt.loadNpmTasks('grunt-contrib-less');
236     // Register JS tasks.
237     grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
238     grunt.registerTask('amd', ['jshint', 'uglify']);
239     grunt.registerTask('js', ['amd', 'shifter']);
241     // Register CSS taks.
242     grunt.registerTask('css', ['less:bootstrapbase']);
244     // Register the startup task.
245     grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
247     // Register the default task.
248     grunt.registerTask('default', ['startup']);