From 80434ca7b6086a1497f9cdd0133ab078e187f2d7 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Fri, 18 Dec 2015 21:54:52 +0400 Subject: [PATCH] Fix snapshot resolution. --- .../com/beust/kobalt/maven/RepoFinder.kt | 17 +++++++---- .../com/beust/kobalt/maven/SimpleDep.kt | 26 ++++++++++------- .../kotlin/com/beust/kobalt/misc/Versions.kt | 10 +++---- .../com/beust/kobalt/maven/DownloadTest.kt | 29 ++++++++++++++++++- 4 files changed, 60 insertions(+), 22 deletions(-) diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt index 296d8c9e..365ca110 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/RepoFinder.kt @@ -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, - val hasJar: Boolean = true, val snapshotVersion: Version? = null) + val hasJar: Boolean = true, val snapshotVersion: Version? = null) private val FOUND_REPOS: LoadingCache = CacheBuilder.newBuilder() .build(object : CacheLoader() { @@ -98,10 +98,15 @@ public class RepoFinder @Inject constructor(val executors: KobaltExecutors) { if (version.isSnapshot()) { val dep = SimpleDep(mavenId) val isLocal = repoUrl.startsWith(FileDependency.PREFIX_FILE) - val snapshotVersion = if (isLocal) version - else findSnapshotVersion(dep.toMetadataXmlPath(false, isLocal, version.version), repoUrl) + val metadataXmlPath = dep.toMetadataXmlPath(false, isLocal, version.version) + val snapshotVersion = + if (isLocal) version + else findSnapshotVersion(metadataXmlPath, repoUrl, mavenId.version) 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) } else { return RepoResult(repo, false) @@ -184,7 +189,7 @@ public class RepoFinder @Inject constructor(val executors: KobaltExecutors) { 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 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 @@ -194,7 +199,7 @@ public class RepoFinder @Inject constructor(val executors: KobaltExecutors) { val ts = timestamp.evaluate(doc, XPathConstants.STRING) val bn = buildNumber.evaluate(doc, XPathConstants.STRING) if (! Strings.isEmpty(ts.toString()) && ! Strings.isEmpty(bn.toString())) { - return Version.of(ts.toString() + "-" + bn.toString()) + return Version(snapshotVersion, ts.toString() + "-" + bn.toString()) } else { val lastUpdated = XPATH.compile("/metadata/versioning/lastUpdated") if (! lastUpdated.toString().isEmpty()) { diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/SimpleDep.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/SimpleDep.kt index e9c99892..cd4ba5fc 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/SimpleDep.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/SimpleDep.kt @@ -1,6 +1,6 @@ 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) { companion object { @@ -11,23 +11,29 @@ open class SimpleDep(open val mavenId: MavenId) : UnversionedDep(mavenId.groupId val version: String get() = mavenId.version!! - private fun toFile(v: String, snapshotVersion: String?, suffix: String) : String { - val fv = if (v.contains("SNAPSHOT")) v.replace("SNAPSHOT", "") else v - val result = Strings.join("/", arrayListOf(toDirectory(v, false) + - artifactId + "-" + fv + (snapshotVersion ?: "") + suffix)) - return result + private fun toFile(version: Version, suffix: String): String { + val list = + if (version.snapshotTimestamp != null) { + listOf(toDirectory(version.version, false), + 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 toJarFile(r: RepoFinder.RepoResult) = toFile(r.snapshotVersion ?: r.version!!, suffix) + val suffix : String get() { val packaging = mavenId.packaging diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/Versions.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/Versions.kt index d70ea3ea..1d3a3fd7 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/Versions.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/Versions.kt @@ -3,10 +3,7 @@ package com.beust.kobalt.misc import com.beust.kobalt.maven.MavenId import com.google.common.base.CharMatcher import java.math.BigInteger -import java.util.Arrays -import java.util.Comparator -import java.util.Locale -import java.util.TreeMap +import java.util.* public class Versions { companion object { @@ -39,7 +36,7 @@ public class Versions { } } -class Version(val version: String): Comparable { +class Version(val version: String, val snapshotTimestamp: String? = null): Comparable { companion object { private val comparator = VersionComparator() @@ -48,6 +45,9 @@ class Version(val version: String): Comparable { } } + val noSnapshotVersion: String + get() = version.replace("-SNAPSHOT", "") + internal val items: List private var hash: Int = -1 diff --git a/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt b/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt index c6bc996f..bca80d32 100644 --- a/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt +++ b/src/test/kotlin/com/beust/kobalt/maven/DownloadTest.kt @@ -1,8 +1,10 @@ package com.beust.kobalt.maven +import com.beust.kobalt.HostConfig import com.beust.kobalt.KobaltTest import com.beust.kobalt.maven.dependency.MavenDependency import com.beust.kobalt.misc.KobaltExecutors +import com.beust.kobalt.misc.Version import com.beust.kobalt.misc.warn import org.testng.Assert import org.testng.annotations.BeforeClass @@ -18,7 +20,8 @@ import kotlin.properties.Delegates public class DownloadTest @Inject constructor( val depFactory: DepFactory, val localRepo: LocalRepo, - val executors: KobaltExecutors) : KobaltTest() { + val executors: KobaltExecutors, + val repoFinder: RepoFinder) : KobaltTest() { var executor: ExecutorService by Delegates.notNull() @BeforeClass @@ -105,5 +108,29 @@ public class DownloadTest @Inject constructor( Assert.assertNotNull(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") + } + }