mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 00:17:11 -07:00
Merge pull request #14 from evanchooly/master
import repositories from the pom
This commit is contained in:
commit
08fafcff33
6 changed files with 96 additions and 24 deletions
|
@ -19,6 +19,15 @@ import java.util.HashMap
|
||||||
* Generate a new project.
|
* Generate a new project.
|
||||||
*/
|
*/
|
||||||
public class ProjectGenerator : KobaltLogger {
|
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) {
|
fun run(args: Args) {
|
||||||
if (File(args.buildFile).exists()) {
|
if (File(args.buildFile).exists()) {
|
||||||
log(1, "Build file ${args.buildFile} already exists, not overwriting it")
|
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("mainDependencies", mainDeps)
|
||||||
map.put("testDependencies", testDeps)
|
map.put("testDependencies", testDeps)
|
||||||
File("pom.xml").let {
|
File("pom.xml").let {
|
||||||
if (it.exists()) {
|
if (it.absoluteFile.exists()) {
|
||||||
importPom(it, mainDeps, testDeps, map)
|
importPom(it, mainDeps, testDeps, map)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -68,12 +77,13 @@ public class ProjectGenerator : KobaltLogger {
|
||||||
|
|
||||||
private fun importPom(pomFile: File, mainDeps: ArrayList<Dependency>, testDeps: ArrayList<Dependency>,
|
private fun importPom(pomFile: File, mainDeps: ArrayList<Dependency>, testDeps: ArrayList<Dependency>,
|
||||||
map: HashMap<String, Any?>) {
|
map: HashMap<String, Any?>) {
|
||||||
var pom = Pom("imported", pomFile)
|
var pom = Pom("imported", pomFile.absoluteFile)
|
||||||
with(map) {
|
with(map) {
|
||||||
put("group", pom.groupId ?: "com.example")
|
put("group", pom.groupId ?: "com.example")
|
||||||
put("artifactId", pom.artifactId ?: "com.example")
|
put("artifactId", pom.artifactId ?: "com.example")
|
||||||
put("version", pom.version ?: "0.1")
|
put("version", pom.version ?: "0.1")
|
||||||
put("name", pom.name ?: pom.artifactId)
|
put("name", pom.name ?: pom.artifactId)
|
||||||
|
put("repositories", pom.repositories.map({ "\"${it}\"" }).join(","))
|
||||||
}
|
}
|
||||||
|
|
||||||
val properties = pom.properties
|
val properties = pom.properties
|
||||||
|
@ -101,18 +111,6 @@ public class ProjectGenerator : KobaltLogger {
|
||||||
dep
|
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.
|
* Detect all the languages contained in this project.
|
||||||
|
@ -129,3 +127,7 @@ public class ProjectGenerator : KobaltLogger {
|
||||||
return result.map { it.first }
|
return result.map { it.first }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun String.upperFirst(): String {
|
||||||
|
return if (this.isBlank()) this else this.substring(0, 1).toUpperCase() + this.substring(1)
|
||||||
|
}
|
||||||
|
|
|
@ -45,7 +45,7 @@ public class Kobalt {
|
||||||
|
|
||||||
val repos = ArrayList<String>(DEFAULT_REPOS)
|
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 PROPERTY_KOBALT_VERSION = "kobalt.version"
|
||||||
private val KOBALT_PROPERTIES = "kobalt.properties"
|
private val KOBALT_PROPERTIES = "kobalt.properties"
|
||||||
|
|
|
@ -11,7 +11,7 @@ import javax.xml.xpath.XPathConstants
|
||||||
import kotlin.dom.childElements
|
import kotlin.dom.childElements
|
||||||
|
|
||||||
public class Pom @javax.inject.Inject constructor(@Assisted val id: String,
|
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_FACTORY = javax.xml.xpath.XPathFactory.newInstance()
|
||||||
val XPATH = XPATH_FACTORY.newXPath()
|
val XPATH = XPATH_FACTORY.newXPath()
|
||||||
var groupId: String? = null
|
var groupId: String? = null
|
||||||
|
@ -19,21 +19,22 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String,
|
||||||
var version: String? = null
|
var version: String? = null
|
||||||
var name: String? = null
|
var name: String? = null
|
||||||
var properties = sortedMapOf<String, String>()
|
var properties = sortedMapOf<String, String>()
|
||||||
|
var repositories = listOf<String>()
|
||||||
|
|
||||||
public interface IFactory {
|
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,
|
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} */
|
/** When a variable is used in a maven file, e.g. ${version} */
|
||||||
private val VAR = "$" + "{"
|
private val VAR = "$" + "{"
|
||||||
|
|
||||||
val mustDownload: Boolean
|
val mustDownload: Boolean
|
||||||
get() = ! optional && "provided" != scope && "test" != scope
|
get() = !optional && "provided" != scope && "test" != scope
|
||||||
|
|
||||||
val isValid : Boolean
|
val isValid: Boolean
|
||||||
get() {
|
get() {
|
||||||
var result = false
|
var result = false
|
||||||
if (version.contains(VAR)) {
|
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)
|
artifactId = XPATH.compile("/project/artifactId").evaluate(document)
|
||||||
version = XPATH.compile("/project/version").evaluate(document)
|
version = XPATH.compile("/project/version").evaluate(document)
|
||||||
name = XPATH.compile("/project/name").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
|
val propertiesList = XPATH.compile("/project/properties").evaluate(document, XPathConstants.NODESET) as NodeList
|
||||||
var elem = list.item(0) as Element?
|
var propsElem = propertiesList.item(0) as Element?
|
||||||
elem.childElements().forEach {
|
propsElem.childElements().forEach {
|
||||||
properties.put(it.nodeName, it.textContent)
|
properties.put(it.nodeName, it.textContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@ import com.beust.kobalt.*
|
||||||
import com.beust.kobalt.plugin.packaging.assemble
|
import com.beust.kobalt.plugin.packaging.assemble
|
||||||
{{imports}}
|
{{imports}}
|
||||||
|
|
||||||
|
val repos = repos({{{repositories}}})
|
||||||
|
|
||||||
{{#properties}}
|
{{#properties}}
|
||||||
val {{first}} = "{{second}}"
|
val {{first}} = "{{second}}"
|
||||||
{{/properties}}
|
{{/properties}}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
package com.beust.kobalt.maven
|
package com.beust.kobalt.maven
|
||||||
|
|
||||||
|
import com.beust.kobalt.Args
|
||||||
|
import com.beust.kobalt.ProjectGenerator
|
||||||
import org.testng.Assert
|
import org.testng.Assert
|
||||||
import org.testng.annotations.Test
|
import org.testng.annotations.Test
|
||||||
import java.io.File
|
import java.io.File
|
||||||
|
@ -7,7 +9,8 @@ import java.io.File
|
||||||
class PomTest {
|
class PomTest {
|
||||||
@Test
|
@Test
|
||||||
fun importPom() {
|
fun importPom() {
|
||||||
val pom = Pom("testing", File("src/test/resources/pom.xml"));
|
val pomSrc = File("src/test/resources/pom.xml")
|
||||||
|
val pom = Pom("testing", pomSrc);
|
||||||
|
|
||||||
Assert.assertEquals(pom.groupId, "com.foo.bob")
|
Assert.assertEquals(pom.groupId, "com.foo.bob")
|
||||||
Assert.assertEquals(pom.artifactId, "rawr")
|
Assert.assertEquals(pom.artifactId, "rawr")
|
||||||
|
@ -15,5 +18,54 @@ class PomTest {
|
||||||
Assert.assertEquals(pom.version, "1.2.3")
|
Assert.assertEquals(pom.version, "1.2.3")
|
||||||
Assert.assertEquals(pom.properties.get("commons.version"), "2.1.1")
|
Assert.assertEquals(pom.properties.get("commons.version"), "2.1.1")
|
||||||
Assert.assertEquals(pom.properties.get("guice.version"), "4.0")
|
Assert.assertEquals(pom.properties.get("guice.version"), "4.0")
|
||||||
|
|
||||||
|
validateGeneratedFile(pom, pomSrc)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun importBasicPom() {
|
||||||
|
val pomSrc = File("src/test/resources/pom-norepositories-properties.xml")
|
||||||
|
val pom = Pom("testing", pomSrc);
|
||||||
|
|
||||||
|
Assert.assertEquals(pom.groupId, "com.foo.bob")
|
||||||
|
Assert.assertEquals(pom.artifactId, "rawr")
|
||||||
|
Assert.assertEquals(pom.name, "rawr")
|
||||||
|
Assert.assertEquals(pom.version, "1.2.3")
|
||||||
|
Assert.assertTrue(pom.properties.isEmpty())
|
||||||
|
Assert.assertTrue(pom.repositories.isEmpty())
|
||||||
|
|
||||||
|
validateGeneratedFile(pom, pomSrc)
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun validateGeneratedFile(pom: Pom, pomSrc: File) {
|
||||||
|
val temp = File(System.getProperty("java.io.tmpdir"))
|
||||||
|
val original = System.getProperty("user.dir")
|
||||||
|
System.setProperty("user.dir", temp.absolutePath)
|
||||||
|
val pomFile = File(temp, "pom.xml")
|
||||||
|
pomFile.deleteOnExit()
|
||||||
|
pomSrc.copyTo(pomFile, true)
|
||||||
|
try {
|
||||||
|
val file = File(temp, "Build.kt")
|
||||||
|
file.delete()
|
||||||
|
|
||||||
|
file.deleteOnExit()
|
||||||
|
val args = Args()
|
||||||
|
args.buildFile = file.absolutePath
|
||||||
|
args.init = true
|
||||||
|
ProjectGenerator().run(args)
|
||||||
|
var contents = file.readText()
|
||||||
|
Assert.assertTrue(contents.contains("group = \"${pom.groupId}\""), "Should find the group defined")
|
||||||
|
Assert.assertTrue(contents.contains("name = \"${pom.name}\""), "Should find the name defined")
|
||||||
|
Assert.assertTrue(contents.contains("version = \"${pom.version}\""), "Should find the version defined")
|
||||||
|
pom.properties.forEach {
|
||||||
|
Assert.assertTrue(contents.contains("val ${ProjectGenerator.translate(it.key)} = \"${it.value}\""), "Should find the " +
|
||||||
|
"property defined")
|
||||||
|
}
|
||||||
|
pom.repositories.forEach {
|
||||||
|
Assert.assertTrue(contents.contains(it), "Should find the repository defined")
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
System.getProperty("user.dir", original)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
11
src/test/resources/pom-norepositories-properties.xml
Normal file
11
src/test/resources/pom-norepositories-properties.xml
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.foo.bob</groupId>
|
||||||
|
<artifactId>rawr</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<version>1.2.3</version>
|
||||||
|
|
||||||
|
<name>rawr</name>
|
||||||
|
</project>
|
Loading…
Add table
Add a link
Reference in a new issue