assertProcess support nodejs ChildProcess

This commit is contained in:
2021-07-26 15:09:14 -06:00
parent ccb22c82a8
commit ee15d243f8
5 changed files with 47 additions and 39 deletions

View File

@@ -1,12 +1,13 @@
package kiss; package kiss;
#if macro #if macro
import kiss.Kiss; import kiss.Kiss;
import kiss.Helpers; import kiss.Helpers;
import sys.FileSystem; import sys.FileSystem;
import sys.io.File; import sys.io.File;
import haxe.io.Path; import haxe.io.Path;
import haxe.macro.Expr;
import haxe.macro.Context;
using StringTools; using StringTools;
using haxe.io.Path; using haxe.io.Path;
@@ -38,9 +39,9 @@ typedef CompilationArgs = {
class CompilerTools { class CompilerTools {
/** /**
* Compile a kiss file into a standalone script. * 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 k = Kiss.defaultKissState();
var beginExpsInFile = Kiss.load(kissFile, k, "", true); var beginExpsInFile = Kiss.load(kissFile, k, "", true);
return compileToScript([beginExpsInFile], args); return compileToScript([beginExpsInFile], args);
@@ -48,9 +49,9 @@ class CompilerTools {
/** /**
* Compile kiss expressions into a standalone script * 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 folder exists, delete it
if (FileSystem.exists(args.outputFolder)) { if (FileSystem.exists(args.outputFolder)) {
for (file in FileSystem.readDirectory(args.outputFolder)) { for (file in FileSystem.readDirectory(args.outputFolder)) {
@@ -113,16 +114,16 @@ class CompilerTools {
// run haxelib install on given hxml and generated hxml // run haxelib install on given hxml and generated hxml
var hxmlFiles = [buildHxmlFile]; var hxmlFiles = [buildHxmlFile];
Helpers.assertProcess("haxelib", ["install", "--always", buildHxmlFile]); Prelude.assertProcess("haxelib", ["install", "--always", buildHxmlFile]);
if (args.hxmlFile != null) { if (args.hxmlFile != null) {
hxmlFiles.push(args.hxmlFile); 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) // TODO install language-specific dependencies from langProjectFile (which might be tricky because we can't set the working directory)
// Compile the script // 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 command = "";
var scriptExt = ""; var scriptExt = "";
@@ -135,8 +136,10 @@ class CompilerTools {
scriptExt = "py"; scriptExt = "py";
} }
// return lambda that calls new Process() that runs the target-specific file // return an expression for a lambda that calls new Process() that runs the target-specific file
return () -> Helpers.assertProcess(command, [Path.join([args.outputFolder, '$mainClassName.$scriptExt'])]); var callingCode = '() -> kiss.Prelude.assertProcess("$command", [haxe.io.Path.join(["${args.outputFolder}", "$mainClassName.$scriptExt"])])';
trace(callingCode);
return Context.parse(callingCode, Context.currentPos());
} }
} }
#end #end

View File

@@ -496,16 +496,6 @@ class Helpers {
// Get the path to a haxelib the user has installed // Get the path to a haxelib the user has installed
public static function libPath(haxelibName:String) { public static function libPath(haxelibName:String) {
return assertProcess("haxelib", ["libpath", haxelibName]); return Prelude.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();
}
} }
} }

View File

@@ -73,6 +73,7 @@ class Kiss {
"has" => Symbol("Lambda.has"), "has" => Symbol("Lambda.has"),
"count" => Symbol("Lambda.count"), "count" => Symbol("Lambda.count"),
"enumerate" => Symbol("Prelude.enumerate"), "enumerate" => Symbol("Prelude.enumerate"),
"assertProcess" => Symbol("Prelude.assertProcess"),
// These work with (apply) because they are added as "opAliases" in Macros.kiss: // These work with (apply) because they are added as "opAliases" in Macros.kiss:
"min" => Symbol("Prelude.min"), "min" => Symbol("Prelude.min"),
"max" => Symbol("Prelude.max"), "max" => Symbol("Prelude.max"),

View File

@@ -367,8 +367,8 @@ class Prelude {
#if (!macro && hxnodejs) #if (!macro && hxnodejs)
var kissProcess = ChildProcess.spawnSync("haxelib", ["run", "kiss", "convert", "--all", "--hscript"], {input: '${kissStr}\n'}); var kissProcess = ChildProcess.spawnSync("haxelib", ["run", "kiss", "convert", "--all", "--hscript"], {input: '${kissStr}\n'});
if (kissProcess.status != 0) { if (kissProcess.status != 0) {
var error:String = kissProcess.stderr; var error:Buffer = kissProcess.stderr;
throw 'failed to convert ${kissStr} to hscript: ${error}'; throw 'failed to convert ${kissStr} to hscript: ${error.toString()}';
} }
var output:Buffer = kissProcess.stdout; var output:Buffer = kissProcess.stdout;
return output.toString(); return output.toString();
@@ -392,4 +392,28 @@ class Prelude {
throw "Can't convert Kiss to HScript on this target."; throw "Can't convert Kiss to HScript on this target.";
#end #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
}
} }

View File

@@ -10,37 +10,27 @@ import haxe.macro.Context;
class CompilerToolsTestCase extends Test { class CompilerToolsTestCase extends Test {
function testCompileHelloWorldJs() { function testCompileHelloWorldJs() {
Assert.equals("Hello world!", _testCompileHelloWorldJs()); Assert.equals("Hello world!", _testCompileHelloWorldJs()());
} }
static macro function _testCompileHelloWorldJs() { static macro function _testCompileHelloWorldJs() {
var runHelloWorld = CompilerTools.compileFileToScript( return CompilerTools.compileFileToScript(
"kiss/template/src/template/Main.kiss", { "kiss/template/src/template/Main.kiss", {
lang: JavaScript, lang: JavaScript,
outputFolder: "bin/helloWorldJsTest", outputFolder: "bin/helloWorldJsTest",
}); });
return {
pos: Context.currentPos(),
expr: EConst(CString(runHelloWorld(), DoubleQuotes))
};
} }
function testCompileHelloWorldPy() { function testCompileHelloWorldPy() {
Assert.equals("Hello world!", _testCompileHelloWorldPy()); Assert.equals("Hello world!", _testCompileHelloWorldPy()());
} }
static macro function _testCompileHelloWorldPy() { static macro function _testCompileHelloWorldPy() {
var runHelloWorld = CompilerTools.compileFileToScript( return CompilerTools.compileFileToScript(
"kiss/template/src/template/Main.kiss", { "kiss/template/src/template/Main.kiss", {
lang: Python, lang: Python,
outputFolder: "bin/helloWorldPyTest", outputFolder: "bin/helloWorldPyTest",
}); });
return {
pos: Context.currentPos(),
expr: EConst(CString(runHelloWorld(), DoubleQuotes))
};
} }
// TODO test what happens when passing more arguments/files // TODO test what happens when passing more arguments/files