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/>. | |
0b777a06 | 15 | /* jshint node: true, browser: false */ |
3adb62b7 | 16 | /* eslint-env node */ |
adeb96d2 DW |
17 | |
18 | /** | |
19 | * @copyright 2014 Andrew Nicols | |
20 | * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | |
21 | */ | |
22 | ||
23 | /** | |
24 | * Grunt configuration | |
25 | */ | |
26 | ||
27 | module.exports = function(grunt) { | |
28 | var path = require('path'), | |
e67585f8 | 29 | tasks = {}, |
00cceb7f DP |
30 | cwd = process.env.PWD || process.cwd(); |
31 | ||
32 | // Windows users can't run grunt in a subdirectory, so allow them to set | |
33 | // the root by passing --root=path/to/dir. | |
34 | if (grunt.option('root')) { | |
35 | var root = grunt.option('root'); | |
36 | if (grunt.file.exists(__dirname, root)) { | |
37 | cwd = path.join(__dirname, root); | |
38 | grunt.log.ok('Setting root to '+cwd); | |
39 | } else { | |
40 | grunt.fail.fatal('Setting root to '+root+' failed - path does not exist'); | |
41 | } | |
42 | } | |
43 | ||
44 | var inAMD = path.basename(cwd) == 'amd'; | |
adeb96d2 | 45 | |
5cc5f311 DP |
46 | // Globbing pattern for matching all AMD JS source files. |
47 | var amdSrc = [inAMD ? cwd + '/src/*.js' : '**/amd/src/*.js']; | |
48 | ||
49 | /** | |
50 | * Function to generate the destination for the uglify task | |
51 | * (e.g. build/file.min.js). This function will be passed to | |
52 | * the rename property of files array when building dynamically: | |
53 | * http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically | |
54 | * | |
55 | * @param {String} destPath the current destination | |
56 | * @param {String} srcPath the matched src path | |
57 | * @return {String} The rewritten destination path. | |
58 | */ | |
59 | var uglify_rename = function (destPath, srcPath) { | |
60 | destPath = srcPath.replace('src', 'build'); | |
61 | destPath = destPath.replace('.js', '.min.js'); | |
62 | destPath = path.resolve(cwd, destPath); | |
63 | return destPath; | |
64 | }; | |
65 | ||
adeb96d2 DW |
66 | // Project configuration. |
67 | grunt.initConfig({ | |
68 | jshint: { | |
69 | options: {jshintrc: '.jshintrc'}, | |
5cc5f311 | 70 | amd: { src: amdSrc } |
adeb96d2 | 71 | }, |
3adb62b7 DP |
72 | eslint: { |
73 | // Even though warnings dont stop the build we don't display warnings by default because: | |
74 | // * At this moment we've got too many core warnings | |
75 | // * It will complain about ignored files (https://github.com/sindresorhus/grunt-eslint/issues/119) | |
76 | // * It's better experience to use editor integrations or eslint natively | |
77 | options: { quiet: !grunt.option('show-lint-warnings') }, | |
be4b3cc6 DP |
78 | // Check AMD files. We add some stricter rules which we can't apply to the default configuration due |
79 | // to YUI rollups. | |
80 | amd: { | |
81 | src: amdSrc, | |
82 | options: { rules: {'no-undef': 'error', 'no-unused-vars': 'error', 'no-empty': 'error', 'no-unused-expressions': 'error'} } | |
83 | }, | |
84 | // Check YUI module source files. | |
a1587268 | 85 | yui: { |
be4b3cc6 | 86 | src: ['**/yui/src/**/*.js'] |
a1587268 | 87 | } |
3adb62b7 | 88 | }, |
adeb96d2 | 89 | uglify: { |
5cc5f311 DP |
90 | amd: { |
91 | files: [{ | |
92 | expand: true, | |
93 | src: amdSrc, | |
94 | rename: uglify_rename | |
95 | }] | |
adeb96d2 | 96 | } |
a4a52e56 DP |
97 | }, |
98 | less: { | |
99 | bootstrapbase: { | |
100 | files: { | |
101 | "theme/bootstrapbase/style/moodle.css": "theme/bootstrapbase/less/moodle.less", | |
102 | "theme/bootstrapbase/style/editor.css": "theme/bootstrapbase/less/editor.less", | |
103 | }, | |
104 | options: { | |
105 | compress: true | |
106 | } | |
107 | } | |
8efbb7b1 DP |
108 | }, |
109 | watch: { | |
110 | options: { | |
111 | nospawn: true // We need not to spawn so config can be changed dynamically. | |
112 | }, | |
113 | amd: { | |
114 | files: ['**/amd/src/**/*.js'], | |
115 | tasks: ['amd'] | |
116 | }, | |
117 | bootstrapbase: { | |
118 | files: ["theme/bootstrapbase/less/**/*.less"], | |
119 | tasks: ["less:bootstrapbase"] | |
120 | }, | |
121 | yui: { | |
122 | files: ['**/yui/src/**/*.js'], | |
a1587268 | 123 | tasks: ['yui'] |
8efbb7b1 | 124 | }, |
1aa454ed DP |
125 | }, |
126 | shifter: { | |
127 | options: { | |
128 | recursive: true, | |
129 | paths: [cwd] | |
130 | } | |
adeb96d2 DW |
131 | } |
132 | }); | |
133 | ||
1aa454ed DP |
134 | /** |
135 | * Shifter task. Is configured with a path to a specific file or a directory, | |
136 | * in the case of a specific file it will work out the right module to be built. | |
137 | * | |
138 | * Note that this task runs the invidiaul shifter jobs async (becase it spawns | |
139 | * so be careful to to call done(). | |
140 | */ | |
adeb96d2 | 141 | tasks.shifter = function() { |
1aa454ed | 142 | var async = require('async'), |
adeb96d2 | 143 | done = this.async(), |
1aa454ed | 144 | options = grunt.config('shifter.options'); |
adeb96d2 | 145 | |
1aa454ed DP |
146 | // Run the shifter processes one at a time to avoid confusing output. |
147 | async.eachSeries(options.paths, function (src, filedone) { | |
148 | var args = []; | |
e67585f8 DW |
149 | args.push( path.normalize(__dirname + '/node_modules/shifter/bin/shifter')); |
150 | ||
1aa454ed DP |
151 | // Always ignore the node_modules directory. |
152 | args.push('--excludes', 'node_modules'); | |
153 | ||
adeb96d2 | 154 | // Determine the most appropriate options to run with based upon the current location. |
1aa454ed DP |
155 | if (grunt.file.isMatch('**/yui/**/*.js', src)) { |
156 | // When passed a JS file, build our containing module (this happen with | |
157 | // watch). | |
158 | grunt.log.debug('Shifter passed a specific JS file'); | |
159 | src = path.dirname(path.dirname(src)); | |
160 | options.recursive = false; | |
161 | } else if (grunt.file.isMatch('**/yui/src', src)) { | |
162 | // When in a src directory --walk all modules. | |
adeb96d2 DW |
163 | grunt.log.debug('In a src directory'); |
164 | args.push('--walk'); | |
1aa454ed DP |
165 | options.recursive = false; |
166 | } else if (grunt.file.isMatch('**/yui/src/*', src)) { | |
167 | // When in module, only build our module. | |
adeb96d2 | 168 | grunt.log.debug('In a module directory'); |
adeb96d2 | 169 | options.recursive = false; |
1aa454ed DP |
170 | } else if (grunt.file.isMatch('**/yui/src/*/js', src)) { |
171 | // When in module src, only build our module. | |
172 | grunt.log.debug('In a source directory'); | |
173 | src = path.dirname(src); | |
174 | options.recursive = false; | |
adeb96d2 DW |
175 | } |
176 | ||
1aa454ed DP |
177 | if (grunt.option('watch')) { |
178 | grunt.fail.fatal('The --watch option has been removed, please use `grunt watch` instead'); | |
adeb96d2 DW |
179 | } |
180 | ||
adeb96d2 DW |
181 | // Add the stderr option if appropriate |
182 | if (grunt.option('verbose')) { | |
183 | args.push('--lint-stderr'); | |
184 | } | |
185 | ||
a07afffc DP |
186 | if (grunt.option('no-color')) { |
187 | args.push('--color=false'); | |
188 | } | |
189 | ||
8f76bfb6 DM |
190 | var execShifter = function() { |
191 | ||
1aa454ed DP |
192 | grunt.log.ok("Running shifter on " + src); |
193 | grunt.util.spawn({ | |
194 | cmd: "node", | |
195 | args: args, | |
196 | opts: {cwd: src, stdio: 'inherit', env: process.env} | |
197 | }, function (error, result, code) { | |
8f76bfb6 DM |
198 | if (code) { |
199 | grunt.fail.fatal('Shifter failed with code: ' + code); | |
200 | } else { | |
201 | grunt.log.ok('Shifter build complete.'); | |
1aa454ed | 202 | filedone(); |
8f76bfb6 DM |
203 | } |
204 | }); | |
205 | }; | |
206 | ||
adeb96d2 | 207 | // Actually run shifter. |
8f76bfb6 DM |
208 | if (!options.recursive) { |
209 | execShifter(); | |
210 | } else { | |
211 | // Check that there are yui modules otherwise shifter ends with exit code 1. | |
1aa454ed DP |
212 | if (grunt.file.expand({cwd: src}, '**/yui/src/**/*.js').length > 0) { |
213 | args.push('--recursive'); | |
214 | execShifter(); | |
215 | } else { | |
216 | grunt.log.ok('No YUI modules to build.'); | |
217 | filedone(); | |
218 | } | |
8f76bfb6 | 219 | } |
1aa454ed | 220 | }, done); |
adeb96d2 DW |
221 | }; |
222 | ||
223 | tasks.startup = function() { | |
224 | // Are we in a YUI directory? | |
e67585f8 | 225 | if (path.basename(path.resolve(cwd, '../../')) == 'yui') { |
a1587268 | 226 | grunt.task.run('yui'); |
adeb96d2 | 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 | ||
1aa454ed DP |
237 | // On watch, we dynamically modify config to build only affected files. This |
238 | // method is slightly complicated to deal with multiple changed files at once (copied | |
239 | // from the grunt-contrib-watch readme). | |
240 | var changedFiles = Object.create(null); | |
241 | var onChange = grunt.util._.debounce(function() { | |
242 | var files = Object.keys(changedFiles); | |
243 | grunt.config('jshint.amd.src', files); | |
244 | grunt.config('uglify.amd.files', [{ expand: true, src: files, rename: uglify_rename }]); | |
245 | grunt.config('shifter.options.paths', files); | |
246 | changedFiles = Object.create(null); | |
247 | }, 200); | |
adeb96d2 | 248 | |
8efbb7b1 | 249 | grunt.event.on('watch', function(action, filepath) { |
1aa454ed DP |
250 | changedFiles[filepath] = action; |
251 | onChange(); | |
8efbb7b1 DP |
252 | }); |
253 | ||
adeb96d2 DW |
254 | // Register NPM tasks. |
255 | grunt.loadNpmTasks('grunt-contrib-uglify'); | |
256 | grunt.loadNpmTasks('grunt-contrib-jshint'); | |
a4a52e56 | 257 | grunt.loadNpmTasks('grunt-contrib-less'); |
8efbb7b1 | 258 | grunt.loadNpmTasks('grunt-contrib-watch'); |
3adb62b7 | 259 | grunt.loadNpmTasks('grunt-eslint'); |
adeb96d2 | 260 | |
65d070ae | 261 | // Register JS tasks. |
adeb96d2 | 262 | grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter); |
a1587268 | 263 | grunt.registerTask('yui', ['eslint:yui', 'shifter']); |
3adb62b7 | 264 | grunt.registerTask('amd', ['eslint:amd', 'jshint', 'uglify']); |
a1587268 | 265 | grunt.registerTask('js', ['amd', 'yui']); |
65d070ae DP |
266 | |
267 | // Register CSS taks. | |
268 | grunt.registerTask('css', ['less:bootstrapbase']); | |
adeb96d2 DW |
269 | |
270 | // Register the startup task. | |
271 | grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup); | |
272 | ||
273 | // Register the default task. | |
274 | grunt.registerTask('default', ['startup']); | |
275 | }; |