diff --git a/src/kiss/CompilerTools.hx b/src/kiss/CompilerTools.hx new file mode 100644 index 0000000..7920af9 --- /dev/null +++ b/src/kiss/CompilerTools.hx @@ -0,0 +1,47 @@ +package kiss; + +#if macro +import kiss.Kiss; + +enum CompileLang { + JavaScript; + Python; +} + +typedef CompilationArgs = { + lang:CompileLang, + outputFolder:String, + // path to a folder where the script will be compiled + ?importHxFile:String, + // path to a file with haxe import statements in it + ?hxmlFile:String, + // path to a file with hxml args in it (SHOULD NOT specify target or main class) + ?langProjectFile:String, + // path to a package.json or requirements.txt file + ?mainHxFile:String, + // path to a haxe file defining the Main class +} + +/** + * Multi-purpose tools for compiling Kiss projects. + */ +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. + */ + public static function compileFileToScript(kissFile:String, args:CompilationArgs):() -> String { + var k = Kiss.defaultKissState(); + var beginExpsInFile = Kiss.load(kissFile, k, "", true); + return compileToScript([beginExpsInFile], args); + } + + /** + * Compile kiss expressions into a standalone script + * @return 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 { + return () -> ""; + } +} +#end diff --git a/src/kiss/Kiss.hx b/src/kiss/Kiss.hx index 4b673de..df44f4a 100644 --- a/src/kiss/Kiss.hx +++ b/src/kiss/Kiss.hx @@ -185,7 +185,7 @@ class Kiss { }); } - public static function load(kissFile:String, k:KissState, ?loadingDirectory:String):Null { + public static function load(kissFile:String, k:KissState, ?loadingDirectory:String, loadAllExps = false):Null { if (loadingDirectory == null) loadingDirectory = k.loadingDirectory; @@ -221,7 +221,9 @@ class Kiss { } return true; } - if (!isEmpty(expr)) { + // When calling from build(), we can't add all expressions to the (begin) returned by (load), because that will + // cause double-evaluation of field forms + if (loadAllExps || !isEmpty(expr)) { loadedExps.push(nextExp); } }); diff --git a/src/test/cases/CompilerToolsTestCase.hx b/src/test/cases/CompilerToolsTestCase.hx new file mode 100644 index 0000000..153e7e4 --- /dev/null +++ b/src/test/cases/CompilerToolsTestCase.hx @@ -0,0 +1,28 @@ +package test.cases; + +import utest.Assert; +import utest.Test; +import kiss.CompilerTools; +#if macro +import haxe.macro.Expr; +import haxe.macro.Context; +#end + +class CompilerToolsTestCase extends Test { + function testCompileHelloWorld() { + Assert.equals("Hello, world!", _testCompileHelloWorld()); + } + + static macro function _testCompileHelloWorld() { + var runHelloWorld = CompilerTools.compileFileToScript( + "kiss/template/src/template/Main.kiss", { + lang: JavaScript, + outputFolder: "bin/js", + }); + + return { + pos: Context.currentPos(), + expr: EConst(CString(runHelloWorld(), DoubleQuotes)) + }; + } +}