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