mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Expand —init to files and URL’s.
This commit is contained in:
parent
d583eb0066
commit
832c602563
4 changed files with 89 additions and 49 deletions
|
@ -0,0 +1,60 @@
|
|||
package com.beust.kobalt.api
|
||||
|
||||
import com.beust.kobalt.Args
|
||||
import com.beust.kobalt.misc.KFiles
|
||||
import com.beust.kobalt.misc.kobaltLog
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
import java.io.FileOutputStream
|
||||
import java.io.IOException
|
||||
import java.net.URL
|
||||
import java.util.jar.JarInputStream
|
||||
|
||||
/**
|
||||
* Base class for templates that simply decompress a jar file found on the classpath to generate their project.
|
||||
*/
|
||||
interface InputStreamJarTemplate : ITemplate {
|
||||
val inputStream: JarInputStream
|
||||
|
||||
override fun generateTemplate(args: Args, classLoader: ClassLoader) {
|
||||
extractFile(inputStream, File("."))
|
||||
}
|
||||
|
||||
private 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
|
||||
}
|
||||
|
||||
kobaltLog(2, " Extracting: $entry to ${f.absolutePath}")
|
||||
FileOutputStream(f).use { fos ->
|
||||
KFiles.copy(ins, fos)
|
||||
}
|
||||
entry = ins.nextEntry
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class ResourceJarTemplate(val jarName: String, val classLoader: ClassLoader) : InputStreamJarTemplate {
|
||||
override val inputStream = JarInputStream(classLoader.getResource(jarName).openConnection().inputStream)
|
||||
}
|
||||
|
||||
abstract class FileJarTemplate(val fileName: String, val classLoader: ClassLoader) : InputStreamJarTemplate {
|
||||
override val inputStream = JarInputStream(FileInputStream(File(fileName)))
|
||||
}
|
||||
|
||||
abstract class HttpJarTemplate(val url: String, val classLoader: ClassLoader) : InputStreamJarTemplate {
|
||||
override val inputStream : JarInputStream
|
||||
get() {
|
||||
try {
|
||||
val result = URL(url).openConnection().inputStream
|
||||
return JarInputStream(result)
|
||||
} catch(ex: IOException) {
|
||||
throw IllegalArgumentException("Couldn't connect to $url")
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,41 +0,0 @@
|
|||
package com.beust.kobalt.api
|
||||
|
||||
import com.beust.kobalt.Args
|
||||
import com.beust.kobalt.misc.KFiles
|
||||
import com.beust.kobalt.misc.kobaltLog
|
||||
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) : ITemplate {
|
||||
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
|
||||
}
|
||||
|
||||
kobaltLog(2, "Extracting: $entry to ${f.absolutePath}")
|
||||
FileOutputStream(f).use { fos ->
|
||||
KFiles.copy(ins, fos)
|
||||
}
|
||||
entry = ins.nextEntry
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun generateTemplate(args: Args, classLoader: ClassLoader) {
|
||||
kobaltLog(2, "Generating template with class loader $classLoader")
|
||||
val destDir = File(".")
|
||||
val ins = JarInputStream(classLoader.getResource(jarName).openConnection().inputStream)
|
||||
extractFile(ins, destDir)
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue