mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-27 00:38:11 -07:00
Support for archetypes.
This commit is contained in:
parent
1821f46e1c
commit
b57b1845bc
9 changed files with 51 additions and 48 deletions
|
@ -118,7 +118,7 @@ private class Main @Inject constructor(
|
|||
println(AsciiArt.banner + Kobalt.version + "\n")
|
||||
}
|
||||
|
||||
if (args.init) {
|
||||
if (args.archetypes != null) {
|
||||
//
|
||||
// --init: create a new build project and install the wrapper
|
||||
// Make sure the wrapper won't call us back with --noLaunch
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package com.beust.kobalt.app
|
||||
|
||||
import com.beust.kobalt.Args
|
||||
import com.beust.kobalt.api.IInitContributor
|
||||
import com.beust.kobalt.maven.Pom
|
||||
import com.beust.kobalt.misc.KFiles
|
||||
import com.beust.kobalt.misc.log
|
||||
import com.github.mustachejava.DefaultMustacheFactory
|
||||
import java.io.*
|
||||
import java.util.*
|
||||
|
@ -10,20 +11,35 @@ import java.util.*
|
|||
/**
|
||||
* Abstract base class for the build generators that use build-template.mustache.
|
||||
*/
|
||||
abstract class BuildGenerator : IInitContributor<File> {
|
||||
abstract class BuildGenerator : IInitContributor {
|
||||
abstract val defaultSourceDirectories : HashSet<String>
|
||||
abstract val defaultTestDirectories : HashSet<String>
|
||||
abstract val directive : String
|
||||
abstract val name : String
|
||||
override abstract val name : String
|
||||
abstract val fileMatch : (String) -> Boolean
|
||||
|
||||
override fun generateBuildFile(os: OutputStream) {
|
||||
PrintWriter(os).use {
|
||||
it.print(buildFileContent)
|
||||
companion object {
|
||||
/**
|
||||
* Turns a dot property into a proper Kotlin identifier, e.g. common.version -> commonVersion
|
||||
*/
|
||||
fun toIdentifier(key: String): String {
|
||||
fun upperFirst(s: String) = if (s.isBlank()) s else s.substring(0, 1).toUpperCase() + s.substring(1)
|
||||
|
||||
return key.split('.').mapIndexed( { index, value -> if (index == 0) value else upperFirst(value) })
|
||||
.joinToString("")
|
||||
}
|
||||
}
|
||||
|
||||
override fun affinity(arg: File) = KFiles.findRecursively(arg, fileMatch).size
|
||||
override fun generateArchetype(args: Args) {
|
||||
val file = File(args.buildFile)
|
||||
if (! file.exists()) {
|
||||
PrintWriter(FileOutputStream(file)).use {
|
||||
it.print(buildFileContent)
|
||||
}
|
||||
} else {
|
||||
log(1, "Build file already exists, not overwriting it")
|
||||
}
|
||||
}
|
||||
|
||||
private fun importPom(pomFile: File, mainDeps: ArrayList<Pom.Dependency>, testDeps: ArrayList<Pom.Dependency>,
|
||||
map: HashMap<String, Any?>) {
|
||||
|
@ -37,7 +53,7 @@ abstract class BuildGenerator : IInitContributor<File> {
|
|||
}
|
||||
|
||||
val properties = pom.properties
|
||||
val mapped = properties.entries.associateBy({ it.key }, { ProjectGenerator.toIdentifier(it.key) })
|
||||
val mapped = properties.entries.associateBy({ it.key }, { toIdentifier(it.key) })
|
||||
|
||||
map.put("properties", properties.entries.map({ Pair(mapped[it.key], it.value) }))
|
||||
|
||||
|
|
|
@ -1,43 +1,23 @@
|
|||
package com.beust.kobalt.app
|
||||
|
||||
import com.beust.kobalt.Args
|
||||
import com.beust.kobalt.internal.ActorUtils
|
||||
import com.beust.kobalt.internal.PluginInfo
|
||||
import com.beust.kobalt.misc.log
|
||||
import com.google.inject.Inject
|
||||
import java.io.File
|
||||
import java.io.FileOutputStream
|
||||
|
||||
/**
|
||||
* Invoked with --init. Generate a new project.
|
||||
*/
|
||||
public class ProjectGenerator @Inject constructor(val pluginInfo: PluginInfo){
|
||||
companion object {
|
||||
/**
|
||||
* Turns a dot property into a proper Kotlin identifier, e.g. common.version -> commonVersion
|
||||
*/
|
||||
fun toIdentifier(key: String): String {
|
||||
fun upperFirst(s: String) = if (s.isBlank()) s else s.substring(0, 1).toUpperCase() + s.substring(1)
|
||||
|
||||
return key.split('.').mapIndexed( { index, value -> if (index == 0) value else upperFirst(value) })
|
||||
.joinToString("")
|
||||
}
|
||||
}
|
||||
|
||||
fun run(args: Args) {
|
||||
val contributor = ActorUtils.selectAffinityActor(pluginInfo.initContributors, File("."))
|
||||
File(args.buildFile).parentFile.mkdirs()
|
||||
if (contributor != null) {
|
||||
with(File(args.buildFile)) {
|
||||
if (exists()) {
|
||||
log(1, "Build file $path already exists, not overwriting it")
|
||||
} else {
|
||||
contributor.generateBuildFile(FileOutputStream(this))
|
||||
log(1, "Created $path")
|
||||
}
|
||||
args.archetypes?.let { archetypes ->
|
||||
val contributors = pluginInfo.initContributors.filter { archetypes.contains(it.name) }
|
||||
contributors.forEach {
|
||||
log(2, "Running archetype ${it.name}")
|
||||
it.generateArchetype(args)
|
||||
}
|
||||
} else {
|
||||
log(1, "Couldn't identify project, not generating any build file")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@ package com.beust.kobalt.maven
|
|||
import com.beust.kobalt.Args
|
||||
import com.beust.kobalt.KobaltTest
|
||||
import com.beust.kobalt.api.Kobalt
|
||||
import com.beust.kobalt.app.BuildGenerator
|
||||
import com.beust.kobalt.app.ProjectGenerator
|
||||
import com.beust.kobalt.internal.PluginInfo
|
||||
import com.google.inject.Inject
|
||||
|
@ -55,7 +56,7 @@ class PomTest @Inject constructor() : KobaltTest() {
|
|||
file.deleteOnExit()
|
||||
val args = Args()
|
||||
args.buildFile = file.absolutePath
|
||||
args.init = true
|
||||
args.archetypes = "java"
|
||||
|
||||
ProjectGenerator(Kobalt.INJECTOR.getInstance(PluginInfo::class.java)).run(args)
|
||||
|
||||
|
@ -64,8 +65,9 @@ class PomTest @Inject constructor() : KobaltTest() {
|
|||
Assert.assertTrue(contents.contains("name = \"${pom.name}\""), "Should find the name defined")
|
||||
Assert.assertTrue(contents.contains("version = \"${pom.version}\""), "Should find the version defined")
|
||||
pom.properties.forEach {
|
||||
Assert.assertTrue(contents.contains("val ${ProjectGenerator.toIdentifier(it.key)} = \"${it.value}\""), "Should find the " +
|
||||
"property defined")
|
||||
Assert.assertTrue(contents.contains(
|
||||
"val ${BuildGenerator.toIdentifier(it.key)} = \"${it.value}\""), "Should find the " +
|
||||
"property defined")
|
||||
}
|
||||
pom.repositories.forEach {
|
||||
Assert.assertTrue(contents.contains(it), "Should find the repository defined")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue