forked from Kotlin/kotlin-script-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add script definition with dynamic dependencies downloading example
- Loading branch information
Showing
9 changed files
with
204 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
val kotlinVersion: String by rootProject.extra | ||
|
||
dependencies { | ||
implementation(project(":jvm:basic:jvm-maven-deps:script")) | ||
implementation("org.jetbrains.kotlin:kotlin-scripting-jvm-host:$kotlinVersion") | ||
testImplementation("junit:junit:4.12") | ||
} | ||
|
36 changes: 36 additions & 0 deletions
36
.../host/src/main/kotlin/org/jetbrains/kotlin/script/examples/jvm/resolve/maven/host/host.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* 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.ScriptWithMavenDeps | ||
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.jvmhost.BasicJvmScriptingHost | ||
import kotlin.script.experimental.jvmhost.createJvmCompilationConfigurationFromTemplate | ||
|
||
fun evalFile(scriptFile: File): ResultWithDiagnostics<EvaluationResult> { | ||
|
||
val compilationConfiguration = createJvmCompilationConfigurationFromTemplate<ScriptWithMavenDeps>() | ||
|
||
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}") | ||
} | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...rc/test/kotlin/org/jetbrains/kotlin/script/examples/jvm/resolve/maven/test/resolveTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2000-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* 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.scriptwithdeps.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.scriptwithdeps.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.scriptwithdeps.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") }) | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
jvm/basic/jvm-maven-deps/host/testData/hello-maven-resolve-error.scriptwithdeps.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
@file:DependsOn("abracadabra") | ||
|
||
println("Hello, World!") | ||
|
7 changes: 7 additions & 0 deletions
7
jvm/basic/jvm-maven-deps/host/testData/hello-maven-resolve-junit.scriptwithdeps.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
|
||
@file:DependsOn("junit:junit:4.11") | ||
|
||
org.junit.Assert.assertTrue(true) | ||
|
||
println("Hello, World!") | ||
|
5 changes: 5 additions & 0 deletions
5
jvm/basic/jvm-maven-deps/host/testData/hello-unresolved-junit.scriptwithdeps.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
org.junit.Assert.assertTrue(true) | ||
|
||
println("Hello, World!") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
|
||
plugins { | ||
kotlin("jvm") | ||
} | ||
|
||
val kotlinVersion: String by rootProject.extra | ||
|
||
dependencies { | ||
implementation("org.jetbrains.kotlin:kotlin-scripting-jvm:$kotlinVersion") | ||
implementation("org.jetbrains.kotlin:kotlin-script-util:$kotlinVersion") | ||
runtimeOnly("com.jcabi:jcabi-aether:0.10.1") | ||
runtimeOnly("org.sonatype.aether:aether-api:1.13.1") | ||
runtimeOnly("org.apache.maven:maven-core:3.0.3") | ||
} |
75 changes: 75 additions & 0 deletions
75
...cript/src/main/kotlin/org/jetbrains/kotlin/script/examples/jvm/resolve/maven/scriptDef.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2010-2018 JetBrains s.r.o. and Kotlin Programming Language contributors. | ||
* 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 | ||
|
||
import org.jetbrains.kotlin.script.util.DependsOn | ||
import org.jetbrains.kotlin.script.util.FilesAndMavenResolver | ||
import org.jetbrains.kotlin.script.util.Repository | ||
import java.io.File | ||
import kotlin.script.dependencies.ScriptContents | ||
import kotlin.script.dependencies.ScriptDependenciesResolver | ||
import kotlin.script.experimental.annotations.KotlinScript | ||
import kotlin.script.experimental.api.* | ||
import kotlin.script.experimental.jvm.JvmDependency | ||
import kotlin.script.experimental.jvm.compat.mapLegacyDiagnosticSeverity | ||
import kotlin.script.experimental.jvm.compat.mapLegacyScriptPosition | ||
import kotlin.script.experimental.jvm.dependenciesFromCurrentContext | ||
import kotlin.script.experimental.jvm.jvm | ||
import kotlin.script.experimental.jvm.withUpdatedClasspath | ||
|
||
@KotlinScript( | ||
fileExtension = "scriptwithdeps.kts", | ||
compilationConfiguration = ScriptWithMavenDepsConfiguration::class | ||
) | ||
abstract class ScriptWithMavenDeps | ||
|
||
object ScriptWithMavenDepsConfiguration : ScriptCompilationConfiguration( | ||
{ | ||
defaultImports(DependsOn::class, Repository::class) | ||
jvm { | ||
dependenciesFromCurrentContext( | ||
"script", // script library jar name | ||
"kotlin-script-util" // DependsOn annotation is taken from script-util | ||
) | ||
} | ||
refineConfiguration { | ||
onAnnotations(DependsOn::class, Repository::class, handler = ::configureMavenDepsOnAnnotations) | ||
} | ||
} | ||
) | ||
|
||
private val resolver = FilesAndMavenResolver() | ||
|
||
fun configureMavenDepsOnAnnotations(context: ScriptConfigurationRefinementContext): ResultWithDiagnostics<ScriptCompilationConfiguration> { | ||
val annotations = context.collectedData?.get(ScriptCollectedData.foundAnnotations)?.takeIf { it.isNotEmpty() } | ||
?: return context.compilationConfiguration.asSuccess() | ||
val scriptContents = object : ScriptContents { | ||
override val annotations: Iterable<Annotation> = annotations | ||
override val file: File? = null | ||
override val text: CharSequence? = null | ||
} | ||
val diagnostics = arrayListOf<ScriptDiagnostic>() | ||
fun report(severity: ScriptDependenciesResolver.ReportSeverity, message: String, position: ScriptContents.Position?) { | ||
diagnostics.add( | ||
ScriptDiagnostic( | ||
message, | ||
mapLegacyDiagnosticSeverity(severity), | ||
context.script.locationId, | ||
mapLegacyScriptPosition(position) | ||
) | ||
) | ||
} | ||
return try { | ||
val newDepsFromResolver = resolver.resolve(scriptContents, emptyMap(), ::report, null).get() | ||
?: return context.compilationConfiguration.asSuccess(diagnostics) | ||
val resolvedClasspath = newDepsFromResolver.classpath.toList().takeIf { it.isNotEmpty() } | ||
?: return context.compilationConfiguration.asSuccess(diagnostics) | ||
context.compilationConfiguration.withUpdatedClasspath(resolvedClasspath).asSuccess(diagnostics) | ||
} catch (e: Throwable) { | ||
ResultWithDiagnostics.Failure(*diagnostics.toTypedArray(), e.asDiagnostics(path = context.script.locationId)) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters