From f6fe0e086efd530f05896230d243cf66add786c9 Mon Sep 17 00:00:00 2001 From: Cedric Beust Date: Sun, 14 Feb 2016 05:49:19 -0800 Subject: [PATCH] Improve the archetype support. --- .../src/main/kotlin/com/beust/kobalt/Args.kt | 3 ++ .../com/beust/kobalt/api/IInitContributor.kt | 14 +++++++++ .../beust/kobalt/internal/KobaltPluginXml.kt | 2 +- src/main/kotlin/com/beust/kobalt/Main.kt | 17 ++++++----- .../kotlin/com/beust/kobalt/app/Archetypes.kt | 30 +++++++++++++++++++ .../com/beust/kobalt/app/BuildGenerator.kt | 7 +++-- .../com/beust/kobalt/app/ProjectGenerator.kt | 16 ++++++---- .../kobalt/app/java/JavaBuildGenerator.kt | 1 + .../kobalt/app/kotlin/KotlinBuildGenerator.kt | 1 + .../resources/META-INF/kobalt-core-plugin.xml | 3 +- 10 files changed, 75 insertions(+), 19 deletions(-) create mode 100644 src/main/kotlin/com/beust/kobalt/app/Archetypes.kt diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Args.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Args.kt index e79e6cb4..13e9675c 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Args.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/Args.kt @@ -32,6 +32,9 @@ class Args { @Parameter(names = arrayOf("-i", "--init"), description = "Invoke the archetypes named, separated by a comma") var archetypes : String? = null + @Parameter(names = arrayOf("--listArchetypes"), description = "List the available archetypes") + var listArchetypes: Boolean = false + @Parameter(names = arrayOf("--log"), description = "Define the log level (1-3)") var log: Int = 1 diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IInitContributor.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IInitContributor.kt index 7cbab7ec..e0477254 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IInitContributor.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/api/IInitContributor.kt @@ -7,12 +7,26 @@ import com.beust.kobalt.Args * a new project). */ interface IInitContributor { + val archetypes: List +} + +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 + /** * Generate the files for this archetype. The parameter is the arguments that were passed to the kobaltw * command. diff --git a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltPluginXml.kt b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltPluginXml.kt index dc7172dd..8437c4b7 100644 --- a/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltPluginXml.kt +++ b/modules/kobalt-plugin-api/src/main/kotlin/com/beust/kobalt/internal/KobaltPluginXml.kt @@ -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 as IInitContributor) + if (this is IInitContributor) initContributors.add(this) if (this is IPlugin) plugins.add(this) if (this is IProjectContributor) projectContributors.add(this) if (this is IRepoContributor) repoContributors.add(this) diff --git a/src/main/kotlin/com/beust/kobalt/Main.kt b/src/main/kotlin/com/beust/kobalt/Main.kt index f59f1a31..283513b6 100644 --- a/src/main/kotlin/com/beust/kobalt/Main.kt +++ b/src/main/kotlin/com/beust/kobalt/Main.kt @@ -1,14 +1,8 @@ package com.beust.kobalt import com.beust.jcommander.JCommander -import com.beust.kobalt.api.IClasspathDependency -import com.beust.kobalt.api.Kobalt -import com.beust.kobalt.api.PluginTask -import com.beust.kobalt.api.Project -import com.beust.kobalt.app.BuildFileCompiler -import com.beust.kobalt.app.MainModule -import com.beust.kobalt.app.ProjectGenerator -import com.beust.kobalt.app.UpdateKobalt +import com.beust.kobalt.api.* +import com.beust.kobalt.app.* import com.beust.kobalt.app.remote.KobaltClient import com.beust.kobalt.app.remote.KobaltServer import com.beust.kobalt.internal.KobaltSettings @@ -18,6 +12,7 @@ import com.beust.kobalt.internal.build.BuildFile import com.beust.kobalt.maven.DepFactory import com.beust.kobalt.maven.Http import com.beust.kobalt.misc.* +import com.google.common.collect.ArrayListMultimap import com.google.common.collect.HashMultimap import com.google.inject.Guice import java.io.File @@ -82,6 +77,12 @@ private class Main @Inject constructor( // pluginInfo.plugins.forEach { Plugins.addPluginInstance(it) } + // --listArchetypes + if (args.listArchetypes) { + Archetypes().list(pluginInfo) + return 0 + } + if (args.client) { client.run() return 0 diff --git a/src/main/kotlin/com/beust/kobalt/app/Archetypes.kt b/src/main/kotlin/com/beust/kobalt/app/Archetypes.kt new file mode 100644 index 00000000..e8aa3a1d --- /dev/null +++ b/src/main/kotlin/com/beust/kobalt/app/Archetypes.kt @@ -0,0 +1,30 @@ +package com.beust.kobalt.app + +import com.beust.kobalt.api.IArchetype +import com.beust.kobalt.api.IInitContributor +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()) + + fun list(pluginInfo: PluginInfo) { + val map = ArrayListMultimap.create() + pluginInfo.initContributors.forEach { + it.archetypes.forEach { + map.put(it.pluginName, it) + } + } + + log(1, "Available archetypes") + map.keySet().forEach { + log(1, " Plug-in: $it") + map[it].forEach { + log(1, " \"" + it.archetypeName + "\"\t\t" + it.archetypeDescription) + } + } + } +} diff --git a/src/main/kotlin/com/beust/kobalt/app/BuildGenerator.kt b/src/main/kotlin/com/beust/kobalt/app/BuildGenerator.kt index 9572f478..3455525f 100644 --- a/src/main/kotlin/com/beust/kobalt/app/BuildGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/app/BuildGenerator.kt @@ -1,9 +1,11 @@ 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.maven.Pom import com.beust.kobalt.misc.log +import com.beust.kobalt.plugin.KobaltPlugin import com.github.mustachejava.DefaultMustacheFactory import java.io.* import java.util.* @@ -11,11 +13,12 @@ import java.util.* /** * Abstract base class for the build generators that use build-template.mustache. */ -abstract class BuildGenerator : IInitContributor { +abstract class BuildGenerator : IArchetype { + override val pluginName = KobaltPlugin.PLUGIN_NAME + abstract val defaultSourceDirectories : HashSet abstract val defaultTestDirectories : HashSet abstract val directive : String - override abstract val archetypeName: String abstract val fileMatch : (String) -> Boolean companion object { diff --git a/src/main/kotlin/com/beust/kobalt/app/ProjectGenerator.kt b/src/main/kotlin/com/beust/kobalt/app/ProjectGenerator.kt index 5b57d420..02453286 100644 --- a/src/main/kotlin/com/beust/kobalt/app/ProjectGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/app/ProjectGenerator.kt @@ -1,6 +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.internal.PluginInfo import com.beust.kobalt.misc.log @@ -11,18 +12,21 @@ import java.io.File /** * Invoked with --init. Generate a new project. */ -public class ProjectGenerator @Inject constructor(val pluginInfo: PluginInfo){ +class ProjectGenerator @Inject constructor(val pluginInfo: PluginInfo){ fun run(args: Args) { File(args.buildFile).parentFile.mkdirs() - val map = hashMapOf() + val map = hashMapOf() pluginInfo.initContributors.forEach { - map.put(it.archetypeName, it) + it.archetypes.forEach { + map.put(it.archetypeName, it) + } } + args.archetypes?.split(",")?.forEach { archetypeName -> - val contributor = map[archetypeName] - if (contributor != null) { + val archetype = map[archetypeName] + if (archetype != null) { log(2, "Running archetype $archetypeName") - contributor.generateArchetype(args) + archetype.generateArchetype(args) } else { warn("Couldn't find any archetype named $archetypeName") } 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 925fed1f..ca8651c4 100644 --- a/src/main/kotlin/com/beust/kobalt/app/java/JavaBuildGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/app/java/JavaBuildGenerator.kt @@ -7,5 +7,6 @@ class JavaBuildGenerator: BuildGenerator() { 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 fileMatch = { f: String -> f.endsWith(".java") } } diff --git a/src/main/kotlin/com/beust/kobalt/app/kotlin/KotlinBuildGenerator.kt b/src/main/kotlin/com/beust/kobalt/app/kotlin/KotlinBuildGenerator.kt index a50a2e97..517ac880 100644 --- a/src/main/kotlin/com/beust/kobalt/app/kotlin/KotlinBuildGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/app/kotlin/KotlinBuildGenerator.kt @@ -7,6 +7,7 @@ class KotlinBuildGenerator : BuildGenerator() { 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 fileMatch = { f: String -> f.endsWith(".kt") } } diff --git a/src/main/resources/META-INF/kobalt-core-plugin.xml b/src/main/resources/META-INF/kobalt-core-plugin.xml index df60d90d..e4e467cb 100644 --- a/src/main/resources/META-INF/kobalt-core-plugin.xml +++ b/src/main/resources/META-INF/kobalt-core-plugin.xml @@ -14,8 +14,7 @@ com.beust.kobalt.internal.JvmCompilerPlugin - com.beust.kobalt.app.java.JavaBuildGenerator - com.beust.kobalt.app.kotlin.KotlinBuildGenerator + com.beust.kobalt.app.Archetypes com.beust.kobalt.internal.JUnitRunner