diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/Mustache.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/Mustache.kt new file mode 100644 index 00000000..64b8668f --- /dev/null +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/Mustache.kt @@ -0,0 +1,19 @@ +package com.beust.kobalt.internal + +import com.github.mustachejava.DefaultMustacheFactory +import java.io.* + +class Mustache { + companion object { + fun generateFile(mustacheIns: InputStream, createdFile: File, map: Map) { + val sw = StringWriter() + val pw = PrintWriter(sw) + var mf = DefaultMustacheFactory() + mf.compile(InputStreamReader(mustacheIns), "kobalt").execute(pw, map).flush() + with(createdFile) { + parentFile.mkdirs() + writeText(sw.toString()) + } + } + } +} diff --git a/src/main/kotlin/com/beust/kobalt/app/BuildGenerator.kt b/src/main/kotlin/com/beust/kobalt/app/BuildGenerator.kt index 5750d737..6605effd 100644 --- a/src/main/kotlin/com/beust/kobalt/app/BuildGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/app/BuildGenerator.kt @@ -4,6 +4,7 @@ import com.beust.kobalt.Args import com.beust.kobalt.api.ITemplate import com.beust.kobalt.api.ITemplateContributor import com.beust.kobalt.maven.Pom +import com.beust.kobalt.misc.KFiles import com.beust.kobalt.misc.log import com.beust.kobalt.plugin.KobaltPlugin import com.github.mustachejava.DefaultMustacheFactory @@ -20,8 +21,18 @@ abstract class BuildGenerator : ITemplate { abstract val defaultTestDirectories : HashSet abstract val directive : String abstract val fileMatch : (String) -> Boolean + open fun generateAdditionalFiles(args: Args, classLoader: ClassLoader) {} + + private fun maybeGenerateAdditionalFiles(args: Args, classLoader: ClassLoader) { + val existingFiles = KFiles.findRecursively(File("."), fileMatch) + if (existingFiles.isEmpty()) { + generateAdditionalFiles(args, classLoader) + } + } companion object { + val PACKAGE_NAME = "com.example" + /** * Turns a dot property into a proper Kotlin identifier, e.g. common.version -> commonVersion */ @@ -34,6 +45,11 @@ abstract class BuildGenerator : ITemplate { } override fun generateTemplate(args: Args, classLoader: ClassLoader) { + generateBuildFile(args, classLoader) + maybeGenerateAdditionalFiles(args, classLoader) + } + + private fun generateBuildFile(args: Args, classLoader: ClassLoader) { val file = File(args.buildFile) if (! file.exists()) { PrintWriter(FileOutputStream(file)).use { @@ -48,8 +64,8 @@ abstract class BuildGenerator : ITemplate { map: HashMap) { var pom = Pom("imported", pomFile.absoluteFile) with(map) { - put("group", pom.groupId ?: "com.example") - put("artifactId", pom.artifactId ?: "com.example") + put("group", pom.groupId ?: PACKAGE_NAME) + put("artifactId", pom.artifactId ?: PACKAGE_NAME) put("version", pom.version ?: "0.1") put("name", pom.name ?: pom.artifactId) put("repositories", pom.repositories.map({ "\"$it\"" }).joinToString(",")) @@ -86,7 +102,7 @@ abstract class BuildGenerator : ITemplate { val currentDir = File(".").absoluteFile.parentFile with(map) { put("name", currentDir.name) - put("group", "com.example") + put("group", PACKAGE_NAME) put("version", "0.1") put("directory", currentDir.absolutePath) put("sourceDirectories", defaultSourceDirectories) @@ -109,9 +125,9 @@ abstract class BuildGenerator : ITemplate { .getResource(ITemplateContributor.DIRECTORY_NAME + "/build.mustache").openStream() val sw = StringWriter() val pw = PrintWriter(sw) - var mf = DefaultMustacheFactory(); - var mustache = mf.compile(InputStreamReader(fileInputStream), "kobalt"); - mustache.execute(pw, map).flush(); + var mf = DefaultMustacheFactory() + var mustache = mf.compile(InputStreamReader(fileInputStream), "kobalt") + mustache.execute(pw, map).flush() return sw.toString() } } 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 c8806436..5cd07255 100644 --- a/src/main/kotlin/com/beust/kobalt/app/java/JavaBuildGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/app/java/JavaBuildGenerator.kt @@ -1,6 +1,12 @@ 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") @@ -9,4 +15,28 @@ class JavaBuildGenerator: BuildGenerator() { override val templateName = "java" 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") + } + + } } diff --git a/src/main/resources/templates/build.mustache b/src/main/resources/templates/build.mustache index 89744403..de405bbf 100644 --- a/src/main/resources/templates/build.mustache +++ b/src/main/resources/templates/build.mustache @@ -1,5 +1,6 @@ import com.beust.kobalt.* -import com.beust.kobalt.plugin.packaging.assemble +import com.beust.kobalt.plugin.packaging.* +import com.beust.kobalt.plugin.application.* {{imports}} val repos = repos({{{repositories}}}) @@ -35,7 +36,7 @@ val p = {{directive}} { } dependenciesTest { -// compile("org.testng:testng:6.9.5") + compile("org.testng:testng:6.9.9") {{#testDependencies}} compile("{{groupId}}:{{artifactId}}:{{version}}") {{/testDependencies}} @@ -46,4 +47,10 @@ val p = {{directive}} { jar { } } + + application { + mainClass = "com.example.Example" + } + + } diff --git a/src/main/resources/templates/java/java-test.mustache b/src/main/resources/templates/java/java-test.mustache new file mode 100644 index 00000000..32bc2723 --- /dev/null +++ b/src/main/resources/templates/java/java-test.mustache @@ -0,0 +1,9 @@ +package {{packageName}}; +import org.testng.annotations.Test; + +public class ExampleTest { + @Test + public void f() { + System.out.println("Running test"); + } +} \ No newline at end of file diff --git a/src/main/resources/templates/java/java.mustache b/src/main/resources/templates/java/java.mustache new file mode 100644 index 00000000..0513da21 --- /dev/null +++ b/src/main/resources/templates/java/java.mustache @@ -0,0 +1,7 @@ +package {{packageName}}; + +class Example { + public static void main(String[] argv) { + System.out.println("Hello world"); + } +} \ No newline at end of file