Skip to content

Commit

Permalink
something idk
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysdh540 committed Jul 18, 2024
1 parent d27ca7c commit 049b64c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 26 deletions.
5 changes: 2 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,8 @@ val compressJar = tasks.register<ProcessJar>("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")
Expand Down
8 changes: 5 additions & 3 deletions buildSrc/src/main/kotlin/Compressors.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand Down
40 changes: 20 additions & 20 deletions buildSrc/src/main/kotlin/ProcessJar.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<FileProcessor>()
abstract val processors: ListProperty<FileProcessor>

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<String>, processor: FileProcessor) {
fun addFileProcessor(extensions: Iterable<String> = emptySet(),
names: Iterable<String> = emptySet(),
paths: Iterable<String> = 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)
}
}
Expand Down Expand Up @@ -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 ->
Expand Down

0 comments on commit 049b64c

Please sign in to comment.