mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Better Java template.
This commit is contained in:
parent
68358201bf
commit
0f00887736
6 changed files with 96 additions and 8 deletions
|
@ -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<String, Any>) {
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -4,6 +4,7 @@ import com.beust.kobalt.Args
|
||||||
import com.beust.kobalt.api.ITemplate
|
import com.beust.kobalt.api.ITemplate
|
||||||
import com.beust.kobalt.api.ITemplateContributor
|
import com.beust.kobalt.api.ITemplateContributor
|
||||||
import com.beust.kobalt.maven.Pom
|
import com.beust.kobalt.maven.Pom
|
||||||
|
import com.beust.kobalt.misc.KFiles
|
||||||
import com.beust.kobalt.misc.log
|
import com.beust.kobalt.misc.log
|
||||||
import com.beust.kobalt.plugin.KobaltPlugin
|
import com.beust.kobalt.plugin.KobaltPlugin
|
||||||
import com.github.mustachejava.DefaultMustacheFactory
|
import com.github.mustachejava.DefaultMustacheFactory
|
||||||
|
@ -20,8 +21,18 @@ abstract class BuildGenerator : ITemplate {
|
||||||
abstract val defaultTestDirectories : HashSet<String>
|
abstract val defaultTestDirectories : HashSet<String>
|
||||||
abstract val directive : String
|
abstract val directive : String
|
||||||
abstract val fileMatch : (String) -> Boolean
|
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 {
|
companion object {
|
||||||
|
val PACKAGE_NAME = "com.example"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Turns a dot property into a proper Kotlin identifier, e.g. common.version -> commonVersion
|
* 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) {
|
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)
|
val file = File(args.buildFile)
|
||||||
if (! file.exists()) {
|
if (! file.exists()) {
|
||||||
PrintWriter(FileOutputStream(file)).use {
|
PrintWriter(FileOutputStream(file)).use {
|
||||||
|
@ -48,8 +64,8 @@ abstract class BuildGenerator : ITemplate {
|
||||||
map: HashMap<String, Any?>) {
|
map: HashMap<String, Any?>) {
|
||||||
var pom = Pom("imported", pomFile.absoluteFile)
|
var pom = Pom("imported", pomFile.absoluteFile)
|
||||||
with(map) {
|
with(map) {
|
||||||
put("group", pom.groupId ?: "com.example")
|
put("group", pom.groupId ?: PACKAGE_NAME)
|
||||||
put("artifactId", pom.artifactId ?: "com.example")
|
put("artifactId", pom.artifactId ?: PACKAGE_NAME)
|
||||||
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\"" }).joinToString(","))
|
put("repositories", pom.repositories.map({ "\"$it\"" }).joinToString(","))
|
||||||
|
@ -86,7 +102,7 @@ abstract class BuildGenerator : ITemplate {
|
||||||
val currentDir = File(".").absoluteFile.parentFile
|
val currentDir = File(".").absoluteFile.parentFile
|
||||||
with(map) {
|
with(map) {
|
||||||
put("name", currentDir.name)
|
put("name", currentDir.name)
|
||||||
put("group", "com.example")
|
put("group", PACKAGE_NAME)
|
||||||
put("version", "0.1")
|
put("version", "0.1")
|
||||||
put("directory", currentDir.absolutePath)
|
put("directory", currentDir.absolutePath)
|
||||||
put("sourceDirectories", defaultSourceDirectories)
|
put("sourceDirectories", defaultSourceDirectories)
|
||||||
|
@ -109,9 +125,9 @@ abstract class BuildGenerator : ITemplate {
|
||||||
.getResource(ITemplateContributor.DIRECTORY_NAME + "/build.mustache").openStream()
|
.getResource(ITemplateContributor.DIRECTORY_NAME + "/build.mustache").openStream()
|
||||||
val sw = StringWriter()
|
val sw = StringWriter()
|
||||||
val pw = PrintWriter(sw)
|
val pw = PrintWriter(sw)
|
||||||
var mf = DefaultMustacheFactory();
|
var mf = DefaultMustacheFactory()
|
||||||
var mustache = mf.compile(InputStreamReader(fileInputStream), "kobalt");
|
var mustache = mf.compile(InputStreamReader(fileInputStream), "kobalt")
|
||||||
mustache.execute(pw, map).flush();
|
mustache.execute(pw, map).flush()
|
||||||
return sw.toString()
|
return sw.toString()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,12 @@
|
||||||
package com.beust.kobalt.app.java
|
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.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() {
|
class JavaBuildGenerator: BuildGenerator() {
|
||||||
override val defaultSourceDirectories = hashSetOf("src/main/java")
|
override val defaultSourceDirectories = hashSetOf("src/main/java")
|
||||||
|
@ -9,4 +15,28 @@ class JavaBuildGenerator: BuildGenerator() {
|
||||||
override val templateName = "java"
|
override val templateName = "java"
|
||||||
override val templateDescription = "Generate a simple Java project"
|
override val templateDescription = "Generate a simple Java project"
|
||||||
override val fileMatch = { f: String -> f.endsWith(".java") }
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import com.beust.kobalt.*
|
import com.beust.kobalt.*
|
||||||
import com.beust.kobalt.plugin.packaging.assemble
|
import com.beust.kobalt.plugin.packaging.*
|
||||||
|
import com.beust.kobalt.plugin.application.*
|
||||||
{{imports}}
|
{{imports}}
|
||||||
|
|
||||||
val repos = repos({{{repositories}}})
|
val repos = repos({{{repositories}}})
|
||||||
|
@ -35,7 +36,7 @@ val p = {{directive}} {
|
||||||
}
|
}
|
||||||
|
|
||||||
dependenciesTest {
|
dependenciesTest {
|
||||||
// compile("org.testng:testng:6.9.5")
|
compile("org.testng:testng:6.9.9")
|
||||||
{{#testDependencies}}
|
{{#testDependencies}}
|
||||||
compile("{{groupId}}:{{artifactId}}:{{version}}")
|
compile("{{groupId}}:{{artifactId}}:{{version}}")
|
||||||
{{/testDependencies}}
|
{{/testDependencies}}
|
||||||
|
@ -46,4 +47,10 @@ val p = {{directive}} {
|
||||||
jar {
|
jar {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
application {
|
||||||
|
mainClass = "com.example.Example"
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
9
src/main/resources/templates/java/java-test.mustache
Normal file
9
src/main/resources/templates/java/java-test.mustache
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
package {{packageName}};
|
||||||
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
public class ExampleTest {
|
||||||
|
@Test
|
||||||
|
public void f() {
|
||||||
|
System.out.println("Running test");
|
||||||
|
}
|
||||||
|
}
|
7
src/main/resources/templates/java/java.mustache
Normal file
7
src/main/resources/templates/java/java.mustache
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
package {{packageName}};
|
||||||
|
|
||||||
|
class Example {
|
||||||
|
public static void main(String[] argv) {
|
||||||
|
System.out.println("Hello world");
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue