diff --git a/lib/executor/base.js b/lib/executor/base.js index 7411a1f..4e4fcc4 100644 --- a/lib/executor/base.js +++ b/lib/executor/base.js @@ -4,6 +4,7 @@ var Steppy = require('twostep').Steppy, path = require('path'), _ = require('underscore'), EventEmitter = require('events').EventEmitter, + fs = require('fs'), inherits = require('util').inherits; function Executor(params) { @@ -20,15 +21,138 @@ Executor.prototype.throttledEmit = _(function() { }).throttle(500); Executor.prototype._getSources = function(params, callback) { + var self = this, + scm; + Steppy( + function() { + self._getChanges(params, this.slot()); + }, + function(err, data) { + scm = data.scm; + this.pass(data.changes); + scm.update(data.rev, this.slot()); + }, + function(err, changes) { + scm.getCurrent(this.slot()); + this.pass(changes); + scm.getRev(params.rev, this.slot()); + }, + function(err, currentRev, changes, latestRev) { + this.pass({ + rev: currentRev, + changes: changes, + isLatest: currentRev.id === latestRev.id + }); + }, + callback + ); }; -Executor.prototype._runStep = function(params, callback) { +Executor.prototype._runStep = function(step, callback) { + var self = this, + params = _(step).clone(); + Steppy( + function() { + if (params.type !== 'shell') { + throw new Error('Unknown step type: ' + params.type); + } + // set command cwd to executor cwd + params.cwd = self.cwd; + var command = self._createCommand( + _({ + emitIn: true, + emitOut: true, + emitErr: true, + attachStderr: true + }).extend(params) + ); + + command.on('stdin', function(data) { + self.emit('data', '> ' + String(data)); + }); + + command.on('stdout', function(data) { + self.emit('data', String(data)); + }); + + command.on('stderr', function(data) { + self.emit('data', 'stderr: ' + String(data)); + }); + + // TODO: should be fixed properly, currently it's quick fix for + // NODE_ENV which affects npm install/prune calls + params.options = params.options || {}; + params.options.env = params.options.env || process.env; + delete params.options.env.NODE_ENV; + + command.run(params, this.slot()) + }, + callback + ); +}; + +Executor.prototype._getChanges = function(params, callback) { + var self = this, + scm, isFirstRun, oldRev; + Steppy( + function() { + var slot = this.slot(); + fs.exists(self.cwd, function(exists) { + slot(null, exists); + }); + }, + function(err, exists) { + var scmParams = {type: params.type}; + if (exists) { + scmParams.cwd = self.cwd; + isFirstRun = false; + } else { + scmParams.repository = params.repository; + isFirstRun = true; + } + scm = self._createScm(scmParams); + + scm.on('stdin', function(data) { + self.emit('data', '> ' + String(data)); + }); + + if (isFirstRun) { + this.pass(null); + } else { + scm.getCurrent(this.slot()); + } + }, + function(err, id) { + oldRev = id; + + if (isFirstRun) { + scm.clone(self.cwd, params.rev, this.slot()); + } else { + scm.pull(params.rev, this.slot()) + } + }, + function() { + scm.getChanges(oldRev && oldRev.id, params.rev, this.slot()); + }, + function(err, changes) { + var target = self._getTarget(params.rev, changes); + this.pass({ + scm: scm, + oldRev: oldRev, + rev: target.rev, + changes: target.changes + }); + }, + callback + ); }; // Does current project scm has new changes to build Executor.prototype.hasScmChanges = function(callback) { - + this._getChanges(this.project.scm, function(err, data) { + callback(err, !err && data.changes.length > 0); + }); }; Executor.prototype.run = function(params, callback) { diff --git a/lib/executor/local.js b/lib/executor/local.js index 6043cd1..6c25f04 100644 --- a/lib/executor/local.js +++ b/lib/executor/local.js @@ -5,7 +5,6 @@ var Steppy = require('twostep').Steppy, ParentExecutor = require('./base').Executor, createScm = require('../scm').createScm, createCommand = require('../command').createCommand, - fs = require('fs'), _ = require('underscore'); function Executor(params) { @@ -20,137 +19,6 @@ Executor.prototype._createScm = function(params) { return createScm(params); }; -Executor.prototype._getChanges = function(params, callback) { - var self = this, - scm, isFirstRun, oldRev; - Steppy( - function() { - var slot = this.slot(); - fs.exists(self.cwd, function(exists) { - slot(null, exists); - }); - }, - function(err, exists) { - var scmParams = {type: params.type}; - if (exists) { - scmParams.cwd = self.cwd; - isFirstRun = false; - } else { - scmParams.repository = params.repository; - isFirstRun = true; - } - scm = self._createScm(scmParams); - - scm.on('stdin', function(data) { - self.emit('data', '> ' + String(data)); - }); - - if (isFirstRun) { - this.pass(null); - } else { - scm.getCurrent(this.slot()); - } - }, - function(err, id) { - oldRev = id; - - if (isFirstRun) { - scm.clone(self.cwd, params.rev, this.slot()); - } else { - scm.pull(params.rev, this.slot()) - } - }, - function() { - scm.getChanges(oldRev && oldRev.id, params.rev, this.slot()); - }, - function(err, changes) { - var target = self._getTarget(params.rev, changes); - this.pass({ - scm: scm, - oldRev: oldRev, - rev: target.rev, - changes: target.changes - }); - }, - callback - ); +Executor.prototype._createCommand = function(params) { + return createCommand(params); }; - -Executor.prototype.hasScmChanges = function(callback) { - this._getChanges(this.project.scm, function(err, data) { - callback(err, !err && data.changes.length > 0); - }); -}; - -Executor.prototype._getSources = function(params, callback) { - var self = this, - scm; - Steppy( - function() { - self._getChanges(params, this.slot()); - }, - function(err, data) { - scm = data.scm; - this.pass(data.changes); - scm.update(data.rev, this.slot()); - }, - function(err, changes) { - scm.getCurrent(this.slot()); - this.pass(changes); - scm.getRev(params.rev, this.slot()); - }, - function(err, currentRev, changes, latestRev) { - this.pass({ - rev: currentRev, - changes: changes, - isLatest: currentRev.id === latestRev.id - }); - }, - callback - ); -}; - -Executor.prototype._runStep = function(step, callback) { - var self = this, - params = _(step).clone(); - - Steppy( - function() { - if (params.type !== 'shell') { - throw new Error('Unknown step type: ' + params.type); - } - // set command cwd to executor cwd - params.cwd = self.cwd; - var command = createCommand( - _({ - emitIn: true, - emitOut: true, - emitErr: true, - attachStderr: true - }).extend(params) - ); - - command.on('stdin', function(data) { - self.emit('data', '> ' + String(data)); - }); - - command.on('stdout', function(data) { - self.emit('data', String(data)); - }); - - command.on('stderr', function(data) { - self.emit('data', 'stderr: ' + String(data)); - }); - - // TODO: should be fixed properly, currently it's quick fix for - // NODE_ENV which affects npm install/prune calls - params.options = params.options || {}; - params.options.env = params.options.env || process.env; - delete params.options.env.NODE_ENV; - - command.run(params, this.slot()) - }, - callback - ); -}; -