1 let path = require('path');
2 let fs = require('fs');
3 let Task = require('./task/task').Task;
5 // Split a task to two parts, name space and task name.
6 // For example, given 'foo:bin/a%.c', return an object with
9 function splitNs(task) {
10 let parts = task.split(':');
11 let name = parts.pop();
12 let ns = resolveNs(parts);
19 // Return the namespace based on an array of names.
20 // For example, given ['foo', 'baz' ], return the namespace
22 // default -> foo -> baz
24 // where default is the global root namespace
25 // and -> means child namespace.
26 function resolveNs(parts) {
27 let ns = jake.defaultNamespace;
28 for(let i = 0, l = parts.length; ns && i < l; i++) {
29 ns = ns.childNamespaces[parts[i]];
34 // Given a pattern p, say 'foo:bin/a%.c'
35 // Return an object with
41 let task = splitNs(p);
44 let split = path.basename(name).split('%');
47 dir: path.dirname(name),
53 // Test whether string a is a suffix of string b
54 function stringEndWith(a, b) {
56 return (l = b.lastIndexOf(a)) == -1 ? false : l + a.length == b.length;
59 // Replace the suffix a of the string s with b.
60 // Note that, it is assumed a is a suffix of s.
61 function stringReplaceSuffix(s, a, b) {
62 return s.slice(0, s.lastIndexOf(a)) + b;
67 this.pattern = opts.pattern;
68 this.source = opts.source;
69 this.prereqs = opts.prereqs;
70 this.action = opts.action;
71 this.opts = opts.opts;
72 this.desc = opts.desc;
76 // Create a file task based on this rule for the specified
79 // FIXME: Right now this just throws away any passed-in args
80 // for the synthsized task (taskArgs param)
82 createTask(fullName, level) {
93 let name = Task.getBaseTaskName(fullName);
94 let nsPath = Task.getBaseNamespacePath(fullName);
95 let ns = this.ns.resolveNamespace(nsPath);
97 pattern = this.pattern;
100 if (typeof source == 'string') {
101 src = Rule.getSource(name, pattern, source);
107 // TODO: Write a utility function that appends a
108 // taskname to a namespace path
109 src = nsPath.split(':').filter(function (item) {
111 }).concat(src).join(':');
113 // Generate the prerequisite for the matching task.
114 // It is the original prerequisites plus the prerequisite
115 // representing source file, i.e.,
117 // rule( '%.o', '%.c', ['some.h'] ...
119 // If the objective is main.o, then new task should be
121 // file( 'main.o', ['main.c', 'some.h' ] ...
122 prereqs = this.prereqs.slice(); // Get a copy to work with
123 prereqs.unshift(src);
126 // 1. an existing task
127 // 2. an existing file on disk
128 // 3. a valid rule (i.e., not at too deep a level)
129 valid = prereqs.some(function (p) {
131 return ns.resolveTask(p) ||
132 fs.existsSync(Task.getBaseTaskName(p)) ||
133 jake.attemptRule(p, ns, level + 1);
136 // If any of the prereqs aren't valid, the rule isn't valid
140 // Otherwise, hunky-dory, finish creating the task for the rule
142 // Create the action for the task
143 action = function () {
145 self.action.apply(task);
150 // Insert the file task into Jake
152 // Since createTask function stores the task as a child task
153 // of currentNamespace. Here we temporariliy switch the namespace.
154 // FIXME: Should allow optional ns passed in instead of this hack
155 tNs = jake.currentNamespace;
156 jake.currentNamespace = ns;
157 createdTask = jake.createTask('file', name, prereqs, action, opts);
158 createdTask.source = src.split(':').pop();
159 jake.currentNamespace = tNs;
166 return Rule.match(this.pattern, name);
169 // Test wether the a prerequisite matchs the pattern.
170 // The arg 'pattern' does not have namespace as prefix.
171 // For example, the following tests are true
174 // bin/%.o | bin/main.o
175 // bin/%.o | foo:bin/main.o
177 // The following tests are false (trivally)
180 // bin/%.o | foobin/main.o
181 // bin/%.o | bin/main.oo
182 static match(pattern, name) {
188 if (pattern instanceof RegExp) {
189 return pattern.test(name);
191 else if (pattern.indexOf('%') == -1) {
192 // No Pattern. No Folder. No Namespace.
193 // A Simple Suffix Rule. Just test suffix
194 return stringEndWith(pattern, name);
197 // Resolve the dir, prefix and suffix of pattern
198 p = resolve(pattern);
200 // Resolve the namespace and task-name
201 task = splitNs(name);
204 // Set the objective as the task-name
207 // Namespace is already matched.
210 if (path.dirname(obj) != p.dir) {
214 filename = path.basename(obj);
216 // Check file name length
217 if ((p.prefix.length + p.suffix.length + 1) > filename.length) {
218 // Length does not match.
223 if (filename.indexOf(p.prefix) !== 0) {
228 if (!stringEndWith(p.suffix, filename)) {
237 // Generate the source based on
238 // - name name for the synthesized task
239 // - pattern pattern for the objective
240 // - source pattern for the source
242 // Return the source with properties
243 // - dep the prerequisite of source
244 // (with the namespace)
246 // - file the file name of source
247 // (without the namespace)
249 // For example, given
251 // - name foo:bin/main.o
255 // return 'foo:src/main.c',
257 static getSource(name, pattern, source) {
264 // Regex pattern -- use to look up the extension
265 if (pattern instanceof RegExp) {
266 match = pattern.exec(name);
268 if (typeof source == 'function') {
272 src = stringReplaceSuffix(name, match[0], source);
278 // Simple string suffix replacement
279 if (pattern.indexOf('%') == -1) {
280 if (typeof source == 'function') {
284 src = stringReplaceSuffix(name, pattern, source);
287 // Percent-based substitution
289 pat = pattern.replace('%', '(.*?)');
290 pat = new RegExp(pat);
291 match = pat.exec(name);
293 if (typeof source == 'function') {
298 file = source.replace('%', file);
300 src = name.replace(dep, file);