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

Fix snapshot resolution.

This commit is contained in:
Cedric Beust 2015-12-18 21:54:52 +04:00
parent 8acd8b8fce
commit 80434ca7b6
4 changed files with 60 additions and 22 deletions

View file

@ -28,7 +28,7 @@ public class RepoFinder @Inject constructor(val executors: KobaltExecutors) {
} }
data class RepoResult(val hostConfig: HostConfig, val found: Boolean, val version: Version? = null, data class RepoResult(val hostConfig: HostConfig, val found: Boolean, val version: Version? = null,
val hasJar: Boolean = true, val snapshotVersion: Version? = null) val hasJar: Boolean = true, val snapshotVersion: Version? = null)
private val FOUND_REPOS: LoadingCache<String, RepoResult> = CacheBuilder.newBuilder() private val FOUND_REPOS: LoadingCache<String, RepoResult> = CacheBuilder.newBuilder()
.build(object : CacheLoader<String, RepoResult>() { .build(object : CacheLoader<String, RepoResult>() {
@ -98,10 +98,15 @@ public class RepoFinder @Inject constructor(val executors: KobaltExecutors) {
if (version.isSnapshot()) { if (version.isSnapshot()) {
val dep = SimpleDep(mavenId) val dep = SimpleDep(mavenId)
val isLocal = repoUrl.startsWith(FileDependency.PREFIX_FILE) val isLocal = repoUrl.startsWith(FileDependency.PREFIX_FILE)
val snapshotVersion = if (isLocal) version val metadataXmlPath = dep.toMetadataXmlPath(false, isLocal, version.version)
else findSnapshotVersion(dep.toMetadataXmlPath(false, isLocal, version.version), repoUrl) val snapshotVersion =
if (isLocal) version
else findSnapshotVersion(metadataXmlPath, repoUrl, mavenId.version)
if (snapshotVersion != null) { if (snapshotVersion != null) {
return RepoResult(repo, true, version, true /* hasJar, potential bug here */, val url = repoUrl + metadataXmlPath
val kurl = Kurl(HostConfig(url))
val found = kurl.exists
return RepoResult(repo, found, version, true /* hasJar, potential bug here */,
snapshotVersion) snapshotVersion)
} else { } else {
return RepoResult(repo, false) return RepoResult(repo, false)
@ -184,7 +189,7 @@ public class RepoFinder @Inject constructor(val executors: KobaltExecutors) {
return null return null
} }
fun findSnapshotVersion(metadataPath: String, repoUrl: String): Version? { fun findSnapshotVersion(metadataPath: String, repoUrl: String, snapshotVersion: String): Version? {
val timestamp = XPATH.compile("/metadata/versioning/snapshot/timestamp") val timestamp = XPATH.compile("/metadata/versioning/snapshot/timestamp")
val buildNumber = XPATH.compile("/metadata/versioning/snapshot/buildNumber") val buildNumber = XPATH.compile("/metadata/versioning/snapshot/buildNumber")
// No version in this dependency, find out the most recent one by parsing maven-metadata.xml, if it exists // No version in this dependency, find out the most recent one by parsing maven-metadata.xml, if it exists
@ -194,7 +199,7 @@ public class RepoFinder @Inject constructor(val executors: KobaltExecutors) {
val ts = timestamp.evaluate(doc, XPathConstants.STRING) val ts = timestamp.evaluate(doc, XPathConstants.STRING)
val bn = buildNumber.evaluate(doc, XPathConstants.STRING) val bn = buildNumber.evaluate(doc, XPathConstants.STRING)
if (! Strings.isEmpty(ts.toString()) && ! Strings.isEmpty(bn.toString())) { if (! Strings.isEmpty(ts.toString()) && ! Strings.isEmpty(bn.toString())) {
return Version.of(ts.toString() + "-" + bn.toString()) return Version(snapshotVersion, ts.toString() + "-" + bn.toString())
} else { } else {
val lastUpdated = XPATH.compile("/metadata/versioning/lastUpdated") val lastUpdated = XPATH.compile("/metadata/versioning/lastUpdated")
if (! lastUpdated.toString().isEmpty()) { if (! lastUpdated.toString().isEmpty()) {

View file

@ -1,6 +1,6 @@
package com.beust.kobalt.maven package com.beust.kobalt.maven
import com.beust.kobalt.misc.Strings import com.beust.kobalt.misc.Version
open class SimpleDep(open val mavenId: MavenId) : UnversionedDep(mavenId.groupId, mavenId.artifactId) { open class SimpleDep(open val mavenId: MavenId) : UnversionedDep(mavenId.groupId, mavenId.artifactId) {
companion object { companion object {
@ -11,23 +11,29 @@ open class SimpleDep(open val mavenId: MavenId) : UnversionedDep(mavenId.groupId
val version: String get() = mavenId.version!! val version: String get() = mavenId.version!!
private fun toFile(v: String, snapshotVersion: String?, suffix: String) : String { private fun toFile(version: Version, suffix: String): String {
val fv = if (v.contains("SNAPSHOT")) v.replace("SNAPSHOT", "") else v val list =
val result = Strings.join("/", arrayListOf(toDirectory(v, false) + if (version.snapshotTimestamp != null) {
artifactId + "-" + fv + (snapshotVersion ?: "") + suffix)) listOf(toDirectory(version.version, false),
return result artifactId + "-" + version.noSnapshotVersion + "-" + version.snapshotTimestamp + suffix)
} else {
listOf(toDirectory(version.version, false), artifactId + "-" + version.version + suffix)
}
return list.joinToString("/")
} }
fun toPomFile(v: String) = toFile(v, "", ".pom") fun toPomFile(v: String) = toFile(Version.of(v), ".pom")
fun toPomFile(r: RepoFinder.RepoResult) = toFile(r.version!!.version, r.snapshotVersion?.version, ".pom") fun toPomFile(r: RepoFinder.RepoResult) = toFile(r.snapshotVersion ?: r.version!!, ".pom")
fun toJarFile(v: String = version) = toFile(v, "", suffix) fun toJarFile(v: String = version) = toFile(Version.of(v), suffix)
fun toJarFile(r: RepoFinder.RepoResult) = toFile(r.version!!.version, r.snapshotVersion?.version, suffix) fun toJarFile(v: Version) = toFile(v, suffix)
fun toPomFileName() = "$artifactId-$version.pom" fun toPomFileName() = "$artifactId-$version.pom"
fun toJarFile(r: RepoFinder.RepoResult) = toFile(r.snapshotVersion ?: r.version!!, suffix)
val suffix : String val suffix : String
get() { get() {
val packaging = mavenId.packaging val packaging = mavenId.packaging

View file

@ -3,10 +3,7 @@ package com.beust.kobalt.misc
import com.beust.kobalt.maven.MavenId import com.beust.kobalt.maven.MavenId
import com.google.common.base.CharMatcher import com.google.common.base.CharMatcher
import java.math.BigInteger import java.math.BigInteger
import java.util.Arrays import java.util.*
import java.util.Comparator
import java.util.Locale
import java.util.TreeMap
public class Versions { public class Versions {
companion object { companion object {
@ -39,7 +36,7 @@ public class Versions {
} }
} }
class Version(val version: String): Comparable<Version> { class Version(val version: String, val snapshotTimestamp: String? = null): Comparable<Version> {
companion object { companion object {
private val comparator = VersionComparator() private val comparator = VersionComparator()
@ -48,6 +45,9 @@ class Version(val version: String): Comparable<Version> {
} }
} }
val noSnapshotVersion: String
get() = version.replace("-SNAPSHOT", "")
internal val items: List<Item> internal val items: List<Item>
private var hash: Int = -1 private var hash: Int = -1

View file

@ -1,8 +1,10 @@
package com.beust.kobalt.maven package com.beust.kobalt.maven
import com.beust.kobalt.HostConfig
import com.beust.kobalt.KobaltTest import com.beust.kobalt.KobaltTest
import com.beust.kobalt.maven.dependency.MavenDependency import com.beust.kobalt.maven.dependency.MavenDependency
import com.beust.kobalt.misc.KobaltExecutors import com.beust.kobalt.misc.KobaltExecutors
import com.beust.kobalt.misc.Version
import com.beust.kobalt.misc.warn import com.beust.kobalt.misc.warn
import org.testng.Assert import org.testng.Assert
import org.testng.annotations.BeforeClass import org.testng.annotations.BeforeClass
@ -18,7 +20,8 @@ import kotlin.properties.Delegates
public class DownloadTest @Inject constructor( public class DownloadTest @Inject constructor(
val depFactory: DepFactory, val depFactory: DepFactory,
val localRepo: LocalRepo, val localRepo: LocalRepo,
val executors: KobaltExecutors) : KobaltTest() { val executors: KobaltExecutors,
val repoFinder: RepoFinder) : KobaltTest() {
var executor: ExecutorService by Delegates.notNull() var executor: ExecutorService by Delegates.notNull()
@BeforeClass @BeforeClass
@ -105,5 +108,29 @@ public class DownloadTest @Inject constructor(
Assert.assertNotNull(file) Assert.assertNotNull(file)
Assert.assertTrue(file.exists(), "Should find ${file}") Assert.assertTrue(file.exists(), "Should find ${file}")
} }
@Test
fun snapshotTest() {
val id = "org.jetbrains.spek:spek:0.1-SNAPSHOT"
val mavenId = MavenId.create(id)
val dep = SimpleDep(mavenId)
// TODO: allow tests to add their own repo. The following call requires
// "http://repository.jetbrains.com/all" to work
// For now, just hardcoding the result we should have received
// val repoResult = repoFinder.findCorrectRepo(id)
val repoResult = RepoFinder.RepoResult(HostConfig("http://repository.jetbrains.com/all/"),
true, Version.of("0.1-SNAPSHOT"), true, Version("0.1-SNAPSHOT", "20151011.112011-29"))
val jarFile = dep.toJarFile(repoResult)
val url = repoResult.hostConfig.url + jarFile
val metadataXmlPath = dep.toMetadataXmlPath(false, false, "0.1-SNAPSHOT")
Assert.assertEquals(metadataXmlPath, "org/jetbrains/spek/spek/0.1-SNAPSHOT/maven-metadata.xml")
Assert.assertTrue(Kurl(HostConfig(url)).exists, "Should exist: $url")
}
} }