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

KobaltPlugin template.

This commit is contained in:
Cedric Beust 2016-02-16 18:07:18 -08:00
parent 9532188c3b
commit d5e6a04b5d
6 changed files with 77 additions and 1 deletions

View file

@ -27,6 +27,11 @@ interface IArchetype {
*/ */
val pluginName: String 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 * Generate the files for this archetype. The parameter is the arguments that were passed to the kobaltw
* command. * command.

View file

@ -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)
}
}

View file

@ -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)
}

View file

@ -2,7 +2,6 @@ package com.beust.kobalt.app
import com.beust.kobalt.Args import com.beust.kobalt.Args
import com.beust.kobalt.api.IArchetype import com.beust.kobalt.api.IArchetype
import com.beust.kobalt.api.IInitContributor
import com.beust.kobalt.internal.PluginInfo import com.beust.kobalt.internal.PluginInfo
import com.beust.kobalt.misc.log import com.beust.kobalt.misc.log
import com.beust.kobalt.misc.warn import com.beust.kobalt.misc.warn
@ -27,6 +26,7 @@ class ProjectGenerator @Inject constructor(val pluginInfo: PluginInfo){
if (archetype != null) { if (archetype != null) {
log(2, "Running archetype $archetypeName") log(2, "Running archetype $archetypeName")
archetype.generateArchetype(args, classLoader) archetype.generateArchetype(args, classLoader)
log(1, "\n\n" + archetype.instructions)
} else { } else {
warn("Couldn't find any archetype named $archetypeName") warn("Couldn't find any archetype named $archetypeName")
} }

View file

@ -21,5 +21,8 @@
<class-name>com.beust.kobalt.internal.TestNgRunner</class-name> <class-name>com.beust.kobalt.internal.TestNgRunner</class-name>
<class-name>com.beust.kobalt.internal.SpekRunner</class-name> <class-name>com.beust.kobalt.internal.SpekRunner</class-name>
<!-- Templates -->
<class-name>com.beust.kobalt.app.KobaltPluginTemplate</class-name>
</plugin-actors> </plugin-actors>
</kobalt-plugin> </kobalt-plugin>

Binary file not shown.