Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1441 ariadna 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
/* jshint node: true, browser: false */
16
/* eslint-env node */
17
 
18
/**
19
 * @copyright  2021 Andrew Nicols
20
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
21
 */
22
 
23
/* eslint-env node */
24
 
25
module.exports = grunt => {
26
    /**
27
     * Shifter task. Is configured with a path to a specific file or a directory,
28
     * in the case of a specific file it will work out the right module to be built.
29
     *
30
     * Note that this task runs the invidiaul shifter jobs async (becase it spawns
31
     * so be careful to to call done().
32
     */
33
    const handler = function() {
34
        const done = this.async();
35
        const options = grunt.config('shifter.options');
36
        const async = require('async');
37
        const path = require('path');
38
 
39
        // Run the shifter processes one at a time to avoid confusing output.
40
        async.eachSeries(options.paths, function(src, filedone) {
41
            var args = [];
42
            args.push(path.normalize(process.cwd() + '/node_modules/shifter/bin/shifter'));
43
 
44
            // Always ignore the node_modules directory.
45
            args.push('--excludes', 'node_modules');
46
 
47
            // Determine the most appropriate options to run with based upon the current location.
48
            if (grunt.file.isMatch('**/yui/**/*.js', src)) {
49
                // When passed a JS file, build our containing module (this happen with
50
                // watch).
51
                grunt.log.debug('Shifter passed a specific JS file');
52
                src = path.dirname(path.dirname(src));
53
                options.recursive = false;
54
            } else if (grunt.file.isMatch('**/yui/src', src)) {
55
                // When in a src directory --walk all modules.
56
                grunt.log.debug('In a src directory');
57
                args.push('--walk');
58
                options.recursive = false;
59
            } else if (grunt.file.isMatch('**/yui/src/*', src)) {
60
                // When in module, only build our module.
61
                grunt.log.debug('In a module directory');
62
                options.recursive = false;
63
            } else if (grunt.file.isMatch('**/yui/src/*/js', src)) {
64
                // When in module src, only build our module.
65
                grunt.log.debug('In a source directory');
66
                src = path.dirname(src);
67
                options.recursive = false;
68
            }
69
 
70
            if (grunt.option('watch')) {
71
                grunt.fail.fatal('The --watch option has been removed, please use `grunt watch` instead');
72
            }
73
 
74
            // Add the stderr option if appropriate
75
            if (grunt.option('verbose')) {
76
                args.push('--lint-stderr');
77
            }
78
 
79
            if (grunt.option('no-color')) {
80
                args.push('--color=false');
81
            }
82
 
83
            var execShifter = function() {
84
 
85
                grunt.log.ok("Running shifter on " + src);
86
                grunt.util.spawn({
87
                    cmd: "node",
88
                    args: args,
89
                    opts: {cwd: src, stdio: 'inherit', env: process.env}
90
                }, function(error, result, code) {
91
                    if (code) {
92
                        grunt.fail.fatal('Shifter failed with code: ' + code);
93
                    } else {
94
                        grunt.log.ok('Shifter build complete.');
95
                        filedone();
96
                    }
97
                });
98
            };
99
 
100
            // Actually run shifter.
101
            if (!options.recursive) {
102
                execShifter();
103
            } else {
104
                // Check that there are yui modules otherwise shifter ends with exit code 1.
105
                if (grunt.file.expand({cwd: src}, '**/yui/src/**/*.js').length > 0) {
106
                    args.push('--recursive');
107
                    execShifter();
108
                } else {
109
                    grunt.log.ok('No YUI modules to build.');
110
                    filedone();
111
                }
112
            }
113
        }, done);
114
    };
115
 
116
    // Register the shifter task.
117
    grunt.registerTask('shifter', 'Run Shifter against the current directory', handler);
118
 
119
    // Configure it.
120
    grunt.config.set('shifter', {
121
        options: {
122
            recursive: true,
123
            // Shifter takes a relative path.
124
            paths: grunt.moodleEnv.files ? grunt.moodleEnv.files : [grunt.moodleEnv.runDir]
125
        }
126
    });
127
 
128
    grunt.config.merge({
129
        watch: {
130
            yui: {
131
                files: grunt.moodleEnv.inComponent
132
                    ? ['yui/src/*.json', 'yui/src/**/*.js']
133
                    : ['**/yui/src/**/*.js'],
134
                tasks: ['yui']
135
            },
136
        },
137
    });
138
 
139
    // On watch, we dynamically modify config to build only affected files. This
140
    // method is slightly complicated to deal with multiple changed files at once (copied
141
    // from the grunt-contrib-watch readme).
142
    let changedFiles = Object.create(null);
143
    const onChange = grunt.util._.debounce(function() {
144
        const files = Object.keys(changedFiles);
145
        grunt.config('shifter.options.paths', files);
146
        changedFiles = Object.create(null);
147
    }, 200);
148
 
149
    grunt.event.on('watch', (action, filepath) => {
150
        changedFiles[filepath] = action;
151
        onChange();
152
    });
153
 
154
    return handler;
155
};