Skip to content

Commit

Permalink
Add usage sample
Browse files Browse the repository at this point in the history
  • Loading branch information
ILIYANGERMANOV committed Oct 11, 2023
1 parent f75d531 commit 8b251d3
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 8 deletions.
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
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: 0 additions & 4 deletions samples/hello.qc

This file was deleted.

4 changes: 2 additions & 2 deletions samples/hello_input.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"name": "Iliyan",
"wishNiceDay": true
"name": "QuickCode",
"morning": true
}
18 changes: 17 additions & 1 deletion src/main/kotlin/com/ivy/quickcode/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>) {
// 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)
Expand All @@ -31,11 +32,26 @@ fun main(args: Array<String>) {
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.")
}

0 comments on commit 8b251d3

Please sign in to comment.