From 5f739035e0e78ebde638700eb6b12e77e633d906 Mon Sep 17 00:00:00 2001 From: evanchooly Date: Tue, 6 Oct 2015 21:14:33 -0400 Subject: [PATCH 1/6] maven pom importing --- src/main/kotlin/com/beust/kobalt/Main.kt | 1 + .../com/beust/kobalt/ProjectGenerator.kt | 32 ++++ src/main/kotlin/com/beust/kobalt/maven/Pom.kt | 10 +- src/main/resources/build-template.mustache | 5 + .../kotlin/com/beust/kobalt/maven/PomTest.kt | 19 +++ src/test/resources/pom.xml | 152 ++++++++++++++++++ 6 files changed, 216 insertions(+), 3 deletions(-) create mode 100644 src/test/kotlin/com/beust/kobalt/maven/PomTest.kt create mode 100644 src/test/resources/pom.xml diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index 484b33ef..7dc11647 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -55,6 +55,7 @@ private class Main @Inject constructor( println(Banner.get() + Kobalt.version + "\n") // runTest() runWithArgs(jc, args) + log(1, "************ shutting down executors") executors.shutdown() debug("All done") }) diff --git a/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt b/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt index f46968c2..7a7c2129 100644 --- a/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt @@ -75,14 +75,46 @@ public class ProjectGenerator : KobaltLogger { put("version", pom.version ?: "0.1") put("name", pom.name ?: pom.artifactId) } + + val properties = pom.properties + val mapped = properties.entrySet().toMap({it.key}, {translate(it.key)}) + + map.put("properties", properties + .entrySet() + .map({ Pair(mapped.get(it.key), it.value) })) val partition = pom.dependencies.groupBy { it.scope } .flatMap { it.value } + .map { updateVersion(it, mapped) } .sortedBy { it.groupId + ":" + it.artifactId } .partition { it.scope != "test" } + mainDeps.addAll(partition.first) testDeps.addAll(partition.second) } + private fun updateVersion(dep: Dependency, mapped: Map): Dependency { + if ( dep.version.startsWith("\${")) { + val property = dep.version.substring(2, dep.version.length() - 1) + println("property = ${property}") + return Dependency(dep.groupId, dep.artifactId, "\${${mapped.get(property)}}", dep.optional, dep.scope) + } else { + return dep + } + } + + private fun translate(key: String) : String { + val split = key.split('.') + return split.mapIndexed( { index, value -> if (index == 0) value else value.upperFirst() }).join("") + } + + private fun String.upperFirst(): String { + if (this.isBlank()) { + return this + } else { + return this.substring(0, 1).toUpperCase() + this.substring(1) + } + } + /** * Detect all the languages contained in this project. */ diff --git a/src/main/kotlin/com/beust/kobalt/maven/Pom.kt b/src/main/kotlin/com/beust/kobalt/maven/Pom.kt index d5537735..deb7bbdd 100644 --- a/src/main/kotlin/com/beust/kobalt/maven/Pom.kt +++ b/src/main/kotlin/com/beust/kobalt/maven/Pom.kt @@ -5,12 +5,10 @@ import com.beust.kobalt.misc.ToString import com.google.inject.assistedinject.Assisted import org.w3c.dom.Element import org.w3c.dom.NodeList -import org.w3c.dom.Text import org.xml.sax.InputSource import java.io.FileReader import javax.xml.xpath.XPathConstants -import kotlin.dom.get -import kotlin.dom.toXmlString +import kotlin.dom.childElements public class Pom @javax.inject.Inject constructor(@Assisted val id: String, @Assisted documentFile: java.io.File) : KobaltLogger { @@ -65,6 +63,12 @@ public class Pom @javax.inject.Inject constructor(@Assisted val id: String, version = XPATH.compile("/project/version").evaluate(document) name = XPATH.compile("/project/name").evaluate(document) + var list = XPATH.compile("/project/properties").evaluate(document, XPathConstants.NODESET) as NodeList + var elem = list.item(0) as Element? + elem.childElements().forEach { + properties.put(it.nodeName, it.textContent) + } + val deps = DEPENDENCIES.evaluate(document, XPathConstants.NODESET) as NodeList for (i in 0..deps.getLength() - 1) { val d = deps.item(i) as NodeList diff --git a/src/main/resources/build-template.mustache b/src/main/resources/build-template.mustache index 89a96586..79d8e088 100644 --- a/src/main/resources/build-template.mustache +++ b/src/main/resources/build-template.mustache @@ -2,7 +2,12 @@ import com.beust.kobalt.* import com.beust.kobalt.plugin.packaging.assemble {{imports}} +{{#properties}} +val {{first}} = {{second}} +{{/properties}} + val p = {{directive}} { + name = "{{name}}" group = "{{group}}" artifactId = name diff --git a/src/test/kotlin/com/beust/kobalt/maven/PomTest.kt b/src/test/kotlin/com/beust/kobalt/maven/PomTest.kt new file mode 100644 index 00000000..376cd79a --- /dev/null +++ b/src/test/kotlin/com/beust/kobalt/maven/PomTest.kt @@ -0,0 +1,19 @@ +package com.beust.kobalt.maven + +import org.testng.Assert +import org.testng.annotations.Test +import java.io.File + +class PomTest { + @Test + fun importPom() { + val pom = Pom("testing", File("src/test/resources/pom.xml")); + + 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.assertEquals(pom.properties.get("commons.version"), "2.1.1") + Assert.assertEquals(pom.properties.get("guice.version"), "4.0") + } +} \ No newline at end of file diff --git a/src/test/resources/pom.xml b/src/test/resources/pom.xml new file mode 100644 index 00000000..43be77d6 --- /dev/null +++ b/src/test/resources/pom.xml @@ -0,0 +1,152 @@ + + 4.0.0 + + com.foo.bob + rawr + jar + 1.2.3 + + rawr + + + + sonatype + https://oss.sonatype.org/content/repositories/snapshots/ + + + + + install + + + src/main/resources + + something.properties + + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/test/kotlin + + + org.apache.maven.plugins + maven-compiler-plugin + 2.5.1 + true + + 1.8 + 1.8 + + + + org.jetbrains.kotlin + kotlin-maven-plugin + ${kotlin.version} + + + + compile + compile + + compile + + + + + test-compile + test-compile + + test-compile + + + + + + org.apache.maven.plugins + maven-release-plugin + 2.4.1 + + + + org.codehaus.mojo + exec-maven-plugin + 1.2.1 + + + com.foo.bob.Rawr + test + + -Xmx512m + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.4 + + + + true + lib/ + com.foo.bob.Rawr + + + + + + + + + org.apache.commons + commons-email + ${commons.version} + + + + com.google.inject + guice + no_aop + ${guice.version} + + + + org.slf4j + slf4j-api + ${slf4j.version} + + + + org.testng + testng + 6.8.7 + test + + + + + + + + org.codehaus.mojo + cobertura-maven-plugin + 2.5.2 + + + org.apache.maven.plugins + maven-surefire-report-plugin + 2.16 + + false + + + + + + + 2.1.1 + 4.0 + UTF-8 + + From 87d8b8621ea5993d4ad9f8f5cf0d40f841029ece Mon Sep 17 00:00:00 2001 From: evanchooly Date: Tue, 6 Oct 2015 21:23:34 -0400 Subject: [PATCH 2/6] removed debugging --- src/main/kotlin/com/beust/kobalt/Main.kt | 1 - src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt | 1 - 2 files changed, 2 deletions(-) diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index 7dc11647..484b33ef 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -55,7 +55,6 @@ private class Main @Inject constructor( println(Banner.get() + Kobalt.version + "\n") // runTest() runWithArgs(jc, args) - log(1, "************ shutting down executors") executors.shutdown() debug("All done") }) diff --git a/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt b/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt index 7a7c2129..39ca4829 100644 --- a/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt @@ -95,7 +95,6 @@ public class ProjectGenerator : KobaltLogger { private fun updateVersion(dep: Dependency, mapped: Map): Dependency { if ( dep.version.startsWith("\${")) { val property = dep.version.substring(2, dep.version.length() - 1) - println("property = ${property}") return Dependency(dep.groupId, dep.artifactId, "\${${mapped.get(property)}}", dep.optional, dep.scope) } else { return dep From cfe4d62c30e3f66b826f3888239ab977d57068d5 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Tue, 6 Oct 2015 06:02:32 -0700 Subject: [PATCH 3/6] Refactor. --- src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt b/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt index 39ca4829..0ed7d919 100644 --- a/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/ProjectGenerator.kt @@ -82,6 +82,7 @@ public class ProjectGenerator : KobaltLogger { map.put("properties", properties .entrySet() .map({ Pair(mapped.get(it.key), it.value) })) + val partition = pom.dependencies.groupBy { it.scope } .flatMap { it.value } .map { updateVersion(it, mapped) } From a8a4ee0a99ffc0fb3cb55fc6b6720111739815d4 Mon Sep 17 00:00:00 2001 From: evanchooly Date: Tue, 6 Oct 2015 21:14:33 -0400 Subject: [PATCH 4/6] maven pom importing --- src/main/kotlin/com/beust/kobalt/Main.kt | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index 484b33ef..7dc11647 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -55,6 +55,7 @@ private class Main @Inject constructor( println(Banner.get() + Kobalt.version + "\n") // runTest() runWithArgs(jc, args) + log(1, "************ shutting down executors") executors.shutdown() debug("All done") }) From bfc30dda3c43e6267952464d92bc6fe8415a8c9d Mon Sep 17 00:00:00 2001 From: evanchooly Date: Tue, 6 Oct 2015 21:23:34 -0400 Subject: [PATCH 5/6] removed debugging --- src/main/kotlin/com/beust/kobalt/Main.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index 7dc11647..484b33ef 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -55,7 +55,6 @@ private class Main @Inject constructor( println(Banner.get() + Kobalt.version + "\n") // runTest() runWithArgs(jc, args) - log(1, "************ shutting down executors") executors.shutdown() debug("All done") }) From 39102cd3f337c9fa76856d59cb851bba1c39f82f Mon Sep 17 00:00:00 2001 From: evanchooly Date: Tue, 6 Oct 2015 22:15:46 -0400 Subject: [PATCH 6/6] the properties need quotes --- src/main/resources/build-template.mustache | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/build-template.mustache b/src/main/resources/build-template.mustache index 79d8e088..663f032c 100644 --- a/src/main/resources/build-template.mustache +++ b/src/main/resources/build-template.mustache @@ -3,7 +3,7 @@ import com.beust.kobalt.plugin.packaging.assemble {{imports}} {{#properties}} -val {{first}} = {{second}} +val {{first}} = "{{second}}" {{/properties}} val p = {{directive}} {