mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
More template work.
This commit is contained in:
parent
0f00887736
commit
f47c2bc8b3
7 changed files with 53 additions and 37 deletions
|
@ -3,6 +3,7 @@ package com.beust.kobalt.app
|
||||||
import com.beust.kobalt.Args
|
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.internal.Mustache
|
||||||
import com.beust.kobalt.maven.Pom
|
import com.beust.kobalt.maven.Pom
|
||||||
import com.beust.kobalt.misc.KFiles
|
import com.beust.kobalt.misc.KFiles
|
||||||
import com.beust.kobalt.misc.log
|
import com.beust.kobalt.misc.log
|
||||||
|
@ -21,11 +22,33 @@ 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) {}
|
abstract val fileMap: List<FileInfo>
|
||||||
|
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) {
|
private fun maybeGenerateAdditionalFiles(args: Args, classLoader: ClassLoader) {
|
||||||
val existingFiles = KFiles.findRecursively(File("."), fileMatch)
|
val existingFiles =
|
||||||
if (existingFiles.isEmpty()) {
|
if (File("src").exists()) {
|
||||||
|
KFiles.findRecursively(File("src"), fileMatch).size > 0
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! existingFiles) {
|
||||||
generateAdditionalFiles(args, classLoader)
|
generateAdditionalFiles(args, classLoader)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,10 +120,11 @@ abstract class BuildGenerator : ITemplate {
|
||||||
|
|
||||||
private val buildFileContent: String
|
private val buildFileContent: String
|
||||||
get() {
|
get() {
|
||||||
val map = hashMapOf<String, Any?>()
|
|
||||||
map.put("directive", directive)
|
|
||||||
val currentDir = File(".").absoluteFile.parentFile
|
val currentDir = File(".").absoluteFile.parentFile
|
||||||
|
val map = hashMapOf<String, Any?>()
|
||||||
with(map) {
|
with(map) {
|
||||||
|
put("mainClass", mainClass)
|
||||||
|
put("directive", directive)
|
||||||
put("name", currentDir.name)
|
put("name", currentDir.name)
|
||||||
put("group", PACKAGE_NAME)
|
put("group", PACKAGE_NAME)
|
||||||
put("version", "0.1")
|
put("version", "0.1")
|
||||||
|
|
|
@ -1,12 +1,6 @@
|
||||||
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")
|
||||||
|
@ -16,27 +10,9 @@ class JavaBuildGenerator: BuildGenerator() {
|
||||||
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 val instructions = "Now you can run either `./kobaltw test` or `./kobaltw run`"
|
||||||
|
override val mainClass = "Main"
|
||||||
override fun generateAdditionalFiles(args: Args, classLoader: ClassLoader) {
|
override val fileMap = listOf(
|
||||||
println("Generating Java files")
|
FileInfo("src/main/java/" + PACKAGE_NAME.replace(".", "/"), "Main.java", "java.mustache"),
|
||||||
|
FileInfo("src/test/java/" + PACKAGE_NAME.replace(".", "/"), "ExampleTest.java", "java-test.mustache")
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,5 +9,10 @@ class KotlinBuildGenerator : BuildGenerator() {
|
||||||
override val templateName = "kotlin"
|
override val templateName = "kotlin"
|
||||||
override val templateDescription = "Generate a simple Kotlin project"
|
override val templateDescription = "Generate a simple Kotlin project"
|
||||||
override val fileMatch = { f: String -> f.endsWith(".kt") }
|
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")
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -49,7 +49,7 @@ val p = {{directive}} {
|
||||||
}
|
}
|
||||||
|
|
||||||
application {
|
application {
|
||||||
mainClass = "com.example.Example"
|
mainClass = "com.example.{{mainClass}}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
package {{packageName}};
|
package {{packageName}};
|
||||||
|
|
||||||
class Example {
|
class Main {
|
||||||
public static void main(String[] argv) {
|
public static void main(String[] argv) {
|
||||||
System.out.println("Hello world");
|
System.out.println("\n\nHello Java world from Kobalt\n\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
8
src/main/resources/templates/kotlin/kotlin-test.mustache
Normal file
8
src/main/resources/templates/kotlin/kotlin-test.mustache
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
package {{packageName}}
|
||||||
|
|
||||||
|
import org.testng.annotations.Test
|
||||||
|
|
||||||
|
class ExampleTest {
|
||||||
|
@Test
|
||||||
|
fun f() = println("Running test")
|
||||||
|
}
|
3
src/main/resources/templates/kotlin/kotlin.mustache
Normal file
3
src/main/resources/templates/kotlin/kotlin.mustache
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
package {{packageName}}
|
||||||
|
|
||||||
|
fun main(args: Array<String>) = println("\n\nHello Kotlin world from Kobalt\n\n")
|
Loading…
Add table
Add a link
Reference in a new issue