diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TestNgRunner.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TestNgRunner.kt index 0e1724d2..10648b27 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TestNgRunner.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/TestNgRunner.kt @@ -7,7 +7,7 @@ import com.beust.kobalt.api.KobaltContext import com.beust.kobalt.api.Project import com.beust.kobalt.maven.aether.AetherDependency import com.beust.kobalt.misc.KFiles -import com.beust.kobalt.misc.Versions +import com.beust.kobalt.misc.StringVersion import com.beust.kobalt.misc.runCommand import com.beust.kobalt.misc.warn import org.testng.remote.RemoteArgs @@ -59,7 +59,7 @@ class TestNgRunner : GenericTestRunner() { } } - val VERSION_6_10 = 600100000L + val VERSION_6_10 = StringVersion("6.10") override fun runTests(project: Project, context: KobaltContext, classpath: List, configName: String): Boolean { @@ -68,7 +68,7 @@ class TestNgRunner : GenericTestRunner() { val testngDependency = (project.testDependencies.filter { it.id.contains("testng") } .firstOrNull() as AetherDependency).version - val testngDependencyVersion = Versions.toLongVersion(testngDependency) + val testngDependencyVersion = StringVersion(testngDependency) val result = if (testngDependencyVersion >= VERSION_6_10) { context.logger.log(project.name, 1, "Modern TestNG, displaying colors") @@ -91,7 +91,6 @@ class TestNgRunner : GenericTestRunner() { transitiveClosure(listOf(jf, tr, testng)) } - val v = Versions.toLongVersion("6.10") val cp = (classpath + dep).distinct().map { it.jarFile.get() } .joinToString(File.pathSeparator) val passedArgs = listOf( diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/LocalRepo.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/LocalRepo.kt index 21d36172..35f8c50a 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/LocalRepo.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/LocalRepo.kt @@ -1,11 +1,8 @@ package com.beust.kobalt.maven import com.beust.kobalt.internal.KobaltSettings -import com.beust.kobalt.misc.KFiles -import com.beust.kobalt.misc.Versions import com.google.inject.Inject import java.io.File -import java.util.* import javax.inject.Singleton @Singleton @@ -13,42 +10,7 @@ open class LocalRepo @Inject constructor(val kobaltSettings: KobaltSettings) { val localRepo: File get() = kobaltSettings.localCache - fun existsPom(d: LocalDep, v: String) : Boolean { - return File(d.toAbsolutePomFile(v)).exists() - } - - fun existsJar(d: LocalDep, v: String) : Boolean { - return File(d.toAbsoluteJarFilePath(v)).exists() - } - - /** - * If the dependency is local, return the correct version for it - */ - fun findLocalVersion(groupId: String, artifactId: String, packaging: String? = null) : String? { - // No version: look at all the directories under group/artifactId, pick the latest and see - // if it contains a maven and jar file - val dir = toFullPath(KFiles.joinDir(groupId.replace(".", File.separator), artifactId)) - val files = File(dir).listFiles() - - if (files != null) { - val directories = files.filter { it.isDirectory } - if (directories.size > 0) { - Collections.sort(directories, { f1, f2 -> - val v1 = Versions.toLongVersion(f1.name) - val v2 = Versions.toLongVersion(f2.name) - v2.compareTo(v1) // we want the most recent at position 0 - }) - val result = directories[0].name - val newDep = LocalDep(MavenId.create(groupId, artifactId, packaging, null, result), this) - if (existsPom(newDep, result) && existsJar(newDep, result)) { - return result - } - } - } - return null - } - - fun toFullPath(path: String) = File(localRepo, path).absolutePath + fun toFullPath(path: String): String = File(localRepo, path).absolutePath } diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/AetherDependency.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/AetherDependency.kt index 5cd532fe..f7de93a2 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/AetherDependency.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/maven/aether/AetherDependency.kt @@ -7,7 +7,7 @@ import com.beust.kobalt.maven.CompletedFuture import com.beust.kobalt.maven.LocalDep import com.beust.kobalt.maven.LocalRepo import com.beust.kobalt.maven.MavenId -import com.beust.kobalt.misc.Versions +import com.beust.kobalt.misc.StringVersion import com.beust.kobalt.misc.warn import org.eclipse.aether.artifact.Artifact import java.io.File @@ -69,8 +69,7 @@ class AetherDependency(val artifact: Artifact, override val optional: Boolean = override val excluded = arrayListOf() override fun compareTo(other: AetherDependency): Int { - return Versions.toLongVersion(artifact.version).compareTo(Versions.toLongVersion( - other.artifact.version)) + return StringVersion(artifact.version).compareTo(StringVersion(other.artifact.version)) } override fun hashCode() = id.hashCode() diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/CheckVersions.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/CheckVersions.kt index f046322d..f6c6a49e 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/CheckVersions.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/CheckVersions.kt @@ -34,7 +34,7 @@ class CheckVersions @Inject constructor(val depManager: DependencyManager, versions?.highestVersion.toString() } if (highest != dep.id - && Versions.toLongVersion(highest) > Versions.toLongVersion(dep.version)) { + && StringVersion(highest) > StringVersion(dep.version)) { newVersions.add(artifact.groupId + ":" + artifact.artifactId + ":" + highest) } } catch(e: KobaltException) { diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/StringVersion.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/StringVersion.kt new file mode 100644 index 00000000..fc620554 --- /dev/null +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/misc/StringVersion.kt @@ -0,0 +1,46 @@ +package com.beust.kobalt.misc + +import java.lang.Long +import java.lang.NumberFormatException +import java.util.* + +/** + * Compare string versions, e.g. "1.2.0", "0.9", etc... + */ +class StringVersion(val version: String) : Comparable { + override fun compareTo(other: StringVersion): Int { + val s1 = arrayListOf().apply { addAll(version.split('.')) } + val s2 = arrayListOf().apply { addAll(other.version.split('.')) } + + // Normalize both strings, so they have the same length, e.g. 1 -> 1.0.0 + val max = Math.max(s1.size, s2.size) + val shorterList : ArrayList = if (s1.size == max) s2 else s1 + repeat(max - shorterList.size) { + shorterList.add("0") + } + + // Compare each section + repeat(max) { index -> + try { + fun parse(s: String) = Long.parseLong(s.filter(Char::isDigit)) + + val v1 = parse(s1[index]) + val v2 = parse(s2[index]) + if (v1 < v2) return -1 + else if (v1 > v2) return 1 + } catch(ex: NumberFormatException) { + warn("Couldn't parse version $version or $other") + return -1 + } + } + return 0 + } + + override fun equals(other: Any?) = + if (other is StringVersion) this.compareTo(other) == 0 + else false + + override fun hashCode() = version.hashCode() + + override fun toString() = version +} 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 f915723f..205c2ced 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 @@ -1,79 +1,10 @@ package com.beust.kobalt.misc import com.beust.kobalt.maven.MavenId -import com.google.common.base.CharMatcher -import java.lang.Character -import java.lang.IllegalStateException -import java.lang.Integer -import java.lang.Math -import java.lang.NumberFormatException +import java.lang.* import java.math.BigInteger import java.util.* -/** - * Allow to compare string versions. - */ -class StringVersion(val version: String) { - val array = version.split('.') - - enum class Compare { LT, EQ, GT } - - fun compareTo(other: String) : Compare { - val s1 = arrayListOf().apply { addAll(version.split('.')) } - val s2 = arrayListOf().apply { addAll(other.split('.')) } - val max = Math.max(s1.size, s2.size) - val shorterList : ArrayList = if (s1.size == max) s2 else s1 - repeat(max - shorterList.size) { - shorterList.add("0") - } - - repeat(max) { index -> - try { - val v1 = Integer.parseInt(s1[index]) - val v2 = Integer.parseInt(s2[index]) - if (v1 < v2) return Compare.LT - else if (v1 > v2) return Compare.GT - } catch(ex: NumberFormatException) { - warn("Couldn't parse version $version or $other") - return Compare.LT - } - } - return Compare.EQ - } -} - -class Versions { - companion object { - /** - * Turn "6.9.4" into 600090004 - */ - fun toLongVersion(version: String) : Long { - val count = version.countChar('.') - val normalizedVersion = - if (count == 2) version else if (count == 1) version + ".0" - else version + ".0.0" - - fun parseLong(s: String, radix: Int) : Long { - try { - return java.lang.Long.parseLong(s, radix) - } catch(ex: NumberFormatException) { - warn("Couldn't parse version \"$version\"") - return 0L - } - } - - return normalizedVersion - .split('.') - .take(3) - .map { - val s = CharMatcher.inRange('0', '9').or(CharMatcher.`is`('.')).retainFrom(it) - parseLong(s, 10) - } - .fold(0L, { n, s -> s + n * 10000 }) - } - } -} - class Version(val version: String, val snapshotTimestamp: String? = null): Comparable { companion object { diff --git a/src/main/kotlin/com/beust/kobalt/app/UpdateKobalt.kt b/src/main/kotlin/com/beust/kobalt/app/UpdateKobalt.kt index 838d37f1..b4528270 100644 --- a/src/main/kotlin/com/beust/kobalt/app/UpdateKobalt.kt +++ b/src/main/kotlin/com/beust/kobalt/app/UpdateKobalt.kt @@ -39,8 +39,8 @@ class UpdateKobalt @Inject constructor(val github: GithubApi2, val wrapperProper try { val latestVersionString = latestVersionFuture.get() - val latestVersion = Versions.toLongVersion(latestVersionString) - val current = Versions.toLongVersion(Kobalt.version) + val latestVersion = StringVersion(latestVersionString) + val current = StringVersion(Kobalt.version) val distFile = File(KFiles.distributionsDir) if (latestVersion > current) { if (distFile.exists()) { diff --git a/src/main/kotlin/com/beust/kobalt/app/remote/RemoteDependencyData.kt b/src/main/kotlin/com/beust/kobalt/app/remote/RemoteDependencyData.kt index 73aca2c5..68bb5914 100644 --- a/src/main/kotlin/com/beust/kobalt/app/remote/RemoteDependencyData.kt +++ b/src/main/kotlin/com/beust/kobalt/app/remote/RemoteDependencyData.kt @@ -12,7 +12,7 @@ import com.beust.kobalt.internal.build.BuildFile import com.beust.kobalt.maven.DependencyManager import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.KobaltExecutors -import com.beust.kobalt.misc.Versions +import com.beust.kobalt.misc.StringVersion import com.beust.kobalt.misc.log import com.google.inject.Inject import java.io.File @@ -90,7 +90,7 @@ class RemoteDependencyData @Inject constructor(val executors: KobaltExecutors, v val currentLatest = latestVersions[shortId] if (currentLatest == null) latestVersions[shortId] = mid.version!! else mid.version?.let { v -> - if (Versions.toLongVersion(currentLatest) < Versions.toLongVersion(v)) { + if (StringVersion(currentLatest) < StringVersion(v)) { latestVersions[shortId] = v } } diff --git a/src/test/kotlin/com/beust/kobalt/internal/StringVersionTest.kt b/src/test/kotlin/com/beust/kobalt/internal/StringVersionTest.kt new file mode 100644 index 00000000..fa5596e1 --- /dev/null +++ b/src/test/kotlin/com/beust/kobalt/internal/StringVersionTest.kt @@ -0,0 +1,56 @@ +package com.beust.kobalt.internal + +import com.beust.kobalt.misc.StringVersion +import org.assertj.core.api.Assertions.assertThat +import org.testng.annotations.DataProvider +import org.testng.annotations.Test + + +/** + * Make sure we parse version numbers correctly. + */ +class StringVersionTest { + + @DataProvider + fun versionsEqual() : Array> + = arrayOf( + arrayOf("1.0", "1"), + arrayOf("1", "1"), + arrayOf("1.0.0", "1"), + arrayOf("1.0", "1.0.0") + ) + + @Test(dataProvider = "versionsEqual") + fun versionComparisonsEqual(v1: String, v2: String) { + val sv1 = StringVersion(v1) + val sv2 = StringVersion(v2) + assertThat(sv1).isEqualTo(sv2) + assertThat(sv2).isEqualTo(sv1) + } + + @DataProvider + fun versionsNotEqual() : Array> + = arrayOf( + arrayOf("0.9", "1"), + arrayOf("0.9.2", "1"), + arrayOf("1", "1.2.3"), + arrayOf("1.2", "1.2.3"), + arrayOf("1.2.2", "1.2.3"), + arrayOf("1.2.3", "1.2.4"), + arrayOf("1", "1.2.3.4"), + arrayOf("1.2", "1.2.3.4"), + arrayOf("1.2.3", "1.2.3.4"), + arrayOf("1.2.3.3", "1.2.3.4"), + arrayOf("1.2.3.4", "1.2.3.5"), + arrayOf("4.5.0.201609210915-r", "4.5.0.201609210916-r") + ) + + @Test(dataProvider = "versionsNotEqual") + fun versionComparisonsNotEqual(v1: String, v2: String) { + val sv1 = StringVersion(v1) + val sv2 = StringVersion(v2) + assertThat(sv1).isLessThan(sv2) + assertThat(sv2).isGreaterThan(sv1) + assertThat(sv1).isNotEqualTo(sv2) + } +} diff --git a/src/test/kotlin/com/beust/kobalt/internal/VersionTest.kt b/src/test/kotlin/com/beust/kobalt/internal/VersionTest.kt deleted file mode 100644 index ab275e7f..00000000 --- a/src/test/kotlin/com/beust/kobalt/internal/VersionTest.kt +++ /dev/null @@ -1,63 +0,0 @@ -package com.beust.kobalt.internal - -import com.beust.kobalt.misc.StringVersion -import com.beust.kobalt.misc.Versions -import org.assertj.core.api.Assertions.assertThat -import org.testng.annotations.DataProvider -import org.testng.annotations.Test - - -/** - * Make sure we parse version numbers correctly. - */ -class VersionTest { - - @DataProvider - fun dp() : Array> - = arrayOf( - arrayOf("0.938", 9380000), - arrayOf("1.2", 100020000L), - arrayOf("1.2.3", 100020003L) - ) - - @Test(dataProvider = "dp") - fun versionConversionShouldWork(version: String, expected: Long) { - assertThat(Versions.toLongVersion(version)).isEqualTo(expected) - } - - @DataProvider - fun versionsEqual() : Array> - = arrayOf( - arrayOf("1", "1"), - arrayOf("1.0", "1"), - arrayOf("1.0.0", "1"), - arrayOf("1.0", "1.0.0") - ) - - @Test(dataProvider = "versionsEqual") - fun versionComparisonsEqual(v1: String, v2: String) { - assertThat(StringVersion(v1).compareTo(v2)).isEqualTo(StringVersion.Compare.EQ) - assertThat(StringVersion(v2).compareTo(v1)).isEqualTo(StringVersion.Compare.EQ) - } - - @DataProvider - fun versionsNotEqual() : Array> - = arrayOf( - arrayOf("1", "1.2.3", StringVersion.Compare.LT), - arrayOf("1.2", "1.2.3", StringVersion.Compare.LT), - arrayOf("1.2.2", "1.2.3", StringVersion.Compare.LT), - arrayOf("1.2.4", "1.2.3", StringVersion.Compare.GT), - arrayOf("1", "1.2.3.4", StringVersion.Compare.LT), - arrayOf("1.2", "1.2.3.4", StringVersion.Compare.LT), - arrayOf("1.2.3", "1.2.3.4", StringVersion.Compare.LT), - arrayOf("1.2.3.3", "1.2.3.4", StringVersion.Compare.LT), - arrayOf("1.2.3.5", "1.2.3.4", StringVersion.Compare.GT) - ) - - @Test(dataProvider = "versionsNotEqual") - fun versionComparisonsNotEqual(v1: String, v2: String, expected: StringVersion.Compare) { - assertThat(StringVersion(v1).compareTo(v2)).isEqualTo(expected) - assertThat(StringVersion(v2).compareTo(v1)).isEqualTo( - if (expected == StringVersion.Compare.LT) StringVersion.Compare.GT else StringVersion.Compare.LT) - } -} diff --git a/src/test/kotlin/com/beust/kobalt/maven/DependencyTest.kt b/src/test/kotlin/com/beust/kobalt/maven/DependencyTest.kt index f52f9518..9cbf3ce1 100644 --- a/src/test/kotlin/com/beust/kobalt/maven/DependencyTest.kt +++ b/src/test/kotlin/com/beust/kobalt/maven/DependencyTest.kt @@ -2,7 +2,7 @@ package com.beust.kobalt.maven import com.beust.kobalt.TestModule import com.beust.kobalt.misc.KobaltExecutors -import com.beust.kobalt.misc.Versions +import com.beust.kobalt.misc.StringVersion import org.testng.Assert import org.testng.annotations.* import java.util.concurrent.ExecutorService @@ -40,10 +40,10 @@ class DependencyTest @Inject constructor(val executors: KobaltExecutors) { @Test(dataProvider = "dpVersions") fun versionSorting(k: String, v: String) { - val dep1 = Versions.toLongVersion(k) - val dep2 = Versions.toLongVersion(v) - Assert.assertTrue(dep1.compareTo(dep2) < 0) - Assert.assertTrue(dep2.compareTo(dep1) > 0) + val dep1 = StringVersion(k) + val dep2 = StringVersion(v) + Assert.assertTrue(dep1 < dep2) + Assert.assertTrue(dep2 > dep1) } }