92409d45cc743786b4a649d7755697cdc7a477a1.svn-base
3.79 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
* grunt
* http://gruntjs.com/
*
* Copyright (c) 2012 "Cowboy" Ben Alman
* Licensed under the MIT license.
* https://github.com/gruntjs/grunt/blob/master/LICENSE-MIT
*/
var grunt = require('../grunt');
// The actual config data.
var data;
// Get/set config data. If data hasn't been set, return null. If value was
// passed, set value. If props string wasn't passed, return all data. Otherwise,
// return the prop's value.
var config = module.exports = function(prop, value) {
if (arguments.length === 2) {
// Two arguments were passed, set the property's value.
return config.set(prop, value);
} else {
// Get the property's value (or the entire data object).
return config.get(prop);
}
};
// If prop is an array, convert it to a props string.
function getPropString(prop) {
if (grunt.utils.kindOf(prop) === 'array') {
return prop.map(config.escape).join('.');
}
return prop;
}
// Recursively expand config directives.
function processDirectives() {
// These directives should be processed now.
var toProcess = ['config', 'json'];
data = grunt.utils.recurse(data, function(value) {
if (typeof value !== 'string') { return value; }
var parts = grunt.task.getDirectiveParts(value) || [];
return toProcess.indexOf(parts[0]) !== -1 ? grunt.task.directive(value) : value;
});
}
// Get config data.
config.get = function(prop) {
// Abort if no config data exists.
if (!data) { return null; }
// If prop is an array, convert it to a prop string.
prop = getPropString(prop);
if (prop) {
// A property string/array was passed, get that property's value.
return grunt.utils.namespace.get(data, prop);
} else {
// Nothing was passed. Return a shalow clone of the actual config data.
return grunt.utils._.clone(data);
}
};
// Set config data.
config.set = function(prop, value) {
// Abort if no config data exists.
if (!data) { return null; }
// If prop is an array, convert it to a prop string.
prop = getPropString(prop);
// Set the property's value.
var result = grunt.utils.namespace.set(data, prop, value);
// Process directives.
processDirectives();
// Return result.
return result;
};
// Get config data, processing it as a template if necessary.
config.process = function(prop) {
return grunt.utils.recurse(config.get(prop), function(value) {
if (typeof value !== 'string') { return value; }
return grunt.template.process(value, data);
});
};
// Initialize config data.
config.init = function(obj) {
grunt.verbose.write('Initializing config...').ok();
// Initialize data.
data = obj || {};
// Process directives.
processDirectives();
// Return data.
return data;
};
// Escape any . in name with \. so dot-based namespacing works properly.
config.escape = function(str) {
return str.replace(/\./g, '\\.');
};
// Test to see if required config params have been defined. If not, throw an
// exception (use this inside of a task).
config.requires = function() {
var p = grunt.utils.pluralize;
var props = grunt.utils.toArray(arguments).map(getPropString);
var msg = 'Verifying propert' + p(props.length, 'y/ies') +
' ' + grunt.log.wordlist(props) + ' exist' + p(props.length, 's') +
' in config...';
grunt.verbose.write(msg);
var failProps = data && props.filter(function(prop) {
return config.get(prop) === undefined;
}).map(function(prop) {
return '"' + prop + '"';
});
if (data && failProps.length === 0) {
grunt.verbose.ok();
return true;
} else {
grunt.verbose.or.write(msg);
grunt.log.error().error('Unable to process task.');
if (!data) {
throw grunt.task.taskError('Unable to load config.');
} else {
throw grunt.task.taskError('Required config propert' +
p(failProps.length, 'y/ies') + ' ' + failProps.join(', ') +
' missing.');
}
}
};