Permalink
Browse files

Begin work on file- and directory-tasks.

  • Loading branch information...
1 parent d0c214f commit e2fd14dc00ddc8b88424ccc23694b51111678d5f @mde mde committed Dec 30, 2011
Showing with 81 additions and 26 deletions.
  1. +1 −1 lib/api.js
  2. +36 −6 lib/task.js
  3. +44 −19 lib/task_visitor.js
View
2 lib/api.js
@@ -57,7 +57,7 @@ var api = new (function () {
this.complete = function () {
var current = jake._invocationChain.pop();
if (current) {
- current.emit('complete');
+ current.complete();
}
};
View
42 lib/task.js
@@ -1,4 +1,5 @@
-var EventEmitter = require('events').EventEmitter
+var fs = require('fs')
+ , EventEmitter = require('events').EventEmitter
, TaskMixin
, TaskVisitor = require('./task_visitor').TaskVisitor;
@@ -13,7 +14,7 @@ var Task = function () {
Task.prototype = new EventEmitter();
Task.prototype.constructor = Task;
TaskMixin = new (function () {
- this.initialize = function (name, prereqs, action, options, type) {
+ this.initialize = function (name, prereqs, action, options) {
var opts = options || {};
this._currentPrereqIndex = 0;
@@ -22,7 +23,6 @@ TaskMixin = new (function () {
this.prereqs = prereqs;
this.action = action;
this.async = false;
- this.type = type;
this.done = false;
this.fullName = null;
this.desription = null;
@@ -65,30 +65,60 @@ TaskMixin = new (function () {
};
this.run = function () {
+ var runAction = false;
if (!this.done && typeof this.action == 'function') {
+ runAction = true;
this.action.apply(this, this.args);
}
- if (!this.async) {
+ if (!(runAction && this.async)) {
complete();
}
};
+ this.complete = function () {
+ this.done = true;
+ this.emit('complete');
+ };
+
})();
for (var p in TaskMixin) {
Task.prototype[p] = TaskMixin[p];
}
-var FileTask = function (name, prereqs, action, opts, type) {
+
+FileDirectoryMixin = new (function () {
+ this.updateModTime = function () {
+ var stats = fs.statSync(this.name);
+ this.modTime = stats.mtime;
+ };
+
+ this.complete = function () {
+ this.updateModTime();
+ this.done = true;
+ this.emit('complete');
+ };
+
+})();
+
+var FileTask = function (name, prereqs, action, opts) {
+ this.modTime = null;
this.constructor.prototype.initialize.apply(this, arguments);
};
FileTask.prototype = new Task();
FileTask.prototype.constructor = FileTask;
+for (var p in FileDirectoryMixin) {
+ FileTask.prototype[p] = FileDirectoryMixin[p];
+}
-var DirectoryTask = function (name, prereqs, action, opts, type) {
+var DirectoryTask = function (name, prereqs, action, opts) {
+ this.modTime = null;
this.constructor.prototype.initialize.apply(this, arguments);
};
DirectoryTask.prototype = new Task();
DirectoryTask.prototype.constructor = DirectoryTask;
+for (var p in FileDirectoryMixin) {
+ DirectoryTask.prototype[p] = FileDirectoryMixin[p];
+}
exports.Task = Task;
exports.FileTask = FileTask;
View
63 lib/task_visitor.js
@@ -3,18 +3,28 @@ 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
- };
- };
+ var taskArr = name.split('[')
+ , taskName = taskArr[0]
+ , taskArgs = [];
+ if (taskArr[1]) {
+ taskArgs = taskArr[1].replace(/\]$/, '');
+ taskArgs = taskArgs.split(',');
+ }
+ return {
+ name: taskName
+ , args: taskArgs
+ };
+ }
+ , isFileOrDirectory = function (t) {
+ return (t instanceof FileTask ||
+ t instanceof DirectoryTask);
+ }
+ , isFile = function (t) {
+ return t instanceof FileTask;
+ }
+ , prereqIsNewerThan = function (prereq, time) {
+ return _modTimes[prereq.name] > time;
+ };
this.visit = function (task) {
if (task.prereqs && task.prereqs.length) {
@@ -30,17 +40,32 @@ var TaskVisitor = new (function () {
, index = task._currentPrereqIndex
, name = task.prereqs[index]
, prereq
- , parsed;
+ , parsed
+ , filePath
+ , stats;
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);
+
+ // Task doesn't exist, assume static file -- create a
+ // dummy FileTask if file actually exists
+ if (!prereq) {
+ filePath = name.split(':')[1] || name;
+ try {
+ stats = fs.statSync(filePath);
+ prereq = new jake.FileTask(name);
+ prereq.modTime = stats.mtime;
+ }
+ catch(e) {
+ throw new Error('Prerequisite file ' + name + ' does not exist');
+ }
}
+
+ // Do when done
+ prereq.addListener('complete', function () {
+ task.handlePrereqComplete(prereq);
+ });
+ prereq.invoke.apply(prereq, parsed.args);
}
};

0 comments on commit e2fd14d

Please sign in to comment.