mirror of
https://github.com/ethauvin/kobalt.git
synced 2025-04-26 08:27:12 -07:00
Use use() more.
This commit is contained in:
parent
d26ae330a7
commit
0e728289c8
5 changed files with 40 additions and 68 deletions
|
@ -205,24 +205,19 @@ public class Plugins @Inject constructor (val taskManagerProvider : Provider<Tas
|
|||
//
|
||||
// Inspect the jar, open the manifest, instantiate the main class and add it to the plugin repo
|
||||
//
|
||||
var fis: FileInputStream? = null
|
||||
var jis: JarInputStream? = null
|
||||
try {
|
||||
fis = FileInputStream(it.jarFile.get())
|
||||
jis = JarInputStream(fis)
|
||||
val manifest = jis.manifest
|
||||
val mainClass = manifest.mainAttributes.getValue(Plugins.MANIFEST_PLUGIN_CLASS) ?:
|
||||
throw KobaltException("Couldn't find \"${Plugins.MANIFEST_PLUGIN_CLASS}\" in the " +
|
||||
"manifest of $it")
|
||||
FileInputStream(it.jarFile.get()).use { fis ->
|
||||
JarInputStream(fis).use { jis ->
|
||||
val manifest = jis.manifest
|
||||
val mainClass = manifest.mainAttributes.getValue(Plugins.MANIFEST_PLUGIN_CLASS) ?:
|
||||
throw KobaltException("Couldn't find \"${Plugins.MANIFEST_PLUGIN_CLASS}\" in the " +
|
||||
"manifest of $it")
|
||||
|
||||
val pluginClassName = mainClass.removeSuffix(" ")
|
||||
val c = instantiateClassName(classLoader, pluginClassName)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
Plugins.addPlugin(c as Class<BasePlugin>)
|
||||
log(1, "Added plugin $c")
|
||||
} finally {
|
||||
jis?.close()
|
||||
fis?.close()
|
||||
val pluginClassName = mainClass.removeSuffix(" ")
|
||||
val c = instantiateClassName(classLoader, pluginClassName)
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
Plugins.addPlugin(c as Class<BasePlugin>)
|
||||
log(1, "Added plugin $c")
|
||||
}
|
||||
}
|
||||
}
|
||||
executor.shutdown()
|
||||
|
|
|
@ -68,17 +68,13 @@ class Kurl @Inject constructor(@Assisted val url: String) {
|
|||
val string: String
|
||||
get() {
|
||||
val sb = StringBuilder()
|
||||
connection.inputStream.let { inputStream ->
|
||||
connection.inputStream.use { inputStream ->
|
||||
val reader = BufferedReader(InputStreamReader(inputStream))
|
||||
|
||||
var line: String? = reader.readLine()
|
||||
try {
|
||||
while (line != null) {
|
||||
sb.append(line).append('\n')
|
||||
line = reader.readLine()
|
||||
}
|
||||
} finally {
|
||||
inputStream.close()
|
||||
while (line != null) {
|
||||
sb.append(line).append('\n')
|
||||
line = reader.readLine()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -179,14 +179,4 @@ public class KFiles {
|
|||
function: Function1<String, Boolean>): List<String> {
|
||||
return KFiles.findRecursively(rootDir, directories, function)
|
||||
}
|
||||
|
||||
public fun saveFile(file: File, bytes: ByteArray) {
|
||||
file.parentFile.mkdirs()
|
||||
val os = file.outputStream()
|
||||
try {
|
||||
os.write(bytes)
|
||||
} finally {
|
||||
os.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,7 +13,6 @@ import com.beust.kobalt.maven.IClasspathDependency
|
|||
import com.beust.kobalt.maven.LocalRepo
|
||||
import com.beust.kobalt.misc.KFiles
|
||||
import com.beust.kobalt.misc.KobaltExecutors
|
||||
import com.beust.kobalt.misc.log
|
||||
import java.io.File
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
|
@ -87,8 +86,6 @@ class KotlinPlugin @Inject constructor(
|
|||
|
||||
private fun compilePrivate(project: Project, cpList: List<IClasspathDependency>, sources: List<String>,
|
||||
outputDirectory: File): TaskResult {
|
||||
log(1, " Compiling ${sources.size} files with classpath size ${cpList.size}")
|
||||
|
||||
return kotlinCompilePrivate {
|
||||
classpath(cpList.map { it.jarFile.get().absolutePath })
|
||||
sourceFiles(sources)
|
||||
|
|
|
@ -1,13 +1,11 @@
|
|||
package com.beust.kobalt.plugin.packaging
|
||||
|
||||
import com.beust.kobalt.IFileSpec
|
||||
import com.beust.kobalt.file
|
||||
import com.beust.kobalt.misc.log
|
||||
import java.io.*
|
||||
import java.util.jar.JarEntry
|
||||
import java.util.jar.JarFile
|
||||
import java.util.jar.JarInputStream
|
||||
import java.util.jar.JarOutputStream
|
||||
import java.util.zip.ZipEntry
|
||||
import java.util.zip.ZipOutputStream
|
||||
|
||||
|
@ -137,46 +135,42 @@ public class JarUtils {
|
|||
}
|
||||
}
|
||||
|
||||
fun removeDuplicateEntries(fromJarFile: File, toFile: File) {
|
||||
val fromFile = JarFile(fromJarFile)
|
||||
var entries = fromFile.entries()
|
||||
val os = JarOutputStream(FileOutputStream(toFile))
|
||||
val seen = hashSetOf<String>()
|
||||
while (entries.hasMoreElements()) {
|
||||
val entry = entries.nextElement()
|
||||
if (! seen.contains(entry.name)) {
|
||||
val ins = fromFile.getInputStream(entry)
|
||||
addEntry(ins, JarEntry(entry), os)
|
||||
}
|
||||
seen.add(entry.name)
|
||||
}
|
||||
os.close()
|
||||
|
||||
log(1, "Deduplicated $fromFile.name")
|
||||
}
|
||||
// fun removeDuplicateEntries(fromJarFile: File, toFile: File) {
|
||||
// val fromFile = JarFile(fromJarFile)
|
||||
// var entries = fromFile.entries()
|
||||
// val os = JarOutputStream(FileOutputStream(toFile))
|
||||
// val seen = hashSetOf<String>()
|
||||
// while (entries.hasMoreElements()) {
|
||||
// val entry = entries.nextElement()
|
||||
// if (! seen.contains(entry.name)) {
|
||||
// val ins = fromFile.getInputStream(entry)
|
||||
// addEntry(ins, JarEntry(entry), os)
|
||||
// }
|
||||
// seen.add(entry.name)
|
||||
// }
|
||||
// os.close()
|
||||
//
|
||||
// log(1, "Deduplicated $fromFile.name")
|
||||
// }
|
||||
|
||||
fun extractJarFile(jarFile: File, destDir: File) {
|
||||
val jar = java.util.jar.JarFile(jarFile)
|
||||
val enumEntries = jar.entries()
|
||||
while (enumEntries.hasMoreElements()) {
|
||||
val file = enumEntries.nextElement() as JarEntry
|
||||
val file = enumEntries.nextElement()
|
||||
val f = File(destDir.path + java.io.File.separator + file.name)
|
||||
if (file.isDirectory) {
|
||||
f.mkdir()
|
||||
continue
|
||||
}
|
||||
var ins: InputStream? = null
|
||||
var fos: OutputStream? = null
|
||||
try {
|
||||
ins = jar.getInputStream(file)
|
||||
|
||||
jar.getInputStream(file).use { ins ->
|
||||
f.parentFile.mkdirs()
|
||||
fos = FileOutputStream(f)
|
||||
while (ins.available() > 0) {
|
||||
fos.write(ins.read())
|
||||
FileOutputStream(f).use { fos ->
|
||||
while (ins.available() > 0) {
|
||||
fos.write(ins.read())
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
fos?.close()
|
||||
ins?.close()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue