This repository has been archived by the owner on Sep 4, 2019. It is now read-only.
/
rule.js
executable file
·328 lines (289 loc) · 7.92 KB
/
rule.js
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
var path = require('path')
, fs = require('fs')
, Matcher
, rule = {}
, Rule;
// Define a helper object with some utility functions
Matcher = new (function () {
// Split a task to two parts, name space and task name.
// For example, given 'foo:bin/a%.c', return an object with
// - 'ns' : foo
// - 'name' : bin/a%.c
this.split = function(task) {
var parts = task.split(':')
, name = parts.pop()
, ns = this.resolveNS( parts );
return {
'name' : name,
'ns' : ns
};
};
// Return the namespace based on an array of names.
// For example, given ['foo', 'baz' ], return the namespace
//
// default -> foo -> baz
//
// where default is the global root namespace
// and -> means child namespace.
this.resolveNS = function(parts) {
var ns = jake.defaultNamespace;
for(var i = 0, l = parts.length; ns && i < l; i++) {
ns = ns.childNamespaces[parts[i]];
}
return ns;
};
// Given a pattern p, say 'foo:bin/a%.c'
// Return an object with
// - 'ns' : foo
// - 'dir' : bin
// - 'prefix' : a
// - 'suffix' : .c
this.resolve = function(p) {
var task = this.split(p),
name = task.name,
ns = task.ns;
var split = path.basename(name).split('%');
return {
ns: ns
, dir: path.dirname(name)
, prefix: split[0]
, suffix: split[1]
};
};
// Test whether string a is a suffix of string b
this.stringEndWith = function (a,b) {
var l;
return (l = b.lastIndexOf(a)) == -1 ? false : l + a.length == b.length;
};
// Replace the suffix a of the string s with b.
// Note that, it is assumed a is a suffix of s.
this.stringReplaceSuffix = function (s, a, b) {
return s.slice(0,s.lastIndexOf(a)) + b;
};
// Test wether the a prerequisite matchs the pattern.
// The arg 'pattern' does not have namespace as prefix.
// For example, the following tests are true
//
// pattern | name
// bin/%.o | bin/main.o
// bin/%.o | foo:bin/main.o
//
// The following tests are false (trivally)
//
// pattern | name
// bin/%.o | foobin/main.o
// bin/%.o | bin/main.oo
this.match = function(pattern, name) {
var p
, task
, ns
, obj
, filename;
if (pattern instanceof RegExp) {
return pattern.test(name);
}
else if (pattern.indexOf('%') == -1) {
// No Pattern. No Folder. No Namespace.
// A Simple Suffix Rule. Just test suffix
return this.stringEndWith(pattern, name);
}
else {
// Resolve the dir, prefix and suffix of pattern
p = this.resolve(pattern);
// Resolve the namespace and task-name
task = this.split(name);
name = task.name;
ns = task.ns;
// Set the objective as the task-name
obj = name;
// Namespace is already matched.
// Check dir
if (path.dirname(obj) != p.dir) {
return false;
}
filename = path.basename(obj);
// Check file name length
if ((p.prefix.length + p.suffix.length + 1) > filename.length) {
// Length does not match.
return false;
}
// Check prefix
if (filename.indexOf(p.prefix) !== 0) {
return false;
}
// Check suffix
if (!this.stringEndWith(p.suffix, filename)) {
return false;
}
// OK. Find a match.
return true;
}
};
// Generate the source based on
// - name name for the synthesized task
// - pattern pattern for the objective
// - source pattern for the source
//
// Return the source with properties
// - dep the prerequisite of source
// (with the namespace)
//
// - file the file name of source
// (without the namespace)
//
// For example, given
//
// - name foo:bin/main.o
// - pattern bin/%.o
// - source src/%.c
//
// return 'foo:src/main.c',
//
this.getSource = function(name, pattern, source) {
var dep
, pat
, match
, file
, src;
// Regex pattern -- use to look up the extension
if (pattern instanceof RegExp) {
match = pattern.exec(name);
if (match) {
if (typeof source == 'function') {
src = source(name);
}
else {
src = this.stringReplaceSuffix(name, match[0], source);
}
}
}
// Assume string
else {
// Simple string suffix replacement
if (pattern.indexOf('%') == -1) {
if (typeof source == 'function') {
src = source(name);
}
else {
src = this.stringReplaceSuffix(name, pattern, source);
}
}
// Percent-based substitution
else {
pat = pattern.replace('%', '(.*?)');
pat = new RegExp(pat);
match = pat.exec(name);
if (match) {
if (typeof source == 'function') {
src = source(name);
}
else {
file = match[1];
file = source.replace('%', file);
dep = match[0];
src = name.replace(dep, file);
}
}
}
}
return src;
};
})();
Rule = function (opts) {
this.pattern = opts.pattern;
this.source = opts.source;
this.prereqs = opts.prereqs;
this.action = opts.action;
this.opts = opts.opts;
this.desc = opts.desc;
this.ns = opts.ns;
};
Rule.prototype = new (function () {
// Create a file task based on this rule for the specified
// task-name
// ======
// FIXME: Right now this just throws away any passed-in args
// for the synthsized task (taskArgs param)
// ======
this.createTask = function (fullName, level) {
var self = this
, pattern
, source
, action
, opts
, prereqs
, parts
, valid
, name
, nsPath
, ns
, src
, tNs
, createdTask;
parts = fullName.split(':');
name = parts.pop();
nsPath = parts.join(':');
ns = this.ns.resolveNamespace(nsPath);
pattern = this.pattern;
source = this.source;
if (typeof source == 'string') {
src = Matcher.getSource(name, pattern, source);
}
else {
src = source(name);
}
parts.push(src);
src = parts.join(':');
// Generate the prerequisite for the matching task.
// It is the original prerequisites plus the prerequisite
// representing source file, i.e.,
//
// rule( '%.o', '%.c', ['some.h'] ...
//
// If the objective is main.o, then new task should be
//
// file( 'main.o', ['main.c', 'some.h' ] ...
prereqs = this.prereqs;
prereqs.unshift(src);
// Prereq should be:
// 1. an existing task
// 2. an existing file on disk
// 3. a valid rule (i.e., not at too deep a level)
valid = prereqs.some(function (p) {
var ns = self.ns;
return ns.resolveTask(p) ||
fs.existsSync(p) ||
jake.attemptRule(p, ns, level + 1);
});
// If any of the prereqs aren't valid, the rule isn't valid
if (!valid) {
return null;
}
// Otherwise, hunky-dory, finish creating the task for the rule
else {
// Create the action for the task
action = function () {
var task = this;
self.action.apply(task);
};
opts = this.opts;
// Insert the file task into Jake
//
// Since createTask function stores the task as a child task
// of currentNamespace. Here we temporariliy switch the namespace.
// FIXME: Should allow optional ns passed in instead of this hack
tNs = jake.currentNamespace;
jake.currentNamespace = ns;
createdTask = jake.createTask('file', name, prereqs, action, opts);
createdTask.source = src.split(':').pop();
jake.currentNamespace = tNs;
return createdTask;
}
};
this.match = function (name) {
return Matcher.match(this.pattern, name);
};
})();
rule.Rule = Rule;
rule.Matcher = Matcher;
module.exports = rule;