Last active
August 29, 2015 14:00
-
-
Save corburn/11360594 to your computer and use it in GitHub Desktop.
A script to monitor umple files, generate Java with the umple tool, compile with javac, and run the program.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "umplemon", | |
"version": "0.0.0", | |
"description": "Monitor, compile, and run Java umple files", | |
"main": "watch.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "Jason Travis", | |
"license": "GPLv3", | |
"devDependencies": { | |
"gaze": "^0.6.3", | |
"async": "^0.7.0", | |
"rimraf": "^2.2.6" | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
// Assumes umple file defines namespace as the filename to lowercase sans .ump extension | |
// main class is the filename sans .ump extension | |
// Example: | |
// AviationControlSystem.ump | |
// namespace aviationcontrolsystem; | |
// class AviationControlSystem { | |
// public static void main(String[] args) { ... } | |
// } | |
var async = require('async'); | |
var exec = require('child_process').exec; | |
var spawn = require('child_process').spawn; | |
var gaze = require('gaze'); | |
var path = require('path'); | |
var rimraf = require('rimraf'); | |
var sys = require('sys'); | |
// trim path from __filename | |
var prgname = __filename.slice(__dirname.length+1,__filename.length); | |
var srcDir = './src'; | |
var binDir = './bin'; | |
var umple = 'umple_1.20.0.3845.jar'; | |
var child; | |
gaze(['**/*.ump'], function(err, watcher) { | |
this.on('all', function(event, filepath) { | |
var package = filepath.split('/')[filepath.split('/').length - 1].replace(/\..+$/, '').toLowerCase(); | |
var mainClass = filepath.split('/')[filepath.split('/').length - 1].replace(/\..+$/, ''); | |
console.log(prgname + ': ' + filepath + ' was modified'); | |
if (!filepath.match(/\.ump$/)) { | |
console.error(filepath + ' does not appear to be an umple file'); | |
return; | |
} | |
async.series({ | |
clean: function(cb) { | |
console.log(prgname + ': clean workspace...'); | |
async.parallel([ | |
function(callback) { | |
var p = path.join(__dirname, srcDir, package); | |
console.log(prgname + ': rm -rf ' + p); | |
rimraf(p, function(err) { | |
callback(err); | |
}); | |
}, | |
function(callback) { | |
var p = path.join(__dirname, binDir, package); | |
console.log(prgname + ': rm -rf ' + p); | |
rimraf(p, function(err) { | |
callback(err); | |
}); | |
} | |
], function(err, results) { | |
cb(err); | |
}); | |
}, | |
generate: function(cb) { | |
var cmd = 'java -jar ' + umple + ' --path ' + srcDir + ' --generate Java ' + filepath; | |
console.log(prgname + ': generate Java...'); | |
console.log(prgname + ': ' + cmd); | |
var child = exec(cmd, function (err, stdout, stderr) { | |
if (stdout) { | |
sys.print('STDOUT\n' + stdout); | |
} | |
if (stderr) { | |
sys.print('STDERR\n' + stderr); | |
} | |
cb(err); | |
}); | |
}, | |
compile: function(cb) { | |
var cmd = 'javac -classpath ' + srcDir + ' -d ' + binDir + ' ' + srcDir + '/' + package + '/*.java'; | |
console.log(prgname + ': compile...'); | |
console.log(prgname + ': ' + cmd); | |
var child = exec(cmd, function (err, stdout, stderr) { | |
if (stdout) { | |
sys.print('STDOUT\n' + stdout); | |
} | |
if (stderr) { | |
sys.print('STDERR\n' + stderr); | |
} | |
sys.print('DONE\n'); | |
cb(err); | |
}); | |
}, | |
run: function(cb) { | |
return; | |
var cmd = 'java -classpath ' + binDir + ' ' + package + '.' + mainClass; | |
var args = cmd.split(' '); | |
var bin = args.shift(); | |
if(child) { | |
// Kill the old child process before starting a new one | |
child.kill('SIGINT'); | |
} | |
console.log(prgname + ': running...'); | |
console.log(prgname + ': ' + cmd); | |
child = spawn(bin, args); | |
child.stdout.setEncoding('utf8'); | |
child.stdout.on('data', function (data) { | |
console.log(data); | |
}); | |
child.stderr.on('data', function (data) { | |
//data += ''; | |
//console.log(data.replace('\n', '\nstderr: ')); | |
console.log(data); | |
}); | |
child.on('exit', function (code) { | |
console.log('child process exited with code ' + code); | |
cb(); | |
}); | |
} | |
}, function (err, results) { | |
if (err) throw err; | |
//console.log(results); | |
}); | |
}); | |
}); | |
// http://nodejs.org/api.html#_child_processes | |
// http://stackoverflow.com/questions/9781214/parse-output-of-spawned-node-js-child-process-line-by-line |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment