Refactor scripting examples:

- split into script def and host part
- remove bad dependencies
- organize it in a way that should be typical for user scenarios
This commit is contained in:
Ilya Chernikov
2018-03-23 16:08:22 +01:00
parent 7cca1a72d3
commit 2c3a50e4b3
22 changed files with 97 additions and 102 deletions
@@ -0,0 +1 @@
import org.jetbrains.kotlin.gradle.dsl.Coroutines
@@ -0,0 +1,52 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.script.examples.jvm.simple.host
import org.jetbrains.kotlin.script.examples.jvm.simple.MyScript
import org.jetbrains.kotlin.script.util.*
import java.io.File
import kotlin.script.experimental.api.*
import kotlin.script.experimental.definitions.ScriptDefinitionFromAnnotatedBaseClass
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.*
import kotlin.script.experimental.jvmhost.impl.KJVMCompilerImpl
inline fun myJvmConfig(
from: HeterogeneousMap = HeterogeneousMap(),
crossinline body: JvmScriptCompileConfigurationParams.Builder.() -> Unit = {}
) = jvmConfigWithJavaHome(from) {
signature<MyScript>()
dependencies(scriptCompilationClasspathFromContext("script" /* script library jar name */))
body()
}
fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
val scriptCompiler = JvmScriptCompiler(KJVMCompilerImpl(), DummyCompiledJvmScriptCache())
val scriptDefinition = ScriptDefinitionFromAnnotatedBaseClass(MyScript::class)
val host = JvmBasicScriptingHost(
scriptDefinition.configurator,
scriptCompiler,
scriptDefinition.runner
)
return host.eval(myJvmConfig { add(scriptFile.toScriptSource().toConfigEntry()) }, ScriptEvaluationEnvironment())
}
fun main(vararg args: String) {
if (args.size != 1) {
println("usage: <app> <script file>")
} else {
val scriptFile = File(args[0])
println("Executing script $scriptFile")
val res = evalFile(scriptFile)
res.reports.forEach {
println(" : ${it.message}" + if (it.exception == null) "" else ": ${it.exception}")
}
}
}
@@ -0,0 +1,36 @@
/*
* Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.script.examples.jvm.simple.test
import org.jetbrains.kotlin.script.examples.jvm.simple.host.evalFile
import org.junit.Assert
import java.io.File
import org.junit.Test
import kotlin.script.experimental.api.ResultWithDiagnostics
class SimpleTest {
@Test
fun testSimple() {
val res = evalFile(File("testData/hello.kts"))
Assert.assertTrue(
"test failed:\n ${res.reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception}" }}",
res is ResultWithDiagnostics.Success
)
}
@Test
fun testError() {
val res = evalFile(File("testData/error.kts"))
Assert.assertTrue(
"test failed - expecting a failure with the message \"Unresolved reference: abracadabra\" but received " +
(if (res is ResultWithDiagnostics.Failure) "failure" else "success") +
":\n ${res.reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception}" }}",
res is ResultWithDiagnostics.Failure && res.reports.any { it.message.contains("Unresolved reference: abracadabra") })
}
}
@@ -0,0 +1,3 @@
abracadabra("Hello, World!")
@@ -0,0 +1,3 @@
println("Hello, World!")