6857e391cb8d65ebac3a661f76880ff06b8843e2.svn-base
2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* grunt
* http://gruntjs.com/
*
* Copyright (c) 2012 "Cowboy" Ben Alman
* Licensed under the MIT license.
* https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
*/
module.exports = function(grunt) {
// ==========================================================================
// HELPERS
// ==========================================================================
// Get a config property. Most useful as a directive like <config:foo.bar>.
grunt.registerHelper('config', grunt.config);
// Read a JSON file. Most useful as a directive like <json:package.json>.
var jsons = {};
grunt.registerHelper('json', function(filepath) {
// Don't re-fetch if being called as a directive and JSON is already cached.
if (!this.directive || !(filepath in jsons)) {
jsons[filepath] = grunt.file.readJSON(filepath);
}
return jsons[filepath];
});
// Return the given source coude with any leading banner comment stripped.
grunt.registerHelper('strip_banner', function(src, options) {
if (!options) { options = {}; }
var m = [];
if (options.line) {
// Strip // ... leading banners.
m.push('(?:.*\\/\\/.*\\n)*\\s*');
}
if (options.block) {
// Strips all /* ... */ block comment banners.
m.push('\\/\\*[\\s\\S]*?\\*\\/');
} else {
// Strips only /* ... */ block comment banners, excluding /*! ... */.
m.push('\\/\\*[^!][\\s\\S]*?\\*\\/');
}
var re = new RegExp('^\\s*(?:' + m.join('|') + ')\\s*', '');
return src.replace(re, '');
});
// Get a source file's contents with any leading banner comment stripped. If
// used as a directive, get options from the flags object.
grunt.registerHelper('file_strip_banner', function(filepath, opts) {
var src = grunt.file.read(filepath);
return grunt.helper('strip_banner', src, this.directive ? this.flags : opts);
});
// Process a file as a template.
grunt.registerHelper('file_template', function(filepath) {
var src = grunt.file.read(filepath);
return grunt.template.process(src);
});
// Generate banner from template.
grunt.registerHelper('banner', function(prop) {
if (!prop) { prop = 'meta.banner'; }
var banner;
var tmpl = grunt.config(prop);
if (tmpl) {
// Now, log.
grunt.verbose.write('Generating banner...');
try {
// Compile and run template, using config object as the data source.
banner = grunt.template.process(tmpl) + grunt.utils.linefeed;
grunt.verbose.ok();
} catch(e) {
banner = '';
grunt.verbose.error();
grunt.warn(e, 11);
}
} else {
grunt.warn('No "' + prop + '" banner template defined.', 11);
banner = '';
}
return banner;
});
};