1 | /* |
---|
2 | ** grunt-path-check -- Grunt Task for Checking Existence of Programs on PATH |
---|
3 | ** Copyright (c) 2013 Ralf S. Engelschall <rse@engelschall.com> |
---|
4 | ** |
---|
5 | ** Permission is hereby granted, free of charge, to any person obtaining |
---|
6 | ** a copy of this software and associated documentation files (the |
---|
7 | ** "Software"), to deal in the Software without restriction, including |
---|
8 | ** without limitation the rights to use, copy, modify, merge, publish, |
---|
9 | ** distribute, sublicense, and/or sell copies of the Software, and to |
---|
10 | ** permit persons to whom the Software is furnished to do so, subject to |
---|
11 | ** the following conditions: |
---|
12 | ** |
---|
13 | ** The above copyright notice and this permission notice shall be included |
---|
14 | ** in all copies or substantial portions of the Software. |
---|
15 | ** |
---|
16 | ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
---|
17 | ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
---|
18 | ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
---|
19 | ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
---|
20 | ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
---|
21 | ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
---|
22 | ** SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
---|
23 | */ |
---|
24 | |
---|
25 | /* global module: false */ |
---|
26 | module.exports = function (grunt) { |
---|
27 | /* global require: false */ |
---|
28 | var path = require("path"); |
---|
29 | var chalk = require("chalk"); |
---|
30 | |
---|
31 | /* define the Grunt task */ |
---|
32 | grunt.registerMultiTask("path-check", "Check Existence of Programs on PATH", function () { |
---|
33 | /* prepare task options */ |
---|
34 | var options = this.options({ |
---|
35 | mandatory: true, |
---|
36 | tasks: [] |
---|
37 | }); |
---|
38 | grunt.verbose.writeflags(options, "Options"); |
---|
39 | |
---|
40 | /* retrieve the PATH elements */ |
---|
41 | /* global process: false */ |
---|
42 | var PATH = process.env.PATH.split(path.delimiter); |
---|
43 | |
---|
44 | /* iterate over all src-dest file pairs */ |
---|
45 | var errors = 0; |
---|
46 | this.files.forEach(function (f) { |
---|
47 | /* iterate over all programs */ |
---|
48 | f.orig.src.forEach(function (name) { |
---|
49 | /* iterate over all path elements */ |
---|
50 | var found = false; |
---|
51 | PATH.forEach(function (dir) { |
---|
52 | /* short-circuiting */ |
---|
53 | if (found) |
---|
54 | return; |
---|
55 | |
---|
56 | /* check for program existence */ |
---|
57 | var file = path.join(dir, name); |
---|
58 | if (grunt.file.exists(file)) { |
---|
59 | grunt.log.writeln("Program \"" + chalk.green(name) + "\" found under \"" + chalk.green(file) + "\"."); |
---|
60 | found = true; |
---|
61 | } |
---|
62 | }); |
---|
63 | |
---|
64 | /* just record the fact that a program is not found |
---|
65 | (intentionally still does not abort Grunt processing) */ |
---|
66 | if (!found) { |
---|
67 | grunt.log.warn("Program \"" + chalk.red(name) + "\" not found in PATH."); |
---|
68 | errors++; |
---|
69 | } |
---|
70 | }); |
---|
71 | }); |
---|
72 | if (errors > 0) { |
---|
73 | if (options.mandatory) |
---|
74 | grunt.fail.warn(chalk.red(errors) + " program(s) not found in PATH!"); |
---|
75 | else |
---|
76 | grunt.log.warn(chalk.red(errors) + " program(s) not found in PATH!"); |
---|
77 | if (options.tasks.length > 0) |
---|
78 | grunt.log.writeln("Skipping dependent tasks: \"" + |
---|
79 | options.tasks |
---|
80 | .map(function (task) { return chalk.red(task); }) |
---|
81 | .join("\", \"") + |
---|
82 | "\""); |
---|
83 | } |
---|
84 | else if (options.tasks.length > 0) { |
---|
85 | grunt.log.writeln("Running dependent tasks: \"" + |
---|
86 | options.tasks |
---|
87 | .map(function (task) { return chalk.green(task); }) |
---|
88 | .join("\", \"") + |
---|
89 | "\""); |
---|
90 | grunt.task.run(options.tasks); |
---|
91 | } |
---|
92 | }); |
---|
93 | }; |
---|
94 | |
---|