mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
KobaltPlugin template.
This commit is contained in:
parent
9532188c3b
commit
d5e6a04b5d
6 changed files with 77 additions and 1 deletions
|
@ -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.
|
||||
|
|
49
src/main/kotlin/com/beust/kobalt/app/JarTemplate.kt
Normal file
49
src/main/kotlin/com/beust/kobalt/app/JarTemplate.kt
Normal 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)
|
||||
}
|
||||
|
||||
}
|
19
src/main/kotlin/com/beust/kobalt/app/KobaltPluginTemplate.kt
Normal file
19
src/main/kotlin/com/beust/kobalt/app/KobaltPluginTemplate.kt
Normal 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)
|
||||
}
|
|
@ -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")
|
||||
}
|
||||
|
|
|
@ -21,5 +21,8 @@
|
|||
<class-name>com.beust.kobalt.internal.TestNgRunner</class-name>
|
||||
<class-name>com.beust.kobalt.internal.SpekRunner</class-name>
|
||||
|
||||
<!-- Templates -->
|
||||
<class-name>com.beust.kobalt.app.KobaltPluginTemplate</class-name>
|
||||
|
||||
</plugin-actors>
|
||||
</kobalt-plugin>
|
BIN
src/main/resources/templates/plugin.jar
Normal file
BIN
src/main/resources/templates/plugin.jar
Normal file
Binary file not shown.
Loading…
Add table
Add a link
Reference in a new issue