rule.js
7.69 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
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
let path = require('path');
let fs = require('fs');
let Task = require('./task/task').Task;
// 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
function splitNs(task) {
let parts = task.split(':');
let name = parts.pop();
let ns = 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.
function resolveNs(parts) {
let ns = jake.defaultNamespace;
for(let 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
function resolve(p) {
let task = splitNs(p);
let name = task.name;
let ns = task.ns;
let 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
function stringEndWith(a, b) {
let 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.
function stringReplaceSuffix(s, a, b) {
return s.slice(0, s.lastIndexOf(a)) + b;
}
class Rule {
constructor(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;
}
// 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)
// ======
createTask(fullName, level) {
let self = this;
let pattern;
let source;
let action;
let opts;
let prereqs;
let valid;
let src;
let tNs;
let createdTask;
let name = Task.getBaseTaskName(fullName);
let nsPath = Task.getBaseNamespacePath(fullName);
let ns = this.ns.resolveNamespace(nsPath);
pattern = this.pattern;
source = this.source;
if (typeof source == 'string') {
src = Rule.getSource(name, pattern, source);
}
else {
src = source(name);
}
// TODO: Write a utility function that appends a
// taskname to a namespace path
src = nsPath.split(':').filter(function (item) {
return !!item;
}).concat(src).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.slice(); // Get a copy to work with
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) {
let ns = self.ns;
return ns.resolveTask(p) ||
fs.existsSync(Task.getBaseTaskName(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 () {
let 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;
}
}
match(name) {
return Rule.match(this.pattern, name);
}
// 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
static match(pattern, name) {
let p;
let task;
let obj;
let 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 stringEndWith(pattern, name);
}
else {
// Resolve the dir, prefix and suffix of pattern
p = resolve(pattern);
// Resolve the namespace and task-name
task = splitNs(name);
name = task.name;
// 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 (!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',
//
static getSource(name, pattern, source) {
let dep;
let pat;
let match;
let file;
let 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 = 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 = 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;
}
}
exports.Rule = Rule;