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,48 @@
/*
* 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.resolve.maven.host
import org.jetbrains.kotlin.script.examples.jvm.resolve.maven.MyScriptWithMavenDeps
import org.jetbrains.kotlin.script.examples.jvm.resolve.maven.myJvmConfig
import java.io.File
import kotlin.script.experimental.api.EvaluationResult
import kotlin.script.experimental.api.ResultWithDiagnostics
import kotlin.script.experimental.api.ScriptEvaluationEnvironment
import kotlin.script.experimental.api.toConfigEntry
import kotlin.script.experimental.definitions.ScriptDefinitionFromAnnotatedBaseClass
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.DummyCompiledJvmScriptCache
import kotlin.script.experimental.jvm.JvmBasicScriptingHost
import kotlin.script.experimental.jvm.JvmScriptCompiler
import kotlin.script.experimental.jvmhost.impl.KJVMCompilerImpl
fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
val scriptCompiler = JvmScriptCompiler(KJVMCompilerImpl(), DummyCompiledJvmScriptCache())
val scriptDefinition = ScriptDefinitionFromAnnotatedBaseClass(MyScriptWithMavenDeps::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,47 @@
/*
* 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.resolve.maven.test
import org.jetbrains.kotlin.script.examples.jvm.resolve.maven.host.evalFile
import org.junit.Assert
import org.junit.Test
import java.io.File
import kotlin.script.experimental.api.ResultWithDiagnostics
class ResolveTest {
@Test
fun testResolveJunit() {
val res = evalFile(File("testData/hello-maven-resolve-junit.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 testUnresolvedJunit() {
val res = evalFile(File("testData/hello-unresolved-junit.kts"))
Assert.assertTrue(
"test failed - expecting a failure with the message \"Unresolved reference: junit\" 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: junit") })
}
@Test
fun testResolveError() {
val res = evalFile(File("testData/hello-maven-resolve-error.kts"))
Assert.assertTrue(
"test failed - expecting a failure with the message \"Unknown set of arguments to maven resolver: 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("Unknown set of arguments to maven resolver: abracadabra") })
}
}
@@ -0,0 +1,5 @@
@file:DependsOn("abracadabra")
println("Hello, World!")
@@ -0,0 +1,7 @@
@file:DependsOn("junit:junit:4.11")
org.junit.Assert.assertTrue(true)
println("Hello, World!")
@@ -0,0 +1,5 @@
org.junit.Assert.assertTrue(true)
println("Hello, World!")