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

Merge pull request #14 from evanchooly/master

import repositories from the pom
This commit is contained in:
Cedric Beust 2015-10-12 12:51:15 -07:00
commit 08fafcff33
6 changed files with 96 additions and 24 deletions

View file

@ -19,6 +19,15 @@ import java.util.HashMap
* Generate a new project.
*/
public class ProjectGenerator : KobaltLogger {
companion object {
/**
* Turns a dot property into a proper Kotlin identifier, e.g. common.version -> commonVersion
*/
fun translate(key: String): String {
return key.split('.').mapIndexed( { index, value -> if (index == 0) value else value.upperFirst() }).join("")
}
}
fun run(args: Args) {
if (File(args.buildFile).exists()) {
log(1, "Build file ${args.buildFile} already exists, not overwriting it")
@ -52,7 +61,7 @@ public class ProjectGenerator : KobaltLogger {
map.put("mainDependencies", mainDeps)
map.put("testDependencies", testDeps)
File("pom.xml").let {
if (it.exists()) {
if (it.absoluteFile.exists()) {
importPom(it, mainDeps, testDeps, map)
}
}
@ -68,12 +77,13 @@ public class ProjectGenerator : KobaltLogger {
private fun importPom(pomFile: File, mainDeps: ArrayList<Dependency>, testDeps: ArrayList<Dependency>,
map: HashMap<String, Any?>) {
var pom = Pom("imported", pomFile)
var pom = Pom("imported", pomFile.absoluteFile)
with(map) {
put("group", pom.groupId ?: "com.example")
put("artifactId", pom.artifactId ?: "com.example")
put("version", pom.version ?: "0.1")
put("name", pom.name ?: pom.artifactId)
put("repositories", pom.repositories.map({ "\"${it}\"" }).join(","))
}
val properties = pom.properties
@ -101,18 +111,6 @@ public class ProjectGenerator : KobaltLogger {
dep
}
/**
* Turns a dot property into a proper Kotlin identifier, e.g. common.version -> commonVersion
*/
private fun translate(key: String) =
key.split('.').mapIndexed( { index, value -> if (index == 0) value else value.upperFirst() }).join("")
private fun String.upperFirst() =
if (this.isBlank()) {
this
} else {
this.substring(0, 1).toUpperCase() + this.substring(1)
}
/**
* Detect all the languages contained in this project.
@ -129,3 +127,7 @@ public class ProjectGenerator : KobaltLogger {
return result.map { it.first }
}
}
private fun String.upperFirst(): String {
return if (this.isBlank()) this else this.substring(0, 1).toUpperCase() + this.substring(1)
}

View file

@ -45,7 +45,7 @@ public class Kobalt {
val repos = ArrayList<String>(DEFAULT_REPOS)
fun addRepo(repo: String) = repos.add(repo)
fun addRepo(repo: String) = repos.add(if (repo.endsWith("/")) repo else repo + "/")
private val PROPERTY_KOBALT_VERSION = "kobalt.version"
private val KOBALT_PROPERTIES = "kobalt.properties"

View file

@ -11,7 +11,7 @@ import javax.xml.xpath.XPathConstants
import kotlin.dom.childElements
public class Pom @javax.inject.Inject constructor(@Assisted val id: String,
@Assisted documentFile: java.io.File) : KobaltLogger {
@Assisted documentFile: java.io.File) : KobaltLogger {
val XPATH_FACTORY = javax.xml.xpath.XPathFactory.newInstance()
val XPATH = XPATH_FACTORY.newXPath()
var groupId: String? = null
@ -19,21 +19,22 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String,
var version: String? = null
var name: String? = null
var properties = sortedMapOf<String, String>()
var repositories = listOf<String>()
public interface IFactory {
fun create(@Assisted id: String, @Assisted documentFile : java.io.File) : Pom
fun create(@Assisted id: String, @Assisted documentFile: java.io.File): Pom
}
data public class Dependency(val groupId: String, val artifactId: String, val version: String,
val optional: Boolean = false, val scope: String? = null) : KobaltLogger {
val optional: Boolean = false, val scope: String? = null) : KobaltLogger {
/** When a variable is used in a maven file, e.g. ${version} */
private val VAR = "$" + "{"
val mustDownload: Boolean
get() = ! optional && "provided" != scope && "test" != scope
get() = !optional && "provided" != scope && "test" != scope
val isValid : Boolean
val isValid: Boolean
get() {
var result = false
if (version.contains(VAR)) {
@ -62,10 +63,14 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String,
artifactId = XPATH.compile("/project/artifactId").evaluate(document)
version = XPATH.compile("/project/version").evaluate(document)
name = XPATH.compile("/project/name").evaluate(document)
var repositoriesList = XPATH.compile("/project/repositories").evaluate(document, XPathConstants.NODESET) as NodeList
var repoElem = repositoriesList.item(0) as Element?
repositories = repoElem.childElements()
.map({ it.getElementsByTagName("url").item(0).textContent })
var list = XPATH.compile("/project/properties").evaluate(document, XPathConstants.NODESET) as NodeList
var elem = list.item(0) as Element?
elem.childElements().forEach {
val propertiesList = XPATH.compile("/project/properties").evaluate(document, XPathConstants.NODESET) as NodeList
var propsElem = propertiesList.item(0) as Element?
propsElem.childElements().forEach {
properties.put(it.nodeName, it.textContent)
}

View file

@ -2,6 +2,8 @@ import com.beust.kobalt.*
import com.beust.kobalt.plugin.packaging.assemble
{{imports}}
val repos = repos({{{repositories}}})
{{#properties}}
val {{first}} = "{{second}}"
{{/properties}}