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'),
29 // Project configuration.
32 options: {jshintrc: '.jshintrc'},
33 files: ['**/amd/src/*.js']
37 files: grunt.file.expandMapping(
38 ['**/src/*.js', '!**/node_modules/**'],
42 rename: function(destBase, destPath) {
43 destPath = destPath.replace('src', 'build');
44 destPath = destPath.replace('.js', '.min.js');
45 destPath = path.resolve(process.env.PWD, destPath);
54 tasks.shifter = function() {
55 var exec = require('child_process').spawn,
66 // Determine the most appropriate options to run with based upon the current location.
67 if (path.basename(process.env.PWD) === 'src') {
68 // Detect whether we're in a src directory.
69 grunt.log.debug('In a src directory');
72 } else if (path.basename(path.dirname(process.env.PWD)) === 'src') {
73 // Detect whether we're in a module directory.
74 grunt.log.debug('In a module directory');
75 options.module = true;
78 if (grunt.option('watch')) {
79 if (!options.walk && !options.module) {
80 grunt.fail.fatal('Unable to watch unless in a src or module directory');
83 // It is not advisable to run with recursivity and watch - this
84 // leads to building the build directory in a race-like fashion.
85 grunt.log.debug('Detected a watch - disabling recursivity');
86 options.recursive = false;
90 if (options.recursive) {
91 args.push('--recursive');
94 // Always ignore the node_modules directory.
95 args.push('--excludes', 'node_modules');
97 // Add the stderr option if appropriate
98 if (grunt.option('verbose')) {
99 args.push('--lint-stderr');
102 // Actually run shifter.
103 shifter = exec(process.cwd() + '/node_modules/shifter/bin/shifter', args, {
104 cwd: process.env.PWD,
109 // Tidy up after exec.
110 shifter.on('exit', function (code) {
112 grunt.fail.fatal('Shifter failed with code: ' + code);
114 grunt.log.ok('Shifter build complete.');
120 tasks.startup = function() {
121 // Are we in a YUI directory?
122 if (path.basename(path.resolve(process.env.PWD, '../../')) == 'yui') {
123 grunt.task.run('shifter');
124 // Are we in an AMD directory?
125 } else if (path.basename(process.env.PWD) == 'amd') {
126 grunt.task.run('jshint');
127 grunt.task.run('uglify');
130 grunt.task.run('shifter');
131 grunt.task.run('jshint');
132 grunt.task.run('uglify');
137 // Register NPM tasks.
138 grunt.loadNpmTasks('grunt-contrib-uglify');
139 grunt.loadNpmTasks('grunt-contrib-jshint');
141 // Register the shifter task.
142 grunt.registerTask('shifter', 'Run Shifter against the current directory', tasks.shifter);
144 // Register the startup task.
145 grunt.registerTask('startup', 'Run the correct tasks for the current directory', tasks.startup);
147 // Register the default task.
148 grunt.registerTask('default', ['startup']);