Permalink
Browse files

Dependencies should run before, not after; tasks should only run once.

  • Loading branch information...
1 parent c13cee1 commit e2a7e16617a197731f7e7ae0cc6bda23e64d152f @mde mde committed Nov 27, 2010
Showing with 70 additions and 27 deletions.
  1. +46 −27 lib/jake.js
  2. +24 −0 tests/Jakefile
View
@@ -145,7 +145,11 @@ jake = new function () {
// Local reference for scopage
var _this = this
// The list of tasks/dependencies to run, parsed recursively
+ // and run bottom-up, so dependencies run first
, _taskList = []
+ // A dictionary of loaded tasks, to ensure that all tasks
+ // run once and only once
+ , _taskDict = {}
// The args passed to the 'jake' invocation, after the task name
, _args;
@@ -157,28 +161,32 @@ jake = new function () {
* or may not be an Array
*/
var _isArray = function (obj) {
- return obj &&
- typeof obj === 'object' &&
- typeof obj.length === 'number' &&
- typeof obj.splice === 'function' &&
- !(obj.propertyIsEnumerable('length'));
- }
+ return obj &&
+ typeof obj === 'object' &&
+ typeof obj.length === 'number' &&
+ typeof obj.splice === 'function' &&
+ !(obj.propertyIsEnumerable('length'));
+ }
/**
* Parses all dependencies of a task (and their dependencies, etc.)
- * recursively, and adds them to the _taskList queue.
+ * recursively, and prepends them to the _taskList queue, so dependencies
+ * run first.
* @param {String} name The name of the current task whose
* dependencies are being parsed.
*/
, _parseDeps = function (name) {
- _taskList.push(name);
- var task = _this.getTask(name)
- , deps = task.deps;
- if (deps && _isArray(deps) && deps.length) {
- for (var i = 0, ii = deps.length; i < ii; i++) {
- _parseDeps(deps[i]);
+ _taskList.unshift(name);
+ var task = _this.getTask(name)
+ , deps = task.deps;
+ if (deps && _isArray(deps) && deps.length) {
+ // Reverse the array so when items are prepended, the end up
+ // in the same order as defined at a particular level
+ deps.reverse();
+ for (var i = 0, ii = deps.length; i < ii; i++) {
+ _parseDeps(deps[i]);
+ }
}
- }
- };
+ };
// Public properties
// =================
@@ -257,21 +265,32 @@ jake = new function () {
, passArgs;
// If there are still tasks to run, do it
if (name) {
- task = this.getTask(name);
-
- // TODO Do this once instead of on each iteration
- parsed = this.parseArgs(_args);
- passArgs = parsed.cmds;
- if (parsed.opts) {
- passArgs = parsed.cmds.concat(parsed.opts);
+ // Run tasks only once, even if it ends up in the task queue
+ // multiple times
+ if (_taskDict[name]) {
+ complete();
}
+ // Okie, we haven't done this one
+ else {
+ // Flag this one as done, no repeatsies
+ _taskDict[name] = true;
- // Run this mofo
- task.handler.apply(task, passArgs);
+ task = this.getTask(name);
- // Async tasks call this themselves
- if (!task.async) {
- complete();
+ // TODO Do this once instead of on each iteration
+ parsed = this.parseArgs(_args);
+ passArgs = parsed.cmds;
+ if (parsed.opts) {
+ passArgs = parsed.cmds.concat(parsed.opts);
+ }
+
+ // Run this mofo
+ task.handler.apply(task, passArgs);
+
+ // Async tasks call this themselves
+ if (!task.async) {
+ complete();
+ }
}
}
};
View
@@ -6,6 +6,30 @@ task('default', [], function () {
console.log(sys.inspect(arguments));
});
+desc('This is the asdf task. It depends on both qwer and the default');
+task('asdf', ['qwer', 'default'], function () {
+ console.log('Running the asdf task.');
+ console.log(sys.inspect(arguments));
+});
+
+desc('This is the qwer task. It depends on zxcv, uiop, and the default');
+task('qwer', ['zxcv', 'uiop', 'default'], function () {
+ console.log('Running the qwer task.');
+ console.log(sys.inspect(arguments));
+});
+
+desc('This is the zxcv task. It depends on the default');
+task('zxcv', ['default'], function () {
+ console.log('Running the zxcv task.');
+ console.log(sys.inspect(arguments));
+});
+
+desc('This is the uiop task. It depends on the default');
+task('uiop', ['default'], function () {
+ console.log('Running the uiop task.');
+ console.log(sys.inspect(arguments));
+});
+
namespace('foo', function () {
desc('This the foo:bar task');
task('bar', ['default', 'foo:qux', 'foo:baz'], function () {

0 comments on commit e2a7e16

Please sign in to comment.