Switch example to the new maven resolving API
This commit is contained in:
+1
-1
@@ -42,6 +42,6 @@ class ResolveTest {
|
||||
"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") })
|
||||
res is ResultWithDiagnostics.Failure && res.reports.any { it.message.contains("File 'abracadabra' not found") })
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ plugins {
|
||||
|
||||
dependencies {
|
||||
compile(project(":kotlin-scripting-jvm"))
|
||||
compile(project(":kotlin-scripting-dependencies-maven"))
|
||||
compile(project(":kotlin-script-util"))
|
||||
runtime("com.jcabi:jcabi-aether:0.10.1")
|
||||
runtime("org.sonatype.aether:aether-api:1.13.1")
|
||||
|
||||
+27
-32
@@ -5,20 +5,18 @@
|
||||
|
||||
package org.jetbrains.kotlin.script.examples.jvm.resolve.maven
|
||||
|
||||
import kotlinx.coroutines.runBlocking
|
||||
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.dependencies.CompoundDependenciesResolver
|
||||
import kotlin.script.experimental.dependencies.FileSystemDependenciesResolver
|
||||
import kotlin.script.experimental.dependencies.maven.MavenDependenciesResolver
|
||||
import kotlin.script.experimental.dependencies.tryAddRepository
|
||||
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",
|
||||
@@ -41,36 +39,33 @@ object ScriptWithMavenDepsConfiguration : ScriptCompilationConfiguration(
|
||||
}
|
||||
)
|
||||
|
||||
private val resolver = FilesAndMavenResolver()
|
||||
private val resolver = CompoundDependenciesResolver(FileSystemDependenciesResolver(), MavenDependenciesResolver())
|
||||
|
||||
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
|
||||
annotations.forEach { annotation ->
|
||||
when (annotation) {
|
||||
is Repository -> {
|
||||
val repositoryCoordinates = with(annotation) { value.takeIf { it.isNotBlank() } ?: url }
|
||||
if (!resolver.tryAddRepository(repositoryCoordinates))
|
||||
return makeFailureResult("Unrecognized repository coordinates: $repositoryCoordinates")
|
||||
}
|
||||
is DependsOn -> {}
|
||||
else -> return makeFailureResult("Unknown annotation ${annotation.javaClass}")
|
||||
}
|
||||
}
|
||||
val diagnostics = arrayListOf<ScriptDiagnostic>()
|
||||
fun report(severity: ScriptDependenciesResolver.ReportSeverity, message: String, position: ScriptContents.Position?) {
|
||||
diagnostics.add(
|
||||
ScriptDiagnostic(
|
||||
ScriptDiagnostic.unspecifiedError,
|
||||
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))
|
||||
return annotations.filterIsInstance(DependsOn::class.java).flatMapSuccess { dep ->
|
||||
val artifactCoordinates =
|
||||
if (dep.value.isNotBlank()) dep.value
|
||||
else listOf(dep.groupId, dep.artifactId, dep.version).filter { it.isNotBlank() }.joinToString(":")
|
||||
runBlocking {
|
||||
resolver.resolve(artifactCoordinates)
|
||||
}
|
||||
}.onSuccess {
|
||||
context.compilationConfiguration.with {
|
||||
dependencies.append(JvmDependency(it))
|
||||
}.asSuccess()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user