Skip to content

Commit

Permalink
Integrate task builder in automaton with tests, #49
Browse files Browse the repository at this point in the history
  • Loading branch information
satazor committed Feb 17, 2013
1 parent 88db179 commit da8f3c4
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 123 deletions.
53 changes: 42 additions & 11 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var d = require('dejavu'),
inter = require('./lib/string/cast-interpolate'),
validate = require('./lib/validate_task'),
Logger = require('./lib/Logger'),
TaskBuilder = require('./lib/TaskBuilder'),
GruntRunner = require('./lib/grunt/Runner')
;

Expand Down Expand Up @@ -48,11 +49,12 @@ var Automaton = d.Class.declare({
* If the task already exists, it will be replaced.
* The task will be validaded only when its run.
*
* @param {Object} task The task definition
* @param {Object|Function} task The task definition
*
* @return {Automaton} Chainable!
*/
addTask: function (task) {
task = this._getTaskObject(task);
validate(task);

assert(task.id, 'Can only add tasks with an id');
Expand Down Expand Up @@ -200,6 +202,29 @@ var Automaton = d.Class.declare({
return stream;
},

/**
* Get the task object in case the task is a function that will build the object.
*
* @param {Object|Function} task The task object or the builder
*
* @return {Object} The task
*/
_getTaskObject: function (task) {
var builder;

if (utils.lang.isFunction(task)) {
builder = new TaskBuilder();
try {
task(builder);
} catch (e) {
throw new Error('Unable to get task from builder: ' + e.message);
}
task = builder.toObject();
}

return task;
},

/**
* Create a batch for a task.
* The batch is a sequence of functions that form the task.
Expand All @@ -220,9 +245,15 @@ var Automaton = d.Class.declare({
// grab its real definition
this._assertTaskLoaded(def.task);
def.task = this.getTask(def.task);
// otherwise, trigger validation if is the root task
} else if (def.depth === 1) {
validate(def.task);
} else {
// if task is a function then needs a builder
if (utils.lang.isFunction(def.task)) {
def.task = this._getTaskObject(def.task);
}
// trigger validation if is the root task
if (def.depth === 1) {
validate(def.task);
}
}

// fill in the options with default values where the option was not provided
Expand Down Expand Up @@ -271,12 +302,12 @@ var Automaton = d.Class.declare({
def.task.tasks.forEach(function (subtask) {
var subtaskDef = this._createTaskDefinition(subtask, def);

// if it's a function
if (utils.lang.isFunction(subtaskDef.task)) {
batch.push(this._batchFunctionTask(subtaskDef));
// if it's a grunt task
} else if (subtaskDef.grunt) {
if (subtaskDef.grunt) {
batch.push(this._batchGruntTask(subtaskDef));
// if it's a inline function
} else if (utils.lang.isFunction(subtaskDef.task) && subtaskDef.task.length !== 1) {
batch.push(this._batchFunctionTask(subtaskDef));
// then it must be another task
} else {
batch.push(this._batchTask(subtaskDef));
Expand All @@ -298,7 +329,7 @@ var Automaton = d.Class.declare({
}
}.$bind(this);

// return a final function with everything set it
// return a final function which calls everything in order
return function (next) {
// run pre-task
preTaskFunc(function (err, disabled) {
Expand All @@ -307,14 +338,14 @@ var Automaton = d.Class.declare({
return next();
}

// if there was an error, run pos-task afterwards
// if there was an error in the pre-task, run pos-task immediately
if (err) {
return posTaskFunc(err, next);
}

// run each subtask
async.series(batch, function (err) {
// finally run the pos-task
// finally run the pos-task, even if there was an error
posTaskFunc(err, next);
});
});
Expand Down
25 changes: 25 additions & 0 deletions test/helpers/tasks/callback-builder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict';

module.exports = function (task) {
task
.id('callback-builder')
.description('Callback task')

.option('setupCallback', null, function () {})
.option('teardownCallback', null, function () {})
.option('callback', null, function () {})
.option('someOption', null, 'default')

.setup(function (opt, ctx, next) {
opt.setupCallback.call(this, opt, ctx);
next();
})
.teardown(function (opt, ctx, next) {
opt.teardownCallback.call(this, opt, ctx);
next();
})
.do(function (opt, ctx, next) {
opt.callback.call(this, opt, ctx);
next();
});
};
Loading

0 comments on commit da8f3c4

Please sign in to comment.