diff --git a/kiss/src/kiss/CompilerTools.hx b/kiss/src/kiss/CompilerTools.hx index 4574b709..804f7a94 100644 --- a/kiss/src/kiss/CompilerTools.hx +++ b/kiss/src/kiss/CompilerTools.hx @@ -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, args:CompilationArgs):() -> String { + public static function compileToScript(exps:Array, 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 diff --git a/kiss/src/kiss/Helpers.hx b/kiss/src/kiss/Helpers.hx index f47775fe..b2130b65 100644 --- a/kiss/src/kiss/Helpers.hx +++ b/kiss/src/kiss/Helpers.hx @@ -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 { - 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]); } } diff --git a/kiss/src/kiss/Kiss.hx b/kiss/src/kiss/Kiss.hx index 1b5f3390..cd0b4ea9 100644 --- a/kiss/src/kiss/Kiss.hx +++ b/kiss/src/kiss/Kiss.hx @@ -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"), diff --git a/kiss/src/kiss/Prelude.hx b/kiss/src/kiss/Prelude.hx index 2c154abf..7a5c839e 100644 --- a/kiss/src/kiss/Prelude.hx +++ b/kiss/src/kiss/Prelude.hx @@ -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 { + #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 + } } diff --git a/kiss/src/test/cases/CompilerToolsTestCase.hx b/kiss/src/test/cases/CompilerToolsTestCase.hx index 5734b837..e7d037bb 100644 --- a/kiss/src/test/cases/CompilerToolsTestCase.hx +++ b/kiss/src/test/cases/CompilerToolsTestCase.hx @@ -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