diff --git a/.gitignore b/.gitignore index c2658d7..c9e0386 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -node_modules/ +node_modules +test/workspace diff --git a/lib/executor/base.js b/lib/executor/base.js new file mode 100644 index 0000000..c76c6c3 --- /dev/null +++ b/lib/executor/base.js @@ -0,0 +1,35 @@ +'use strict'; + +var Steppy = require('twostep').Steppy; + +function Executor(params) { + this.cwd = params.cwd; +} + +exports.Executor = Executor; + +Executor.prototype._getSources = function(params, callback) { +}; + +Executor.prototype._runStep = function(params, callback) { + +}; + +Executor.prototype.run = function(params, callback) { + var self = this; + Steppy( + function() { + self._getSources(params.scm, this.slot()); + }, + function() { + var funcs = params.steps.map(function(step) { + return function() { + self._runStep(step, this.slot()); + }; + }); + funcs.push(this.slot()); + Steppy.apply(this, funcs); + }, + callback + ); +}; \ No newline at end of file diff --git a/lib/executor/index.js b/lib/executor/index.js new file mode 100644 index 0000000..c17c42d --- /dev/null +++ b/lib/executor/index.js @@ -0,0 +1,6 @@ +'use strict'; + +exports.createExecutor = function(params) { + var Constructor = require('./' + params.type).Executor; + return new Constructor(params); +}; diff --git a/lib/executor/local.js b/lib/executor/local.js new file mode 100644 index 0000000..f77a942 --- /dev/null +++ b/lib/executor/local.js @@ -0,0 +1,67 @@ +'use strict'; + +var Steppy = require('twostep').Steppy, + inherits = require('util').inherits, + ParentExecutor = require('./base').Executor, + createScm = require('../scm').createScm, + createCommand = require('../command').createCommand, + fs = require('fs'); + +function Executor(params) { + ParentExecutor.call(this, params); +} + +inherits(Executor, ParentExecutor); + +exports.Executor = Executor; + +Executor.prototype._getSources = function(params, callback) { + var self = this, + scm, isFirstRun; + 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 = createScm(scmParams); + if (isFirstRun) { + scm.clone(self.cwd, params.rev, this.slot()); + } else { + scm.pull(params.rev, this.slot()) + } + }, + function() { + if (!isFirstRun) { + scm.update(params.rev, this.slot()); + } else { + this.pass(null); + } + }, + callback + ); +}; + +Executor.prototype._runStep = function(params, callback) { + Steppy( + function() { + if (params.type !== 'shell') { + throw new Error('Unknown step type: ' + params.type); + } + var command = createCommand(params); + command.run(params, this.slot()) + }, + callback + ); +}; + diff --git a/test/executor.js b/test/executor.js new file mode 100644 index 0000000..838663f --- /dev/null +++ b/test/executor.js @@ -0,0 +1,51 @@ +'use strict'; + +var expect = require('expect.js'), + path = require('path'), + fs = require('fs'), + createExecutor = require('../lib/executor').createExecutor, + SpawnCommand = require('../lib/command/spawn').Command; + + +['local'].forEach(function(type) { + describe(type + ' executor', function() { + var workspacePath = path.join(__dirname, 'workspace'); + + function rmdir(dir, callback) { + new SpawnCommand().run({cmd: 'rm', args: ['-R', dir]}, callback); + } + + it('remove test workspace dir if it exists', function(done) { + if (fs.exists(workspacePath, function(isExists) { + if (isExists) { + rmdir(workspacePath, done); + } else { + done(); + } + })); + }); + + var executor; + + it('instance should be created without errors', function() { + executor = createExecutor({ + type: type, + cwd: workspacePath + }); + }); + + it('should run', function(done) { + executor.run({ + scm: { + type: 'mercurial', + repository: path.join(__dirname, 'repos', 'mercurial'), + rev: 'default' + }, + steps: [ + {type: 'shell', cmd: 'echo 1'}, + {type: 'shell', cmd: 'echo 2'} + ] + }, done); + }); + }); +}); diff --git a/test/index.js b/test/index.js index b0a54db..5071165 100644 --- a/test/index.js +++ b/test/index.js @@ -2,3 +2,4 @@ require('./commands'); require('./scm') +require('./executor')