From f47c2bc8b3627afc3c19ea00883950e365bd0bf4 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Sat, 27 Feb 2016 19:23:29 -0800 Subject: [PATCH] More template work. --- .../com/beust/kobalt/app/BuildGenerator.kt | 34 ++++++++++++++++--- .../kobalt/app/java/JavaBuildGenerator.kt | 34 +++---------------- .../kobalt/app/kotlin/KotlinBuildGenerator.kt | 5 +++ src/main/resources/templates/build.mustache | 2 +- .../resources/templates/java/java.mustache | 4 +-- .../templates/kotlin/kotlin-test.mustache | 8 +++++ .../templates/kotlin/kotlin.mustache | 3 ++ 7 files changed, 53 insertions(+), 37 deletions(-) create mode 100644 src/main/resources/templates/kotlin/kotlin-test.mustache create mode 100644 src/main/resources/templates/kotlin/kotlin.mustache diff --git a/src/main/kotlin/com/beust/kobalt/app/BuildGenerator.kt b/src/main/kotlin/com/beust/kobalt/app/BuildGenerator.kt index 6605effd..2e9e4893 100644 --- a/src/main/kotlin/com/beust/kobalt/app/BuildGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/app/BuildGenerator.kt @@ -3,6 +3,7 @@ package com.beust.kobalt.app import com.beust.kobalt.Args import com.beust.kobalt.api.ITemplate import com.beust.kobalt.api.ITemplateContributor +import com.beust.kobalt.internal.Mustache import com.beust.kobalt.maven.Pom import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.log @@ -21,11 +22,33 @@ abstract class BuildGenerator : ITemplate { abstract val defaultTestDirectories : HashSet abstract val directive : String abstract val fileMatch : (String) -> Boolean - open fun generateAdditionalFiles(args: Args, classLoader: ClassLoader) {} + abstract val fileMap: List + abstract val mainClass : String + + class FileInfo(val dir: String, val fileName: String, val mustacheFileName: String) + + private fun generateAdditionalFiles(args: Args, classLoader: ClassLoader) { + val map = mapOf("packageName" to PACKAGE_NAME) + + fileMap.forEach { + val mustache = it.mustacheFileName + val fileInputStream = javaClass.classLoader + .getResource(ITemplateContributor.DIRECTORY_NAME + "/$templateName/$mustache").openStream() + val createdFile = File(KFiles.joinDir(it.dir, it.fileName)) + Mustache.generateFile(fileInputStream, File(KFiles.joinDir(it.dir, it.fileName)), map) + log(2, "Created $createdFile") + } + } private fun maybeGenerateAdditionalFiles(args: Args, classLoader: ClassLoader) { - val existingFiles = KFiles.findRecursively(File("."), fileMatch) - if (existingFiles.isEmpty()) { + val existingFiles = + if (File("src").exists()) { + KFiles.findRecursively(File("src"), fileMatch).size > 0 + } else { + false + } + + if (! existingFiles) { generateAdditionalFiles(args, classLoader) } } @@ -97,10 +120,11 @@ abstract class BuildGenerator : ITemplate { private val buildFileContent: String get() { - val map = hashMapOf() - map.put("directive", directive) val currentDir = File(".").absoluteFile.parentFile + val map = hashMapOf() with(map) { + put("mainClass", mainClass) + put("directive", directive) put("name", currentDir.name) put("group", PACKAGE_NAME) put("version", "0.1") diff --git a/src/main/kotlin/com/beust/kobalt/app/java/JavaBuildGenerator.kt b/src/main/kotlin/com/beust/kobalt/app/java/JavaBuildGenerator.kt index 5cd07255..e80353ec 100644 --- a/src/main/kotlin/com/beust/kobalt/app/java/JavaBuildGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/app/java/JavaBuildGenerator.kt @@ -1,12 +1,6 @@ package com.beust.kobalt.app.java -import com.beust.kobalt.Args -import com.beust.kobalt.api.ITemplateContributor import com.beust.kobalt.app.BuildGenerator -import com.beust.kobalt.internal.Mustache -import com.beust.kobalt.misc.KFiles -import com.beust.kobalt.misc.log -import java.io.File class JavaBuildGenerator: BuildGenerator() { override val defaultSourceDirectories = hashSetOf("src/main/java") @@ -16,27 +10,9 @@ class JavaBuildGenerator: BuildGenerator() { override val templateDescription = "Generate a simple Java project" override val fileMatch = { f: String -> f.endsWith(".java") } override val instructions = "Now you can run either `./kobaltw test` or `./kobaltw run`" - - override fun generateAdditionalFiles(args: Args, classLoader: ClassLoader) { - println("Generating Java files") - - class FileInfo(val dir: String, val fileName: String, val mustacheFileName: String) - - val fileMap = listOf( - FileInfo("src/main/java/" + PACKAGE_NAME.replace(".", "/"), "Example.java", "java.mustache"), - FileInfo("src/test/java/" + PACKAGE_NAME.replace(".", "/"), "ExampleTest.java", "java-test.mustache") - ) - - val map = mapOf("packageName" to PACKAGE_NAME) - - fileMap.forEach { - val mustache = it.mustacheFileName - val fileInputStream = javaClass.classLoader - .getResource(ITemplateContributor.DIRECTORY_NAME + "/$templateName/$mustache").openStream() - val createdFile = File(KFiles.joinDir(it.dir, it.fileName)) - Mustache.generateFile(fileInputStream, File(KFiles.joinDir(it.dir, it.fileName)), map) - log(2, "Created $createdFile") - } - - } + override val mainClass = "Main" + override val fileMap = listOf( + FileInfo("src/main/java/" + PACKAGE_NAME.replace(".", "/"), "Main.java", "java.mustache"), + FileInfo("src/test/java/" + PACKAGE_NAME.replace(".", "/"), "ExampleTest.java", "java-test.mustache") + ) } diff --git a/src/main/kotlin/com/beust/kobalt/app/kotlin/KotlinBuildGenerator.kt b/src/main/kotlin/com/beust/kobalt/app/kotlin/KotlinBuildGenerator.kt index c1144843..cd404447 100644 --- a/src/main/kotlin/com/beust/kobalt/app/kotlin/KotlinBuildGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/app/kotlin/KotlinBuildGenerator.kt @@ -9,5 +9,10 @@ class KotlinBuildGenerator : BuildGenerator() { override val templateName = "kotlin" override val templateDescription = "Generate a simple Kotlin project" override val fileMatch = { f: String -> f.endsWith(".kt") } + override val mainClass = "MainKt" + override val fileMap = listOf( + FileInfo("src/main/kotlin/" + PACKAGE_NAME.replace(".", "/"), "Main.kt", "kotlin.mustache"), + FileInfo("src/test/kotlin/" + PACKAGE_NAME.replace(".", "/"), "MainTest.kt", "kotlin-test.mustache") + ) } diff --git a/src/main/resources/templates/build.mustache b/src/main/resources/templates/build.mustache index de405bbf..b2d804cb 100644 --- a/src/main/resources/templates/build.mustache +++ b/src/main/resources/templates/build.mustache @@ -49,7 +49,7 @@ val p = {{directive}} { } application { - mainClass = "com.example.Example" + mainClass = "com.example.{{mainClass}}" } diff --git a/src/main/resources/templates/java/java.mustache b/src/main/resources/templates/java/java.mustache index 0513da21..c4c93c05 100644 --- a/src/main/resources/templates/java/java.mustache +++ b/src/main/resources/templates/java/java.mustache @@ -1,7 +1,7 @@ package {{packageName}}; -class Example { +class Main { public static void main(String[] argv) { - System.out.println("Hello world"); + System.out.println("\n\nHello Java world from Kobalt\n\n"); } } \ No newline at end of file diff --git a/src/main/resources/templates/kotlin/kotlin-test.mustache b/src/main/resources/templates/kotlin/kotlin-test.mustache new file mode 100644 index 00000000..fd0d348f --- /dev/null +++ b/src/main/resources/templates/kotlin/kotlin-test.mustache @@ -0,0 +1,8 @@ +package {{packageName}} + +import org.testng.annotations.Test + +class ExampleTest { + @Test + fun f() = println("Running test") +} \ No newline at end of file diff --git a/src/main/resources/templates/kotlin/kotlin.mustache b/src/main/resources/templates/kotlin/kotlin.mustache new file mode 100644 index 00000000..246cdec2 --- /dev/null +++ b/src/main/resources/templates/kotlin/kotlin.mustache @@ -0,0 +1,3 @@ +package {{packageName}} + +fun main(args: Array) = println("\n\nHello Kotlin world from Kobalt\n\n") \ No newline at end of file