From d27ca7c8dce1d91d92b3df3c574a73539f9be6db Mon Sep 17 00:00:00 2001 From: Rhys <98863820+rhysdh540@users.noreply.github.com> Date: Tue, 16 Jul 2024 17:55:27 -0400 Subject: [PATCH] move some processors to a separate file --- build.gradle.kts | 25 ++------------------ buildSrc/src/main/kotlin/Compressors.kt | 31 +++++++++++++++++++++++++ buildSrc/src/main/kotlin/ProcessJar.kt | 4 ++++ 3 files changed, 37 insertions(+), 23 deletions(-) create mode 100644 buildSrc/src/main/kotlin/Compressors.kt diff --git a/build.gradle.kts b/build.gradle.kts index 3dd47ef..9d9996c 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -286,30 +286,9 @@ val compressJar = tasks.register("compressJar") { archiveVersion = "modVersion"() archiveClassifier = "" - addFileProcessor("json", "mcmeta") { // remove whitespace/comments from json files - val json = JsonSlurper().parse(it) - it.outputStream().write(JsonOutput.toJson(json).toByteArray()) - } + addFileProcessor(setOf("json", "mcmeta"), Compressors.json) - addFileProcessor("jar") { // store jar files with no compression - val tmp = it.copyTo(File.createTempFile(it.nameWithoutExtension, ".jar"), overwrite = true) - JarInputStream(tmp.inputStream()).use { ins -> - JarOutputStream(it.outputStream()).use { out -> - out.setLevel(Deflater.NO_COMPRESSION) - var entry: JarEntry? = ins.nextEntry as JarEntry? - while (entry != null) { - out.putNextEntry(JarEntry(entry.name)) - ins.copyTo(out) - out.closeEntry() - ins.closeEntry() - entry = ins.nextEntry as JarEntry? - } - - out.finish() - out.flush() - } - } - } + addFileProcessor(setOf("jar"), Compressors.storeJars) addDirProcessor { dir -> // proguard val temp = temporaryDir.resolve("proguard") diff --git a/buildSrc/src/main/kotlin/Compressors.kt b/buildSrc/src/main/kotlin/Compressors.kt new file mode 100644 index 0000000..477c08e --- /dev/null +++ b/buildSrc/src/main/kotlin/Compressors.kt @@ -0,0 +1,31 @@ +import groovy.json.JsonOutput +import groovy.json.JsonSlurper +import java.io.File +import java.util.jar.JarEntry +import java.util.jar.JarInputStream +import java.util.jar.JarOutputStream +import java.util.zip.Deflater + +object Compressors { + val json = { input: File -> + input.outputStream().write(JsonOutput.toJson(JsonSlurper().parse(input)).toByteArray()) + } + + val storeJars = { input: File -> + val tmp = input.copyTo(File.createTempFile(input.nameWithoutExtension, ".jar"), overwrite = true) + JarInputStream(tmp.inputStream()).use { ins -> + JarOutputStream(input.outputStream()).use { out -> + out.setLevel(Deflater.NO_COMPRESSION) + while (true) { + out.putNextEntry(JarEntry((ins.nextEntry ?: break).name)) + ins.copyTo(out) + out.closeEntry() + ins.closeEntry() + } + + out.finish() + out.flush() + } + } + } +} diff --git a/buildSrc/src/main/kotlin/ProcessJar.kt b/buildSrc/src/main/kotlin/ProcessJar.kt index 3b3c3ed..4411ab9 100644 --- a/buildSrc/src/main/kotlin/ProcessJar.kt +++ b/buildSrc/src/main/kotlin/ProcessJar.kt @@ -28,6 +28,10 @@ open class ProcessJar : Jar() { } fun addFileProcessor(vararg extensions: String, processor: FileProcessor) { + addFileProcessor(extensions.asIterable(), processor) + } + + fun addFileProcessor(extensions: Iterable, processor: FileProcessor) { processors.add { it.walkTopDown().forEach { file -> if (file.extension in extensions)