Add generic interfaces to dependency resolvers
This commit is contained in:
@@ -17,32 +17,33 @@ import org.apache.ivy.plugins.resolver.ChainResolver
|
|||||||
import org.apache.ivy.plugins.resolver.URLResolver
|
import org.apache.ivy.plugins.resolver.URLResolver
|
||||||
import org.apache.ivy.util.DefaultMessageLogger
|
import org.apache.ivy.util.DefaultMessageLogger
|
||||||
import org.apache.ivy.util.Message
|
import org.apache.ivy.util.Message
|
||||||
import org.jetbrains.kotlin.script.util.DependsOn
|
|
||||||
import org.jetbrains.kotlin.script.util.KotlinAnnotatedScriptDependenciesResolver
|
import org.jetbrains.kotlin.script.util.KotlinAnnotatedScriptDependenciesResolver
|
||||||
import org.jetbrains.kotlin.script.util.Repository
|
|
||||||
import org.jetbrains.kotlin.script.util.resolvers.DirectResolver
|
import org.jetbrains.kotlin.script.util.resolvers.DirectResolver
|
||||||
import org.jetbrains.kotlin.script.util.resolvers.Resolver
|
import org.jetbrains.kotlin.script.util.resolvers.experimental.GenericArtifactCoordinates
|
||||||
|
import org.jetbrains.kotlin.script.util.resolvers.experimental.GenericRepositoryCoordinates
|
||||||
|
import org.jetbrains.kotlin.script.util.resolvers.experimental.GenericRepositoryWithBridge
|
||||||
|
import org.jetbrains.kotlin.script.util.resolvers.experimental.MavenArtifactCoordinates
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.net.MalformedURLException
|
import java.net.MalformedURLException
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
|
||||||
class IvyResolver : Resolver {
|
class IvyResolver : GenericRepositoryWithBridge {
|
||||||
|
|
||||||
private fun String.isValidParam() = isNotBlank()
|
private fun String?.isValidParam() = this?.isNotBlank() ?: false
|
||||||
|
|
||||||
override fun tryResolve(dependsOn: DependsOn): Iterable<File>? {
|
override fun tryResolve(artifactCoordinates: GenericArtifactCoordinates): Iterable<File>? = with (artifactCoordinates) {
|
||||||
val artifactId = when {
|
val artifactId =
|
||||||
dependsOn.groupId.isValidParam() || dependsOn.artifactId.isValidParam() -> {
|
if (this is MavenArtifactCoordinates && (groupId.isValidParam() || artifactId.isValidParam())) {
|
||||||
listOf(dependsOn.groupId, dependsOn.artifactId, dependsOn.version)
|
listOf(groupId.orEmpty(), artifactId.orEmpty(), version.orEmpty())
|
||||||
|
} else {
|
||||||
|
val stringCoordinates = string
|
||||||
|
if (stringCoordinates.isValidParam() && stringCoordinates.count { it == ':' } == 2) {
|
||||||
|
stringCoordinates.split(':')
|
||||||
|
} else {
|
||||||
|
error("Unknown set of arguments to maven resolver: $stringCoordinates")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
dependsOn.value.isValidParam() && dependsOn.value.count { it == ':' } == 2 -> {
|
resolveArtifact(artifactId)
|
||||||
dependsOn.value.split(':')
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
error("Unknown set of arguments to maven resolver: ${dependsOn.value}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return resolveArtifact(artifactId)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private val ivyResolvers = arrayListOf<URLResolver>()
|
private val ivyResolvers = arrayListOf<URLResolver>()
|
||||||
@@ -101,17 +102,19 @@ class IvyResolver : Resolver {
|
|||||||
return report.allArtifactsReports.map { it.localFile }
|
return report.allArtifactsReports.map { it.localFile }
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun tryAddRepo(annotation: Repository): Boolean {
|
override fun tryAddRepository(repositoryCoordinates: GenericRepositoryCoordinates): Boolean {
|
||||||
val urlStr = annotation.url.takeIf { it.isValidParam() } ?: annotation.value.takeIf { it.isValidParam() } ?: return false
|
val url = repositoryCoordinates.url
|
||||||
val url = urlStr.toRepositoryUrlOrNull() ?: return false
|
if (url != null) {
|
||||||
ivyResolvers.add(
|
ivyResolvers.add(
|
||||||
URLResolver().apply {
|
URLResolver().apply {
|
||||||
isM2compatible = true
|
isM2compatible = true
|
||||||
name = annotation.id.takeIf { it.isValidParam() } ?: url.host
|
name = repositoryCoordinates.name.takeIf { it.isValidParam() } ?: url.host
|
||||||
addArtifactPattern("${url.toString().let { if (it.endsWith('/')) it else "$it/" }}$DEFAULT_ARTIFACT_PATTERN")
|
addArtifactPattern("${url.toString().let { if (it.endsWith('/')) it else "$it/" }}$DEFAULT_ARTIFACT_PATTERN")
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
return true
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
@@ -123,12 +126,5 @@ class IvyResolver : Resolver {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun String.toRepositoryUrlOrNull(): URL? =
|
|
||||||
try {
|
|
||||||
URL(this)
|
|
||||||
} catch (_: MalformedURLException) {
|
|
||||||
null
|
|
||||||
}
|
|
||||||
|
|
||||||
class FilesAndIvyResolver :
|
class FilesAndIvyResolver :
|
||||||
KotlinAnnotatedScriptDependenciesResolver(emptyList(), arrayListOf(DirectResolver(), IvyResolver()).asIterable())
|
KotlinAnnotatedScriptDependenciesResolver(emptyList(), arrayListOf(DirectResolver(), IvyResolver()).asIterable())
|
||||||
|
|||||||
+20
-21
@@ -16,25 +16,21 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.script.util.resolvers
|
package org.jetbrains.kotlin.script.util.resolvers
|
||||||
|
|
||||||
import org.jetbrains.kotlin.script.util.DependsOn
|
|
||||||
import org.jetbrains.kotlin.script.util.Repository
|
import org.jetbrains.kotlin.script.util.Repository
|
||||||
|
import org.jetbrains.kotlin.script.util.resolvers.experimental.*
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.net.MalformedURLException
|
import java.net.MalformedURLException
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
|
||||||
interface Resolver {
|
class DirectResolver : GenericRepositoryWithBridge {
|
||||||
fun tryResolve(dependsOn: DependsOn): Iterable<File>?
|
override fun tryResolve(artifactCoordinates: GenericArtifactCoordinates): Iterable<File>? =
|
||||||
fun tryAddRepo(annotation: Repository): Boolean
|
artifactCoordinates.string.takeUnless(String::isBlank)
|
||||||
|
?.let(::File)?.takeIf { it.exists() && (it.isFile || it.isDirectory) }?.let { listOf(it) }
|
||||||
|
|
||||||
|
override fun tryAddRepository(repositoryCoordinates: GenericRepositoryCoordinates): Boolean = false
|
||||||
}
|
}
|
||||||
|
|
||||||
class DirectResolver : Resolver {
|
class FlatLibDirectoryResolver(vararg paths: File) : GenericRepositoryWithBridge {
|
||||||
override fun tryResolve(dependsOn: DependsOn): Iterable<File>? =
|
|
||||||
dependsOn.value.takeUnless(String::isBlank)?.let(::File)?.takeIf { it.exists() && (it.isFile || it.isDirectory) }?.let { listOf(it) }
|
|
||||||
|
|
||||||
override fun tryAddRepo(annotation: Repository): Boolean = false
|
|
||||||
}
|
|
||||||
|
|
||||||
class FlatLibDirectoryResolver(vararg paths: File) : Resolver {
|
|
||||||
|
|
||||||
private val localRepos = arrayListOf<File>()
|
private val localRepos = arrayListOf<File>()
|
||||||
|
|
||||||
@@ -45,10 +41,10 @@ class FlatLibDirectoryResolver(vararg paths: File) : Resolver {
|
|||||||
localRepos.addAll(paths)
|
localRepos.addAll(paths)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun tryResolve(dependsOn: DependsOn): Iterable<File>? {
|
override fun tryResolve(artifactCoordinates: GenericArtifactCoordinates): Iterable<File>? {
|
||||||
for (path in localRepos) {
|
for (path in localRepos) {
|
||||||
// TODO: add coordinates and wildcard matching
|
// TODO: add coordinates and wildcard matching
|
||||||
val res = dependsOn.value.takeUnless(String::isBlank)
|
val res = artifactCoordinates.string.takeUnless(String::isBlank)
|
||||||
?.let { File(path, it) }
|
?.let { File(path, it) }
|
||||||
?.takeIf { it.exists() && (it.isFile || it.isDirectory) }
|
?.takeIf { it.exists() && (it.isFile || it.isDirectory) }
|
||||||
if (res != null) return listOf(res)
|
if (res != null) return listOf(res)
|
||||||
@@ -56,18 +52,21 @@ class FlatLibDirectoryResolver(vararg paths: File) : Resolver {
|
|||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun tryAddRepo(annotation: Repository): Boolean {
|
override fun tryAddRepository(repositoryCoordinates: GenericRepositoryCoordinates): Boolean {
|
||||||
val urlStr = annotation.url.takeIf { it.isNotBlank() } ?: annotation.value.takeIf { it.isNotBlank() } ?: return false
|
val repoDir = repositoryCoordinates.file ?: return false
|
||||||
val dirFromUrl = urlStr.toRepositoryUrlOrNull()?.takeIf { it.protocol == "file" }?.path
|
|
||||||
val repoDir = (dirFromUrl ?: urlStr).toRepositoryFileOrNull() ?: return false
|
|
||||||
localRepos.add(repoDir)
|
localRepos.add(repoDir)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun tryCreate(annotation: Repository): FlatLibDirectoryResolver? =
|
fun tryCreate(annotation: Repository): FlatLibDirectoryResolver? = tryCreate(
|
||||||
annotation.value.takeUnless(String::isBlank)?.let(::File)?.takeIf { it.exists() && it.isDirectory }
|
BasicRepositoryCoordinates(
|
||||||
?.let { FlatLibDirectoryResolver(it) }
|
annotation.url.takeUnless(String::isBlank) ?: annotation.value, annotation.id.takeUnless(String::isBlank)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
fun tryCreate(repositoryCoordinates: GenericRepositoryCoordinates): FlatLibDirectoryResolver? =
|
||||||
|
repositoryCoordinates.file?.let { FlatLibDirectoryResolver(it) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+67
@@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
* 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.util.resolvers.experimental
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.script.util.DependsOn
|
||||||
|
import org.jetbrains.kotlin.script.util.Repository
|
||||||
|
import org.jetbrains.kotlin.script.util.resolvers.Resolver
|
||||||
|
import org.jetbrains.kotlin.script.util.resolvers.toRepositoryFileOrNull
|
||||||
|
import org.jetbrains.kotlin.script.util.resolvers.toRepositoryUrlOrNull
|
||||||
|
import java.io.File
|
||||||
|
import java.net.URL
|
||||||
|
|
||||||
|
interface GenericArtifactCoordinates {
|
||||||
|
val string: String
|
||||||
|
}
|
||||||
|
|
||||||
|
interface GenericRepositoryCoordinates {
|
||||||
|
val string: String
|
||||||
|
val name: String? get() = null
|
||||||
|
val url: URL? get() = string.toRepositoryUrlOrNull()
|
||||||
|
val file: File? get() = (url?.takeIf { it.protocol == "file" }?.path ?: string).toRepositoryFileOrNull()
|
||||||
|
}
|
||||||
|
|
||||||
|
interface GenericResolver {
|
||||||
|
fun tryResolve(artifactCoordinates: GenericArtifactCoordinates): Iterable<File>?
|
||||||
|
fun tryAddRepository(repositoryCoordinates: GenericRepositoryCoordinates): Boolean
|
||||||
|
|
||||||
|
fun tryResolve(artifactCoordinates: String): Iterable<File>? = tryResolve(BasicArtifactCoordinates(artifactCoordinates))
|
||||||
|
|
||||||
|
fun tryAddRepository(repositoryCoordinates: String, id: String? = null): Boolean =
|
||||||
|
tryAddRepository(BasicRepositoryCoordinates(repositoryCoordinates, id))
|
||||||
|
}
|
||||||
|
|
||||||
|
open class BasicArtifactCoordinates(override val string: String) : GenericArtifactCoordinates
|
||||||
|
|
||||||
|
open class BasicRepositoryCoordinates(override val string: String, override val name: String? = null) : GenericRepositoryCoordinates
|
||||||
|
|
||||||
|
interface GenericRepositoryWithBridge : GenericResolver, Resolver {
|
||||||
|
override fun tryResolve(dependsOn: DependsOn): Iterable<File>? =
|
||||||
|
tryResolve(
|
||||||
|
with(dependsOn) {
|
||||||
|
MavenArtifactCoordinates(value, groupId, artifactId, version)
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
override fun tryAddRepo(annotation: Repository): Boolean =
|
||||||
|
with(annotation) {
|
||||||
|
tryAddRepository(
|
||||||
|
value.takeIf { it.isNotBlank() } ?: url,
|
||||||
|
id.takeIf { it.isNotBlank() }
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
open class MavenArtifactCoordinates(
|
||||||
|
val value: String?,
|
||||||
|
val groupId: String?,
|
||||||
|
val artifactId: String?,
|
||||||
|
val version: String?
|
||||||
|
) : GenericArtifactCoordinates {
|
||||||
|
override val string: String
|
||||||
|
get() = value.takeIf { it?.isNotBlank() ?: false }
|
||||||
|
?: listOf(groupId, artifactId, version).filter { it?.isNotBlank() ?: false }.joinToString(":")
|
||||||
|
}
|
||||||
+36
-25
@@ -20,7 +20,10 @@ package org.jetbrains.kotlin.script.util.resolvers
|
|||||||
|
|
||||||
import com.jcabi.aether.Aether
|
import com.jcabi.aether.Aether
|
||||||
import org.jetbrains.kotlin.script.util.DependsOn
|
import org.jetbrains.kotlin.script.util.DependsOn
|
||||||
import org.jetbrains.kotlin.script.util.Repository
|
import org.jetbrains.kotlin.script.util.resolvers.experimental.GenericArtifactCoordinates
|
||||||
|
import org.jetbrains.kotlin.script.util.resolvers.experimental.GenericRepositoryCoordinates
|
||||||
|
import org.jetbrains.kotlin.script.util.resolvers.experimental.GenericRepositoryWithBridge
|
||||||
|
import org.jetbrains.kotlin.script.util.resolvers.experimental.MavenArtifactCoordinates
|
||||||
import org.sonatype.aether.repository.RemoteRepository
|
import org.sonatype.aether.repository.RemoteRepository
|
||||||
import org.sonatype.aether.resolution.DependencyResolutionException
|
import org.sonatype.aether.resolution.DependencyResolutionException
|
||||||
import org.sonatype.aether.util.artifact.DefaultArtifact
|
import org.sonatype.aether.util.artifact.DefaultArtifact
|
||||||
@@ -30,7 +33,7 @@ import java.util.*
|
|||||||
|
|
||||||
val mavenCentral = RemoteRepository("maven-central", "default", "https://repo.maven.apache.org/maven2/")
|
val mavenCentral = RemoteRepository("maven-central", "default", "https://repo.maven.apache.org/maven2/")
|
||||||
|
|
||||||
class MavenResolver(val reportError: ((String) -> Unit)? = null): Resolver {
|
class MavenResolver(val reportError: ((String) -> Unit)? = null): GenericRepositoryWithBridge {
|
||||||
|
|
||||||
// TODO: make robust
|
// TODO: make robust
|
||||||
val localRepo = File(File(System.getProperty("user.home")!!, ".m2"), "repository")
|
val localRepo = File(File(System.getProperty("user.home")!!, ".m2"), "repository")
|
||||||
@@ -39,9 +42,9 @@ class MavenResolver(val reportError: ((String) -> Unit)? = null): Resolver {
|
|||||||
|
|
||||||
private fun currentRepos() = if (repos.isEmpty()) arrayListOf(mavenCentral) else repos
|
private fun currentRepos() = if (repos.isEmpty()) arrayListOf(mavenCentral) else repos
|
||||||
|
|
||||||
private fun String.isValidParam() = isNotBlank()
|
private fun String?.isValidParam() = this?.isNotBlank() ?: false
|
||||||
|
|
||||||
override fun tryResolve(dependsOn: DependsOn): Iterable<File>? {
|
override fun tryResolve(artifactCoordinates: GenericArtifactCoordinates): Iterable<File>? {
|
||||||
|
|
||||||
fun error(msg: String) {
|
fun error(msg: String) {
|
||||||
reportError?.invoke(msg) ?: throw RuntimeException(msg)
|
reportError?.invoke(msg) ?: throw RuntimeException(msg)
|
||||||
@@ -49,16 +52,22 @@ class MavenResolver(val reportError: ((String) -> Unit)? = null): Resolver {
|
|||||||
|
|
||||||
fun String?.orNullIfBlank(): String? = this?.takeUnless(String::isBlank)
|
fun String?.orNullIfBlank(): String? = this?.takeUnless(String::isBlank)
|
||||||
|
|
||||||
val artifactId: DefaultArtifact = when {
|
val artifactId: DefaultArtifact = with(artifactCoordinates) {
|
||||||
dependsOn.groupId.isValidParam() || dependsOn.artifactId.isValidParam() -> {
|
if (this is MavenArtifactCoordinates && (groupId.isValidParam() || artifactId.isValidParam())) {
|
||||||
DefaultArtifact(dependsOn.groupId.orNullIfBlank(), dependsOn.artifactId.orNullIfBlank(), null, dependsOn.version.orNullIfBlank())
|
DefaultArtifact(
|
||||||
}
|
groupId.orNullIfBlank(),
|
||||||
dependsOn.value.isValidParam() && dependsOn.value.count { it == ':' } == 2 -> {
|
artifactId.orNullIfBlank(),
|
||||||
DefaultArtifact(dependsOn.value)
|
null,
|
||||||
}
|
version.orNullIfBlank()
|
||||||
else -> {
|
)
|
||||||
error("Unknown set of arguments to maven resolver: ${dependsOn.value}")
|
} else {
|
||||||
return null
|
val coordinatesString = string
|
||||||
|
if (coordinatesString.isValidParam() && coordinatesString.count { it == ':' } == 2) {
|
||||||
|
DefaultArtifact(coordinatesString)
|
||||||
|
} else {
|
||||||
|
error("Unknown set of arguments to maven resolver: $coordinatesString")
|
||||||
|
return null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -69,22 +78,24 @@ class MavenResolver(val reportError: ((String) -> Unit)? = null): Resolver {
|
|||||||
else {
|
else {
|
||||||
error("resolving ${artifactId.artifactId} failed: no results")
|
error("resolving ${artifactId.artifactId} failed: no results")
|
||||||
}
|
}
|
||||||
}
|
} catch (e: DependencyResolutionException) {
|
||||||
catch (e: DependencyResolutionException) {
|
|
||||||
reportError?.invoke("resolving ${artifactId.artifactId} failed: $e") ?: throw e
|
reportError?.invoke("resolving ${artifactId.artifactId} failed: $e") ?: throw e
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun tryAddRepo(annotation: Repository): Boolean {
|
override fun tryAddRepository(repositoryCoordinates: GenericRepositoryCoordinates): Boolean {
|
||||||
val urlStr = annotation.url.takeIf { it.isValidParam() } ?: annotation.value.takeIf { it.isValidParam() } ?: return false
|
val url = repositoryCoordinates.url
|
||||||
urlStr.toRepositoryUrlOrNull() ?: return false
|
if (url != null) {
|
||||||
repos.add(
|
repos.add(
|
||||||
RemoteRepository(
|
RemoteRepository(
|
||||||
if (annotation.id.isValidParam()) annotation.id else "central",
|
if (repositoryCoordinates.name.isValidParam()) repositoryCoordinates.name else url.host,
|
||||||
"default",
|
"default",
|
||||||
urlStr
|
url.toString()
|
||||||
))
|
)
|
||||||
return true
|
)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+15
@@ -0,0 +1,15 @@
|
|||||||
|
/*
|
||||||
|
* 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.util.resolvers
|
||||||
|
|
||||||
|
import org.jetbrains.kotlin.script.util.DependsOn
|
||||||
|
import org.jetbrains.kotlin.script.util.Repository
|
||||||
|
import java.io.File
|
||||||
|
|
||||||
|
interface Resolver {
|
||||||
|
fun tryResolve(dependsOn: DependsOn): Iterable<File>?
|
||||||
|
fun tryAddRepo(annotation: Repository): Boolean
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user