Add dependency scopes option for scripting resolvers
This commit is contained in:
+7
-1
@@ -21,6 +21,7 @@ import kotlin.script.experimental.dependencies.impl.makeResolveFailureResult
|
||||
import kotlin.script.experimental.dependencies.impl.toRepositoryUrlOrNull
|
||||
import kotlin.script.experimental.dependencies.maven.impl.AetherResolveSession
|
||||
import kotlin.script.experimental.dependencies.maven.impl.mavenCentral
|
||||
import kotlin.script.experimental.dependencies.impl.dependencyScopes
|
||||
|
||||
|
||||
class MavenRepositoryCoordinates(
|
||||
@@ -62,7 +63,12 @@ class MavenDependenciesResolver : ExternalDependenciesResolver {
|
||||
val artifactId = artifactCoordinates.toMavenArtifact()!!
|
||||
|
||||
try {
|
||||
val deps = AetherResolveSession(localRepo, remoteRepositories()).resolve(artifactId, JavaScopes.RUNTIME)
|
||||
val dependencyScopes = options.dependencyScopes ?: listOf(JavaScopes.COMPILE, JavaScopes.RUNTIME)
|
||||
val deps = AetherResolveSession(
|
||||
localRepo, remoteRepositories()
|
||||
).resolve(
|
||||
artifactId, dependencyScopes.joinToString(",")
|
||||
)
|
||||
if (deps != null)
|
||||
return ResultWithDiagnostics.Success(deps.map { it.file })
|
||||
} catch (e: DependencyResolutionException) {
|
||||
|
||||
+36
-5
@@ -14,16 +14,21 @@ import kotlin.reflect.full.primaryConstructor
|
||||
import kotlin.script.experimental.dependencies.maven.MavenDependenciesResolver
|
||||
import kotlin.script.experimental.api.ResultWithDiagnostics
|
||||
import kotlin.script.experimental.api.valueOrThrow
|
||||
import kotlin.script.experimental.dependencies.DependsOn
|
||||
import kotlin.script.experimental.dependencies.Repository
|
||||
import kotlin.script.experimental.dependencies.resolveFromAnnotations
|
||||
import kotlin.script.experimental.dependencies.*
|
||||
import kotlin.script.experimental.dependencies.impl.DependenciesResolverOptionsName
|
||||
import kotlin.script.experimental.dependencies.impl.makeExternalDependenciesResolverOptions
|
||||
import kotlin.script.experimental.dependencies.impl.set
|
||||
|
||||
@ExperimentalContracts
|
||||
class MavenResolverTest : ResolversTestBase() {
|
||||
|
||||
fun resolveAndCheck(coordinates: String, checkBody: (Iterable<File>) -> Boolean = { true }) {
|
||||
fun resolveAndCheck(
|
||||
coordinates: String,
|
||||
options: ExternalDependenciesResolver.Options = ExternalDependenciesResolver.Options.Empty,
|
||||
checkBody: (Iterable<File>) -> Boolean = { true }
|
||||
) {
|
||||
val resolver = MavenDependenciesResolver()
|
||||
val result = runBlocking { resolver.resolve(coordinates) }
|
||||
val result = runBlocking { resolver.resolve(coordinates, options) }
|
||||
if (result is ResultWithDiagnostics.Failure) {
|
||||
Assert.fail(result.reports.joinToString("\n") { it.exception?.toString() ?: it.message })
|
||||
}
|
||||
@@ -40,6 +45,32 @@ class MavenResolverTest : ResolversTestBase() {
|
||||
}
|
||||
}
|
||||
|
||||
@ExperimentalStdlibApi
|
||||
fun testResolveWithRuntime() {
|
||||
val compileOnly = "compile"
|
||||
val compileRuntime = "compile,runtime"
|
||||
val resolvedFilesCount = mutableMapOf<String, Int>()
|
||||
|
||||
listOf(compileOnly, compileRuntime).forEach { scopes ->
|
||||
resolveAndCheck(
|
||||
"org.uberfire:uberfire-io:7.39.0.Final",
|
||||
makeExternalDependenciesResolverOptions(buildMap {
|
||||
this[DependenciesResolverOptionsName.SCOPE] = scopes
|
||||
})
|
||||
) { files ->
|
||||
resolvedFilesCount[scopes] = files.count()
|
||||
files.any { it.name.startsWith("uberfire-commons") }
|
||||
}
|
||||
}
|
||||
|
||||
val compileOnlyCount = resolvedFilesCount[compileOnly]!!
|
||||
val compileRuntimeCount = resolvedFilesCount[compileRuntime]!!
|
||||
assertTrue(
|
||||
"Compile only ($compileOnlyCount) dependencies count should be less than compile/runtime ($compileRuntimeCount) one",
|
||||
compileOnlyCount < compileRuntimeCount
|
||||
)
|
||||
}
|
||||
|
||||
fun testResolveVersionsRange() {
|
||||
resolveAndCheck("org.jetbrains.kotlin:kotlin-annotations-jvm:(1.3.40,1.3.60)")
|
||||
}
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ import kotlin.script.experimental.api.*
|
||||
import kotlin.script.experimental.dependencies.ExternalDependenciesResolver
|
||||
|
||||
private val nameRegex = Regex("^[^\\S\\r\\n]*([a-zA-Z][a-zA-Z0-9-_]*)\\b")
|
||||
private val valueRegex = Regex("^[^\\S\\r\\n]*([a-zA-Z0-9-_]+)\\b")
|
||||
private val valueRegex = Regex("^[^\\S\\r\\n]*([a-zA-Z0-9-_,]+)\\b")
|
||||
private val equalsRegex = Regex("^[^\\S\\r\\n]*=")
|
||||
|
||||
/**
|
||||
|
||||
+35
@@ -0,0 +1,35 @@
|
||||
/*
|
||||
* Copyright 2010-2020 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 kotlin.script.experimental.dependencies.impl
|
||||
|
||||
import kotlin.script.experimental.dependencies.ExternalDependenciesResolver
|
||||
|
||||
fun ExternalDependenciesResolver.Options.value(name: DependenciesResolverOptionsName) =
|
||||
value(name.key)
|
||||
|
||||
fun ExternalDependenciesResolver.Options.flag(name: DependenciesResolverOptionsName) =
|
||||
flag(name.key)
|
||||
|
||||
operator fun MutableMap<String, String>.set(key: DependenciesResolverOptionsName, value: String) {
|
||||
put(key.key, value)
|
||||
}
|
||||
|
||||
/**
|
||||
* These names are for convenience only.
|
||||
* They don't have to be implemented in all resolvers.
|
||||
*/
|
||||
enum class DependenciesResolverOptionsName(optionName: String? = null) {
|
||||
TRANSITIVE,
|
||||
SCOPE;
|
||||
|
||||
val key = optionName ?: name.toLowerCase()
|
||||
}
|
||||
|
||||
val ExternalDependenciesResolver.Options.transitive
|
||||
get() = flag(DependenciesResolverOptionsName.TRANSITIVE)
|
||||
|
||||
val ExternalDependenciesResolver.Options.dependencyScopes
|
||||
get() = value(DependenciesResolverOptionsName.SCOPE)?.split(",")
|
||||
Reference in New Issue
Block a user