Skip to content

Commit 99ed79a

Browse files
committed
1 parent 6e8c86b commit 99ed79a

File tree

8 files changed

+136
-0
lines changed

8 files changed

+136
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
plugins {
3+
kotlin("jvm")
4+
}
5+
6+
val kotlinVersion: String by rootProject.extra
7+
8+
dependencies {
9+
implementation(project(":jvm:basic:jvm-simple-script:script"))
10+
implementation("org.jetbrains.kotlin:kotlin-scripting-jvm-host:$kotlinVersion")
11+
testImplementation("junit:junit:4.12")
12+
}
13+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.script.examples.jvm.simple.host
7+
8+
import org.jetbrains.kotlin.script.examples.jvm.simple.SimpleScript
9+
import java.io.File
10+
import kotlin.script.experimental.api.EvaluationResult
11+
import kotlin.script.experimental.api.ResultWithDiagnostics
12+
import kotlin.script.experimental.host.toScriptSource
13+
import kotlin.script.experimental.jvm.dependenciesFromClassloader
14+
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext
15+
import kotlin.script.experimental.jvm.jvm
16+
import kotlin.script.experimental.jvm.updateClasspath
17+
import kotlin.script.experimental.jvmhost.BasicJvmScriptingHost
18+
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate
19+
20+
fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> {
21+
val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<SimpleScript> {
22+
jvm {
23+
// configure dependencies for compilation, they should contain at least the script base class and
24+
// its dependencise
25+
// variant 1: try to extract current classpath and take only a path to the specified "script.jar"
26+
dependenciesFromCurrentContext(
27+
"script" /* script library jar name (exact or without a version) */
28+
)
29+
// variant 2: try to extract current classpath and use it for the compilation without filtering
30+
// dependenciesFromCurrentContext(wholeClasspath = true)
31+
// variant 3: try to extract a classpath from a particular classloader (or Thread.contextClassLoader by default)
32+
// filtering as in the variat 1 is supported too
33+
// dependenciesFromClassloader(classLoader = SimpleScript::class.java.classLoader, wholeClasspath = true)
34+
// variant 4: explicit classpath
35+
// updateClasspath(listOf(File("/path/to/jar")))
36+
}
37+
}
38+
39+
return BasicJvmScriptingHost().eval(scriptFile.toScriptSource(), compilationConfiguration, null)
40+
}
41+
42+
fun main(vararg args: String) {
43+
if (args.size != 1) {
44+
println("usage: <app> <script file>")
45+
} else {
46+
val scriptFile = File(args[0])
47+
println("Executing script $scriptFile")
48+
49+
val res = evalFile(scriptFile)
50+
51+
res.reports.forEach {
52+
println(" : ${it.message}" + if (it.exception == null) "" else ": ${it.exception}")
53+
}
54+
}
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2000-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.script.examples.jvm.simple.test
7+
8+
import org.jetbrains.kotlin.script.examples.jvm.simple.host.evalFile
9+
import org.junit.Assert
10+
import java.io.File
11+
import org.junit.Test
12+
import kotlin.script.experimental.api.ResultWithDiagnostics
13+
14+
class SimpleTest {
15+
16+
@Test
17+
fun testSimple() {
18+
val res = evalFile(File("testData/hello.simplescript.kts"))
19+
20+
Assert.assertTrue(
21+
"test failed:\n ${res.reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception}" }}",
22+
res is ResultWithDiagnostics.Success
23+
)
24+
}
25+
26+
@Test
27+
fun testError() {
28+
val res = evalFile(File("testData/error.simplescript.kts"))
29+
30+
Assert.assertTrue(
31+
"test failed - expecting a failure with the message \"Unresolved reference: abracadabra\" but received " +
32+
(if (res is ResultWithDiagnostics.Failure) "failure" else "success") +
33+
":\n ${res.reports.joinToString("\n ") { it.message + if (it.exception == null) "" else ": ${it.exception}" }}",
34+
res is ResultWithDiagnostics.Failure && res.reports.any { it.message.contains("Unresolved reference: abracadabra") })
35+
}
36+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
abracadabra("Hello, World!")
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
println("Hello, World!")
3+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
plugins {
3+
kotlin("jvm")
4+
}
5+
6+
val kotlinVersion: String by rootProject.extra
7+
8+
dependencies {
9+
implementation("org.jetbrains.kotlin:kotlin-scripting-jvm:$kotlinVersion")
10+
}
11+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.script.examples.jvm.simple
7+
8+
import kotlin.script.experimental.annotations.KotlinScript
9+
10+
@KotlinScript(fileExtension = "simplescript.kts")
11+
abstract class SimpleScript

settings.gradle.kts

+4
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,7 @@ pluginManagement {
55
maven { url = uri("https://dl.bintray.com/kotlin/kotlin-eap") }
66
}
77
}
8+
9+
include("jvm:basic:jvm-simple-script:script")
10+
include("jvm:basic:jvm-simple-script:host")
11+

0 commit comments

Comments
 (0)