1
0
Fork 0
mirror of https://github.com/ethauvin/kobalt.git synced 2025-04-26 16:28:12 -07:00

Rename archetype -> template.

This commit is contained in:
Cedric Beust 2016-02-16 18:14:28 -08:00
parent d5e6a04b5d
commit 2b3aeabba2
14 changed files with 90 additions and 91 deletions

View file

@ -29,11 +29,11 @@ class Args {
@Parameter(names = arrayOf("--help", "--usage"), description = "Display the help")
var usage: Boolean = false
@Parameter(names = arrayOf("-i", "--init"), description = "Invoke the archetypes named, separated by a comma")
var archetypes : String? = null
@Parameter(names = arrayOf("-i", "--init"), description = "Invoke the templates named, separated by a comma")
var templates: String? = null
@Parameter(names = arrayOf("--listArchetypes"), description = "List the available archetypes")
var listArchetypes: Boolean = false
@Parameter(names = arrayOf("--listTemplates"), description = "List the available templates")
var listTemplates: Boolean = false
@Parameter(names = arrayOf("--log"), description = "Define the log level (1-3)")
var log: Int = 1

View file

@ -1,41 +0,0 @@
package com.beust.kobalt.api
import com.beust.kobalt.Args
/**
* Plugins that want to participate in the --init process (they can generate files to initialize
* a new project).
*/
interface IInitContributor {
val archetypes: List<IArchetype>
}
interface IArchetype {
/**
* The name of this archetype. This is the name that will be looked up when passed to the --init
* argument.
*/
val archetypeName: String
/**
* Description of this archetype.
*/
val archetypeDescription: String
/**
* The plug-in this archetype belongs to.
*/
val pluginName: String
/**
* Instructions to display to the user after a template has been generated.
*/
val instructions : String get() = "Build this project with `./kobaltw assemble`"
/**
* Generate the files for this archetype. The parameter is the arguments that were passed to the kobaltw
* command.
*/
fun generateArchetype(args: Args, classLoader: ClassLoader)
}

View file

@ -0,0 +1,41 @@
package com.beust.kobalt.api
import com.beust.kobalt.Args
/**
* Plugins that want to participate in the --init process (they can generate files to initialize
* a new project).
*/
interface ITemplateContributor {
val templates: List<ITemplate>
}
interface ITemplate {
/**
* The name of this template. This is the name that will be looked up when passed to the --init
* argument.
*/
val templateName: String
/**
* Description of this template.
*/
val templateDescription: String
/**
* The plug-in this template belongs to.
*/
val pluginName: String
/**
* Instructions to display to the user after a template has been generated.
*/
val instructions : String get() = "Build this project with `./kobaltw assemble`"
/**
* Generate the files for this template. The parameter is the arguments that were passed to the kobaltw
* command.
*/
fun generateTemplate(args: Args, classLoader: ClassLoader)
}

View file

@ -59,7 +59,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
val plugins = arrayListOf<IPlugin>()
val projectContributors = arrayListOf<IProjectContributor>()
val classpathContributors = arrayListOf<IClasspathContributor>()
val initContributors = arrayListOf<IInitContributor>()
val initContributors = arrayListOf<ITemplateContributor>()
val repoContributors = arrayListOf<IRepoContributor>()
val compilerFlagContributors = arrayListOf<ICompilerFlagContributor>()
val compilerInterceptors = arrayListOf<ICompilerInterceptor>()
@ -147,7 +147,7 @@ class PluginInfo(val xml: KobaltPluginXml, val classLoader: ClassLoader?) {
if (this is ICompilerFlagContributor) compilerFlagContributors.add(this)
if (this is ICompilerInterceptor) compilerInterceptors.add(this)
if (this is IDocContributor) docContributors.add(this)
if (this is IInitContributor) initContributors.add(this)
if (this is ITemplateContributor) initContributors.add(this)
if (this is IPlugin) plugins.add(this)
if (this is IProjectContributor) projectContributors.add(this)
if (this is IRepoContributor) repoContributors.add(this)

View file

@ -100,9 +100,9 @@ private class Main @Inject constructor(
//
pluginInfo.plugins.forEach { Plugins.addPluginInstance(it) }
// --listArchetypes
if (args.listArchetypes) {
Archetypes().list(pluginInfo)
// --listTemplates
if (args.listTemplates) {
Templates().list(pluginInfo)
return 0
}
@ -148,7 +148,7 @@ private class Main @Inject constructor(
println(AsciiArt.banner + Kobalt.version + "\n")
}
if (args.archetypes != null) {
if (args.templates != null) {
//
// --init: create a new build project and install the wrapper
// Make sure the wrapper won't call us back with --noLaunch

View file

@ -1,8 +1,7 @@
package com.beust.kobalt.app
import com.beust.kobalt.Args
import com.beust.kobalt.api.IArchetype
import com.beust.kobalt.api.IInitContributor
import com.beust.kobalt.api.ITemplate
import com.beust.kobalt.maven.Pom
import com.beust.kobalt.misc.log
import com.beust.kobalt.plugin.KobaltPlugin
@ -13,7 +12,7 @@ import java.util.*
/**
* Abstract base class for the build generators that use build-template.mustache.
*/
abstract class BuildGenerator : IArchetype {
abstract class BuildGenerator : ITemplate {
override val pluginName = KobaltPlugin.PLUGIN_NAME
abstract val defaultSourceDirectories : HashSet<String>
@ -33,7 +32,7 @@ abstract class BuildGenerator : IArchetype {
}
}
override fun generateArchetype(args: Args, classLoader: ClassLoader) {
override fun generateTemplate(args: Args, classLoader: ClassLoader) {
val file = File(args.buildFile)
if (! file.exists()) {
PrintWriter(FileOutputStream(file)).use {
@ -91,7 +90,7 @@ abstract class BuildGenerator : IArchetype {
put("directory", currentDir.absolutePath)
put("sourceDirectories", defaultSourceDirectories)
put("sourceDirectoriesTest", defaultTestDirectories)
put("imports", "import com.beust.kobalt.plugin.$archetypeName.*")
put("imports", "import com.beust.kobalt.plugin.$templateName.*")
put("directive", "project")
}

View file

@ -1,7 +1,7 @@
package com.beust.kobalt.app
import com.beust.kobalt.Args
import com.beust.kobalt.api.IArchetype
import com.beust.kobalt.api.ITemplate
import com.beust.kobalt.misc.log
import java.io.File
import java.io.FileOutputStream
@ -10,7 +10,7 @@ import java.util.jar.JarInputStream
/**
* Base class for templates that simply decompress a jar file to generate their project.
*/
abstract class JarTemplate(val jarName: String) : IArchetype {
abstract class JarTemplate(val jarName: String) : ITemplate {
companion object {
fun extractFile(ins: JarInputStream, destDir: File) {
var entry = ins.nextEntry
@ -39,8 +39,8 @@ abstract class JarTemplate(val jarName: String) : IArchetype {
}
}
override fun generateArchetype(args: Args, classLoader: ClassLoader) {
log(1, "Generating archetype for Android with class loader $classLoader")
override fun generateTemplate(args: Args, classLoader: ClassLoader) {
log(2, "Generating template with class loader $classLoader")
val destDir = File(".")
val ins = JarInputStream(classLoader.getResource(jarName).openConnection().inputStream)
extractFile(ins, destDir)

View file

@ -1,19 +1,19 @@
package com.beust.kobalt.app
import com.beust.kobalt.api.IInitContributor
import com.beust.kobalt.api.ITemplateContributor
import com.beust.kobalt.plugin.KobaltPlugin
/**
* Template that generates a Kobalt plug-in project.
*/
class KobaltPluginTemplate : IInitContributor {
val pluginArchetype = object: JarTemplate("templates/plugin.jar") {
override val archetypeDescription = "Generate a sample Kobalt plug-in project"
class KobaltPluginTemplate : ITemplateContributor {
val pluginTemplate = object: JarTemplate("templates/plugin.jar") {
override val templateDescription = "Generate a sample Kobalt plug-in project"
override val archetypeName = "kobalt-plugin"
override val templateName = "kobalt-plugin"
override val pluginName = KobaltPlugin.PLUGIN_NAME
}
override val archetypes = listOf(pluginArchetype)
override val templates = listOf(pluginTemplate)
}

View file

@ -1,7 +1,7 @@
package com.beust.kobalt.app
import com.beust.kobalt.Args
import com.beust.kobalt.api.IArchetype
import com.beust.kobalt.api.ITemplate
import com.beust.kobalt.internal.PluginInfo
import com.beust.kobalt.misc.log
import com.beust.kobalt.misc.warn
@ -14,21 +14,21 @@ import java.io.File
class ProjectGenerator @Inject constructor(val pluginInfo: PluginInfo){
fun run(args: Args, classLoader: ClassLoader) {
File(args.buildFile).parentFile.mkdirs()
val map = hashMapOf<String, IArchetype>()
val map = hashMapOf<String, ITemplate>()
pluginInfo.initContributors.forEach {
it.archetypes.forEach {
map.put(it.archetypeName, it)
it.templates.forEach {
map.put(it.templateName, it)
}
}
args.archetypes?.split(",")?.forEach { archetypeName ->
val archetype = map[archetypeName]
if (archetype != null) {
log(2, "Running archetype $archetypeName")
archetype.generateArchetype(args, classLoader)
log(1, "\n\n" + archetype.instructions)
args.templates?.split(",")?.forEach { templateName ->
val template = map[templateName]
if (template != null) {
log(2, "Running template $templateName")
template.generateTemplate(args, classLoader)
log(1, "\n\n" + template.instructions)
} else {
warn("Couldn't find any archetype named $archetypeName")
warn("Couldn't find any template named $templateName")
}
}
}

View file

@ -1,29 +1,29 @@
package com.beust.kobalt.app
import com.beust.kobalt.api.IArchetype
import com.beust.kobalt.api.IInitContributor
import com.beust.kobalt.api.ITemplate
import com.beust.kobalt.api.ITemplateContributor
import com.beust.kobalt.app.java.JavaBuildGenerator
import com.beust.kobalt.app.kotlin.KotlinBuildGenerator
import com.beust.kobalt.internal.PluginInfo
import com.beust.kobalt.misc.log
import com.google.common.collect.ArrayListMultimap
class Archetypes : IInitContributor {
override val archetypes = listOf(JavaBuildGenerator(), KotlinBuildGenerator())
class Templates : ITemplateContributor {
override val templates = listOf(JavaBuildGenerator(), KotlinBuildGenerator())
fun list(pluginInfo: PluginInfo) {
val map = ArrayListMultimap.create<String, IArchetype>()
val map = ArrayListMultimap.create<String, ITemplate>()
pluginInfo.initContributors.forEach {
it.archetypes.forEach {
it.templates.forEach {
map.put(it.pluginName, it)
}
}
log(1, "Available archetypes")
log(1, "Available templates")
map.keySet().forEach {
log(1, " Plug-in: $it")
map[it].forEach {
log(1, " \"" + it.archetypeName + "\"\t\t" + it.archetypeDescription)
log(1, " \"" + it.templateName + "\"\t\t" + it.templateDescription)
}
}
}

View file

@ -6,7 +6,7 @@ class JavaBuildGenerator: BuildGenerator() {
override val defaultSourceDirectories = hashSetOf("src/main/java")
override val defaultTestDirectories = hashSetOf("src/test/java")
override val directive = "project"
override val archetypeName = "java"
override val archetypeDescription = "Generates a simple Java project"
override val templateName = "java"
override val templateDescription = "Generates a simple Java project"
override val fileMatch = { f: String -> f.endsWith(".java") }
}

View file

@ -6,8 +6,8 @@ class KotlinBuildGenerator : BuildGenerator() {
override val defaultSourceDirectories = hashSetOf("src/main/kotlin")
override val defaultTestDirectories = hashSetOf("src/test/kotlin")
override val directive = "project"
override val archetypeName = "kotlin"
override val archetypeDescription = "Generates a simple Kotlin project"
override val templateName = "kotlin"
override val templateDescription = "Generates a simple Kotlin project"
override val fileMatch = { f: String -> f.endsWith(".kt") }
}

View file

@ -14,7 +14,7 @@
<class-name>com.beust.kobalt.internal.JvmCompilerPlugin</class-name>
<!-- These classes manage -init for Java and Kotlin -->
<class-name>com.beust.kobalt.app.Archetypes</class-name>
<class-name>com.beust.kobalt.app.Templates</class-name>
<!-- Test runners -->
<class-name>com.beust.kobalt.internal.JUnitRunner</class-name>

View file

@ -56,7 +56,7 @@ class PomTest @Inject constructor() : KobaltTest() {
file.deleteOnExit()
val args = Args()
args.buildFile = file.absolutePath
args.archetypes = "java"
args.templates = "java"
ProjectGenerator(Kobalt.INJECTOR.getInstance(PluginInfo::class.java)).run(args, javaClass.classLoader)