1 | 'use strict';
|
2 |
|
3 | var grunt = require('../grunt');
|
4 |
|
5 |
|
6 | var config = module.exports = function(prop, value) {
|
7 | if (arguments.length === 2) {
|
8 |
|
9 | return config.set(prop, value);
|
10 | } else {
|
11 |
|
12 | return config.get(prop);
|
13 | }
|
14 | };
|
15 |
|
16 |
|
17 | config.data = {};
|
18 |
|
19 |
|
20 | config.escape = function(str) {
|
21 | return str.replace(/\./g, '\\.');
|
22 | };
|
23 |
|
24 |
|
25 | config.getPropString = function(prop) {
|
26 | return Array.isArray(prop) ? prop.map(config.escape).join('.') : prop;
|
27 | };
|
28 |
|
29 |
|
30 | config.getRaw = function(prop) {
|
31 | if (prop) {
|
32 |
|
33 | return grunt.util.namespace.get(config.data, config.getPropString(prop));
|
34 | } else {
|
35 |
|
36 | return config.data;
|
37 | }
|
38 | };
|
39 |
|
40 |
|
41 |
|
42 | var propStringTmplRe = /^<%=\s*([a-z0-9_$]+(?:\.[a-z0-9_$]+)*)\s*%>$/i;
|
43 |
|
44 |
|
45 | config.get = function(prop) {
|
46 | return config.process(config.getRaw(prop));
|
47 | };
|
48 |
|
49 |
|
50 |
|
51 | config.process = function(raw) {
|
52 | return grunt.util.recurse(raw, function(value) {
|
53 |
|
54 | if (typeof value !== 'string') { return value; }
|
55 |
|
56 |
|
57 | var matches = value.match(propStringTmplRe);
|
58 | var result;
|
59 | if (matches) {
|
60 | result = config.get(matches[1]);
|
61 |
|
62 |
|
63 | if (result != null) { return result; }
|
64 | }
|
65 |
|
66 | return grunt.template.process(value, {data: config.data});
|
67 | });
|
68 | };
|
69 |
|
70 |
|
71 | config.set = function(prop, value) {
|
72 | return grunt.util.namespace.set(config.data, config.getPropString(prop), value);
|
73 | };
|
74 |
|
75 |
|
76 | config.merge = function(obj) {
|
77 | grunt.util._.merge(config.data, obj);
|
78 | return config.data;
|
79 | };
|
80 |
|
81 |
|
82 | config.init = function(obj) {
|
83 | grunt.verbose.write('Initializing config...').ok();
|
84 |
|
85 | return (config.data = obj || {});
|
86 | };
|
87 |
|
88 |
|
89 |
|
90 | config.requires = function() {
|
91 | var p = grunt.util.pluralize;
|
92 | var props = grunt.util.toArray(arguments).map(config.getPropString);
|
93 | var msg = 'Verifying propert' + p(props.length, 'y/ies') +
|
94 | ' ' + grunt.log.wordlist(props) + ' exist' + p(props.length, 's') +
|
95 | ' in config...';
|
96 | grunt.verbose.write(msg);
|
97 | var failProps = config.data && props.filter(function(prop) {
|
98 | return config.get(prop) == null;
|
99 | }).map(function(prop) {
|
100 | return '"' + prop + '"';
|
101 | });
|
102 | if (config.data && failProps.length === 0) {
|
103 | grunt.verbose.ok();
|
104 | return true;
|
105 | } else {
|
106 | grunt.verbose.or.write(msg);
|
107 | grunt.log.error().error('Unable to process task.');
|
108 | if (!config.data) {
|
109 | throw grunt.util.error('Unable to load config.');
|
110 | } else {
|
111 | throw grunt.util.error('Required config propert' +
|
112 | p(failProps.length, 'y/ies') + ' ' + failProps.join(', ') + ' missing.');
|
113 | }
|
114 | }
|
115 | };
|