Add embeddable version of the jvm scripting host

#KT-27382 fixed
This commit is contained in:
Ilya Chernikov
2018-11-06 18:55:10 +03:00
parent d64ca8a8f9
commit 9c51f521a9
6 changed files with 142 additions and 0 deletions
@@ -0,0 +1,21 @@
plugins {
kotlin("jvm")
}
dependencies {
compile(project(":examples:scripting-jvm-simple-script"))
compileOnly(project(":kotlin-scripting-jvm-host"))
compile(project(":kotlin-script-util"))
testRuntimeOnly(projectRuntimeJar(":kotlin-compiler-embeddable"))
testRuntimeOnly(project(":kotlin-scripting-jvm-host-embeddable"))
testRuntimeOnly(project(":kotlin-reflect"))
testRuntimeOnly(intellijDep()) { includeJars("guava", rootProject = rootProject) }
testCompile(commonDep("junit"))
}
sourceSets {
"main" { projectDefault() }
"test" { projectDefault() }
}
@@ -0,0 +1,44 @@
/*
* 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.embeddable.host
import org.jetbrains.kotlin.script.examples.jvm.simple.SimpleScript
import java.io.File
import kotlin.script.experimental.api.EvaluationResult
import kotlin.script.experimental.api.ResultWithDiagnostics
import kotlin.script.experimental.host.toScriptSource
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
import kotlin.script.experimental.jvm.jvm
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<SimpleScript> {
jvm {
dependenciesFromCurrentContext(
"scripting-jvm-simple-script", /* script library jar name */
"guava",
wholeClasspath = true
)
}
}
return BasicJvmScriptingHost().eval(scriptFile.toScriptSource(), compilationConfiguration, null)
}
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,26 @@
/*
* 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.embeddable.test
import org.jetbrains.kotlin.script.examples.jvm.embeddable.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() {
// see comments in the script file
val res = evalFile(File("testData/useGuava.simplescript.kts"))
Assert.assertTrue(
"test failed:\n ${res.reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception}" }}",
res is ResultWithDiagnostics.Success
)
}
}
@@ -0,0 +1,11 @@
// assuming to be executed on proguarded compiler in which `newArrayList` is present, but `asList` ist removed
// first pulling `newArrayList`, so with non-embeddable (not shaded) host/compiler the class `Lists` will be pulled from
// the guava embedded into compiler, then using `asList`, which should not be present in this version of the class due to proguarding
// So, the compilation should only succeed if shaded compiler is used and `Lists` are loaded from the non-embedded guava jar
val arr = com.google.common.collect.Lists.newArrayList<Int>()
val lst = listOf("Hello", "Guava")
val glist = com.google.common.collect.Lists.asList(lst.first(), lst[1], emptyArray())
println(glist.joinToString(", ", "", "!"))