Skip to content

Commit

Permalink
move some processors to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
rhysdh540 committed Jul 16, 2024
1 parent d96706d commit d27ca7c
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
25 changes: 2 additions & 23 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -286,30 +286,9 @@ val compressJar = tasks.register<ProcessJar>("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")
Expand Down
31 changes: 31 additions & 0 deletions buildSrc/src/main/kotlin/Compressors.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
}
4 changes: 4 additions & 0 deletions buildSrc/src/main/kotlin/ProcessJar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ open class ProcessJar : Jar() {
}

fun addFileProcessor(vararg extensions: String, processor: FileProcessor) {
addFileProcessor(extensions.asIterable(), processor)
}

fun addFileProcessor(extensions: Iterable<String>, processor: FileProcessor) {
processors.add {
it.walkTopDown().forEach { file ->
if (file.extension in extensions)
Expand Down

0 comments on commit d27ca7c

Please sign in to comment.