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

Evaluate variables in version tags.

This commit is contained in:
Cedric Beust 2016-03-22 02:34:21 +04:00
parent 901dbd1b95
commit a3c9792c12
3 changed files with 44 additions and 26 deletions

View file

@ -1,7 +1,7 @@
package com.beust.kobalt.maven
import com.beust.kobalt.misc.log
import com.beust.kobalt.misc.toString
import com.beust.kobalt.misc.warn
import com.google.inject.assistedinject.Assisted
import kotlinx.dom.childElements
import org.w3c.dom.Element
@ -18,6 +18,33 @@ class Pom @javax.inject.Inject constructor(@Assisted val id: String,
var artifactId: String? = null
var packaging: String? = null
var version: String? = null
/**
* If the version is a string, extract it, look it up and evaluate it if we find it. Otherwise, error.
*/
private fun calculateVersion(s: String) : String {
val v = extractVar(s)
if (v != null) {
val value = properties[v]
if (value != null) {
return value
} else {
warn("Unknown variable for version: " + s)
return ""
}
} else {
return s
}
}
private fun extractVar(s: String) : String? {
if (s.startsWith("\${") && s.endsWith("}")) {
return s.substring(2, s.length - 1)
} else {
return null
}
}
var name: String? = null
var properties = sortedMapOf<String, String>()
var repositories = listOf<String>()
@ -29,28 +56,9 @@ class Pom @javax.inject.Inject constructor(@Assisted val id: String,
data class Dependency(val groupId: String, val artifactId: String, val packaging: String?,
val version: String, val optional: Boolean = false, val scope: String? = null) {
/** When a variable is used in a maven file, e.g. ${version} */
private val VAR = "$" + "{"
val mustDownload: Boolean
get() = !optional && "provided" != scope && "test" != scope
val isValid: Boolean
get() {
var result = false
if (version.contains(VAR)) {
log(3, "Skipping variable version ${this}")
} else if (groupId.contains(VAR)) {
log(3, "Skipping variable groupId ${this}")
} else if (artifactId.contains(VAR)) {
log(3, "Skipping variable artifactId ${this}")
} else {
result = true
}
return result
}
val id: String = "$groupId:$artifactId:$version"
}
@ -81,7 +89,7 @@ class Pom @javax.inject.Inject constructor(@Assisted val id: String,
var groupId: String? = null
var artifactId: String? = null
var packaging: String? = null
var version: String = ""
var readVersion: String = ""
var optional: Boolean? = false
var scope: String? = null
for (j in 0..d.length - 1) {
@ -91,16 +99,17 @@ class Pom @javax.inject.Inject constructor(@Assisted val id: String,
"groupId" -> groupId = e.textContent
"artifactId" -> artifactId = e.textContent
"type" -> packaging = e.textContent
"version" -> version = e.textContent
"version" -> readVersion = e.textContent
"optional" -> optional = "true".equals(e.textContent, true)
"scope" -> scope = e.textContent
}
}
}
val version = calculateVersion(readVersion)
val tmpDependency = Dependency(groupId!!, artifactId!!, packaging, version, optional!!, scope)
dependencies.add(tmpDependency)
}
}
override fun toString() = toString("Pom", id, "id")
override fun toString() = toString("Pom", "id", id)
}

View file

@ -120,8 +120,9 @@ class MavenDependency @Inject constructor(
override fun directDependencies() : List<IClasspathDependency> {
val result = arrayListOf<IClasspathDependency>()
try {
pomFactory.create(id, pomFile.get()).dependencies.filter {
it.mustDownload && it.isValid
val pom = pomFactory.create(id, pomFile.get())
pom.dependencies.filter {
it.mustDownload
}.forEach {
result.add(create(MavenId.toId(it.groupId, it.artifactId, it.packaging, it.version)))
}

View file

@ -22,7 +22,7 @@ class DownloadTest @Inject constructor(
val depFactory: DepFactory,
val localRepo: LocalRepo,
val mdFactory: MavenDependency.IFactory,
val finderFactory: RepoFinderCallable.IFactory,
val dependencyManager: DependencyManager,
val executors: KobaltExecutors) : KobaltTest() {
private var executor: ExecutorService by Delegates.notNull()
@ -165,5 +165,13 @@ class DownloadTest @Inject constructor(
// Assert.assertEquals(results.size, 1)
// Assert.assertEquals(results[0].version, "1.1.0")
}
@Test
fun variablesShouldBeExpanded() {
val dep = dependencyManager.createMaven("org.mapdb:mapdb:3.0.0-M3")
val closure = dependencyManager.transitiveClosure(listOf(dep))
val d = closure.filter { it.id.contains("eclipse-collections-api")}
Assert.assertEquals(d.size, 1)
}
}