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') }, | |
78 | // Check AMD files with standard config | |
79 | amd: { src: amdSrc }, | |
a1587268 DP |
80 | // Some rules disabled for YUI config, because we don't do rollup magic, so its able to be accurate |
81 | // about undefined items. | |
82 | yui: { | |
83 | src: ['**/yui/src/**/*.js'], | |
84 | options: {globals: ['Y', 'YUI'], rules: {'no-undef': 'off', 'no-unused-vars': 'off', 'no-empty': 'off'} } | |
85 | } | |
3adb62b7 | 86 | }, |
adeb96d2 | 87 | uglify: { |
5cc5f311 DP |
88 | amd: { |
89 | files: [{ | |
90 | expand: true, | |
91 | src: amdSrc, | |
92 | rename: uglify_rename | |
93 | }] | |
adeb96d2 | 94 | } |
a4a52e56 DP |
95 | }, |
96 | less: { | |
97 | bootstrapbase: { | |
98 | files: { | |
99 | "theme/bootstrapbase/style/moodle.css": "theme/bootstrapbase/less/moodle.less", | |
100 | "theme/bootstrapbase/style/editor.css": "theme/bootstrapbase/less/editor.less", | |
101 | }, | |
102 | options: { | |
103 | compress: true | |
104 | } | |
105 | } | |
8efbb7b1 DP |
106 | }, |
107 | watch: { | |
108 | options: { | |
109 | nospawn: true // We need not to spawn so config can be changed dynamically. | |
110 | }, | |
111 | amd: { | |
112 | files: ['**/amd/src/**/*.js'], | |
113 | tasks: ['amd'] | |
114 | }, | |
115 | bootstrapbase: { | |
116 | files: ["theme/bootstrapbase/less/**/*.less"], | |
117 | tasks: ["less:bootstrapbase"] | |
118 | }, | |
119 | yui: { | |
120 | files: ['**/yui/src/**/*.js'], | |
a1587268 | 121 | tasks: ['yui'] |
8efbb7b1 | 122 | }, |
1aa454ed DP |
123 | }, |
124 | shifter: { | |
125 | options: { | |
126 | recursive: true, | |
127 | paths: [cwd] | |
128 | } | |
adeb96d2 DW |
129 | } |
130 | }); | |
131 | ||
1aa454ed DP |
132 | /** |
133 | * Shifter task. Is configured with a path to a specific file or a directory, | |
134 | * in the case of a specific file it will work out the right module to be built. | |
135 | * | |
136 | * Note that this task runs the invidiaul shifter jobs async (becase it spawns | |
137 | * so be careful to to call done(). | |
138 | */ | |
adeb96d2 | 139 | tasks.shifter = function() { |
1aa454ed | 140 | var async = require('async'), |
adeb96d2 | 141 | done = this.async(), |
1aa454ed | 142 | options = grunt.config('shifter.options'); |
adeb96d2 | 143 | |
1aa454ed DP |
144 | // Run the shifter processes one at a time to avoid confusing output. |
145 | async.eachSeries(options.paths, function (src, filedone) { | |
146 | var args = []; | |
e67585f8 DW |
147 | args.push( path.normalize(__dirname + '/node_modules/shifter/bin/shifter')); |
148 | ||
1aa454ed DP |
149 | // Always ignore the node_modules directory. |
150 | args.push('--excludes', 'node_modules'); | |
151 | ||
adeb96d2 | 152 | // Determine the most appropriate options to run with based upon the current location. |
1aa454ed DP |
153 | if (grunt.file.isMatch('**/yui/**/*.js', src)) { |
154 | // When passed a JS file, build our containing module (this happen with | |
155 | // watch). | |
156 | grunt.log.debug('Shifter passed a specific JS file'); | |
157 | src = path.dirname(path.dirname(src)); | |
158 | options.recursive = false; | |
159 | } else if (grunt.file.isMatch('**/yui/src', src)) { | |
160 | // When in a src directory --walk all modules. | |
adeb96d2 DW |
161 | grunt.log.debug('In a src directory'); |
162 | args.push('--walk'); | |
1aa454ed DP |
163 | options.recursive = false; |
164 | } else if (grunt.file.isMatch('**/yui/src/*', src)) { | |
165 | // When in module, only build our module. | |
adeb96d2 | 166 | grunt.log.debug('In a module directory'); |
adeb96d2 | 167 | options.recursive = false; |
1aa454ed DP |
168 | } else if (grunt.file.isMatch('**/yui/src/*/js', src)) { |
169 | // When in module src, only build our module. | |
170 | grunt.log.debug('In a source directory'); | |
171 | src = path.dirname(src); | |
172 | options.recursive = false; | |
adeb96d2 DW |
173 | } |
174 | ||
1aa454ed DP |
175 | if (grunt.option('watch')) { |
176 | grunt.fail.fatal('The --watch option has been removed, please use `grunt watch` instead'); | |
adeb96d2 DW |
177 | } |
178 | ||
adeb96d2 DW |
179 | // Add the stderr option if appropriate |
180 | if (grunt.option('verbose')) { | |
181 | args.push('--lint-stderr'); | |
182 | } | |
183 | ||
a07afffc DP |
184 | if (grunt.option('no-color')) { |
185 | args.push('--color=false'); | |
186 | } | |
187 | ||
8f76bfb6 DM |
188 | var execShifter = function() { |
189 | ||
1aa454ed DP |
190 | grunt.log.ok("Running shifter on " + src); |
191 | grunt.util.spawn({ | |
192 | cmd: "node", | |
193 | args: args, | |
194 | opts: {cwd: src, stdio: 'inherit', env: process.env} | |
195 | }, function (error, result, code) { | |
8f76bfb6 DM |
196 | if (code) { |
197 | grunt.fail.fatal('Shifter failed with code: ' + code); | |
198 | } else { | |
199 | grunt.log.ok('Shifter build complete.'); | |
1aa454ed | 200 | filedone(); |
8f76bfb6 DM |
201 | } |
202 | }); | |
203 | }; | |
204 | ||
adeb96d2 | 205 | // Actually run shifter. |
8f76bfb6 DM |
206 | if (!options.recursive) { |
207 | execShifter(); | |
208 | } else { | |
209 | // Check that there are yui modules otherwise shifter ends with exit code 1. | |
1aa454ed DP |
210 | if (grunt.file.expand({cwd: src}, '**/yui/src/**/*.js').length > 0) { |
211 | args.push('--recursive'); | |
212 | execShifter(); | |
213 | } else { | |
214 | grunt.log.ok('No YUI modules to build.'); | |
215 | filedone(); | |
216 | } | |
8f76bfb6 | 217 | } |
1aa454ed | 218 | }, done); |
adeb96d2 DW |
219 | }; |
220 | ||
221 | tasks.startup = function() { | |
222 | // Are we in a YUI directory? | |
e67585f8 | 223 | if (path.basename(path.resolve(cwd, '../../')) == 'yui') { |
a1587268 | 224 | grunt.task.run('yui'); |
adeb96d2 | 225 | // Are we in an AMD directory? |
c9b6feea | 226 | } else if (inAMD) { |
65d070ae | 227 | grunt.task.run('amd'); |
adeb96d2 DW |
228 | } else { |
229 | // Run them all!. | |
65d070ae DP |
230 | grunt.task.run('css'); |
231 | grunt.task.run('js'); | |
adeb96d2 DW |
232 | } |
233 | }; | |
234 | ||
1aa454ed DP |
235 | // On watch, we dynamically modify config to build only affected files. This |
236 | // method is slightly complicated to deal with multiple changed files at once (copied | |
237 | // from the grunt-contrib-watch readme). | |
238 | var changedFiles = Object.create(null); | |
239 | var onChange = grunt.util._.debounce(function() { | |
240 | var files = Object.keys(changedFiles); | |
241 | grunt.config('jshint.amd.src', files); | |
242 | grunt.config('uglify.amd.files', [{ expand: true, src: files, rename: uglify_rename }]); | |
243 | grunt.config('shifter.options.paths', files); | |
244 | changedFiles = Object.create(null); | |
245 | }, 200); | |
adeb96d2 | 246 | |
8efbb7b1 | 247 | grunt.event.on('watch', function(action, filepath) { |
1aa454ed DP |
248 | changedFiles[filepath] = action; |
249 | onChange(); | |
8efbb7b1 DP |
250 | }); |
251 | ||
adeb96d2 DW |
252 | // Register NPM tasks. |
253 | grunt.loadNpmTasks('grunt-contrib-uglify'); | |
254 | grunt.loadNpmTasks('grunt-contrib-jshint'); | |
a4a52e56 | 255 | grunt.loadNpmTasks('grunt-contrib-less'); |
8efbb7b1 | 256 | grunt.loadNpmTasks('grunt-contrib-watch'); |
3adb62b7 | 257 | grunt.loadNpmTasks('grunt-eslint'); |
adeb96d2 | 258 | |
65d070ae | 259 | // Register JS tasks. |
adeb96d2 | 260 | grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter); |
a1587268 | 261 | grunt.registerTask('yui', ['eslint:yui', 'shifter']); |
3adb62b7 | 262 | grunt.registerTask('amd', ['eslint:amd', 'jshint', 'uglify']); |
a1587268 | 263 | grunt.registerTask('js', ['amd', 'yui']); |
65d070ae DP |
264 | |
265 | // Register CSS taks. | |
266 | grunt.registerTask('css', ['less:bootstrapbase']); | |
adeb96d2 DW |
267 | |
268 | // Register the startup task. | |
269 | grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup); | |
270 | ||
271 | // Register the default task. | |
272 | grunt.registerTask('default', ['startup']); | |
273 | }; |