1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 08:27:12 -07:00

Remove usages of MavenDependency.

This commit is contained in:
Cedric Beust 2016-03-25 10:57:59 +04:00
parent c57b8c96e9
commit 6de1b4d893
16 changed files with 211 additions and 266 deletions

View file

@ -16,6 +16,9 @@ interface IClasspathDependency {
/** Identifier for this dependency */
val id: String
/** Version for this identifier */
val version: String
/** Absolute path to the jar file on the local file system */
val jarFile: Future<File>

View file

@ -3,7 +3,7 @@ package com.beust.kobalt.api
import com.beust.kobalt.TestConfig
import com.beust.kobalt.api.annotation.Directive
import com.beust.kobalt.internal.JvmCompilerPlugin
import com.beust.kobalt.maven.dependency.MavenDependency
import com.beust.kobalt.maven.aether.KobaltAether
import com.beust.kobalt.misc.KFiles
import java.io.File
import java.util.*
@ -83,8 +83,8 @@ open class Project(
@Directive
fun dependencies(init: Dependencies.() -> Unit) : Dependencies {
dependencies = Dependencies(this, compileDependencies, compileProvidedDependencies, compileRuntimeDependencies,
excludedDependencies)
dependencies = Dependencies(this, compileDependencies, compileProvidedDependencies,
compileRuntimeDependencies, excludedDependencies)
dependencies!!.init()
return dependencies!!
}
@ -139,7 +139,8 @@ class Sources(val project: Project, val sources: HashSet<String>) {
}
}
class Dependencies(val project: Project, val dependencies: ArrayList<IClasspathDependency>,
class Dependencies(val project: Project,
val dependencies: ArrayList<IClasspathDependency>,
val providedDependencies: ArrayList<IClasspathDependency>,
val runtimeDependencies: ArrayList<IClasspathDependency>,
val excludedDependencies: ArrayList<IClasspathDependency>) {
@ -150,7 +151,7 @@ class Dependencies(val project: Project, val dependencies: ArrayList<IClasspathD
*/
private fun addToDependencies(dependencies: ArrayList<IClasspathDependency>, dep: Array<out String>)
: List<File>
= with(dep.map { MavenDependency.create(it)}) {
= with(dep.map { KobaltAether.create(it)}) {
dependencies.addAll(this)
this.map { it.jarFile.get() }
}

View file

@ -10,7 +10,10 @@ import com.google.inject.Key
import java.util.concurrent.ExecutorService
import javax.inject.Inject
public class DepFactory @Inject constructor(val localRepo: LocalRepo,
/**
* Use this class to create instances of `IClasspathDependency` from an id.
*/
class DepFactory @Inject constructor(val localRepo: LocalRepo,
val executors: KobaltExecutors,
val aether: KobaltAether) {
@ -29,38 +32,10 @@ public class DepFactory @Inject constructor(val localRepo: LocalRepo,
if (id.startsWith(FileDependency.PREFIX_FILE)) {
return FileDependency(id.substring(FileDependency.PREFIX_FILE.length))
} else {
val result = aether.create(id)
val mavenId = MavenId.create(id)
val result = if (mavenId.hasVersion) aether.create(id)
else aether.create(id + "(0,]")
return result
// return deps.root
// val mavenId = MavenId.create(id)
// var tentativeVersion = mavenId.version
// var packaging = mavenId.packaging
// var repoResult: RepoFinder.RepoResult?
//
// val version =
// if (tentativeVersion != null && ! MavenId.isRangedVersion(tentativeVersion)) tentativeVersion
// else {
// var localVersion: String? = tentativeVersion
// if (localFirst) localVersion = localRepo.findLocalVersion(mavenId.groupId, mavenId.artifactId,
// mavenId.packaging)
// if (localFirst && localVersion != null) {
// localVersion
// } else {
// if (! localFirst && showNetworkWarning) {
// warn("The id \"$id\" doesn't contain a version, which will cause a network call")
// }
// repoResult = remoteRepo.findCorrectRepo(id)
// if (!repoResult.found) {
// throw KobaltException("Couldn't resolve $id")
// } else {
// repoResult.version?.version
// }
// }
// }
//
//
// val resultMavenId = MavenId.create(mavenId.groupId, mavenId.artifactId, packaging, version)
// return mavenDependencyFactory.create(resultMavenId, executor, downloadSources, downloadJavadocs)
}
}
}

View file

@ -2,7 +2,6 @@ package com.beust.kobalt.maven
import com.beust.kobalt.api.*
import com.beust.kobalt.maven.dependency.FileDependency
import com.beust.kobalt.maven.dependency.MavenDependency
import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.KobaltExecutors
import com.google.common.collect.ArrayListMultimap
@ -11,14 +10,12 @@ import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class DependencyManager @Inject constructor(val executors: KobaltExecutors,
val depFactory: DepFactory, val mdFactory: MavenDependency.IFactory){
class DependencyManager @Inject constructor(val executors: KobaltExecutors, val depFactory: DepFactory) {
/**
* Create an IClasspathDependency from a Maven id.
*/
fun createMaven(id: String) : IClasspathDependency =
mdFactory.create(MavenId.create(id), executors.miscExecutor, false, false)
fun createMaven(id: String) : IClasspathDependency = depFactory.create(id)
/**
* Create an IClasspathDependency from a path.

View file

@ -32,15 +32,11 @@ open class SimpleDep(open val mavenId: MavenId) : UnversionedDep(mavenId.groupId
fun toPomFile(v: String) = toFile(Version.of(v), ".pom")
fun toPomFile(r: RepoFinder.RepoResult) = toFile(r.snapshotVersion ?: r.version!!, ".pom")
fun toJarFile(v: String = version) = toFile(Version.of(v), suffix)
fun toAarFile(v: String = version) = toFile(Version.of(v), ".aar")
fun toPomFileName() = "$artifactId-$version.pom"
fun toJarFile(r: RepoFinder.RepoResult) = toFile(r.snapshotVersion ?: r.version!!, suffix)
val suffix : String
get() {
val packaging = mavenId.packaging

View file

@ -30,6 +30,15 @@ val TEST_DIR = ".aether/repository"
class DependencyResult(val dependency: IClasspathDependency, val repoUrl: String)
class KobaltAether(val localRepo: File = File(homeDir(TEST_DIR))) {
companion object {
val aether : KobaltAether get() = Kobalt.INJECTOR.getInstance(KobaltAether::class.java)
fun create(id: String) = aether.create(id)
}
/**
* Don't call this method directly, use `DepFactory` instead.
*/
fun create(id: String): IClasspathDependency {
val aether = Aether(localRepo)
val cr = aether.transitiveDependencies(DefaultArtifact(id))
@ -147,6 +156,8 @@ class AetherDependency(val artifact: Artifact): IClasspathDependency, Comparable
override val id: String = toId(artifact)
override val version: String = artifact.version
private fun toId(a: Artifact) = with(a) {
groupId + ":" + artifactId + ":" + version
}

View file

@ -12,6 +12,8 @@ open public class FileDependency(open val fileName: String) : IClasspathDependen
override val id = PREFIX_FILE + fileName
override val version = "0.0"
override val jarFile = CompletedFuture(File(fileName))
override fun toMavenDependencies(): Dependency {

View file

@ -1,142 +1,128 @@
package com.beust.kobalt.maven.dependency
import com.beust.kobalt.HostConfig
import com.beust.kobalt.KobaltException
import com.beust.kobalt.api.IClasspathDependency
import com.beust.kobalt.api.Kobalt
import com.beust.kobalt.maven.*
import com.beust.kobalt.misc.DependencyExecutor
import com.beust.kobalt.misc.Versions
import com.beust.kobalt.misc.log
import com.beust.kobalt.misc.warn
import com.google.inject.Key
import com.google.inject.assistedinject.Assisted
import org.apache.maven.model.Dependency
import java.io.File
import java.util.concurrent.ExecutorService
import java.util.concurrent.Future
import javax.inject.Inject
import kotlin.properties.Delegates
class MavenDependency @Inject constructor(
@Assisted mavenId: MavenId,
@Assisted val executor: ExecutorService,
@Assisted("downloadSources") val downloadSources: Boolean,
@Assisted("downloadJavadocs") val downloadJavadocs: Boolean,
override val localRepo: LocalRepo,
val repoFinder: RepoFinder,
val dependencyManager: DependencyManager,
val downloadManager: DownloadManager)
: LocalDep(mavenId, localRepo), IClasspathDependency, Comparable<MavenDependency> {
override var jarFile: Future<File> by Delegates.notNull()
var pomFile: Future<File> by Delegates.notNull()
interface IFactory {
fun create(mavenId: MavenId, executor: ExecutorService,
@Assisted("downloadSources") downloadSources: Boolean,
@Assisted("downloadJavadocs") downloadJavadocs: Boolean) : MavenDependency
}
init {
val jar = File(localRepo.toFullPath(toJarFile(version)))
val aar = File(localRepo.toFullPath(toAarFile(version)))
val pom = File(localRepo.toFullPath(toPomFile(version)))
fun toSuffix(name: String, suffix: String = "") : String {
val dot = name.lastIndexOf(".")
return name.substring(0, dot) + suffix + name.substring(dot)
}
fun download(url: String, fileName: String, suffix: String = "") : Future<File> {
val localPath = localRepo.toFullPath(toSuffix(fileName, suffix))
return downloadManager.download(HostConfig(toSuffix(url, suffix)), localPath, executor)
}
if (pom.exists() && (jar.exists() || aar.exists())) {
jarFile = CompletedFuture(if (jar.exists()) jar else aar)
pomFile = CompletedFuture(pom)
} else {
val repoResult = repoFinder.findCorrectRepo(mavenId.toId)
if (repoResult.found) {
jarFile =
if (repoResult.archiveUrl != null) {
download(repoResult.archiveUrl, repoResult.path!!)
} else {
CompletedFuture(File("nonexistentFile")) // will be filtered out
}
pomFile = downloadManager.download(HostConfig(url = repoResult.hostConfig.url + toPomFile(repoResult)),
pom.absolutePath, executor)
} else {
throw KobaltException("Couldn't resolve ${mavenId.toId}")
}
}
if (downloadSources || downloadJavadocs) {
val repoResult = repoFinder.findCorrectRepo(mavenId.toId)
if (repoResult.archiveUrl != null && repoResult.path != null) {
if (downloadSources) {
download(repoResult.archiveUrl, repoResult.path, "-sources")
}
if (downloadJavadocs) {
download(repoResult.archiveUrl, repoResult.path, "-javadoc")
}
}
}
}
companion object {
val defaultExecutor =
Kobalt.INJECTOR.getInstance(Key.get(ExecutorService::class.java, DependencyExecutor::class.java))
val depFactory = Kobalt.INJECTOR.getInstance(DepFactory::class.java)
fun create(id: String, downloadSources: Boolean = false, downloadJavadocs: Boolean = false,
executor: ExecutorService = defaultExecutor)
= depFactory.create(id, downloadSources, downloadJavadocs, executor = executor)
fun create(mavenId: MavenId, downloadSources: Boolean = false, downloadJavadocs: Boolean = false,
executor: ExecutorService = defaultExecutor)
= create(mavenId.toId, downloadSources, downloadJavadocs, executor)
}
override fun toString() = mavenId.toId
override val id = mavenId.toId
override fun toMavenDependencies() = let { md ->
Dependency().apply {
groupId = md.groupId
artifactId = md.artifactId
version = md.version
}
}
override fun compareTo(other: MavenDependency): Int {
return Versions.toLongVersion(version).compareTo(Versions.toLongVersion(other.version))
}
override val shortId = "$groupId:$artifactId:"
override fun directDependencies() : List<IClasspathDependency> {
val result = arrayListOf<IClasspathDependency>()
val maybePom = Pom2.parse(pomFile.get(), dependencyManager)
if (maybePom.value != null) {
val pom = maybePom.value
pom.pomProject.dependencies.filter {
it.mustDownload
}.forEach {
if (it.isValid) {
result.add(create(MavenId.toId(it.groupId(pom), it.artifactId(pom), it.packaging, it.version(pom))))
} else {
log(2, "Skipping invalid id: ${it.id(pom)}")
}
}
} else {
warn("Couldn't parse POM file ${pomFile.get()}: " + maybePom.exception?.message, maybePom.exception!!)
}
return result
}
}
//class MavenDependency @Inject constructor(val aether: KobaltAether) : IClasspathDependency by aether {
// fun create(id: String) = aether.create(id)
//}
//class _MavenDependency @Inject constructor(
// @Assisted mavenId: MavenId,
// @Assisted val executor: ExecutorService,
// @Assisted("downloadSources") val downloadSources: Boolean,
// @Assisted("downloadJavadocs") val downloadJavadocs: Boolean,
// override val localRepo: LocalRepo,
// val repoFinder: RepoFinder,
// val dependencyManager: DependencyManager,
// val downloadManager: DownloadManager)
// : LocalDep(mavenId, localRepo), IClasspathDependency, Comparable<_MavenDependency> {
// override var jarFile: Future<File> by Delegates.notNull()
// var pomFile: Future<File> by Delegates.notNull()
//
// interface IFactory {
// fun create(mavenId: MavenId, executor: ExecutorService,
// @Assisted("downloadSources") downloadSources: Boolean,
// @Assisted("downloadJavadocs") downloadJavadocs: Boolean) : _MavenDependency
// }
//
// init {
// val jar = File(localRepo.toFullPath(toJarFile(version)))
// val aar = File(localRepo.toFullPath(toAarFile(version)))
// val pom = File(localRepo.toFullPath(toPomFile(version)))
//
// fun toSuffix(name: String, suffix: String = "") : String {
// val dot = name.lastIndexOf(".")
// return name.substring(0, dot) + suffix + name.substring(dot)
// }
//
// fun download(url: String, fileName: String, suffix: String = "") : Future<File> {
// val localPath = localRepo.toFullPath(toSuffix(fileName, suffix))
// return downloadManager.download(HostConfig(toSuffix(url, suffix)), localPath, executor)
// }
//
// if (pom.exists() && (jar.exists() || aar.exists())) {
// jarFile = CompletedFuture(if (jar.exists()) jar else aar)
// pomFile = CompletedFuture(pom)
// } else {
// val repoResult = repoFinder.findCorrectRepo(mavenId.toId)
//
// if (repoResult.found) {
// jarFile =
// if (repoResult.archiveUrl != null) {
// download(repoResult.archiveUrl, repoResult.path!!)
// } else {
// CompletedFuture(File("nonexistentFile")) // will be filtered out
// }
// pomFile = downloadManager.download(HostConfig(url = repoResult.hostConfig.url + toPomFile(repoResult)),
// pom.absolutePath, executor)
// } else {
// throw KobaltException("Couldn't resolve ${mavenId.toId}")
// }
// }
//
// if (downloadSources || downloadJavadocs) {
// val repoResult = repoFinder.findCorrectRepo(mavenId.toId)
// if (repoResult.archiveUrl != null && repoResult.path != null) {
// if (downloadSources) {
// download(repoResult.archiveUrl, repoResult.path, "-sources")
// }
// if (downloadJavadocs) {
// download(repoResult.archiveUrl, repoResult.path, "-javadoc")
// }
// }
// }
//
// }
//
// companion object {
// val defaultExecutor =
// Kobalt.INJECTOR.getInstance(Key.get(ExecutorService::class.java, DependencyExecutor::class.java))
// val depFactory = Kobalt.INJECTOR.getInstance(DepFactory::class.java)
//
// fun create(id: String, downloadSources: Boolean = false, downloadJavadocs: Boolean = false,
// executor: ExecutorService = defaultExecutor)
// = depFactory.create(id, downloadSources, downloadJavadocs, executor = executor)
//
// fun create(mavenId: MavenId, downloadSources: Boolean = false, downloadJavadocs: Boolean = false,
// executor: ExecutorService = defaultExecutor)
// = create(mavenId.toId, downloadSources, downloadJavadocs, executor)
// }
//
// override fun toString() = mavenId.toId
//
// override val id = mavenId.toId
//
// override fun toMavenDependencies() = let { md ->
// Dependency().apply {
// groupId = md.groupId
// artifactId = md.artifactId
// version = md.version
// }
// }
//
//// override fun compareTo(other: _MavenDependency): Int {
// return Versions.toLongVersion(version).compareTo(Versions.toLongVersion(other.version))
// }
//
// override val shortId = "$groupId:$artifactId:"
//
// override fun directDependencies() : List<IClasspathDependency> {
// val result = arrayListOf<IClasspathDependency>()
// val maybePom = Pom2.parse(pomFile.get(), dependencyManager)
// if (maybePom.value != null) {
// val pom = maybePom.value
// pom.pomProject.dependencies.filter {
// it.mustDownload
// }.forEach {
// if (it.isValid) {
// result.add(create(MavenId.toId(it.groupId(pom), it.artifactId(pom), it.packaging, it.version(pom))))
// } else {
// log(2, "Skipping invalid id: ${it.id(pom)}")
// }
// }
// } else {
// warn("Couldn't parse POM file ${pomFile.get()}: " + maybePom.exception?.message, maybePom.exception!!)
//
// }
// return result
// }
//}
//

View file

@ -4,7 +4,7 @@ import com.beust.kobalt.KobaltException
import com.beust.kobalt.api.Project
import com.beust.kobalt.maven.DepFactory
import com.beust.kobalt.maven.MavenId
import com.beust.kobalt.maven.dependency.MavenDependency
import com.beust.kobalt.maven.aether.AetherDependency
import javax.inject.Inject
/**
@ -24,12 +24,10 @@ public class CheckVersions @Inject constructor(val depFactory : DepFactory,
try {
val dep = depFactory.create(compileDependency.shortId, localFirst = false,
showNetworkWarning = false, executor = executor)
if (dep is MavenDependency) {
val other = compileDependency as MavenDependency
if (dep.id != compileDependency.id
&& Versions.toLongVersion(dep.version) > Versions.toLongVersion(other.version)) {
newVersions.add(dep.id)
}
val other = compileDependency as AetherDependency
if (dep.id != compileDependency.id
&& Versions.toLongVersion(dep.version) > Versions.toLongVersion(other.version)) {
newVersions.add(dep.id)
}
} catch(e: KobaltException) {
log(1, " Cannot resolve ${compileDependency.shortId}. ignoring")