1 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
17 * @copyright 2014 Andrew Nicols
18 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 module.exports = function(grunt) {
26 var path = require('path'),
28 cwd = process.env.PWD || process.cwd();
30 // Project configuration.
33 options: {jshintrc: '.jshintrc'},
34 files: ['**/amd/src/*.js']
38 files: grunt.file.expandMapping(
39 ['**/src/*.js', '!**/node_modules/**'],
43 rename: function(destBase, destPath) {
44 destPath = destPath.replace('src', 'build');
45 destPath = destPath.replace('.js', '.min.js');
46 destPath = path.resolve(cwd, destPath);
55 tasks.shifter = function() {
56 var exec = require('child_process').spawn,
67 args.push( path.normalize(__dirname + '/node_modules/shifter/bin/shifter'));
69 // Determine the most appropriate options to run with based upon the current location.
70 if (path.basename(cwd) === 'src') {
71 // Detect whether we're in a src directory.
72 grunt.log.debug('In a src directory');
75 } else if (path.basename(path.dirname(cwd)) === 'src') {
76 // Detect whether we're in a module directory.
77 grunt.log.debug('In a module directory');
78 options.module = true;
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');
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;
93 if (options.recursive) {
94 args.push('--recursive');
97 // Always ignore the node_modules directory.
98 args.push('--excludes', 'node_modules');
100 // Add the stderr option if appropriate
101 if (grunt.option('verbose')) {
102 args.push('--lint-stderr');
105 // Actually run shifter.
106 shifter = exec("node", args, {
112 // Tidy up after exec.
113 shifter.on('exit', function (code) {
115 grunt.fail.fatal('Shifter failed with code: ' + code);
117 grunt.log.ok('Shifter build complete.');
123 tasks.startup = function() {
124 // Are we in a YUI directory?
125 if (path.basename(path.resolve(cwd, '../../')) == 'yui') {
126 grunt.task.run('shifter');
127 // Are we in an AMD directory?
128 } else if (path.basename(cwd) == 'amd') {
129 grunt.task.run('jshint');
130 grunt.task.run('uglify');
133 grunt.task.run('shifter');
134 grunt.task.run('jshint');
135 grunt.task.run('uglify');
140 // Register NPM tasks.
141 grunt.loadNpmTasks('grunt-contrib-uglify');
142 grunt.loadNpmTasks('grunt-contrib-jshint');
144 // Register the shifter task.
145 grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
147 // Register the startup task.
148 grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
150 // Register the default task.
151 grunt.registerTask('default', ['startup']);