diff --git a/build.gradle.kts b/build.gradle.kts index 9d9996c..ec62673 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -286,9 +286,8 @@ val compressJar = tasks.register("compressJar") { archiveVersion = "modVersion"() archiveClassifier = "" - addFileProcessor(setOf("json", "mcmeta"), Compressors.json) - - addFileProcessor(setOf("jar"), Compressors.storeJars) + addFileProcessor(extensions = setOf("json", "mcmeta"), processor = Compressors.json) + addFileProcessor(extensions = setOf("jar"), processor = 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 index 477c08e..7c18d34 100644 --- a/buildSrc/src/main/kotlin/Compressors.kt +++ b/buildSrc/src/main/kotlin/Compressors.kt @@ -7,11 +7,13 @@ 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()) + // minify json + val json: FileProcessor = { + it.outputStream().write(JsonOutput.toJson(JsonSlurper().parse(it)).toByteArray()) } - val storeJars = { input: File -> + // store JIJs instead of deflating them so that the outer jar compresses the entire thing (most of the time better) + val storeJars: FileProcessor = { input -> val tmp = input.copyTo(File.createTempFile(input.nameWithoutExtension, ".jar"), overwrite = true) JarInputStream(tmp.inputStream()).use { ins -> JarOutputStream(input.outputStream()).use { out -> diff --git a/buildSrc/src/main/kotlin/ProcessJar.kt b/buildSrc/src/main/kotlin/ProcessJar.kt index 4411ab9..feffed1 100644 --- a/buildSrc/src/main/kotlin/ProcessJar.kt +++ b/buildSrc/src/main/kotlin/ProcessJar.kt @@ -1,3 +1,5 @@ +import org.gradle.api.file.RegularFileProperty +import org.gradle.api.provider.ListProperty import org.gradle.api.tasks.InputFile import org.gradle.jvm.tasks.Jar import java.io.File @@ -7,34 +9,40 @@ import java.util.zip.Deflater typealias FileProcessor = (File) -> Unit -open class ProcessJar : Jar() { - val input = project.objects.fileProperty() +abstract class ProcessJar : Jar() { + abstract val input: RegularFileProperty @InputFile get - private val processors = mutableListOf() + abstract val processors: ListProperty init { group = "build" outputs.upToDateWhen { false } + + addFileProcessor(paths = setOf("META-INF/MANIFEST.MF")) { file -> + manifest.from(file) + file.delete() + file.createNewFile() + manifest.effectiveManifest.writeTo(file) + } } fun addFileProcessor(regex: Regex, processor: FileProcessor) { processors.add { it.walkTopDown().forEach { file -> - if (file.extension.matches(regex)) + if (file.path.matches(regex)) processor(file) } } } - fun addFileProcessor(vararg extensions: String, processor: FileProcessor) { - addFileProcessor(extensions.asIterable(), processor) - } - - fun addFileProcessor(extensions: Iterable, processor: FileProcessor) { + fun addFileProcessor(extensions: Iterable = emptySet(), + names: Iterable = emptySet(), + paths: Iterable = emptySet(), + processor: FileProcessor) { processors.add { it.walkTopDown().forEach { file -> - if (file.extension in extensions) + if (file.extension in extensions || file.name in names || file.path in paths) processor(file) } } @@ -62,16 +70,8 @@ open class ProcessJar : Jar() { into(dir) } - processors.forEach { it(dir) } - - // merge manifests - val manifestFile = dir.resolve("META-INF/MANIFEST.MF") - if (manifestFile.exists()) { - manifest.from(manifestFile) - manifestFile.delete() - manifestFile.createNewFile() - manifest.effectiveManifest.writeTo(manifestFile) - } + processors.finalizeValue() + processors.get().forEach { it(dir) } // repack jar JarOutputStream(archiveFile.get().asFile.outputStream()).use { jos ->