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

Better StringVersion.

This commit is contained in:
Cedric Beust 2017-03-22 09:26:24 -07:00
parent 71d4cce999
commit c7714a5286
11 changed files with 119 additions and 189 deletions

View file

@ -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<IClasspathDependency>,
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(

View file

@ -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
}

View file

@ -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<Dependencies.ExcludeConfig>()
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()

View file

@ -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) {

View file

@ -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<StringVersion> {
override fun compareTo(other: StringVersion): Int {
val s1 = arrayListOf<String>().apply { addAll(version.split('.')) }
val s2 = arrayListOf<String>().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<String> = 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
}

View file

@ -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<String>().apply { addAll(version.split('.')) }
val s2 = arrayListOf<String>().apply { addAll(other.split('.')) }
val max = Math.max(s1.size, s2.size)
val shorterList : ArrayList<String> = 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<Version> {
companion object {