Merge branch 'MDL-51048' of git://github.com/timhunt/moodle
[moodle.git] / Gruntfile.js
blob6237dfaf2dde6f7c5327b5b9e4991f22bb88cc31
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         tasks = {},
28         cwd = process.env.PWD || process.cwd();
30     // Project configuration.
31     grunt.initConfig({
32         jshint: {
33             options: {jshintrc: '.jshintrc'},
34             files: ['**/amd/src/*.js']
35         },
36         uglify: {
37             dynamic_mappings: {
38                 files: grunt.file.expandMapping(
39                     ['**/src/*.js', '!**/node_modules/**'],
40                     '',
41                     {
42                         cwd: cwd,
43                         rename: function(destBase, destPath) {
44                             destPath = destPath.replace('src', 'build');
45                             destPath = destPath.replace('.js', '.min.js');
46                             destPath = path.resolve(cwd, destPath);
47                             return destPath;
48                         }
49                     }
50                 )
51             }
52         }
53     });
55     tasks.shifter = function() {
56        var  exec = require('child_process').spawn,
57             done = this.async(),
58             args = [],
59             options = {
60                 recursive: true,
61                 watch: false,
62                 walk: false,
63                 module: false
64             },
65             shifter;
67             args.push( path.normalize(__dirname + '/node_modules/shifter/bin/shifter'));
69             // Determine the most appropriate options to run with based upon the current location.
70             if (path.basename(cwd) === 'src') {
71                 // Detect whether we're in a src directory.
72                 grunt.log.debug('In a src directory');
73                 args.push('--walk');
74                 options.walk = true;
75             } else if (path.basename(path.dirname(cwd)) === 'src') {
76                 // Detect whether we're in a module directory.
77                 grunt.log.debug('In a module directory');
78                 options.module = true;
79             }
81             if (grunt.option('watch')) {
82                 if (!options.walk && !options.module) {
83                     grunt.fail.fatal('Unable to watch unless in a src or module directory');
84                 }
86                 // It is not advisable to run with recursivity and watch - this
87                 // leads to building the build directory in a race-like fashion.
88                 grunt.log.debug('Detected a watch - disabling recursivity');
89                 options.recursive = false;
90                 args.push('--watch');
91             }
93             if (options.recursive) {
94                 args.push('--recursive');
95             }
97             // Always ignore the node_modules directory.
98             args.push('--excludes', 'node_modules');
100             // Add the stderr option if appropriate
101             if (grunt.option('verbose')) {
102                 args.push('--lint-stderr');
103             }
105             // Actually run shifter.
106             shifter = exec("node", args, {
107                 cwd: cwd,
108                 stdio: 'inherit',
109                 env: process.env
110             });
112             // Tidy up after exec.
113             shifter.on('exit', function (code) {
114                 if (code) {
115                     grunt.fail.fatal('Shifter failed with code: ' + code);
116                 } else {
117                     grunt.log.ok('Shifter build complete.');
118                     done();
119                 }
120             });
121     };
123     tasks.startup = function() {
124         // Are we in a YUI directory?
125         if (path.basename(path.resolve(cwd, '../../')) == 'yui') {
126             grunt.task.run('shifter');
127         // Are we in an AMD directory?
128         } else if (path.basename(cwd) == 'amd') {
129             grunt.task.run('jshint');
130             grunt.task.run('uglify');
131         } else {
132             // Run them all!.
133             grunt.task.run('shifter');
134             grunt.task.run('jshint');
135             grunt.task.run('uglify');
136         }
137     };
140     // Register NPM tasks.
141     grunt.loadNpmTasks('grunt-contrib-uglify');
142     grunt.loadNpmTasks('grunt-contrib-jshint');
144     // Register the shifter task.
145     grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
147     // Register the startup task.
148     grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
150     // Register the default task.
151     grunt.registerTask('default', ['startup']);