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

Introduce KobaltContext#filefor(id).

This commit is contained in:
Cedric Beust 2016-07-25 00:41:42 -08:00
parent 5df0d01a21
commit b02ba388da
3 changed files with 80 additions and 51 deletions

View file

@ -1,12 +1,16 @@
package com.beust.kobalt.api
import com.beust.kobalt.Args
import com.beust.kobalt.KobaltException
import com.beust.kobalt.Plugins
import com.beust.kobalt.Variant
import com.beust.kobalt.internal.IncrementalManager
import com.beust.kobalt.internal.KobaltSettings
import com.beust.kobalt.internal.PluginInfo
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.MavenId
import com.beust.kobalt.maven.SimpleDep
import com.beust.kobalt.maven.aether.KobaltAether
import com.beust.kobalt.misc.KobaltExecutors
import java.io.File
@ -22,6 +26,39 @@ class KobaltContext(val args: Args) {
fun findPlugin(name: String) = Plugins.findPlugin(name)
/**
* Files that can be resolved in the local cache.
*/
enum class FileType { JAR, POM, SOURCES, JAVADOC, OTHER }
/**
* @param{id} is the Maven coordinate (e.g. "org.testng:testng:6.9.11"). If you are looking for a file
* that is not described by the enum (e.g. "aar"), use OTHER and make sure your @param{id} contains
* the fully qualified id (e.g. "com.example:example::aar:1.0").
*/
fun fileFor(id: String, fileType: FileType) : File {
val dep = SimpleDep(MavenId.create(id))
fun toQualifier(dep: SimpleDep, ext: String, qualifier: String?) =
dep.groupId + ":" + dep.artifactId +
":$ext" +
(if (qualifier != null) ":$qualifier" else "") +
":" + dep.version
val fullId =
when (fileType) {
FileType.JAR -> toQualifier(dep, "jar", null)
FileType.POM -> toQualifier(dep, "pom", null)
FileType.SOURCES -> toQualifier(dep, "", "sources")
FileType.JAVADOC -> toQualifier(dep, "", "javadoc")
FileType.OTHER -> id
}
val resolved = aether.resolveToArtifact(fullId)
if (resolved != null) {
return resolved.artifact.file
} else {
throw KobaltException("Couldn't resolve $id")
}
}
/** All the projects that are being built during this run */
val allProjects = arrayListOf<Project>()
@ -37,6 +74,7 @@ class KobaltContext(val args: Args) {
lateinit var executors: KobaltExecutors
lateinit var settings: KobaltSettings
lateinit var incrementalManager: IncrementalManager
lateinit var aether: KobaltAether
}
class InternalContext {

View file

@ -57,14 +57,22 @@ class KobaltAether @Inject constructor (val settings: KobaltSettings, val aether
fun resolve(id: String, isTest: Boolean = false): DependencyResult {
log(ConsoleRepositoryListener.LOG_LEVEL, "Resolving $id")
val results = aether.resolve(DefaultArtifact(MavenId.toKobaltId(id)), isTest)
if (results.size > 0) {
return DependencyResult(AetherDependency(results[0].artifact), results[0].repository.toString())
val result = resolveToArtifact(id, isTest)
if (result != null) {
return DependencyResult(AetherDependency(result.artifact), result.repository.toString())
} else {
throw KobaltException("Couldn't resolve $id")
}
}
fun resolveToArtifact(id: String, isTest: Boolean = false): ArtifactResult? {
log(ConsoleRepositoryListener.LOG_LEVEL, "Resolving $id")
val results = aether.resolve(DefaultArtifact(MavenId.toKobaltId(id)), isTest)
if (results.size > 0) {
return results[0]
} else {
return null
}
}
class ExcludeOptionalDependencyFilter : DependencyFilter {
@ -76,18 +84,19 @@ class ExcludeOptionalDependencyFilter: DependencyFilter {
return result
}
}
}
@Singleton
class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: EventBus) {
private val system = Booter.newRepositorySystem()
private val session = Booter.newRepositorySystemSession(system, localRepo, settings, eventBus)
private val classpathFilter = AndDependencyFilter(
ExcludeOptionalDependencyFilter(),
KobaltAether.ExcludeOptionalDependencyFilter(),
DependencyFilterUtils.classpathFilter(JavaScopes.COMPILE),
DependencyFilterUtils.classpathFilter(JavaScopes.TEST))
private val testClasspathFilter = AndDependencyFilter(
ExcludeOptionalDependencyFilter(),
KobaltAether.ExcludeOptionalDependencyFilter(),
DependencyFilterUtils.classpathFilter(JavaScopes.TEST))
private val kobaltRepositories: List<RemoteRepository>
@ -134,30 +143,6 @@ class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: Ev
return result
}
private fun oldResolveVErsion() {
// val artifact = DefaultArtifact(a.groupId, a.artifactId, null, "[0,)")
// val r = system.resolveMetadata(session, kobaltRepositories.map {
// MetadataRequest(metadata, it, null).apply {
// isFavorLocalRepository = false
// }
// })
// val metadata = DefaultMetadata(artifact.groupId, artifact.artifactId, "maven-metadata.xml",
// org.eclipse.aether.metadata.Metadata.Nature.RELEASE)
//
// kobaltRepositories.forEach {
// val request = MetadataRequest(metadata, it, null).apply {
// isFavorLocalRepository = false
// }
// val md = system.resolveMetadata(session, listOf(request))
// if (artifact.groupId.contains("org.testng")) {
// println("DONOTCOMMIT")
// }
// println("Repo: $it " + md)
// }
}
fun resolve(artifact: Artifact, isTest: Boolean = false): List<ArtifactResult> {
fun manageException(ex: Exception, artifact: Artifact): List<ArtifactResult> {
if (artifact.extension == "pom") {
@ -189,7 +174,8 @@ class Aether(val localRepo: File, val settings: KobaltSettings, val eventBus: Ev
class AetherDependency(val artifact: Artifact) : IClasspathDependency, Comparable<AetherDependency> {
val aether: Aether get() = Kobalt.INJECTOR.getInstance(Aether::class.java)
constructor(node: DependencyNode) : this(node.artifact) {}
constructor(node: DependencyNode) : this(node.artifact) {
}
override val id: String = toId(artifact)
@ -290,3 +276,4 @@ fun main(argv: Array<String>) {
//
// println("Artifact: " + d)
}

View file

@ -14,6 +14,8 @@ import com.beust.kobalt.internal.PluginInfo
import com.beust.kobalt.internal.build.BuildFile
import com.beust.kobalt.internal.build.VersionFile
import com.beust.kobalt.maven.DependencyManager
import com.beust.kobalt.maven.LocalRepo
import com.beust.kobalt.maven.aether.KobaltAether
import com.beust.kobalt.misc.KFiles
import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.log
@ -33,7 +35,8 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
@Assisted val pluginInfo: PluginInfo, val files: KFiles, val plugins: Plugins,
val dependencyManager: DependencyManager, val pluginProperties: PluginProperties,
val executors: KobaltExecutors, val buildScriptUtil: BuildScriptUtil, val settings: KobaltSettings,
val incrementalManagerFactory: IncrementalManager.IFactory, val args: Args) {
val incrementalManagerFactory: IncrementalManager.IFactory, val args: Args,
val aether: KobaltAether) {
interface IFactory {
fun create(@Assisted("buildFiles") buildFiles: List<BuildFile>, pluginInfo: PluginInfo) : BuildFileCompiler
@ -53,6 +56,7 @@ public class BuildFileCompiler @Inject constructor(@Assisted("buildFiles") val b
context.executors = executors
context.settings = settings
context.incrementalManager = incrementalManagerFactory.create()
context.aether = aether
Kobalt.context = context
//