forked from jamesshore/lets_code_javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.js
More file actions
314 lines (271 loc) · 7.84 KB
/
task.js
File metadata and controls
314 lines (271 loc) · 7.84 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
312
313
var util = require('util') // Native Node util module
, path = require('path')
, EventEmitter = require('events').EventEmitter
, Task
, TaskBase
, utils = require('../utils')
, async = require('async')
, rule; // Lazy-require this at the bottom
var UNDEFINED_VALUE;
/**
@name jake
@namespace jake
*/
/**
@name jake.Task
@constructor
@augments EventEmitter
@description A Jake Task
@param {String} name The name of the Task
@param {Array} [prereqs] Prerequisites to be run before this task
@param {Function} [action] The action to perform for this task
@param {Object} [opts]
@param {Array} [opts.asyc=false] Perform this task asynchronously.
If you flag a task with this option, you must call the global
`complete` method inside the task's action, for execution to proceed
to the next task.
*/
Task = function () {
// Do constructor-work only on actual instances, not when used
// for inheritance
if (arguments.length) {
this.init.apply(this, arguments);
}
};
util.inherits(Task, EventEmitter);
TaskBase = new (function () {
// Parse any positional args attached to the task-name
var parsePrereqName = function (name) {
var taskArr = name.split('[')
, taskName = taskArr[0]
, taskArgs = [];
if (taskArr[1]) {
taskArgs = taskArr[1].replace(/\]$/, '');
taskArgs = taskArgs.split(',');
}
return {
name: taskName
, args: taskArgs
};
};
/**
@name jake.Task#event:complete
@event
*/
this.init = function (name, prereqs, action, options) {
var opts = options || {};
this._currentPrereqIndex = 0;
this.name = name;
this.prereqs = prereqs;
this.action = action;
this.async = false;
this.taskStatus = Task.runStatuses.UNSTARTED;
this.fullName = null;
this.description = null;
this.args = [];
this.value = UNDEFINED_VALUE;
this.namespace = null;
this.parallelLimit = 1;
// Support legacy async-flag -- if not explicitly passed or falsy, will
// be set to empty-object
if (typeof opts == 'boolean' && opts === true) {
this.async = true;
}
else {
if (opts.async) {
this.async = true;
}
if (opts.parallelLimit) {
this.parallelLimit = opts.parallelLimit;
}
}
};
/**
@name jake.Task#invoke
@function
@description Runs prerequisites, then this task. If the task has already
been run, will not run the task again.
*/
this.invoke = function () {
jake._invocationChain.push(this);
this.args = Array.prototype.slice.call(arguments);
this.runPrereqs();
};
/**
@name jake.Task#execute
@function
@description Runs prerequisites, then this task. If the task has already
been run, will not run the task again.
*/
this.execute = function () {
jake._invocationChain.push(this);
this.args = Array.prototype.slice.call(arguments);
this.reenable();
this.run();
};
this.runPrereqs = function () {
if (this.prereqs && this.prereqs.length) {
if(this.parallelLimit > 1) {
var currenttask = this;
async.eachLimit(currenttask.prereqs,currenttask.parallelLimit,function(name, cb) {
var parsed = parsePrereqName(name);
var prereq = currenttask.namespace.resolveTask(parsed.name) ||
jake.attemptRule(name, currenttask.namespace, 0) ||
jake.createPlaceholderFileTask(name, currenttask.namespace);
if (!prereq) {
throw new Error('Unknown task "' + name + '"');
}
if (prereq.taskStatus === Task.runStatuses.DONE) {
//prereq already done, return
cb();
} else {
//wait for complete before calling cb
prereq.once('complete', function () {
cb();
});
//start te prereq if we are the first to encounter it
if(prereq.taskStatus === Task.runStatuses.UNSTARTED) {
prereq.taskStatus = Task.runStatuses.RUNNING;
prereq.invoke.apply(prereq, parsed.args);
}
}
}, function(err) {
//async callback is called after all prereqs have run.
currenttask.run();
});
} else {
this.nextPrereq();
}
}
else {
this.run();
}
};
this.nextPrereq = function () {
var self = this
, index = this._currentPrereqIndex
, name = this.prereqs[index]
, prereq
, parsed
, filePath
, stats;
if (name) {
parsed = parsePrereqName(name);
prereq = this.namespace.resolveTask(parsed.name) ||
jake.attemptRule(name, this.namespace, 0) ||
jake.createPlaceholderFileTask(name, this.namespace);
if (!prereq) {
throw new Error('Unknown task "' + name + '"');
}
// Do when done
if (prereq.taskStatus === Task.runStatuses.DONE) {
self.handlePrereqComplete(prereq);
} else {
prereq.once('complete', function () {
self.handlePrereqComplete(prereq);
});
if(prereq.taskStatus === Task.runStatuses.UNSTARTED) {
prereq.taskStatus = Task.runStatuses.RUNNING;
prereq.invoke.apply(prereq, parsed.args);
}
}
}
};
/**
@name jake.Task#reenable
@function
@description Reenables a task so that it can be run again.
*/
this.reenable = function (deep) {
var prereqs
, prereq;
this.taskStatus = Task.runStatuses.UNSTARTED;
this.value = UNDEFINED_VALUE;
if (deep && this.prereqs) {
prereqs = this.prereqs;
for (var i = 0, ii = prereqs.length; i < ii; i++) {
prereq = jake.Task[prereqs[i]];
if (prereq) {
prereq.reenable(deep);
}
}
}
};
this.handlePrereqComplete = function (prereq) {
var self = this;
this._currentPrereqIndex++;
if (this._currentPrereqIndex < this.prereqs.length) {
setTimeout(function () {
self.nextPrereq();
}, 0);
}
else {
this.run();
}
};
this.isNeeded = function () {
if (this.taskStatus === Task.runStatuses.DONE || typeof this.action != 'function') {
return false;
}
return true;
};
this.run = function () {
var runAction = this.isNeeded()
, val;
if (runAction) {
this.emit('start');
try {
val = this.action.apply(this, this.args);
if (typeof val == 'object' && typeof val.then == 'function') {
this.async = true;
val.then(
function(result) {
setTimeout(function() {
complete(result);
},
0);
},
function(err) {
setTimeout(function() {
fail(err);
},
0);
});
}
}
catch (e) {
this.emit('error', e);
return; // Bail out, not complete
}
}
else {
this.emit('skip');
}
if (!(runAction && this.async)) {
this.complete(val);
}
};
this.complete = function (val) {
jake._invocationChain.splice(jake._invocationChain.indexOf(this),1);
this._currentPrereqIndex = 0;
this.taskStatus = Task.runStatuses.DONE;
// If 'complete' getting called because task has been
// run already, value will not be passed -- leave in place
if (typeof val != 'undefined') {
this.value = val;
}
this.emit('complete', this.value);
};
})();
utils.mixin(Task.prototype, TaskBase);
Task.getBaseNamespacePath = function (fullName) {
return fullName.split(':').slice(0, -1).join(':');
};
Task.getBaseTaskName = function (fullName) {
return fullName.split(':').pop();
};
//The task is in one of three states
Task.runStatuses = {UNSTARTED: 'unstarted', DONE: 'done', STARTED: 'started'};
exports.Task = Task;
// Lazy-require
rule = require('../rule');