assertProcess support nodejs ChildProcess
This commit is contained in:
@@ -1,12 +1,13 @@
|
||||
package kiss;
|
||||
|
||||
|
||||
#if macro
|
||||
import kiss.Kiss;
|
||||
import kiss.Helpers;
|
||||
import sys.FileSystem;
|
||||
import sys.io.File;
|
||||
import haxe.io.Path;
|
||||
import haxe.macro.Expr;
|
||||
import haxe.macro.Context;
|
||||
|
||||
using StringTools;
|
||||
using haxe.io.Path;
|
||||
@@ -38,9 +39,9 @@ typedef CompilationArgs = {
|
||||
class CompilerTools {
|
||||
/**
|
||||
* Compile a kiss file into a standalone script.
|
||||
* @return A function that, when called, executes the script and returns the script output as a string.
|
||||
* @return An expression of a function that, when called, executes the script and returns the script output as a string.
|
||||
*/
|
||||
public static function compileFileToScript(kissFile:String, args:CompilationArgs):() -> String {
|
||||
public static function compileFileToScript(kissFile:String, args:CompilationArgs):Expr {
|
||||
var k = Kiss.defaultKissState();
|
||||
var beginExpsInFile = Kiss.load(kissFile, k, "", true);
|
||||
return compileToScript([beginExpsInFile], args);
|
||||
@@ -48,9 +49,9 @@ class CompilerTools {
|
||||
|
||||
/**
|
||||
* Compile kiss expressions into a standalone script
|
||||
* @return A function that, when called, executes the script and returns the script output as a string.
|
||||
* @return An expression of a function that, when called, executes the script and returns the script output as a string.
|
||||
*/
|
||||
public static function compileToScript(exps:Array<ReaderExp>, args:CompilationArgs):() -> String {
|
||||
public static function compileToScript(exps:Array<ReaderExp>, args:CompilationArgs):Expr {
|
||||
// if folder exists, delete it
|
||||
if (FileSystem.exists(args.outputFolder)) {
|
||||
for (file in FileSystem.readDirectory(args.outputFolder)) {
|
||||
@@ -75,7 +76,7 @@ class CompilerTools {
|
||||
copyToFolder(file);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// If a main haxe file was given, use it
|
||||
var mainHxFile = if (args.mainHxFile != null) {
|
||||
args.mainHxFile;
|
||||
@@ -113,16 +114,16 @@ class CompilerTools {
|
||||
|
||||
// run haxelib install on given hxml and generated hxml
|
||||
var hxmlFiles = [buildHxmlFile];
|
||||
Helpers.assertProcess("haxelib", ["install", "--always", buildHxmlFile]);
|
||||
Prelude.assertProcess("haxelib", ["install", "--always", buildHxmlFile]);
|
||||
if (args.hxmlFile != null) {
|
||||
hxmlFiles.push(args.hxmlFile);
|
||||
Helpers.assertProcess("haxelib", ["install", "--always", args.hxmlFile]);
|
||||
Prelude.assertProcess("haxelib", ["install", "--always", args.hxmlFile]);
|
||||
}
|
||||
|
||||
// TODO install language-specific dependencies from langProjectFile (which might be tricky because we can't set the working directory)
|
||||
|
||||
// Compile the script
|
||||
Helpers.assertProcess("haxe", ["--cwd", args.outputFolder].concat(hxmlFiles.map(Path.withoutDirectory)));
|
||||
Prelude.assertProcess("haxe", ["--cwd", args.outputFolder].concat(hxmlFiles.map(Path.withoutDirectory)));
|
||||
|
||||
var command = "";
|
||||
var scriptExt = "";
|
||||
@@ -134,9 +135,11 @@ class CompilerTools {
|
||||
command = "python";
|
||||
scriptExt = "py";
|
||||
}
|
||||
|
||||
// return lambda that calls new Process() that runs the target-specific file
|
||||
return () -> Helpers.assertProcess(command, [Path.join([args.outputFolder, '$mainClassName.$scriptExt'])]);
|
||||
|
||||
// return an expression for a lambda that calls new Process() that runs the target-specific file
|
||||
var callingCode = '() -> kiss.Prelude.assertProcess("$command", [haxe.io.Path.join(["${args.outputFolder}", "$mainClassName.$scriptExt"])])';
|
||||
trace(callingCode);
|
||||
return Context.parse(callingCode, Context.currentPos());
|
||||
}
|
||||
}
|
||||
#end
|
||||
|
@@ -496,16 +496,6 @@ class Helpers {
|
||||
|
||||
// Get the path to a haxelib the user has installed
|
||||
public static function libPath(haxelibName:String) {
|
||||
return assertProcess("haxelib", ["libpath", haxelibName]);
|
||||
}
|
||||
|
||||
public static function assertProcess(command:String, args:Array<String>):String {
|
||||
var p = new Process(command, args);
|
||||
switch (p.exitCode()) {
|
||||
case 0:
|
||||
return p.stdout.readAll().toString().trim();
|
||||
default:
|
||||
throw p.stderr.readAll().toString().trim();
|
||||
}
|
||||
return Prelude.assertProcess("haxelib", ["libpath", haxelibName]);
|
||||
}
|
||||
}
|
||||
|
@@ -73,6 +73,7 @@ class Kiss {
|
||||
"has" => Symbol("Lambda.has"),
|
||||
"count" => Symbol("Lambda.count"),
|
||||
"enumerate" => Symbol("Prelude.enumerate"),
|
||||
"assertProcess" => Symbol("Prelude.assertProcess"),
|
||||
// These work with (apply) because they are added as "opAliases" in Macros.kiss:
|
||||
"min" => Symbol("Prelude.min"),
|
||||
"max" => Symbol("Prelude.max"),
|
||||
|
@@ -367,8 +367,8 @@ class Prelude {
|
||||
#if (!macro && hxnodejs)
|
||||
var kissProcess = ChildProcess.spawnSync("haxelib", ["run", "kiss", "convert", "--all", "--hscript"], {input: '${kissStr}\n'});
|
||||
if (kissProcess.status != 0) {
|
||||
var error:String = kissProcess.stderr;
|
||||
throw 'failed to convert ${kissStr} to hscript: ${error}';
|
||||
var error:Buffer = kissProcess.stderr;
|
||||
throw 'failed to convert ${kissStr} to hscript: ${error.toString()}';
|
||||
}
|
||||
var output:Buffer = kissProcess.stdout;
|
||||
return output.toString();
|
||||
@@ -392,4 +392,28 @@ class Prelude {
|
||||
throw "Can't convert Kiss to HScript on this target.";
|
||||
#end
|
||||
}
|
||||
|
||||
public static function assertProcess(command:String, args:Array<String>):String {
|
||||
#if sys
|
||||
var p = new Process(command, args);
|
||||
switch (p.exitCode()) {
|
||||
case 0:
|
||||
return p.stdout.readAll().toString().trim();
|
||||
default:
|
||||
throw p.stderr.readAll().toString().trim();
|
||||
}
|
||||
#elseif hxnodejs
|
||||
var p = ChildProcess.spawnSync(command, args);
|
||||
switch (p.status) {
|
||||
case 0:
|
||||
var output:Buffer = p.stdout;
|
||||
return output.toString();
|
||||
default:
|
||||
var error:String = p.stderr;
|
||||
throw error;
|
||||
}
|
||||
#else
|
||||
throw "Can't run a subprocess on this target."
|
||||
#end
|
||||
}
|
||||
}
|
||||
|
@@ -10,37 +10,27 @@ import haxe.macro.Context;
|
||||
|
||||
class CompilerToolsTestCase extends Test {
|
||||
function testCompileHelloWorldJs() {
|
||||
Assert.equals("Hello world!", _testCompileHelloWorldJs());
|
||||
Assert.equals("Hello world!", _testCompileHelloWorldJs()());
|
||||
}
|
||||
|
||||
static macro function _testCompileHelloWorldJs() {
|
||||
var runHelloWorld = CompilerTools.compileFileToScript(
|
||||
return CompilerTools.compileFileToScript(
|
||||
"kiss/template/src/template/Main.kiss", {
|
||||
lang: JavaScript,
|
||||
outputFolder: "bin/helloWorldJsTest",
|
||||
});
|
||||
|
||||
return {
|
||||
pos: Context.currentPos(),
|
||||
expr: EConst(CString(runHelloWorld(), DoubleQuotes))
|
||||
};
|
||||
}
|
||||
|
||||
function testCompileHelloWorldPy() {
|
||||
Assert.equals("Hello world!", _testCompileHelloWorldPy());
|
||||
Assert.equals("Hello world!", _testCompileHelloWorldPy()());
|
||||
}
|
||||
|
||||
static macro function _testCompileHelloWorldPy() {
|
||||
var runHelloWorld = CompilerTools.compileFileToScript(
|
||||
return CompilerTools.compileFileToScript(
|
||||
"kiss/template/src/template/Main.kiss", {
|
||||
lang: Python,
|
||||
outputFolder: "bin/helloWorldPyTest",
|
||||
});
|
||||
|
||||
return {
|
||||
pos: Context.currentPos(),
|
||||
expr: EConst(CString(runHelloWorld(), DoubleQuotes))
|
||||
};
|
||||
}
|
||||
|
||||
// TODO test what happens when passing more arguments/files
|
||||
|
Reference in New Issue
Block a user