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