Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor compression + advzip re-deflation #7

Merged
merged 10 commits into from
Apr 27, 2024
4 changes: 3 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ on: [ push ]

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
- name: install advzip
run: sudo apt-get install -y advancecomp
- uses: actions/setup-java@v4
with:
distribution: temurin
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ on: [ pull_request ]

jobs:
build:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
- name: install advzip
run: sudo apt-get install -y advancecomp
- uses: actions/setup-java@v4
with:
distribution: temurin
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ on: [ workflow_dispatch ]

jobs:
publish:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
- name: install advzip
run: sudo apt-get install -y advancecomp
- uses: actions/setup-java@v4
with:
distribution: temurin
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/release_dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ on: [ workflow_dispatch ]

jobs:
publish:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
- name: install advzip
run: sudo apt-get install -y advancecomp
- uses: actions/setup-java@v4
with:
distribution: temurin
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/release_pre.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ on: [ workflow_dispatch ]

jobs:
publish:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
- name: install advzip
run: sudo apt-get install -y advancecomp
- uses: actions/setup-java@v4
with:
distribution: temurin
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/release_rc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@ on: [ workflow_dispatch ]

jobs:
publish:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
- name: install advzip
run: sudo apt-get install -y advancecomp
- uses: actions/setup-java@v4
with:
distribution: temurin
Expand Down
142 changes: 41 additions & 101 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@file:Suppress("UnstableApiUsage")
import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import com.github.jengelman.gradle.plugins.shadow.transformers.Transformer
import com.github.jengelman.gradle.plugins.shadow.transformers.TransformerContext
Expand All @@ -20,10 +21,7 @@ import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.tree.ClassNode
import xyz.wagyourtail.unimined.api.minecraft.task.RemapJarTask
import java.util.jar.JarEntry
import java.util.jar.JarFile
import java.util.jar.JarOutputStream
import java.util.zip.Deflater
import java.time.ZonedDateTime

plugins {
id("java")
Expand All @@ -34,25 +32,43 @@ plugins {
id("org.ajoberstar.grgit")
}

operator fun String.invoke(): String {
return (rootProject.properties[this] as String?)!!
}
operator fun String.invoke(): String = rootProject.properties[this] as? String ?: error("Property $this not found")

enum class ReleaseChannel(
val suffix: String? = null,
val releaseType: ReleaseType? = null,
val compress: Boolean,
val deflation: JarShrinkingType,
val classes: ClassShrinkingType,
val json: JsonShrinkingType,
) {
DEV_BUILD(suffix = "dev", compress = false),
PRE_RELEASE(suffix = "pre", compress = false),
RELEASE_CANDIDATE(suffix = "rc", compress = true),
RELEASE(releaseType = ReleaseType.STABLE, compress = true),
DEV_BUILD(
suffix = "dev",
deflation = JarShrinkingType.SEVENZIP,
classes = ClassShrinkingType.STRIP_NONE,
json = JsonShrinkingType.PRETTY_PRINT),
PRE_RELEASE(
suffix = "pre",
deflation = JarShrinkingType.SEVENZIP,
classes = ClassShrinkingType.STRIP_NONE,
json = JsonShrinkingType.PRETTY_PRINT),
RELEASE_CANDIDATE(
suffix = "rc",
deflation = JarShrinkingType.SEVENZIP,
classes = ClassShrinkingType.STRIP_ALL,
json = JsonShrinkingType.MINIFY),
RELEASE(
releaseType = ReleaseType.STABLE,
deflation = JarShrinkingType.SEVENZIP,
classes = ClassShrinkingType.STRIP_ALL,
json = JsonShrinkingType.MINIFY),
}

val isRelease = rootProject.hasProperty("release_channel")
val releaseChannel = if (isRelease) ReleaseChannel.valueOf("release_channel"()) else ReleaseChannel.DEV_BUILD

val headDateTime = grgit.head().dateTime
println("Release Channel: $releaseChannel")

val headDateTime: ZonedDateTime = grgit.head().dateTime

val branchName = grgit.branch.current().name!!
val releaseTagPrefix = "release/"
Expand Down Expand Up @@ -164,9 +180,10 @@ allprojects {
}

dependencies {
val jabelDependency = "com.pkware.jabel:jabel-javac-plugin:${"jabel_version"()}"
annotationProcessor(jabelDependency)
compileOnly(jabelDependency)
"com.pkware.jabel:jabel-javac-plugin:${"jabel_version"()}".also {
annotationProcessor(it)
compileOnly(it)
}
}

tasks.processResources {
Expand Down Expand Up @@ -447,79 +464,16 @@ tasks.assemble {
dependsOn(tasks.shadowJar)
}

abstract class ProcessJarTask : DefaultTask() {
@get:InputFile
abstract val inputJar: RegularFileProperty

@OutputFile
fun getOutputJar(): RegularFile {
return inputJar.get()
}
}

val compressJar = tasks.register<ProcessJarTask>("compressJar") {
val compressJar = tasks.register<CompressJarTask>("compressJar") {
dependsOn(tasks.shadowJar)
group = "build"

val stripLVTs = "strip_lvts"().toBoolean()
val stripSourceFiles = "strip_source_files"().toBoolean()

inputs.property("strip_lvts", stripLVTs)
inputs.property("strip_source_files", stripSourceFiles)

val processClassFiles = stripLVTs || stripSourceFiles

val shadowJar = tasks.shadowJar.get()
inputJar.set(shadowJar.archiveFile)
inputJar = shadowJar.archiveFile.get().asFile

doLast {
val jar = inputJar.get().asFile
val contents = linkedMapOf<String, ByteArray>()
JarFile(jar).use {
it.entries().asIterator().forEach { entry ->
if (!entry.isDirectory) {
contents[entry.name] = it.getInputStream(entry).readAllBytes()
}
}
}

jar.delete()

JarOutputStream(jar.outputStream()).use { out ->
out.setLevel(Deflater.BEST_COMPRESSION)
contents.forEach { var (name, bytes) = it
if (name.endsWith(".json") || name.endsWith(".mcmeta") || name == "mcmod.info") {
bytes = JsonOutput.toJson(JsonSlurper().parse(bytes)).toByteArray()
}

if (processClassFiles && name.endsWith(".class")) {
val reader = ClassReader(bytes)
val classNode = ClassNode()
reader.accept(classNode, 0)

if (stripLVTs) {
classNode.methods.forEach { methodNode ->
methodNode.localVariables?.clear()
methodNode.parameters?.clear()
}
}
if (stripSourceFiles) {
classNode.sourceFile = null
}

val writer = ClassWriter(0)
classNode.accept(writer)
bytes = writer.toByteArray()
}

out.putNextEntry(JarEntry(name))
out.write(bytes)
out.closeEntry()
}
out.finish()
out.close()
}
}
jarShrinkingType = releaseChannel.deflation
classShrinkingType = releaseChannel.classes
jsonShrinkingType = releaseChannel.json
}

afterEvaluate {
Expand All @@ -544,23 +498,9 @@ afterEvaluate {
fun getChangelog(): String {
return file("CHANGELOG.md").readText()
}

fun getTaskForPublish(): TaskProvider<out DefaultTask> {
return if (releaseChannel.compress)
compressJar
else
tasks.shadowJar
}

fun getFileForPublish(): RegularFile {
return if (releaseChannel.compress)
compressJar.get().getOutputJar()
else
tasks.shadowJar.get().archiveFile.get()
}

publishMods {
file = getFileForPublish()
file = compressJar.get().outputJar
type = releaseChannel.releaseType ?: ALPHA
displayName = Zume.version
version = Zume.version
Expand Down Expand Up @@ -656,7 +596,7 @@ afterEvaluate {
}

tasks.withType<PublishModTask> {
dependsOn(getTaskForPublish())
dependsOn(compressJar)
}

tasks.publishMods {
Expand All @@ -683,7 +623,7 @@ afterEvaluate {

val webhookUrl = providers.environmentVariable("DISCORD_WEBHOOK")
val releaseChangeLog = getChangelog()
val file = getFileForPublish().asFile
val file = compressJar.get().outputJar

var content = "# [Zume Test Build ${Zume.version}]" +
"(<https://github.com/Nolij/Zume/releases/tag/${releaseTagPrefix}${Zume.version}>) has been released!\n" +
Expand Down
5 changes: 5 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ plugins {

repositories {
mavenCentral()
}

dependencies {
implementation("org.ow2.asm:asm-tree:9.7")
implementation("com.google.code.gson:gson:2.10.1")
}
Loading
Loading