Add dumpGeneratedScript option

This commit is contained in:
Shadowfacts 2017-08-04 15:52:00 -04:00
parent 08c84c1e67
commit 6378d6cdc7
Signed by: shadowfacts
GPG Key ID: 94A5AB95422746E5
2 changed files with 9 additions and 7 deletions

View File

@ -26,7 +26,7 @@ _result.toString()
ScriptEngineManager()
}
fun render(template: String, data: Map<String, Any>): String {
fun render(template: String, data: Map<String, Any>, dumpGeneratedScript: Boolean = false): String {
@Suppress("NAME_SHADOWING")
var template = template
template = template.replace("\"", "\\\"")
@ -44,16 +44,18 @@ _result.toString()
val script = scriptPrefix + template + scriptSuffix
File("script.kts").apply {
if (!exists()) createNewFile()
writeText(script)
if (dumpGeneratedScript) {
File("script.kts").apply {
if (!exists()) createNewFile()
writeText(script)
}
}
return eval(script, data) as String
}
fun render(template: File, data: Map<String, Any>): String {
return render(template.readText(), data)
fun render(template: File, data: Map<String, Any>, dumpGeneratedScript: Boolean = false): String {
return render(template.readText(), data, dumpGeneratedScript)
}
internal fun eval(script: String, data: Map<String, Any> = mapOf()): Any? {

View File

@ -8,7 +8,7 @@ import java.io.File
fun main(args: Array<String>) {
val res = EKT.render(File("template.ekt"), mapOf(
"value" to 11
))
), dumpGeneratedScript = true)
File("result.txt").apply {
if (!exists()) createNewFile()