diff --git a/.gitignore b/.gitignore index 2f9a57c..755c1c4 100644 --- a/.gitignore +++ b/.gitignore @@ -42,4 +42,8 @@ bin/ .vscode/ ### Mac OS ### -.DS_Store \ No newline at end of file +.DS_Store + +### QuickCode stuff ### +hello +hello.kt \ No newline at end of file diff --git a/README.md b/README.md index fda299a..1b7817c 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,12 @@ It's designed to be learned at a single glance. Use: When you evaluate/execute your template [Compose Hammer](https://plugins.jetbrains.com/plugin/21912-compose-hammer) will ask you to provide values for the defined variables. +### Usage + +``` +./gradlew run --args="samples/hello.kt.qc samples/hello_input.json" +``` + ## Syntax **Keywords:** diff --git a/samples/hello.kt.qc b/samples/hello.kt.qc new file mode 100644 index 0000000..4d438ce --- /dev/null +++ b/samples/hello.kt.qc @@ -0,0 +1,6 @@ +fun main() { + println("Hello, {{name}}!") +#if {{morning}} #then + println("Good morning :)") +#endif +} \ No newline at end of file diff --git a/samples/hello.qc b/samples/hello.qc deleted file mode 100644 index 07d50d8..0000000 --- a/samples/hello.qc +++ /dev/null @@ -1,4 +0,0 @@ -Hello, {{name}}! -#if {{wishNiceDay}} #then -Have a nice day :) -#endif \ No newline at end of file diff --git a/samples/hello_input.json b/samples/hello_input.json index 5ffbf31..a89d3d6 100644 --- a/samples/hello_input.json +++ b/samples/hello_input.json @@ -1,4 +1,4 @@ { - "name": "Iliyan", - "wishNiceDay": true + "name": "QuickCode", + "morning": true } \ No newline at end of file diff --git a/src/main/kotlin/com/ivy/quickcode/Main.kt b/src/main/kotlin/com/ivy/quickcode/Main.kt index 58763bf..ae992bf 100644 --- a/src/main/kotlin/com/ivy/quickcode/Main.kt +++ b/src/main/kotlin/com/ivy/quickcode/Main.kt @@ -7,13 +7,14 @@ import kotlinx.serialization.json.boolean import kotlinx.serialization.json.booleanOrNull import java.nio.file.Files import java.nio.file.Paths +import kotlin.io.path.name fun main(args: Array) { + // TODO: Extract this as a class, handle errors cases and test it if (args.size != 2) { println("Invalid arguments!") return } - val template = readFileContent(args[0]) val inputJson = readFileContent(args[1]) val rawInput: Map = Json.decodeFromString(inputJson) @@ -31,6 +32,10 @@ fun main(args: Array) { val compiler = QuickCodeCompiler() val result = compiler.execute(template, input) println("----------------") + produceOutputFile( + templatePath = args[0], + result = result, + ) println("----------------") println(result) } @@ -38,4 +43,15 @@ fun main(args: Array) { fun readFileContent(relativePath: String): String { val path = Paths.get(relativePath) return Files.readString(path) +} + +fun produceOutputFile(templatePath: String, result: String) { + val path = Paths.get(templatePath) + val fileName = path.fileName.name + val outputFilename = fileName.dropLast(3) + Files.writeString( + Paths.get(outputFilename), + result + ) + println("'$outputFilename' created.") } \ No newline at end of file