Permalink
Browse files

Initial cut at evented task-completion.

  • Loading branch information...
mde committed Dec 29, 2011
1 parent 23221e2 commit d0c214fa9e07b8e56c62956787a450aee26cfd0c
Showing with 162 additions and 66 deletions.
  1. +6 −4 bin/cli.js
  2. +4 −1 lib/api.js
  3. +7 −60 lib/jake.js
  4. +95 −0 lib/task.js
  5. +49 −0 lib/task_visitor.js
  6. +1 −1 package.json
View
@@ -68,16 +68,18 @@ if (!program.preemptiveOption()) {
process.chdir(dirname);
}
+ taskNames = program.taskNames;
+ taskNames = taskNames.length ? taskNames : ['default'];
+ task('__root__', taskNames, function () {});
+
jake.parseAllTasks();
if (opts.tasks) {
jake.showAllTaskDescriptions(opts.tasks);
}
else {
- taskNames = program.taskNames;
- taskNames = taskNames.length ? taskNames : ['default'];
- task('__root__', taskNames, function () {});
- jake.run('__root__', true);
+ //jake.run('__root__', true);
+ jake.Task['__root__'].invoke();
}
}
View
@@ -55,7 +55,10 @@ var api = new (function () {
};
this.complete = function () {
- jake.runNextTask();
+ var current = jake._invocationChain.pop();
+ if (current) {
+ current.emit('complete');
+ }
};
this.fail = function (err, code) {
View
@@ -18,66 +18,11 @@
var jake
, fs = require('fs')
- , path = require('path');
-
-/**
- * @constructor
- * A Jake task
- */
-var Task = function () {
- this.constructor.prototype.initialize.apply(this, arguments);
-};
-
-Task.prototype = new (function () {
- this.initialize = function (name, prereqs, action, options, type) {
- var opts = options || {};
- this.name = name;
- this.prereqs = prereqs;
- this.action = action;
- //this.async = (async === true);
- this.async = false;
- this.type = type;
- this.done = false;
- this.fullName = null;
- this.desription = null;
- // 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;
- }
- }
- };
-
- this.invoke = function () {
- jake.run(this.fullName, true, arguments);
- };
-
- this.execute = function () {
- jake.reenableTask(this.fullName, false);
- jake.run(this.fullName, true, arguments);
- };
-
- this.reenable = function (deep) {
- jake.reenableTask(this.fullName, deep);
- };
-})();
-Task.prototype.constructor = Task;
-
-var FileTask = function (name, prereqs, action, opts, type) {
- this.constructor.prototype.initialize.apply(this, arguments);
-};
-FileTask.prototype = new Task();
-FileTask.prototype.constructor = FileTask;
-
-var DirectoryTask = function (name, prereqs, action, opts, type) {
- this.constructor.prototype.initialize.apply(this, arguments);
-};
-DirectoryTask.prototype = new Task();
-DirectoryTask.prototype.constructor = DirectoryTask;
+ , path = require('path')
+ , task = require('./task')
+ , Task = task.Task
+ , FileTask = task.FileTask
+ , DirectoryTask = task.DirectoryTask;
var Namespace = function (name, parentNamespace) {
this.name = name;
@@ -97,6 +42,8 @@ var Invocation = function (taskName, args) {
*/
jake = new function () {
+ this._invocationChain = [];
+
// Private variables
// =================
// Local reference for scopage
View
@@ -0,0 +1,95 @@
+var EventEmitter = require('events').EventEmitter
+ , TaskMixin
+ , TaskVisitor = require('./task_visitor').TaskVisitor;
+
+/**
+ * @constructor
+ * A Jake task
+ */
+var Task = function () {
+ this.constructor.prototype.initialize.apply(this, arguments);
+};
+
+Task.prototype = new EventEmitter();
+Task.prototype.constructor = Task;
+TaskMixin = new (function () {
+ this.initialize = function (name, prereqs, action, options, type) {
+ var opts = options || {};
+
+ this._currentPrereqIndex = 0;
+
+ this.name = name;
+ this.prereqs = prereqs;
+ this.action = action;
+ this.async = false;
+ this.type = type;
+ this.done = false;
+ this.fullName = null;
+ this.desription = null;
+ this.args = [];
+
+ // 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;
+ }
+ }
+ };
+
+ // Run prereqs, run task
+ this.invoke = function () {
+ this.args = Array.prototype.slice.call(arguments);
+ jake._invocationChain.push(this);
+ TaskVisitor.visit(this);
+ };
+
+ // Reenable, run task (no prereqs)
+ this.execute = function () {
+ };
+
+ this.reenable = function (deep) {
+ };
+
+ this.handlePrereqComplete = function (prereq) {
+ this._currentPrereqIndex++;
+ if (this._currentPrereqIndex < this.prereqs.length) {
+ TaskVisitor.visitNextPrereq(this);
+ }
+ else {
+ this.run();
+ }
+ };
+
+ this.run = function () {
+ if (!this.done && typeof this.action == 'function') {
+ this.action.apply(this, this.args);
+ }
+ if (!this.async) {
+ complete();
+ }
+ };
+
+})();
+for (var p in TaskMixin) {
+ Task.prototype[p] = TaskMixin[p];
+}
+
+var FileTask = function (name, prereqs, action, opts, type) {
+ this.constructor.prototype.initialize.apply(this, arguments);
+};
+FileTask.prototype = new Task();
+FileTask.prototype.constructor = FileTask;
+
+var DirectoryTask = function (name, prereqs, action, opts, type) {
+ this.constructor.prototype.initialize.apply(this, arguments);
+};
+DirectoryTask.prototype = new Task();
+DirectoryTask.prototype.constructor = DirectoryTask;
+
+exports.Task = Task;
+exports.FileTask = FileTask;
+exports.DirectoryTask = DirectoryTask;
View
@@ -0,0 +1,49 @@
+
+var TaskVisitor = 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
+ };
+ };
+
+ this.visit = function (task) {
+ if (task.prereqs && task.prereqs.length) {
+ this.visitNextPrereq.call(this, task);
+ }
+ else {
+ task.run();
+ }
+ };
+
+ this.visitNextPrereq = function (task) {
+ var self = this
+ , index = task._currentPrereqIndex
+ , name = task.prereqs[index]
+ , prereq
+ , parsed;
+ if (name) {
+ parsed = parsePrereqName(name);
+ prereq = jake.getTask(parsed.name);
+ if (prereq) {
+ // Do when done
+ prereq.addListener('complete', function () {
+ task.handlePrereqComplete(prereq);
+ });
+ prereq.invoke.apply(prereq, parsed.args);
+ }
+ }
+ };
+
+})();
+
+exports.TaskVisitor = TaskVisitor;
View
@@ -1,5 +1,5 @@
{ "name": "jake"
-, "version": "0.1.22"
+, "version": "0.2.0"
, "author": "Matthew Eernisse <mde@fleegix.org> (http://fleegix.org)"
, "bin": { "jake": "./bin/cli.js" }
, "main": "./lib"

0 comments on commit d0c214f

Please sign in to comment.