From f75d531bda7739e48aa120009321bb5576798fb9 Mon Sep 17 00:00:00 2001 From: iliyangermanov Date: Wed, 11 Oct 2023 23:29:39 +0300 Subject: [PATCH] Create a simple main --- build.gradle.kts | 4 ++- gradle/libs.versions.toml | 7 +++-- samples/hello.qc | 4 +++ samples/hello_input.json | 4 +++ src/main/kotlin/com/ivy/quickcode/Main.kt | 38 ++++++++++++++++++++++- 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 samples/hello.qc create mode 100644 samples/hello_input.json 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.qc b/samples/hello.qc new file mode 100644 index 0000000..07d50d8 --- /dev/null +++ b/samples/hello.qc @@ -0,0 +1,4 @@ +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 new file mode 100644 index 0000000..5ffbf31 --- /dev/null +++ b/samples/hello_input.json @@ -0,0 +1,4 @@ +{ + "name": "Iliyan", + "wishNiceDay": 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..58763bf 100644 --- a/src/main/kotlin/com/ivy/quickcode/Main.kt +++ b/src/main/kotlin/com/ivy/quickcode/Main.kt @@ -1,5 +1,41 @@ 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 + fun main(args: Array) { - println("QuickCode compiler TODO...") + 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("----------------") + println("----------------") + println(result) +} + +fun readFileContent(relativePath: String): String { + val path = Paths.get(relativePath) + return Files.readString(path) } \ No newline at end of file