diff --git a/.gitignore b/.gitignore index a188c9d..8587078 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ node_modules test/workspace static/js/libs +projects/**/workspace diff --git a/app.js b/app.js index 1eaaa9f..20d2edc 100644 --- a/app.js +++ b/app.js @@ -5,7 +5,7 @@ var nodeStatic = require('node-static'); var jade = require('jade'); var staticServer = new nodeStatic.Server('./static'); -var app = http.createServer(function(req, res, next) { +var server = http.createServer(function(req, res, next) { // serve index for all app pages if (req.url.indexOf('/data.io.js') === -1) { if (req.url.indexOf('/js') === -1) { @@ -19,9 +19,14 @@ var app = http.createServer(function(req, res, next) { } }); -var io = require('socket.io')(app); -var data = require('data.io')(io); +var socketio = require('socket.io')(server); +var dataio = require('data.io')(socketio); -require('./resources')(data); +var app = { + server: server, + dataio: dataio +}; -app.listen(3000); +require('./resources')(app); + +app.server.listen(3000); diff --git a/lib/distributor.js b/lib/distributor.js index ebe1e93..93814bd 100644 --- a/lib/distributor.js +++ b/lib/distributor.js @@ -14,8 +14,8 @@ function Distributor(params) { // queued projects to build self.queue = []; - self.onBuildUpdate = params.onBuildUpdate || function(err, build) { - callback(err, build); + self.onBuildUpdate = params.onBuildUpdate || function(build, callback) { + callback(null, build); }; } diff --git a/lib/executor/local.js b/lib/executor/local.js index f77a942..fa8f625 100644 --- a/lib/executor/local.js +++ b/lib/executor/local.js @@ -53,11 +53,14 @@ Executor.prototype._getSources = function(params, callback) { }; Executor.prototype._runStep = function(params, callback) { + var self = this; 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(params); command.run(params, this.slot()) }, diff --git a/lib/project.js b/lib/project.js index 15e72ac..2688b5b 100644 --- a/lib/project.js +++ b/lib/project.js @@ -35,6 +35,7 @@ exports.load = function(baseDir, name, callback) { exports.validateConfig(config, this.slot()); }, function(err, config) { + config.dir = dir; this.pass(new Project(config)); }, callback diff --git a/resources/builds.js b/resources/builds.js index 9fa8e03..42999c7 100644 --- a/resources/builds.js +++ b/resources/builds.js @@ -1,6 +1,6 @@ 'use strict'; -module.exports = function(data) { +module.exports = function(app) { var builds = [{ project: { name: 'foo' @@ -11,7 +11,7 @@ module.exports = function(data) { status: 'inprogress' }]; - var resource = data.resource('builds'); + var resource = app.dataio.resource('builds'); resource.use('readAll', function(req, res) { console.log('readAll'); diff --git a/resources/projects.js b/resources/projects.js index 8f70424..425ccdd 100644 --- a/resources/projects.js +++ b/resources/projects.js @@ -1,24 +1,41 @@ 'use strict'; var _ = require('underscore'), - project = require('../lib/project'); + project = require('../lib/project'), + Distributor = require('../lib/distributor').Distributor; -var projects, - projectConfigs; +var distributor = new Distributor({ + nodes: [{type: 'local', maxExecutorsCount: 1}] +}); + +var projects, projectsHash; project.loadAll('projects', function(err, loadedProjects) { if (err) throw err; projects = loadedProjects; + projectsHash = _(projects).indexBy(function(project) { + return project.config.name; + }); console.log( 'Loaded projects: ', _(projects).chain().pluck('config').pluck('name').value() ); }); -module.exports = function(data) { - var resource = data.resource('projects'); +module.exports = function(app) { + var resource = app.dataio.resource('projects'); resource.use('read', function(req, res) { res.send(_(projects).pluck('config')); }); + + resource.use('run', function(req, res) { + var projectName = req.data.projectName, + project = projectsHash[projectName]; + console.log('Run the project: %j', project || projectName); + distributor.run(project.config, {}, function(err, build) { + console.log('>>> err, build = ', err && err.stack || err, build) + res.send({err: err, build: build}); + }); + }); }; diff --git a/static/js/app/app.js b/static/js/app/app.js index 63e74bb..796b7e7 100644 --- a/static/js/app/app.js +++ b/static/js/app/app.js @@ -8,10 +8,22 @@ define([ var connect = dataio(socketio.connect()); var projects = connect.resource('projects'); + + var projectsTemplate = _($('#projects-template').html()).template(); + $('#content').on('click', '.js-projects .js-run', function() { + var projectName = $(this).parent('.js-project').data('name'); + projects.sync('run', {projectName: projectName}, function(err, result) { + $('#content').append( + (err && err.message) || + JSON.stringify(result) + ); + }); + }); + projects.sync('read', function(err, projects) { $('#content').html( (err && err.message) || - ('Loaded projects: ' + _(projects).pluck('name').join(', ')) + projectsTemplate({projects: projects}) ); }); }); diff --git a/views/index.jade b/views/index.jade index 1cf6e7e..081c93f 100644 --- a/views/index.jade +++ b/views/index.jade @@ -9,4 +9,16 @@ html body h1 hello world + + script#projects-template(type="text/template") + |
+ | <% _(projects).each(function(project) { %> + |
+ | <%= project.name %> + | + |
+ | <% }); %> + |
+ + #content