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/build.gradle.kts b/build.gradle.kts index 5e8ae00..ad7cbc9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -1,10 +1,12 @@ plugins { application alias(libs.plugins.kotlin) + alias(libs.plugins.kotlinx.serialization) } application { mainClass = "com.ivy.quickcode.MainKt" + applicationName = "qc" } group = "com.ivy" @@ -23,7 +25,7 @@ tasks.test { } kotlin { - jvmToolchain(8) + jvmToolchain(11) } dependencies { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 6f12822..bff1555 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,6 +9,7 @@ kotest = "5.7.2" # Kotlin kotlin-stdlib = { module = "org.jetbrains.kotlin:kotlin-stdlib", version.ref = "kotlin" } kotlin-coroutines-core = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-core", version.ref = "kotlin-coroutines" } +kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version = "1.6.0" } # Arrow arrowkt-core = { module = "io.arrow-kt:arrow-core", version.ref = "arrow" } @@ -26,7 +27,8 @@ kotlin-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-te [bundles] kotlin = [ "kotlin-stdlib", - "kotlin-coroutines-core" + "kotlin-coroutines-core", + "kotlinx-serialization-json" ] testing = [ "mockk", @@ -39,4 +41,5 @@ testing = [ ] [plugins] -kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } \ No newline at end of file +kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" } +kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" } \ No newline at end of file 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_input.json b/samples/hello_input.json new file mode 100644 index 0000000..a89d3d6 --- /dev/null +++ b/samples/hello_input.json @@ -0,0 +1,4 @@ +{ + "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 0bff5f0..ae992bf 100644 --- a/src/main/kotlin/com/ivy/quickcode/Main.kt +++ b/src/main/kotlin/com/ivy/quickcode/Main.kt @@ -1,5 +1,57 @@ package com.ivy.quickcode +import com.ivy.quickcode.data.QCVariableValue +import kotlinx.serialization.json.Json +import kotlinx.serialization.json.JsonPrimitive +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) { - println("QuickCode compiler TODO...") + // 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) + val input = rawInput.map { (key, value) -> + key to when { + value.isString -> QCVariableValue.Str(value.content) + value.booleanOrNull != null -> QCVariableValue.Bool(value.boolean) + else -> error("Unsupported input type \"$key\"") + } + }.toMap() + + println("Input:") + println(input) + + val compiler = QuickCodeCompiler() + val result = compiler.execute(template, input) + println("----------------") + produceOutputFile( + templatePath = args[0], + result = result, + ) + println("----------------") + println(result) +} + +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