MDL-37739 mod_wiki: Wiki ToC header href remove square double brackets.
[moodle.git] / Gruntfile.js
blob3dd3ef01bfb121a86300d3986b7877f0c224d7bc
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(),
30         inAMD = path.basename(cwd) == 'amd';
32     // Project configuration.
33     grunt.initConfig({
34         jshint: {
35             options: {jshintrc: '.jshintrc'},
36             files: [inAMD ? cwd + '/src/*.js' : '**/amd/src/*.js']
37         },
38         uglify: {
39             dynamic_mappings: {
40                 files: grunt.file.expandMapping(
41                     ['**/src/*.js', '!**/node_modules/**'],
42                     '',
43                     {
44                         cwd: cwd,
45                         rename: function(destBase, destPath) {
46                             destPath = destPath.replace('src', 'build');
47                             destPath = destPath.replace('.js', '.min.js');
48                             destPath = path.resolve(cwd, destPath);
49                             return destPath;
50                         }
51                     }
52                 )
53             }
54         },
55         less: {
56             bootstrapbase: {
57                 files: {
58                     "theme/bootstrapbase/style/moodle.css": "theme/bootstrapbase/less/moodle.less",
59                     "theme/bootstrapbase/style/editor.css": "theme/bootstrapbase/less/editor.less",
60                 },
61                 options: {
62                     compress: true
63                 }
64            }
65         }
66     });
68     tasks.shifter = function() {
69        var  exec = require('child_process').spawn,
70             done = this.async(),
71             args = [],
72             options = {
73                 recursive: true,
74                 watch: false,
75                 walk: false,
76                 module: false
77             },
78             shifter;
80             args.push( path.normalize(__dirname + '/node_modules/shifter/bin/shifter'));
82             // Determine the most appropriate options to run with based upon the current location.
83             if (path.basename(cwd) === 'src') {
84                 // Detect whether we're in a src directory.
85                 grunt.log.debug('In a src directory');
86                 args.push('--walk');
87                 options.walk = true;
88             } else if (path.basename(path.dirname(cwd)) === 'src') {
89                 // Detect whether we're in a module directory.
90                 grunt.log.debug('In a module directory');
91                 options.module = true;
92             }
94             if (grunt.option('watch')) {
95                 if (!options.walk && !options.module) {
96                     grunt.fail.fatal('Unable to watch unless in a src or module directory');
97                 }
99                 // It is not advisable to run with recursivity and watch - this
100                 // leads to building the build directory in a race-like fashion.
101                 grunt.log.debug('Detected a watch - disabling recursivity');
102                 options.recursive = false;
103                 args.push('--watch');
104             }
106             if (options.recursive) {
107                 args.push('--recursive');
108             }
110             // Always ignore the node_modules directory.
111             args.push('--excludes', 'node_modules');
113             // Add the stderr option if appropriate
114             if (grunt.option('verbose')) {
115                 args.push('--lint-stderr');
116             }
118             if (grunt.option('no-color')) {
119                 args.push('--color=false');
120             }
122             var execShifter = function() {
124                 shifter = exec("node", args, {
125                     cwd: cwd,
126                     stdio: 'inherit',
127                     env: process.env
128                 });
130                 // Tidy up after exec.
131                 shifter.on('exit', function (code) {
132                     if (code) {
133                         grunt.fail.fatal('Shifter failed with code: ' + code);
134                     } else {
135                         grunt.log.ok('Shifter build complete.');
136                         done();
137                     }
138                 });
139             };
141             // Actually run shifter.
142             if (!options.recursive) {
143                 execShifter();
144             } else {
145                 // Check that there are yui modules otherwise shifter ends with exit code 1.
146                 var found = false;
147                 var hasYuiModules = function(directory, callback) {
148                     fs.readdir(directory, function(err, files) {
149                         if (err) {
150                             return callback(err, null);
151                         }
153                         // If we already found a match there is no need to continue scanning.
154                         if (found === true) {
155                             return;
156                         }
158                         // We need to track the number of files to know when we return a result.
159                         var pending = files.length;
161                         // We first check files, so if there is a match we don't need further
162                         // async calls and we just return a true.
163                         for (var i = 0; i < files.length; i++) {
164                             if (files[i] === 'yui') {
165                                 return callback(null, true);
166                             }
167                         }
169                         // Iterate through subdirs if there were no matches.
170                         files.forEach(function (file) {
172                             var p = path.join(directory, file);
173                             stat = fs.statSync(p);
174                             if (!stat.isDirectory()) {
175                                 pending--;
176                             } else {
178                                 // We defer the pending-1 until we scan the whole dir and subdirs.
179                                 hasYuiModules(p, function(err, result) {
180                                     if (err) {
181                                         return callback(err);
182                                     }
184                                     if (result === true) {
185                                         // Once we get a true we notify the caller.
186                                         found = true;
187                                         return callback(null, true);
188                                     }
190                                     pending--;
191                                     if (pending === 0) {
192                                         // Notify the caller that the whole dir has been scaned and there are no matches.
193                                         return callback(null, false);
194                                     }
195                                 });
196                             }
198                             // No subdirs here, otherwise the return would be deferred until all subdirs are scanned.
199                             if (pending === 0) {
200                                 return callback(null, false);
201                             }
202                         });
203                     });
204                 };
206                 hasYuiModules(cwd, function(err, result) {
207                     if (err) {
208                         grunt.fail.fatal(err.message);
209                     }
211                     if (result === true) {
212                         execShifter();
213                     } else {
214                         grunt.log.ok('No YUI modules to build.');
215                         done();
216                     }
217                 });
218             }
219     };
221     tasks.startup = function() {
222         // Are we in a YUI directory?
223         if (path.basename(path.resolve(cwd, '../../')) == 'yui') {
224             grunt.task.run('shifter');
225         // Are we in an AMD directory?
226         } else if (inAMD) {
227             grunt.task.run('amd');
228         } else {
229             // Run them all!.
230             grunt.task.run('css');
231             grunt.task.run('js');
232         }
233     };
236     // Register NPM tasks.
237     grunt.loadNpmTasks('grunt-contrib-uglify');
238     grunt.loadNpmTasks('grunt-contrib-jshint');
239     grunt.loadNpmTasks('grunt-contrib-less');
241     // Register JS tasks.
242     grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
243     grunt.registerTask('amd', ['jshint', 'uglify']);
244     grunt.registerTask('js', ['amd', 'shifter']);
246     // Register CSS taks.
247     grunt.registerTask('css', ['less:bootstrapbase']);
249     // Register the startup task.
250     grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
252     // Register the default task.
253     grunt.registerTask('default', ['startup']);