weekly release 3.0dev
[moodle.git] / Gruntfile.js
CommitLineData
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
25module.exports = function(grunt) {
26 var path = require('path'),
e67585f8
DW
27 tasks = {},
28 cwd = process.env.PWD || process.cwd();
adeb96d2
DW
29
30 // Project configuration.
31 grunt.initConfig({
32 jshint: {
33 options: {jshintrc: '.jshintrc'},
34 files: ['**/amd/src/*.js']
35 },
36 uglify: {
37 dynamic_mappings: {
38 files: grunt.file.expandMapping(
39 ['**/src/*.js', '!**/node_modules/**'],
40 '',
41 {
e67585f8 42 cwd: cwd,
adeb96d2
DW
43 rename: function(destBase, destPath) {
44 destPath = destPath.replace('src', 'build');
45 destPath = destPath.replace('.js', '.min.js');
e67585f8 46 destPath = path.resolve(cwd, destPath);
adeb96d2
DW
47 return destPath;
48 }
49 }
50 )
51 }
52 }
53 });
54
55 tasks.shifter = function() {
56 var exec = require('child_process').spawn,
57 done = this.async(),
58 args = [],
59 options = {
60 recursive: true,
61 watch: false,
62 walk: false,
63 module: false
64 },
65 shifter;
66
e67585f8
DW
67 args.push( path.normalize(__dirname + '/node_modules/shifter/bin/shifter'));
68
adeb96d2 69 // Determine the most appropriate options to run with based upon the current location.
e67585f8 70 if (path.basename(cwd) === 'src') {
adeb96d2
DW
71 // Detect whether we're in a src directory.
72 grunt.log.debug('In a src directory');
73 args.push('--walk');
74 options.walk = true;
e67585f8 75 } else if (path.basename(path.dirname(cwd)) === 'src') {
adeb96d2
DW
76 // Detect whether we're in a module directory.
77 grunt.log.debug('In a module directory');
78 options.module = true;
79 }
80
81 if (grunt.option('watch')) {
82 if (!options.walk && !options.module) {
83 grunt.fail.fatal('Unable to watch unless in a src or module directory');
84 }
85
86 // It is not advisable to run with recursivity and watch - this
87 // leads to building the build directory in a race-like fashion.
88 grunt.log.debug('Detected a watch - disabling recursivity');
89 options.recursive = false;
90 args.push('--watch');
91 }
92
93 if (options.recursive) {
94 args.push('--recursive');
95 }
96
97 // Always ignore the node_modules directory.
98 args.push('--excludes', 'node_modules');
99
100 // Add the stderr option if appropriate
101 if (grunt.option('verbose')) {
102 args.push('--lint-stderr');
103 }
104
105 // Actually run shifter.
e67585f8
DW
106 shifter = exec("node", args, {
107 cwd: cwd,
adeb96d2
DW
108 stdio: 'inherit',
109 env: process.env
110 });
111
112 // Tidy up after exec.
113 shifter.on('exit', function (code) {
114 if (code) {
115 grunt.fail.fatal('Shifter failed with code: ' + code);
116 } else {
117 grunt.log.ok('Shifter build complete.');
118 done();
119 }
120 });
121 };
122
123 tasks.startup = function() {
124 // Are we in a YUI directory?
e67585f8 125 if (path.basename(path.resolve(cwd, '../../')) == 'yui') {
adeb96d2
DW
126 grunt.task.run('shifter');
127 // Are we in an AMD directory?
e67585f8 128 } else if (path.basename(cwd) == 'amd') {
adeb96d2
DW
129 grunt.task.run('jshint');
130 grunt.task.run('uglify');
131 } else {
132 // Run them all!.
133 grunt.task.run('shifter');
134 grunt.task.run('jshint');
135 grunt.task.run('uglify');
136 }
137 };
138
139
140 // Register NPM tasks.
141 grunt.loadNpmTasks('grunt-contrib-uglify');
142 grunt.loadNpmTasks('grunt-contrib-jshint');
143
144 // Register the shifter task.
145 grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
146
147 // Register the startup task.
148 grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
149
150 // Register the default task.
151 grunt.registerTask('default', ['startup']);
152};