Skip to content

Instantly share code, notes, and snippets.

@simshanith
Last active August 29, 2015 14:06
Show Gist options
  • Save simshanith/456a612ce0341374c2a3 to your computer and use it in GitHub Desktop.
Save simshanith/456a612ce0341374c2a3 to your computer and use it in GitHub Desktop.
Runs list of sublime build variants from command line.
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
var util = require('util');
var buildOrder = [
'Locator Models',
'WCM Services',
'MCCOM Services',
'WCM',
'MCCOM'
];
// slurp project file
fs.readFile(path.join(__dirname, 'mastercard.sublime-project'), function (err, projectFile) {
// parse & grab build systems from project
var project = JSON.parse(projectFile.toString());
var system = project.build_systems.filter(function(system) {
return system.name === 'MasterCard';
})[0];
// create spawning functions
var builds = system.variants.map(function(build) {
// use callback api
build.exec = function(callback) {
var cb = typeof callback === 'function' ? callback : function(){};
var cmd = build.cmd;
var cwd = build.working_dir.replace('${project_path}', __dirname);
var spawn = require('child_process').spawn;
var sep = 100;
var boldSep = "=";
var regSep = "-";
console.log('');
console.log('');
console.log(Array(sep).map(function(){return boldSep;}).join(boldSep));
console.log(Array(sep).map(function(){return boldSep;}).join(boldSep));
console.log('');
console.log(build.name);
console.log(cmd.join(' '));
console.log(cwd);
console.log(Array(sep).map(function(){return regSep;}).join(regSep));
var child = spawn(cmd.shift(), cmd, {
cwd: cwd,
env: build.env
});
child.stdout.on('data', function(data) {
//console.log(build.name);
console.log(data.toString());
});
child.stderr.on('data', function(data) {
//console.error(build.name);
console.error(data.toString());
});
child.on('close', function (code) {
console.log('%s update exited with code %s', build.name, code);
if(code === 0) {
build.success = true;
} else {
build.error = true;
}
// execute callback
cb();
});
};
return build;
});
// reorder.
buildOrder.forEach(function(name, i) {
var reorderBuild = builds.filter(function(build) {
return build.name === name;
});
if( !!reorderBuild.length ) {
reorderBuild = reorderBuild[0];
} else {
throw new Error('no build of name'+name);
}
var reorderIndex = builds.indexOf(reorderBuild);
// if the build exists & isn't in the right spot...
if( !!~reorderIndex && reorderIndex !== i) {
// remove from array
builds.splice(reorderIndex, 1);
// add back to array in correct position
builds.splice(i, 0, reorderBuild);
}
});
//console.log(builds.map(function(build){return build.name}));
// execute in series.
var series = builds.reduce(function(memo, build, i, arr) {
if(!memo) {
return build.exec;
} else {
return function(cb) {
memo(function(){
build.exec(cb);
});
};
}
}, null);
series(function(){
console.log(Array(25).join('~=~'));
console.log('series all done');
console.log(Array(25).join('~=~'));
});
// log successes & failures
process.on('exit', function(code) {
var longestBuild = Math.max.apply(null, builds.map(function(build){return build.name.length;}));
builds.forEach(function(build) {
var filler = ': ';
filler += Array(longestBuild-build.name.length+1).join(' ');
console.log('%s%s%s', build.name, filler, build.success ? 'success' : 'failure');
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment