Guest User

Untitled

a guest
Jul 31st, 2019
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.34 KB | None | 0 0
  1. package com.example.demo
  2.  
  3. import java.io.File
  4. import kotlin.script.experimental.annotations.KotlinScript
  5. import kotlin.script.experimental.api.EvaluationResult
  6. import kotlin.script.experimental.api.ResultWithDiagnostics
  7. import kotlin.script.experimental.host.toScriptSource
  8. import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
  9. import kotlin.script.experimental.jvm.jvm
  10. import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
  11. import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
  12.  
  13. @KotlinScript(fileExtension = "script.kt")
  14. abstract class SimpleScript
  15.  
  16. val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<SimpleScript> {
  17.     jvm {
  18.         dependenciesFromCurrentContext(
  19.           wholeClasspath = true
  20.         )
  21.     }
  22. }
  23.  
  24. fun BasicJvmScriptingHost.evalScript(script: String): ResultWithDiagnostics<EvaluationResult> {
  25.     return eval(script.toScriptSource(), compilationConfiguration, null)
  26. }
  27.  
  28. fun main() {
  29.     println("Executing script...")
  30.     val host = BasicJvmScriptingHost()
  31.     val result = host.evalScript("""
  32.      println("hello World")
  33.    """.trimIndent())
  34.  
  35.     result.reports.forEach { report ->
  36.         println("[${report.severity}] ${report.message}" + (report.exception?.let { ": $it" } ?: ""))
  37.     }
  38.  
  39.     println("Executed script")
  40. }
Add Comment
Please, Sign In to add comment