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 18eb7ea0..0645f5aa 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 @@ -27,6 +27,11 @@ interface IArchetype { */ 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. diff --git a/src/main/kotlin/com/beust/kobalt/app/JarTemplate.kt b/src/main/kotlin/com/beust/kobalt/app/JarTemplate.kt new file mode 100644 index 00000000..6dc3b0b2 --- /dev/null +++ b/src/main/kotlin/com/beust/kobalt/app/JarTemplate.kt @@ -0,0 +1,49 @@ +package com.beust.kobalt.app + +import com.beust.kobalt.Args +import com.beust.kobalt.api.IArchetype +import com.beust.kobalt.misc.log +import java.io.File +import java.io.FileOutputStream +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 { + companion object { + fun extractFile(ins: JarInputStream, destDir: File) { + var entry = ins.nextEntry + while (entry != null) { + val f = File(destDir.path + File.separator + entry.name) + if (entry.isDirectory) { + f.mkdir() + entry = ins.nextEntry + continue + } + + log(2, "Extracting: $entry to ${f.absolutePath}") + FileOutputStream(f).use { fos -> + var read = ins.read() + while (ins.available() > 0 && read != -1) { + fos.write(read) + read = ins.read() + } + } + entry = ins.nextEntry + } + } + + fun log(level: Int, s: String) { + println(" " + s) + } + } + + override fun generateArchetype(args: Args, classLoader: ClassLoader) { + log(1, "Generating archetype for Android with class loader $classLoader") + val destDir = File(".") + val ins = JarInputStream(classLoader.getResource(jarName).openConnection().inputStream) + extractFile(ins, destDir) + } + +} diff --git a/src/main/kotlin/com/beust/kobalt/app/KobaltPluginTemplate.kt b/src/main/kotlin/com/beust/kobalt/app/KobaltPluginTemplate.kt new file mode 100644 index 00000000..548ff11c --- /dev/null +++ b/src/main/kotlin/com/beust/kobalt/app/KobaltPluginTemplate.kt @@ -0,0 +1,19 @@ +package com.beust.kobalt.app + +import com.beust.kobalt.api.IInitContributor +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" + + override val archetypeName = "kobalt-plugin" + + override val pluginName = KobaltPlugin.PLUGIN_NAME + } + + override val archetypes = listOf(pluginArchetype) +} diff --git a/src/main/kotlin/com/beust/kobalt/app/ProjectGenerator.kt b/src/main/kotlin/com/beust/kobalt/app/ProjectGenerator.kt index 467d7f93..a8bd9e48 100644 --- a/src/main/kotlin/com/beust/kobalt/app/ProjectGenerator.kt +++ b/src/main/kotlin/com/beust/kobalt/app/ProjectGenerator.kt @@ -2,7 +2,6 @@ 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 import com.beust.kobalt.misc.warn @@ -27,6 +26,7 @@ class ProjectGenerator @Inject constructor(val pluginInfo: PluginInfo){ if (archetype != null) { log(2, "Running archetype $archetypeName") archetype.generateArchetype(args, classLoader) + log(1, "\n\n" + archetype.instructions) } else { warn("Couldn't find any archetype named $archetypeName") } diff --git a/src/main/resources/META-INF/kobalt-core-plugin.xml b/src/main/resources/META-INF/kobalt-core-plugin.xml index e4e467cb..9ff1bd10 100644 --- a/src/main/resources/META-INF/kobalt-core-plugin.xml +++ b/src/main/resources/META-INF/kobalt-core-plugin.xml @@ -21,5 +21,8 @@ com.beust.kobalt.internal.TestNgRunner com.beust.kobalt.internal.SpekRunner + + com.beust.kobalt.app.KobaltPluginTemplate + \ No newline at end of file diff --git a/src/main/resources/templates/plugin.jar b/src/main/resources/templates/plugin.jar new file mode 100644 index 00000000..8d8683a6 Binary files /dev/null and b/src/main/resources/templates/plugin.jar differ