5 * Copyright (c) 2012 "Cowboy" Ben Alman
6 * Licensed under the MIT license.
7 * https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
10 module.exports = function(grunt) {
12 // ==========================================================================
14 // ==========================================================================
16 // Get a config property. Most useful as a directive like <config:foo.bar>.
17 grunt.registerHelper('config', grunt.config);
19 // Read a JSON file. Most useful as a directive like <json:package.json>.
21 grunt.registerHelper('json', function(filepath) {
22 // Don't re-fetch if being called as a directive and JSON is already cached.
23 if (!this.directive || !(filepath in jsons)) {
24 jsons[filepath] = grunt.file.readJSON(filepath);
26 return jsons[filepath];
29 // Return the given source coude with any leading banner comment stripped.
30 grunt.registerHelper('strip_banner', function(src, options) {
31 if (!options) { options = {}; }
34 // Strip // ... leading banners.
35 m.push('(?:.*\\/\\/.*\\n)*\\s*');
38 // Strips all /* ... */ block comment banners.
39 m.push('\\/\\*[\\s\\S]*?\\*\\/');
41 // Strips only /* ... */ block comment banners, excluding /*! ... */.
42 m.push('\\/\\*[^!][\\s\\S]*?\\*\\/');
44 var re = new RegExp('^\\s*(?:' + m.join('|') + ')\\s*', '');
45 return src.replace(re, '');
48 // Get a source file's contents with any leading banner comment stripped. If
49 // used as a directive, get options from the flags object.
50 grunt.registerHelper('file_strip_banner', function(filepath, opts) {
51 var src = grunt.file.read(filepath);
52 return grunt.helper('strip_banner', src, this.directive ? this.flags : opts);
55 // Process a file as a template.
56 grunt.registerHelper('file_template', function(filepath) {
57 var src = grunt.file.read(filepath);
58 return grunt.template.process(src);
61 // Generate banner from template.
62 grunt.registerHelper('banner', function(prop) {
63 if (!prop) { prop = 'meta.banner'; }
65 var tmpl = grunt.config(prop);
68 grunt.verbose.write('Generating banner...');
70 // Compile and run template, using config object as the data source.
71 banner = grunt.template.process(tmpl) + grunt.utils.linefeed;
75 grunt.verbose.error();
79 grunt.warn('No "' + prop + '" banner template defined.', 11);