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() 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") @Suppress("NAME_SHADOWING")
var template = template var template = template
template = template.replace("\"", "\\\"") template = template.replace("\"", "\\\"")
@ -44,16 +44,18 @@ _result.toString()
val script = scriptPrefix + template + scriptSuffix val script = scriptPrefix + template + scriptSuffix
File("script.kts").apply { if (dumpGeneratedScript) {
if (!exists()) createNewFile() File("script.kts").apply {
writeText(script) if (!exists()) createNewFile()
writeText(script)
}
} }
return eval(script, data) as String return eval(script, data) as String
} }
fun render(template: File, data: Map<String, Any>): String { fun render(template: File, data: Map<String, Any>, dumpGeneratedScript: Boolean = false): String {
return render(template.readText(), data) return render(template.readText(), data, dumpGeneratedScript)
} }
internal fun eval(script: String, data: Map<String, Any> = mapOf()): Any? { 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>) { fun main(args: Array<String>) {
val res = EKT.render(File("template.ekt"), mapOf( val res = EKT.render(File("template.ekt"), mapOf(
"value" to 11 "value" to 11
)) ), dumpGeneratedScript = true)
File("result.txt").apply { File("result.txt").apply {
if (!exists()) createNewFile() if (!exists()) createNewFile()