Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 194 lines (161 sloc) 5.567 kb
#!/usr/bin/env node
var sys = require('sys'),
fs = require('fs'),
path = require('path'),
spawn = require('child_process').spawn;
var VERBOSE = isVerboseMode(),
SRC_DIR = path.join(__dirname, "src"),
OUT_DIR = path.join(__dirname, "dist"),
TMP_DIR = path.join(__dirname, ".jar"),
JAR_NAME = "SGF.jar",
JAR_PATH = path.join(OUT_DIR, JAR_NAME),
MANIFEST = path.join(__dirname, ".manifest.txt")
// First, delete the TMP_DIR and SGF.jar file, if they exist
cleanUp(getSourceFiles, true);
// Second, use the "find" command to get the list of .java
// source files that need to be compiled.
function getSourceFiles() {
var javaFiles = spawn("find", [SRC_DIR, "-name", "*.java"]);
javaFiles.stdoutStr = "";
javaFiles.stdout.addListener('data', function (data) {
javaFiles.stdoutStr += data;
});
javaFiles.addListener('exit', function(code) {
var files = javaFiles.stdoutStr.split("\n");
javaFiles = [];
// We have to filter out any straggler files from TextMate
// that start with "._" but still end with ".java".
files.forEach(function(file) {
if (file.length > 0 && path.basename(file).indexOf("._") !== 0) {
javaFiles.push(file);
}
});
compileSrc(javaFiles);
});
}
// Third, create the TMP_DIR, and run 'javac' to compile the
// .java source files into .class files. `files` is an Array of .java files
// to compile.
function compileSrc(files) {
sys.puts('\tInvoking "javac":');
// Make the TMP_DIR
fs.mkdirSync(TMP_DIR, 0777);
var classpath = [path.join(OUT_DIR, "js.jar"), path.join(OUT_DIR, "commons-codec-1.4.jar")].join(":");
var args = [
'-d', TMP_DIR,
'-classpath', classpath
];
if (VERBOSE) {
args.push("-verbose");
}
// Compile Java source files using "javac"
var javac = spawn("javac", args.concat(files));
javac.stdout.setEncoding("utf8");
javac.stdout.addListener('data', function(data) {
data.split("\n").forEach(function(d) {
if (d.length>0)
sys.puts("JAVAC:\t" + d);
});
});
javac.stderr.setEncoding("utf8");
javac.stderr.addListener('data', function(data) {
data.split("\n").forEach(function(d) {
if (d.length>0)
sys.puts("JAVAC:\t" + d);
});
});
javac.addListener('exit', function(code) {
if (code === 0) {
jar();
} else {
sys.puts('\nFAIL: Aborting since "javac" exited with code: ' + code);
cleanUp(function() {
process.exit(code);
});
}
});
}
// Fourth, run 'jar' to create a JAR file containing the compiled .class
// files and other necessary files for the engine to load.
function jar() {
sys.puts('\n\tInvoking "jar":');
fs.writeFileSync(MANIFEST,
"Main-Class: com.simplegameframework.launcher.Launcher\n" +
"Class-Path: js.jar\n");
var switches = 'cfm';
if (VERBOSE) {
switches += 'v';
}
var jar = spawn("jar", [
switches,
JAR_PATH,
MANIFEST,
// Add the compiled .class files from TMP_DIR
'-C', TMP_DIR, '.',
// Add the JavaScript source files from
// 'com/simplegameframework/engine/js/' in SRC_DIR
'-C', SRC_DIR, 'com/simplegameframework/engine/js/'
]);
jar.stdout.setEncoding("utf8");
jar.stdout.addListener('data', function(data) {
data.split("\n").forEach(function(d) {
if (d.length>0)
sys.puts("JAR:\t" + d);
});
});
jar.stderr.setEncoding("utf8");
jar.stderr.addListener('data', function(data) {
data.split("\n").forEach(function(d) {
if (d.length>0)
sys.puts("JAR:\t" + d);
});
});
jar.addListener('exit', function(code) {
if (code === 0) {
sys.puts('\nSUCCESS: The SGF "Java Engine" has been successfully '+
'built, saved as:\n\t' + JAR_PATH + '\n');
} else {
sys.puts('\nFAIL: Aborting since "jar" exited with code: ' + code);
}
cleanUp(function() {
process.exit(code);
});
});
}
// ---------------------------------------------------------------------------
// Cleans up the build directory. Pass 'true' as a second argument to also
// delete the compiled SGF.jar file, if present.
function cleanUp(callback, deleteJar) {
var tmpDirDeleted = false;
var jarDeleted = false;
var manifestDeleted = false;
function check() {
if (tmpDirDeleted && manifestDeleted && (deleteJar && jarDeleted)) {
callback();
}
}
spawn("rm", ["-rf" + (VERBOSE ? "v" : ""), TMP_DIR]).addListener('exit', function() {
tmpDirDeleted = true;
check();
});
spawn("rm", ["-f" + (VERBOSE ? "v" : ""), MANIFEST]).addListener('exit', function() {
manifestDeleted = true;
check();
});
if (deleteJar) {
spawn("rm", ["-f" + (VERBOSE ? "v" : ""), JAR_PATH]).addListener('exit', function() {
jarDeleted = true;
check();
});
}
}
// Returns true if '--verbose' or '-v' was passed as a command line arg
function isVerboseMode() {
var rtn = false;
process.argv.forEach(function(arg) {
if ("--verbose" == arg || '-v' == arg) {
rtn = true;
}
});
return rtn;
}
Jump to Line
Something went wrong with that request. Please try again.