Permalink
Browse files

Run helper scripts in node if available

  • Loading branch information...
1 parent 4140592 commit 41287cbf8ce352f37bb39f90c8f1f549a9360361 @dfabulich committed Dec 5, 2011
Showing with 282 additions and 67 deletions.
  1. +45 −16 autotest.js
  2. +33 −12 build.xml
  3. +33 −0 generatorNode.js
  4. +52 −9 headless.js
  5. +27 −8 randomtest.js
  6. +27 −4 tests/suite.js
  7. +65 −18 xmltranslator.js
View
61 autotest.js
@@ -17,12 +17,26 @@
* either express or implied.
*/
var gameName = "mygame";
-load("web/scene.js");
-load("web/navigator.js");
-load("web/util.js");
-load("headless.js");
-load("web/"+gameName+"/"+"mygame.js");
-load("editor/embeddable-autotester.js");
+if (typeof java == "undefined") {
+ var fs = require('fs');
+ var path = require('path');
+ eval(fs.readFileSync("web/scene.js", "utf-8"));
+ eval(fs.readFileSync("web/navigator.js", "utf-8"));
+ eval(fs.readFileSync("web/util.js", "utf-8"));
+ eval(fs.readFileSync("headless.js", "utf-8"));
+ eval(fs.readFileSync("web/"+gameName+"/"+"mygame.js", "utf-8"));
+ eval(fs.readFileSync("editor/embeddable-autotester.js", "utf-8"));
+ function print(str) {
+ console.log(str);
+ }
+} else {
+ load("web/scene.js");
+ load("web/navigator.js");
+ load("web/util.js");
+ load("headless.js");
+ load("web/"+gameName+"/"+"mygame.js");
+ load("editor/embeddable-autotester.js");
+}
nav.setStartingStatsClone(stats);
@@ -31,17 +45,22 @@ var sceneList = [];
function debughelp() {
debugger;
}
-
-var list = arguments;
+var list;
+if (isRhino) {
+ list = arguments;
+} else {
+ list = process.argv;
+ list.shift();
+ list.shift();
+}
if (!list.length || (list.length == 1 && !list[0])) {
list = [];
var name = nav.getStartupScene();
while (name) {
list.push(name);
name = nav.nextSceneName(name);
}
- var statsFile = new java.io.File("web/"+gameName+"/scenes/choicescript_stats.txt");
- if (statsFile.exists()) {
+ if (fileExists("web/"+gameName+"/scenes/choicescript_stats.txt")) {
list.push("choicescript_stats");
}
}
@@ -50,10 +69,16 @@ var uncoveredScenes = [];
var uncovered;
function verifyFileName(name) {
- var file = new java.io.File("web/"+gameName+"/scenes/"+name+".txt");
- if (!file.exists()) throw new Error("File does not exist: " + name);
- var canonicalName = file.getCanonicalFile().getName();
- if (name+".txt" != canonicalName) throw new Error("Incorrect capitalization/canonicalization; the file is called " + canonicalName + " but you requested " + name + ".txt");
+ var filePath = "web/"+gameName+"/scenes/"+name+".txt";
+ if (!fileExists(filePath)) throw new Error("File does not exist: " + name);
+ if (isRhino) {
+ var file = new java.io.File(filePath);
+ var canonicalName = file.getCanonicalFile().getName();
+ if (name+".txt" != canonicalName) throw new Error("Incorrect capitalization/canonicalization; the file is called " + canonicalName + " but you requested " + name + ".txt");
+ } else {
+ var canonicalName = path.basename(fs.realpathSync(filePath));
+ if (name+".txt" != canonicalName) throw new Error("Incorrect capitalization/canonicalization; the file is called " + canonicalName + " but you requested " + name + ".txt");
+ }
}
Scene.prototype.verifyFileName = function commandLineVerifyFileName(name) {
@@ -71,7 +96,7 @@ Scene.prototype.conflictingOptions = function() {};
for (var i = 0; i < list.length; i++) {
print(list[i]);
- java.lang.Thread.sleep(100); // sleep to allow print statements to flush :-(
+ if (isRhino) java.lang.Thread.sleep(100); // sleep to allow print statements to flush :-(
verifyFileName(list[i]);
var sceneText = slurpFile("web/"+gameName+"/scenes/"+list[i]+".txt");
window = {console: {log: function(msg) { print(msg); } }};
@@ -80,7 +105,11 @@ for (var i = 0; i < list.length; i++) {
} catch (e) {
print("QUICKTEST FAILED\n");
print(e);
- java.lang.System.exit(1);
+ if (isRhino) {
+ java.lang.System.exit(1);
+ } else {
+ process.exit(1);
+ }
}
if (uncovered) {
uncoveredScenes.push({name:list[i], lines:uncovered});
View
45 build.xml
@@ -29,13 +29,30 @@ either express or implied.
<echo file="web/version.js">window.version="${revision}"</echo>
</target>
- <target name="rhino">
+ <target name="check-node" unless="skipNode">
+ <exec taskName="node" executable="node" resultproperty="exitCode" failIfExecutionFails="false">
+ <arg value="--version" />
+ </exec>
+ <condition property="hasNode">
+ <equals arg1="${exitCode}" arg2="0" />
+ </condition>
+ </target>
+
+ <target name="node" depends="check-node" if="hasNode">
+ <presetdef name="rhino">
+ <exec taskName="node" failonerror="true" executable="node" />
+ </presetdef>
+ <property name="gen.dir" location="${basedir}/web/mygame/scenes" />
+ <property name="test.dir" location="${basedir}/tests" />
+ </target>
+
+ <target name="rhino" depends="node" unless="hasNode">
<condition property="rhinoClass" value="org.mozilla.javascript.tools.debugger.Main">
<isset property="debug" />
</condition>
<property name="rhinoClass" value="org.mozilla.javascript.tools.shell.Main"/>
<presetdef name="rhino">
- <java failonerror="true" classpath="js.jar" classname="${rhinoClass}">
+ <java failonerror="true" classpath="js.jar" classname="${rhinoClass}" fork="true">
<arg line="-w -opt -1 -debug"/>
</java>
</presetdef>
@@ -44,7 +61,7 @@ either express or implied.
</target>
<target name="unittest-core" depends="rhino">
- <rhino fork="true">
+ <rhino>
<arg file="${test.dir}/suite.js" />
<arg file="${test.dir}/qunit.js" />
<arg file="${basedir}/web/scene.js" />
@@ -56,7 +73,7 @@ either express or implied.
</target>
<target name="unittest-util" depends="rhino">
- <rhino fork="true">
+ <rhino>
<arg file="${test.dir}/suite.js" />
<arg file="${test.dir}/qunit.js" />
<arg file="${basedir}/web/scene.js" />
@@ -68,7 +85,7 @@ either express or implied.
</target>
<target name="unittest-autotest" depends="rhino">
- <rhino fork="true">
+ <rhino>
<arg file="${test.dir}/suite.js" />
<arg file="${test.dir}/qunit.js" />
<arg file="${basedir}/web/scene.js" />
@@ -80,7 +97,7 @@ either express or implied.
</target>
<target name="unittest-xmltranslator" depends="rhino">
- <rhino fork="true">
+ <rhino>
<arg file="${test.dir}/suite.js" />
<arg file="${test.dir}/qunit.js" />
<arg file="${basedir}/web/scene.js" />
@@ -99,33 +116,37 @@ either express or implied.
<property name="game" value="mygame" />
<property name="output" value="randomtest-output.txt" />
<echo>Executing randomtest, writing to ${output}</echo>
- <java fork="true" failonerror="true" classpath="js.jar" classname="${rhinoClass}" output="${output}">
+ <rhino output="${output}">
<arg value="randomtest.js" />
<arg value="${num}" />
<arg value="${game}" />
- </java>
+ </rhino>
</target>
<target name="autotest" depends="quicktest" />
<target name="quicktest" depends="rhino">
<property name="vig" value="" />
- <rhino fork="true">
+ <rhino>
<arg value="autotest.js"/>
<arg value="${vig}" />
</rhino>
</target>
<target name="generator" depends="rhino">
- <rhino fork="true">
- <arg file="generator.js"/>
+ <condition property="scriptSuffix" value="Node">
+ <isset property="hasNode" />
+ </condition>
+ <property name="scriptSuffix" value="" />
+ <rhino>
+ <arg file="generator${scriptSuffix}.js"/>
<arg value="${gen.dir}" />
</rhino>
</target>
<target name="xmltranslator" depends="rhino">
<property name="vig" value="" />
- <rhino fork="true">
+ <rhino>
<arg file="xmltranslator.js"/>
<arg value="${gen.dir}" />
<arg value="${vig}" />
View
33 generatorNode.js
@@ -0,0 +1,33 @@
+var fs = require('fs');
+var path = require('path');
+var dir = process.argv[2] || "web/mygame/scenes";
+eval(fs.readFileSync("web/scene.js", "utf-8"));
+eval(fs.readFileSync("web/util.js", "utf-8"));
+eval(fs.readFileSync("headless.js", "utf-8"));
+
+var list = fs.readdirSync(dir);
+
+var i = list.length;
+while (i--) {
+ if (!/\.txt$/.test(list[i])) continue;
+ var filePath = dir + '/' + list[i];
+ var inputMod = fs.statSync(filePath).mtime.getTime();
+ var outputMod = 0;
+ if (path.existsSync(filePath + ".js")) {
+ outputMod = fs.statSync(filePath + ".js").mtime.getTime();;
+ }
+ if (inputMod <= outputMod) {
+ console.log(list[i] + " up to date");
+ continue;
+ }
+ console.log(list[i]);
+ var str = slurpFile(filePath);
+ var scene = new Scene();
+ scene.loadLines(str);
+
+ var writer = fs.createWriteStream(filePath + ".js");
+ writer.write("window.stats.scene.loadLinesFast(" + scene.temps.choice_crc + ", " + toJson(scene.lines)+ ", " + toJson(scene.labels) + ");", "utf-8");
+
+ writer.end();
+}
+
View
61 headless.js
@@ -29,6 +29,8 @@ function println(msg, parent) {
printed.push("<br>");
}
+isRhino = typeof(java) != "undefined";
+
clearScreen = function clearScreen(code) {code.call();}
saveCookie = function(callback) { if (callback) callback.call(); }
doneLoading = function() {}
@@ -37,21 +39,62 @@ printShareLinks = function() {}
printImage = function() {}
showPassword = function() {}
+function fileExists(filePath) {
+ if (isRhino) {
+ return new java.io.File(filePath).exists();
+ } else {
+ return path.existsSync(filePath);
+ }
+}
+
+function fileLastMod(filePath) {
+ if (isRhino) {
+ return new java.io.File("xmltranslator.js").lastModified()
+ } else {
+ if (path.existsSync(filePath)) return fs.statSync(filePath).mtime.getTime();
+ return 0;
+ }
+}
+
+function mkdirs(filePath) {
+ if (isRhino) {
+ new java.io.File(filePath).mkdirs()
+ } else {
+ if (!path.existsSync(filePath)) {
+ var parentDir = path.dirname(filePath);
+ if (!path.existsSync(parentDir)) {
+ mkdirs(parentDir);
+ }
+ fs.mkdirSync(filePath);
+ }
+
+ }
+}
+
function slurpFile(name) {
return slurpFileLines(name).join('\n');
}
function slurpFileLines(name) {
- var lines = [];
- var reader = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(name), "UTF-8"));
- var line = reader.readLine();
- // strip byte order mark
- if (line.charCodeAt(0) == 65279) line = line.substring(1);
- lines.push(line);
- while (line = reader.readLine()) {
- lines.push(line);
+ if (isRhino) {
+ var lines = [];
+ var reader = new java.io.BufferedReader(new java.io.InputStreamReader(new java.io.FileInputStream(name), "UTF-8"));
+ var line = reader.readLine();
+ // strip byte order mark
+ if (line.charCodeAt(0) == 65279) line = line.substring(1);
+ lines.push(line);
+ while (line = reader.readLine()) {
+ lines.push(line);
+ }
+ return lines;
+ } else {
+ var blob = fs.readFileSync(name, "utf-8");
+ var lines = blob.split(/\r?\n/);
+ var firstLine = lines[0]
+ // strip byte order mark
+ if (firstLine.charCodeAt(0) == 65279) line[0] = firstLine.substring(1);
+ return lines;
}
- return lines;
}
function initStore() { return false; }
View
35 randomtest.js
@@ -17,13 +17,32 @@
* either express or implied.
*/
var gameName = "mygame";
-if (arguments[1]) gameName = arguments[1];
-load("web/scene.js");
-load("web/navigator.js");
-load("web/util.js");
-load("headless.js");
-load("seedrandom.js");
-load("web/"+gameName+"/"+"mygame.js");
+var args;
+if (typeof java == "undefined") {
+ args = process.argv;
+ args.shift();
+ args.shift();
+ var fs = require('fs');
+ var path = require('path');
+ eval(fs.readFileSync("web/scene.js", "utf-8"));
+ eval(fs.readFileSync("web/navigator.js", "utf-8"));
+ eval(fs.readFileSync("web/util.js", "utf-8"));
+ eval(fs.readFileSync("headless.js", "utf-8"));
+ eval(fs.readFileSync("seedrandom.js", "utf-8"));
+ eval(fs.readFileSync("web/"+gameName+"/"+"mygame.js", "utf-8"));
+ function print(str) {
+ console.log(str);
+ }
+} else {
+ args = arguments;
+ load("web/scene.js");
+ load("web/navigator.js");
+ load("web/util.js");
+ load("headless.js");
+ load("seedrandom.js");
+ load("web/"+gameName+"/"+"mygame.js");
+}
+if (args[1]) gameName = args[1];
Math.seedrandom(1);
@@ -267,7 +286,7 @@ var sceneNames = [];
nav.setStartingStatsClone(stats);
var iterations = 10000;
-if (arguments[0]) iterations = arguments[0];
+if (args[0]) iterations = args[0];
for (i = 0; i < iterations; i++) {
log("*****" + i);
timeout = null;
View
31 tests/suite.js
@@ -1,4 +1,19 @@
-load(arguments[0]);
+with(this) {
+var isRhino;
+if (typeof java == "undefined") {
+ isRhino = false;
+ var fs = require('fs');
+ var args = process.argv;
+ args.shift();
+ args.shift();
+ var contents = fs.readFileSync(args[0], "utf-8");
+ eval(contents);
+ print = console.log;
+} else {
+ isRhino = true;
+ args = arguments;
+ load(args[0]);
+}
QUnit.init();
QUnit.config.blocking = false;
@@ -32,13 +47,21 @@ QUnit.done = function(results) {
finalResults = results;
}
-for (var i = 1; i < arguments.length; i++) {
- load(arguments[i]);
+for (var i = 1; i < args.length; i++) {
+ if (isRhino) {
+ load(args[i]);
+ } else {
+ var contents = fs.readFileSync(args[i], 'utf-8');
+ eval(contents);
+ }
+
}
if (finalResults.failed) {
print(finalResults.failed, "FAILED out of", finalResults.total, "total");
- java.lang.System.exit(1);
+ isRhino ? java.lang.System.exit(1) : process.exit(1);
} else {
print(finalResults.total, "PASSED");
+}
+
}
View
83 xmltranslator.js
@@ -1,8 +1,27 @@
+var isRhino;
+if (typeof java == "undefined") {
+ isRhino = false;
+ var fs = require('fs');
+ var path = require('path');
+ var args = process.argv;
+ args.shift();
+ args.shift();
+ print = console.log;
+} else {
+ isRhino = true;
+ var args = arguments;
+}
if (typeof xmlTranslatorTestOverride == "undefined") {
- var dir = arguments[0] || "web/mygame/scenes";
- load("web/scene.js");
- load("web/util.js");
- load("headless.js");
+ var dir = args[0] || "web/mygame/scenes";
+ if (isRhino) {
+ load("web/scene.js");
+ load("web/util.js");
+ load("headless.js");
+ } else {
+ eval(fs.readFileSync("web/scene.js", "utf-8"));
+ eval(fs.readFileSync("web/util.js", "utf-8"));
+ eval(fs.readFileSync("headless.js", "utf-8"));
+ }
}
var writer;
@@ -473,10 +492,18 @@ XmlScene.prototype.stat_chart = function xmlStatChart() {
writer.write("</stat-chart>\n");
}
-if (arguments[1]) {
- list = [new java.io.File(dir, arguments[1] + ".txt")];
+if (args[1]) {
+ if (isRhino) {
+ list = [new java.io.File(dir, args[1] + ".txt")];
+ } else {
+ list = [dir + '/' + args[1] + ".txt"];
+ }
} else if (true) {
- var list = new java.io.File(dir).listFiles();
+ if (isRhino) {
+ var list = new java.io.File(dir).listFiles();
+ } else {
+ var list = fs.readdirSync(dir);
+ }
} else {
load("web/navigator.js");
load("web/mygame/mygame.js");
@@ -494,25 +521,40 @@ if (typeof xmlTranslatorTestOverride != "undefined") {
xmlTranslatorTestOverride();
} else {
var i = list.length;
- var translatorLastModified = new java.io.File("xmltranslator.js").lastModified();
+ var translatorLastModified = fileLastMod("xmltranslator.js");
while (i--) {
- if (/(menu|hello)/.test(list[i].getName())) continue;
- if (!/\.txt$/.test(list[i].getName())) continue;
- var inputMod = list[i].lastModified();
+ var fileName;
+ var filePath;
+ if (isRhino) {
+ fileName = list[i].getName();
+ filePath = list[i].getAbsolutePath();
+ } else {
+ fileName = list[i];
+ filePath = dir + "/" + list[i];
+ }
+ if (/(menu|hello)/.test(fileName)) continue;
+ if (!/\.txt$/.test(fileName)) continue;
+ var inputMod = fileLastMod(filePath);
if (inputMod < translatorLastModified) inputMod = translatorLastModified;
- var dir = "./xml/";
- new java.io.File(dir).mkdirs();
- var outputFilePath = dir + list[i].getName() + ".xml";
- var outputMod = new java.io.File(outputFilePath).lastModified();
+ var outputDir = "./xml/";
+ mkdirs(dir);
+ var outputFilePath = outputDir + fileName + ".xml";
+ var outputMod = fileLastMod(outputFilePath);
if (inputMod <= outputMod) {
print(list[i] + " up to date");
continue;
}
print(list[i]);
- var str = slurpFile(list[i]);
+ var str = slurpFile(filePath);
var scene = new XmlScene();
scene.loadLines(str);
- var writer = new java.io.BufferedWriter(new java.io.OutputStreamWriter(new java.io.FileOutputStream(outputFilePath), "UTF-8"));
+ var writer;
+ if (isRhino) {
+ writer = new java.io.BufferedWriter(new java.io.OutputStreamWriter(new java.io.FileOutputStream(outputFilePath), "UTF-8"));
+ } else {
+ writer = fs.createWriteStream(outputFilePath, {flags: 'w', encoding: 'utf-8', mode: 0666});
+ }
+
//writer = {write: function(x){java.lang.System.out.print(x)}, close: function(){java.lang.System.out.println()}};
writer.write("<!DOCTYPE vignette [ \n" +
"<!ATTLIST label id ID #REQUIRED>\n" +
@@ -521,7 +563,12 @@ if (typeof xmlTranslatorTestOverride != "undefined") {
scene.execute();
closePara();
writer.write("</vignette>\n");
- writer.close();
+ if (isRhino) {
+ writer.close();
+ } else {
+ writer.end();
+ }
+
//throw new Error("halt");
}
}

0 comments on commit 41287cb

Please sign in to comment.