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