Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run task #3

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ bin/
.vscode/

### Mac OS ###
.DS_Store
.DS_Store

### QuickCode stuff ###
hello
hello.kt
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:**
Expand Down
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -23,7 +25,7 @@ tasks.test {
}

kotlin {
jvmToolchain(8)
jvmToolchain(11)
}

dependencies {
Expand Down
7 changes: 5 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand All @@ -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",
Expand All @@ -39,4 +41,5 @@ testing = [
]

[plugins]
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlin = { id = "org.jetbrains.kotlin.jvm", version.ref = "kotlin" }
kotlinx-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", version.ref = "kotlin" }
6 changes: 6 additions & 0 deletions samples/hello.kt.qc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
fun main() {
println("Hello, {{name}}!")
#if {{morning}} #then
println("Good morning :)")
#endif
}
4 changes: 4 additions & 0 deletions samples/hello_input.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "QuickCode",
"morning": true
}
54 changes: 53 additions & 1 deletion src/main/kotlin/com/ivy/quickcode/Main.kt
Original file line number Diff line number Diff line change
@@ -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<String>) {
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<String, JsonPrimitive> = 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.")
}