2015-03-17 21:13:37 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var Steppy = require('twostep').Steppy,
|
|
|
|
inherits = require('util').inherits,
|
|
|
|
ParentExecutor = require('./base').Executor,
|
|
|
|
createScm = require('../scm').createScm,
|
|
|
|
createCommand = require('../command').createCommand,
|
2015-05-03 21:53:11 +00:00
|
|
|
fs = require('fs'),
|
|
|
|
_ = require('underscore');
|
2015-03-17 21:13:37 +00:00
|
|
|
|
|
|
|
function Executor(params) {
|
|
|
|
ParentExecutor.call(this, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
inherits(Executor, ParentExecutor);
|
|
|
|
|
|
|
|
exports.Executor = Executor;
|
|
|
|
|
2015-06-28 07:47:06 +00:00
|
|
|
Executor.prototype._createScm = function(params) {
|
|
|
|
return createScm(params);
|
|
|
|
};
|
|
|
|
|
2015-03-17 21:13:37 +00:00
|
|
|
Executor.prototype._getSources = function(params, callback) {
|
|
|
|
var self = this,
|
2015-05-09 16:59:27 +00:00
|
|
|
scm, isFirstRun, oldRev;
|
2015-03-17 21:13:37 +00:00
|
|
|
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;
|
|
|
|
}
|
2015-06-28 07:47:06 +00:00
|
|
|
scm = self._createScm(scmParams);
|
2015-05-09 16:59:27 +00:00
|
|
|
|
|
|
|
if (isFirstRun) {
|
|
|
|
this.pass(null);
|
|
|
|
} else {
|
|
|
|
scm.getCurrent(this.slot());
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function(err, id) {
|
|
|
|
oldRev = id;
|
|
|
|
|
2015-03-17 21:13:37 +00:00
|
|
|
if (isFirstRun) {
|
|
|
|
scm.clone(self.cwd, params.rev, this.slot());
|
|
|
|
} else {
|
|
|
|
scm.pull(params.rev, this.slot())
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function() {
|
2015-06-23 19:18:13 +00:00
|
|
|
scm.getChanges(oldRev && oldRev.id, params.rev, this.slot());
|
2015-03-17 21:13:37 +00:00
|
|
|
},
|
2015-06-23 19:18:13 +00:00
|
|
|
function(err, changes) {
|
|
|
|
var target = self._getTarget(params.rev, changes);
|
|
|
|
this.pass(target.changes);
|
|
|
|
scm.update(target.rev, this.slot());
|
|
|
|
},
|
|
|
|
function(err, changes) {
|
2015-05-09 16:59:27 +00:00
|
|
|
scm.getCurrent(this.slot());
|
2015-06-23 19:18:13 +00:00
|
|
|
this.pass(changes);
|
|
|
|
scm.getRev(params.rev, this.slot());
|
2015-05-09 16:59:27 +00:00
|
|
|
},
|
2015-06-24 21:54:33 +00:00
|
|
|
function(err, currentRev, changes, latestRev) {
|
2015-06-23 19:18:13 +00:00
|
|
|
this.pass({
|
2015-06-24 21:54:33 +00:00
|
|
|
rev: currentRev,
|
2015-06-23 19:18:13 +00:00
|
|
|
changes: changes,
|
2015-06-24 21:54:33 +00:00
|
|
|
isLatest: currentRev.id === latestRev.id
|
2015-06-23 19:18:13 +00:00
|
|
|
});
|
2015-05-09 16:59:27 +00:00
|
|
|
},
|
2015-03-17 21:13:37 +00:00
|
|
|
callback
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
Executor.prototype._runStep = function(params, callback) {
|
2015-05-01 11:11:29 +00:00
|
|
|
var self = this;
|
2015-03-17 21:13:37 +00:00
|
|
|
Steppy(
|
|
|
|
function() {
|
|
|
|
if (params.type !== 'shell') {
|
|
|
|
throw new Error('Unknown step type: ' + params.type);
|
|
|
|
}
|
2015-05-01 11:11:29 +00:00
|
|
|
// set command cwd to executor cwd
|
|
|
|
params.cwd = self.cwd;
|
2015-05-03 21:53:11 +00:00
|
|
|
var command = createCommand(
|
|
|
|
_({
|
|
|
|
emitIn: true,
|
2015-06-28 14:47:34 +00:00
|
|
|
emitOut: true,
|
|
|
|
emitErr: true,
|
|
|
|
attachStderr: true
|
2015-05-03 21:53:11 +00:00
|
|
|
}).extend(params)
|
|
|
|
);
|
|
|
|
|
|
|
|
command.on('stdin', function(data) {
|
|
|
|
self.emit('data', '> ' + String(data));
|
|
|
|
});
|
|
|
|
|
|
|
|
command.on('stdout', function(data) {
|
|
|
|
self.emit('data', String(data));
|
|
|
|
});
|
|
|
|
|
2015-06-28 14:47:34 +00:00
|
|
|
command.on('stderr', function(data) {
|
|
|
|
self.emit('data', 'stderr: ' + String(data));
|
|
|
|
});
|
|
|
|
|
2015-03-17 21:13:37 +00:00
|
|
|
command.run(params, this.slot())
|
|
|
|
},
|
|
|
|
callback
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|