Skip to content
This repository has been archived by the owner on Dec 23, 2018. It is now read-only.

Commit

Permalink
reverted - breaks some commands such as sublimes subl
Browse files Browse the repository at this point in the history
  • Loading branch information
Craig Condon committed Mar 5, 2012
1 parent 1cf6a9a commit 3d0a96a
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 67 deletions.
5 changes: 2 additions & 3 deletions .cupboard
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
[commands]
proj = subl --project project.sublime-project

proj = subl -b --project project.sublime-project

[ignore]
project.sublime-\w+
project.sublime-\w+
Binary file modified lib/beans/.DS_Store
Binary file not shown.
6 changes: 6 additions & 0 deletions lib/beans/cbd.projects/execute
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/bash -e

eval "$@ &"

# this gets parsed out
echo "CHILD-PID:$!"
107 changes: 60 additions & 47 deletions lib/beans/cbd.projects/processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ exec = require('child_process').exec;
*/

var Script = Structr({

/**
*/

'__construct': function(processor, script, ops) {

this._processor = processor;
this._script = script;
this._em = new EventEmitter();
Expand All @@ -22,89 +22,102 @@ var Script = Structr({

this.coloredName = (this._project.name()+':')[Processor.nextTermColor()].bold;
},

/**
*/

'_log': function(data) {
data = data.toString();

var self = this;


var pid = data.match(/CHILD-PID\:(\d+)/),
self = this;

if(pid) {
self._cpid = Number(pid[1]);
return;
}


data.replace(/[\s\r\n]+$/,'').split(/[\r\n]+/g).forEach(function(msg) {

console.log('%s %s', self.coloredName, msg);
});
},

/**
*/

'execute': function(ops, callback) {

this._em.addListener('complete', callback);

//already running, skip
if(!!this._proc) return;


var self = this,
script = this._script.replace(/,/g,';').replace(/\$@/g, ops.args.join(' ')),
project = this._project;


//replace "script with passed args"
for(var param in ops.data) {
var search = new RegExp('\\\$\{'+param+'\}');
script = script.replace(search, ops.data[param]);
}


var proc = this._proc = exec(script, { cwd: project.path() });

var proc = this._proc = spawn(__dirname + '/execute', [script], { cwd: project.path() });


function log(data) {
self._log(data);
}

log(script.bold);




//send user notification if installed ~ growl
// this._router.push('user/notification', { message: ops.command + " " + this._project.name() });


proc.stdout.on('data', log);
proc.stderr.on('data', log);
proc.on('exit', function(code, signal) {
self._proc = null;

// log('done');

var killed = code !== 0;

self._em.emit('complete', killed ? new Error('killed') : null, !killed ? 1 : null);
self._em.removeAllListeners('complete');
});


return this;
},

/**
*/

'kill': function(onKilled) {

var self = this;

if(!this._proc) return onKilled(this);

//self._log('killing: '+ this._script.bold);

this._em.addListener('complete', function() {
onKilled(self);
});

this._proc.kill();


process.kill(this._cpid, 'SIGKILL');
this._proc = null;
}

});


Expand All @@ -113,20 +126,20 @@ var Processor = module.exports = Structr({

/**
*/

'__construct': function(project) {
this.project = project;
this._scripts = {};
this.termColor = Processor.nextTermColor();
},

/**
*/

'execute': function(script, ops, callback) {
var scr = this._scripts[script],
self = this;

function onReady(scr) {
scr.execute(ops, callback);
}
Expand All @@ -138,30 +151,30 @@ var Processor = module.exports = Structr({
onReady(this._scripts[script] = new Script(self, script, ops));
}
},

/**
*/

'static nextTermColor': function() {

if(!this._colors) {

this._colors = ['green','blue','yellow','magenta','cyan','grey'];
this._curColor = 0;
}

return this._colors[this._curColor++ % this._colors.length ];
},

/**
* singleton is used because the project model is instantiated each time find is called.
*/

'static getInstance': function(project) {
if(!this._processors) {
this._processors = {};
}

return this._processors[project.name()] || (this._processors[project.name()] = new module.exports(project));
}
});
Loading

0 comments on commit 3d0a96a

Please sign in to comment.