From da22c31fe8b952414a541b2d2ce91d2db22abc78 Mon Sep 17 00:00:00 2001 From: Nat Quayle Nelson Date: Sun, 13 Jun 2021 21:45:15 -0600 Subject: [PATCH] refactor Main.hx to allow more subcommands --- src/kiss/Main.hx | 31 ++++++++++++++++++++++++------- src/kiss/Prelude.hx | 4 ++-- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/kiss/Main.hx b/src/kiss/Main.hx index 9a79859..bf60eb7 100644 --- a/src/kiss/Main.hx +++ b/src/kiss/Main.hx @@ -15,15 +15,33 @@ class Main { macroMain(); } - // When called from the command-line, `kiss` converts its stdin input to Haxe expressions. - // with --all, it reads everything from stdin at once (for piping). Without --all, it acts as a repl, - // where \ at the end of a line signals that the expression is not complete - // TODO write tests for this - // TODO use this to implement runAtRuntime() for sys targets by running a haxe subprocess + // When called from the command-line, Kiss has various subcommands, some of which can only run in macro context static macro function macroMain():Expr { + var args = Sys.args(); + + // TODO `kiss run` subcommand with optional target option. With no target specified, keep trying targets until one exits with status 0 (side-effect danger) + + switch (args.shift()) { + case "convert": + convert(args); + case other: + // TODO show a helpful list of subcommands + Sys.println('$other is not a kiss subcommand'); + Sys.exit(1); + } + + return macro null; + } + + static function convert(args:Array) { + // `kiss convert` converts its stdin input to Haxe expressions. + // with --all, it reads everything from stdin at once (for piping). Without --all, it acts as a repl, + // where \ at the end of a line signals that the expression is not complete + // TODO write tests for this + // TODO use this to implement runAtRuntime() for sys targets by running a haxe subprocess + var k = Kiss.defaultKissState(); k.wrapListExps = false; - var args = Sys.args(); var pretty = args.indexOf("--pretty") != -1; k.hscript = args.indexOf("--hscript") != -1; @@ -67,6 +85,5 @@ class Main { } var line = ""; - return macro null; } } diff --git a/src/kiss/Prelude.hx b/src/kiss/Prelude.hx index e2b6e05..72e5866 100644 --- a/src/kiss/Prelude.hx +++ b/src/kiss/Prelude.hx @@ -314,7 +314,7 @@ class Prelude { */ public static function convertToHScript(kissStr:String):String { #if (!macro && hxnodejs) - var kissProcess = ChildProcess.spawnSync("haxelib", ["run", "kiss", "--all", "--hscript"], {input: '${kissStr}\n'}); + 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}'; @@ -323,7 +323,7 @@ class Prelude { return output.toString(); #elseif sys if (kissProcess == null) - kissProcess = new Process("haxelib", ["run", "kiss", "--hscript"]); + kissProcess = new Process("haxelib", ["run", "kiss", "convert", "--hscript"]); kissProcess.stdin.writeString('${kissStr.replace("\n", " ")}\n');