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 = {}, |
30db70ab DP |
30 | cwd = process.env.PWD || process.cwd(), |
31 | async = require('async'), | |
32 | DOMParser = require('xmldom').DOMParser, | |
d2c7175a DP |
33 | xpath = require('xpath'), |
34 | semver = require('semver'); | |
35 | ||
36 | // Verify the node version is new enough. | |
37 | var expected = semver.validRange(grunt.file.readJSON('package.json').engines.node); | |
38 | var actual = semver.valid(process.version); | |
39 | if (!semver.satisfies(actual, expected)) { | |
fbd1877f | 40 | grunt.fail.fatal('Node version not satisfied. Require ' + expected + ', version installed: ' + actual); |
d2c7175a | 41 | } |
00cceb7f DP |
42 | |
43 | // Windows users can't run grunt in a subdirectory, so allow them to set | |
44 | // the root by passing --root=path/to/dir. | |
45 | if (grunt.option('root')) { | |
46 | var root = grunt.option('root'); | |
47 | if (grunt.file.exists(__dirname, root)) { | |
48 | cwd = path.join(__dirname, root); | |
b18478b9 | 49 | grunt.log.ok('Setting root to ' + cwd); |
00cceb7f | 50 | } else { |
b18478b9 | 51 | grunt.fail.fatal('Setting root to ' + root + ' failed - path does not exist'); |
00cceb7f DP |
52 | } |
53 | } | |
54 | ||
55 | var inAMD = path.basename(cwd) == 'amd'; | |
adeb96d2 | 56 | |
5cc5f311 DP |
57 | // Globbing pattern for matching all AMD JS source files. |
58 | var amdSrc = [inAMD ? cwd + '/src/*.js' : '**/amd/src/*.js']; | |
59 | ||
60 | /** | |
61 | * Function to generate the destination for the uglify task | |
62 | * (e.g. build/file.min.js). This function will be passed to | |
63 | * the rename property of files array when building dynamically: | |
64 | * http://gruntjs.com/configuring-tasks#building-the-files-object-dynamically | |
65 | * | |
66 | * @param {String} destPath the current destination | |
67 | * @param {String} srcPath the matched src path | |
68 | * @return {String} The rewritten destination path. | |
69 | */ | |
c53f86d4 | 70 | var babelRename = function(destPath, srcPath) { |
5cc5f311 DP |
71 | destPath = srcPath.replace('src', 'build'); |
72 | destPath = destPath.replace('.js', '.min.js'); | |
73 | destPath = path.resolve(cwd, destPath); | |
74 | return destPath; | |
75 | }; | |
76 | ||
30db70ab DP |
77 | /** |
78 | * Find thirdpartylibs.xml and generate an array of paths contained within | |
79 | * them (used to generate ignore files and so on). | |
80 | * | |
81 | * @return {array} The list of thirdparty paths. | |
82 | */ | |
83 | var getThirdPartyPathsFromXML = function() { | |
84 | var thirdpartyfiles = grunt.file.expand('*/**/thirdpartylibs.xml'); | |
85 | var libs = ['node_modules/', 'vendor/']; | |
86 | ||
b18478b9 | 87 | thirdpartyfiles.forEach(function(file) { |
30db70ab DP |
88 | var dirname = path.dirname(file); |
89 | ||
90 | var doc = new DOMParser().parseFromString(grunt.file.read(file)); | |
91 | var nodes = xpath.select("/libraries/library/location/text()", doc); | |
92 | ||
93 | nodes.forEach(function(node) { | |
94 | var lib = path.join(dirname, node.toString()); | |
95 | if (grunt.file.isDir(lib)) { | |
96 | // Ensure trailing slash on dirs. | |
97 | lib = lib.replace(/\/?$/, '/'); | |
98 | } | |
99 | ||
100 | // Look for duplicate paths before adding to array. | |
101 | if (libs.indexOf(lib) === -1) { | |
102 | libs.push(lib); | |
103 | } | |
104 | }); | |
105 | }); | |
106 | return libs; | |
107 | }; | |
108 | ||
adeb96d2 DW |
109 | // Project configuration. |
110 | grunt.initConfig({ | |
3adb62b7 | 111 | eslint: { |
30db70ab DP |
112 | // Even though warnings dont stop the build we don't display warnings by default because |
113 | // at this moment we've got too many core warnings. | |
037de719 | 114 | options: {quiet: !grunt.option('show-lint-warnings')}, |
6f601313 | 115 | amd: {src: amdSrc}, |
be4b3cc6 | 116 | // Check YUI module source files. |
6f601313 | 117 | yui: {src: ['**/yui/src/**/*.js', '!*/**/yui/src/*/meta/*.js']} |
3adb62b7 | 118 | }, |
c53f86d4 RW |
119 | babel: { |
120 | options: { | |
121 | sourceMaps: true, | |
122 | comments: false, | |
123 | plugins: [ | |
124 | 'transform-es2015-modules-amd-lazy', | |
125 | // This plugin modifies the Babel transpiling for "export default" | |
126 | // so that if it's used then only the exported value is returned | |
127 | // by the generated AMD module. | |
128 | // | |
129 | // It also adds the Moodle plugin name to the AMD module definition | |
130 | // so that it can be imported as expected in other modules. | |
131 | path.resolve('babel-plugin-add-module-to-define.js'), | |
132 | '@babel/plugin-syntax-dynamic-import', | |
133 | '@babel/plugin-syntax-import-meta', | |
134 | ['@babel/plugin-proposal-class-properties', {'loose': false}], | |
135 | '@babel/plugin-proposal-json-strings' | |
136 | ], | |
137 | presets: [ | |
138 | ['minify', { | |
139 | // This minification plugin needs to be disabled because it breaks the | |
140 | // source map generation and causes invalid source maps to be output. | |
141 | simplify: false, | |
142 | builtIns: false | |
143 | }], | |
144 | ['@babel/preset-env', { | |
145 | targets: { | |
146 | browsers: [ | |
147 | ">0.25%", | |
148 | "last 2 versions", | |
149 | "not ie <= 10", | |
150 | "not op_mini all", | |
151 | "not Opera > 0", | |
152 | "not dead" | |
153 | ] | |
154 | }, | |
155 | modules: false, | |
156 | useBuiltIns: false | |
157 | }] | |
158 | ] | |
159 | }, | |
160 | dist: { | |
5cc5f311 DP |
161 | files: [{ |
162 | expand: true, | |
163 | src: amdSrc, | |
c53f86d4 RW |
164 | rename: babelRename |
165 | }] | |
adeb96d2 | 166 | } |
a4a52e56 | 167 | }, |
af9edb2e BB |
168 | sass: { |
169 | dist: { | |
170 | files: { | |
de213bf0 BB |
171 | "theme/boost/style/moodle.css": "theme/boost/scss/preset/default.scss", |
172 | "theme/classic/style/moodle.css": "theme/classic/scss/classicgrunt.scss" | |
af9edb2e BB |
173 | } |
174 | }, | |
175 | options: { | |
de213bf0 | 176 | includePaths: ["theme/boost/scss/", "theme/classic/scss/"] |
af9edb2e BB |
177 | } |
178 | }, | |
8efbb7b1 DP |
179 | watch: { |
180 | options: { | |
181 | nospawn: true // We need not to spawn so config can be changed dynamically. | |
182 | }, | |
183 | amd: { | |
184 | files: ['**/amd/src/**/*.js'], | |
185 | tasks: ['amd'] | |
186 | }, | |
8efbb7b1 DP |
187 | yui: { |
188 | files: ['**/yui/src/**/*.js'], | |
a1587268 | 189 | tasks: ['yui'] |
8efbb7b1 | 190 | }, |
d44f7e46 RT |
191 | gherkinlint: { |
192 | files: ['**/tests/behat/*.feature'], | |
193 | tasks: ['gherkinlint'] | |
194 | } | |
1aa454ed DP |
195 | }, |
196 | shifter: { | |
197 | options: { | |
198 | recursive: true, | |
199 | paths: [cwd] | |
200 | } | |
855fc5d8 | 201 | }, |
8b02e2d9 DP |
202 | gherkinlint: { |
203 | options: { | |
204 | files: ['**/tests/behat/*.feature'], | |
205 | } | |
206 | }, | |
855fc5d8 | 207 | stylelint: { |
773e68b0 | 208 | scss: { |
5142f564 | 209 | options: {syntax: 'scss'}, |
773e68b0 | 210 | src: ['*/**/*.scss'] |
d5dff631 DP |
211 | }, |
212 | css: { | |
213 | src: ['*/**/*.css'], | |
214 | options: { | |
215 | configOverrides: { | |
216 | rules: { | |
217 | // These rules have to be disabled in .stylelintrc for scss compat. | |
218 | "at-rule-no-unknown": true, | |
d5dff631 DP |
219 | } |
220 | } | |
221 | } | |
855fc5d8 | 222 | } |
adeb96d2 DW |
223 | } |
224 | }); | |
225 | ||
30db70ab DP |
226 | /** |
227 | * Generate ignore files (utilising thirdpartylibs.xml data) | |
228 | */ | |
229 | tasks.ignorefiles = function() { | |
f8731225 DP |
230 | // An array of paths to third party directories. |
231 | var thirdPartyPaths = getThirdPartyPathsFromXML(); | |
30db70ab DP |
232 | // Generate .eslintignore. |
233 | var eslintIgnores = ['# Generated by "grunt ignorefiles"', '*/**/yui/src/*/meta/', '*/**/build/'].concat(thirdPartyPaths); | |
234 | grunt.file.write('.eslintignore', eslintIgnores.join('\n')); | |
a26d3673 | 235 | // Generate .stylelintignore. |
36650f11 DP |
236 | var stylelintIgnores = [ |
237 | '# Generated by "grunt ignorefiles"', | |
3951d50a | 238 | '**/yui/build/*', |
de213bf0 BB |
239 | 'theme/boost/style/moodle.css', |
240 | 'theme/classic/style/moodle.css', | |
36650f11 | 241 | ].concat(thirdPartyPaths); |
a26d3673 | 242 | grunt.file.write('.stylelintignore', stylelintIgnores.join('\n')); |
30db70ab DP |
243 | }; |
244 | ||
1aa454ed DP |
245 | /** |
246 | * Shifter task. Is configured with a path to a specific file or a directory, | |
247 | * in the case of a specific file it will work out the right module to be built. | |
248 | * | |
249 | * Note that this task runs the invidiaul shifter jobs async (becase it spawns | |
250 | * so be careful to to call done(). | |
251 | */ | |
adeb96d2 | 252 | tasks.shifter = function() { |
30db70ab | 253 | var done = this.async(), |
1aa454ed | 254 | options = grunt.config('shifter.options'); |
adeb96d2 | 255 | |
1aa454ed | 256 | // Run the shifter processes one at a time to avoid confusing output. |
b18478b9 | 257 | async.eachSeries(options.paths, function(src, filedone) { |
1aa454ed | 258 | var args = []; |
b18478b9 | 259 | args.push(path.normalize(__dirname + '/node_modules/shifter/bin/shifter')); |
e67585f8 | 260 | |
1aa454ed DP |
261 | // Always ignore the node_modules directory. |
262 | args.push('--excludes', 'node_modules'); | |
263 | ||
adeb96d2 | 264 | // Determine the most appropriate options to run with based upon the current location. |
1aa454ed DP |
265 | if (grunt.file.isMatch('**/yui/**/*.js', src)) { |
266 | // When passed a JS file, build our containing module (this happen with | |
267 | // watch). | |
268 | grunt.log.debug('Shifter passed a specific JS file'); | |
269 | src = path.dirname(path.dirname(src)); | |
270 | options.recursive = false; | |
271 | } else if (grunt.file.isMatch('**/yui/src', src)) { | |
272 | // When in a src directory --walk all modules. | |
adeb96d2 DW |
273 | grunt.log.debug('In a src directory'); |
274 | args.push('--walk'); | |
1aa454ed DP |
275 | options.recursive = false; |
276 | } else if (grunt.file.isMatch('**/yui/src/*', src)) { | |
277 | // When in module, only build our module. | |
adeb96d2 | 278 | grunt.log.debug('In a module directory'); |
adeb96d2 | 279 | options.recursive = false; |
1aa454ed DP |
280 | } else if (grunt.file.isMatch('**/yui/src/*/js', src)) { |
281 | // When in module src, only build our module. | |
282 | grunt.log.debug('In a source directory'); | |
283 | src = path.dirname(src); | |
284 | options.recursive = false; | |
adeb96d2 DW |
285 | } |
286 | ||
1aa454ed DP |
287 | if (grunt.option('watch')) { |
288 | grunt.fail.fatal('The --watch option has been removed, please use `grunt watch` instead'); | |
adeb96d2 DW |
289 | } |
290 | ||
adeb96d2 DW |
291 | // Add the stderr option if appropriate |
292 | if (grunt.option('verbose')) { | |
293 | args.push('--lint-stderr'); | |
294 | } | |
295 | ||
a07afffc DP |
296 | if (grunt.option('no-color')) { |
297 | args.push('--color=false'); | |
298 | } | |
299 | ||
8f76bfb6 DM |
300 | var execShifter = function() { |
301 | ||
1aa454ed DP |
302 | grunt.log.ok("Running shifter on " + src); |
303 | grunt.util.spawn({ | |
304 | cmd: "node", | |
305 | args: args, | |
306 | opts: {cwd: src, stdio: 'inherit', env: process.env} | |
b18478b9 | 307 | }, function(error, result, code) { |
8f76bfb6 DM |
308 | if (code) { |
309 | grunt.fail.fatal('Shifter failed with code: ' + code); | |
310 | } else { | |
311 | grunt.log.ok('Shifter build complete.'); | |
1aa454ed | 312 | filedone(); |
8f76bfb6 DM |
313 | } |
314 | }); | |
315 | }; | |
316 | ||
adeb96d2 | 317 | // Actually run shifter. |
8f76bfb6 DM |
318 | if (!options.recursive) { |
319 | execShifter(); | |
320 | } else { | |
321 | // Check that there are yui modules otherwise shifter ends with exit code 1. | |
1aa454ed DP |
322 | if (grunt.file.expand({cwd: src}, '**/yui/src/**/*.js').length > 0) { |
323 | args.push('--recursive'); | |
324 | execShifter(); | |
325 | } else { | |
326 | grunt.log.ok('No YUI modules to build.'); | |
327 | filedone(); | |
328 | } | |
8f76bfb6 | 329 | } |
1aa454ed | 330 | }, done); |
adeb96d2 DW |
331 | }; |
332 | ||
8b02e2d9 DP |
333 | tasks.gherkinlint = function() { |
334 | var done = this.async(), | |
335 | options = grunt.config('gherkinlint.options'); | |
336 | ||
337 | var args = grunt.file.expand(options.files); | |
338 | args.unshift(path.normalize(__dirname + '/node_modules/.bin/gherkin-lint')); | |
339 | grunt.util.spawn({ | |
340 | cmd: 'node', | |
341 | args: args, | |
342 | opts: {stdio: 'inherit', env: process.env} | |
343 | }, function(error, result, code) { | |
344 | // Propagate the exit code. | |
58923606 | 345 | done(code === 0); |
8b02e2d9 DP |
346 | }); |
347 | }; | |
348 | ||
adeb96d2 DW |
349 | tasks.startup = function() { |
350 | // Are we in a YUI directory? | |
e67585f8 | 351 | if (path.basename(path.resolve(cwd, '../../')) == 'yui') { |
a1587268 | 352 | grunt.task.run('yui'); |
adeb96d2 | 353 | // Are we in an AMD directory? |
c9b6feea | 354 | } else if (inAMD) { |
65d070ae | 355 | grunt.task.run('amd'); |
adeb96d2 DW |
356 | } else { |
357 | // Run them all!. | |
65d070ae DP |
358 | grunt.task.run('css'); |
359 | grunt.task.run('js'); | |
d44f7e46 | 360 | grunt.task.run('gherkinlint'); |
adeb96d2 DW |
361 | } |
362 | }; | |
363 | ||
1aa454ed DP |
364 | // On watch, we dynamically modify config to build only affected files. This |
365 | // method is slightly complicated to deal with multiple changed files at once (copied | |
366 | // from the grunt-contrib-watch readme). | |
367 | var changedFiles = Object.create(null); | |
368 | var onChange = grunt.util._.debounce(function() { | |
369 | var files = Object.keys(changedFiles); | |
a382101c DP |
370 | grunt.config('eslint.amd.src', files); |
371 | grunt.config('eslint.yui.src', files); | |
1aa454ed | 372 | grunt.config('shifter.options.paths', files); |
d44f7e46 | 373 | grunt.config('gherkinlint.options.files', files); |
c53f86d4 | 374 | grunt.config('babel.dist.files', [{expand: true, src: files, rename: babelRename}]); |
1aa454ed DP |
375 | changedFiles = Object.create(null); |
376 | }, 200); | |
adeb96d2 | 377 | |
8efbb7b1 | 378 | grunt.event.on('watch', function(action, filepath) { |
1aa454ed DP |
379 | changedFiles[filepath] = action; |
380 | onChange(); | |
8efbb7b1 DP |
381 | }); |
382 | ||
adeb96d2 DW |
383 | // Register NPM tasks. |
384 | grunt.loadNpmTasks('grunt-contrib-uglify'); | |
8efbb7b1 | 385 | grunt.loadNpmTasks('grunt-contrib-watch'); |
af9edb2e | 386 | grunt.loadNpmTasks('grunt-sass'); |
3adb62b7 | 387 | grunt.loadNpmTasks('grunt-eslint'); |
855fc5d8 | 388 | grunt.loadNpmTasks('grunt-stylelint'); |
c53f86d4 | 389 | grunt.loadNpmTasks('grunt-babel'); |
adeb96d2 | 390 | |
65d070ae | 391 | // Register JS tasks. |
adeb96d2 | 392 | grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter); |
8b02e2d9 | 393 | grunt.registerTask('gherkinlint', 'Run gherkinlint against the current directory', tasks.gherkinlint); |
30db70ab | 394 | grunt.registerTask('ignorefiles', 'Generate ignore files for linters', tasks.ignorefiles); |
a1587268 | 395 | grunt.registerTask('yui', ['eslint:yui', 'shifter']); |
c53f86d4 | 396 | grunt.registerTask('amd', ['eslint:amd', 'babel']); |
a1587268 | 397 | grunt.registerTask('js', ['amd', 'yui']); |
65d070ae DP |
398 | |
399 | // Register CSS taks. | |
cf89ac3d | 400 | grunt.registerTask('css', ['stylelint:scss', 'sass', 'stylelint:css']); |
adeb96d2 DW |
401 | |
402 | // Register the startup task. | |
403 | grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup); | |
404 | ||
405 | // Register the default task. | |
406 | grunt.registerTask('default', ['startup']); | |
407 | }; |