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

Refactor.

This commit is contained in:
Cedric Beust 2015-12-18 23:03:53 +04:00
parent 7b603c76f4
commit 589c953a9c

View file

@ -34,48 +34,46 @@ public class JarUtils {
public fun addSingleFile(directory: String, file: IncludedFile, outputStream: ZipOutputStream, public fun addSingleFile(directory: String, file: IncludedFile, outputStream: ZipOutputStream,
expandJarFiles: Boolean, onError: (Exception) -> Unit = DEFAULT_HANDLER) { expandJarFiles: Boolean, onError: (Exception) -> Unit = DEFAULT_HANDLER) {
file.specs.forEach { spec -> file.allFromFiles(directory).forEach { source ->
val path = spec.toString() val path = source.path
spec.toFiles(directory + "/" + file.from).forEach { source -> if (source.isDirectory) {
if (source.isDirectory) { log(2, "Writing contents of directory $source")
log(2, "Writing contents of directory $source")
// Directory // Directory
var name = path var name = path
if (!name.isEmpty()) { if (!name.isEmpty()) {
if (!name.endsWith("/")) name += "/" if (!name.endsWith("/")) name += "/"
val entry = JarEntry(name) val entry = JarEntry(name)
entry.time = source.lastModified() entry.time = source.lastModified()
try { try {
outputStream.putNextEntry(entry) outputStream.putNextEntry(entry)
} finally { } finally {
outputStream.closeEntry() outputStream.closeEntry()
} }
}
val includedFile = IncludedFile(From(source.path), To(""), listOf(IFileSpec.Glob("**")))
addSingleFile(".", includedFile, outputStream, expandJarFiles)
} else {
if (expandJarFiles and source.name.endsWith(".jar")) {
log(2, "Writing contents of jar file $source")
val stream = JarInputStream(FileInputStream(source))
var entry = stream.nextEntry
while (entry != null) {
if (! entry.isDirectory && ! KFiles.isExcluded(entry.name, DEFAULT_JAR_EXCLUDES)) {
val ins = JarFile(source).getInputStream(entry)
addEntry(ins, JarEntry(entry), outputStream, onError)
}
entry = stream.nextEntry
} }
val includedFile = IncludedFile(From(source.path), To(""), listOf(IFileSpec.Glob("**")))
addSingleFile(".", includedFile, outputStream, expandJarFiles)
} else { } else {
if (expandJarFiles and source.name.endsWith(".jar")) { val entry = JarEntry((file.to + source.path).replace("\\", "/"))
log(2, "Writing contents of jar file $source") entry.time = source.lastModified()
val stream = JarInputStream(FileInputStream(source)) val fromPath = (file.from + "/" + source.path).replace("\\", "/")
var entry = stream.nextEntry val entryFile = File(directory, fromPath)
while (entry != null) { if (! entryFile.exists()) {
if (! entry.isDirectory && ! KFiles.isExcluded(entry.name, DEFAULT_JAR_EXCLUDES)) { throw AssertionError("File should exist: $entryFile")
val ins = JarFile(source).getInputStream(entry)
addEntry(ins, JarEntry(entry), outputStream, onError)
}
entry = stream.nextEntry
}
} else {
val entry = JarEntry((file.to + source.path).replace("\\", "/"))
entry.time = source.lastModified()
val fromPath = (file.from + "/" + source.path).replace("\\", "/")
val entryFile = File(directory, fromPath)
if (! entryFile.exists()) {
throw AssertionError("File should exist: $entryFile")
}
addEntry(FileInputStream(entryFile), entry, outputStream, onError)
} }
addEntry(FileInputStream(entryFile), entry, outputStream, onError)
} }
} }
} }
@ -153,6 +151,17 @@ class IncludedFile(val fromOriginal: From, val toOriginal: To, val specs: List<I
"files", specs.map { it.toString() }.joinToString(", "), "files", specs.map { it.toString() }.joinToString(", "),
"from", from, "from", from,
"to", to) "to", to)
fun allFromFiles(directory: String): List<File> {
val result = arrayListOf<File>()
specs.forEach { spec ->
val path = spec.toString()
spec.toFiles(directory + "/" + from).forEach { source ->
result.add(source)
}
}
return result
}
} }
class From(override val p: String) : Direction(p) class From(override val p: String) : Direction(p)