Skip to content

Commit

Permalink
Wait, it's all gradle?
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Jun 8, 2024
1 parent cb27355 commit 84a3b6e
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 50 deletions.
52 changes: 2 additions & 50 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,10 @@
*/

import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar
import dev.ithundxr.numismaticsgradle.asm.NumismaticsGradleASM
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import net.fabricmc.loom.api.LoomGradleExtensionAPI
import net.fabricmc.loom.task.RemapJarTask
import org.gradle.configurationcache.extensions.capitalized
import org.objectweb.asm.ClassReader
import org.objectweb.asm.ClassWriter
import org.objectweb.asm.Label
import org.objectweb.asm.Opcodes
import org.objectweb.asm.tree.ClassNode
import org.objectweb.asm.util.CheckClassAdapter
import java.io.ByteArrayOutputStream
import java.util.Locale
import java.util.jar.JarEntry
import java.util.jar.JarFile
import java.util.jar.JarOutputStream
import java.util.zip.Deflater

plugins {
java
Expand All @@ -44,6 +30,7 @@ plugins {
id("me.modmuss50.mod-publish-plugin") version "0.3.4" apply false // https://github.com/modmuss50/mod-publish-plugin
id("com.github.johnrengelman.shadow") version "8.1.1" apply false
id("dev.ithundxr.silk") version "0.11.+" // https://github.com/IThundxr/silk
id("dev.ithundxr.numismatics.gradle") apply false
}

val isRelease = System.getenv("RELEASE_BUILD")?.toBoolean() ?: false
Expand Down Expand Up @@ -79,6 +66,7 @@ allprojects {

subprojects {
apply(plugin = "dev.architectury.loom")
apply(plugin = "dev.ithundxr.numismatics.gradle" )

val capitalizedName = project.name.capitalized()

Expand Down Expand Up @@ -160,9 +148,6 @@ subprojects {
injectAccessWidener = true
dependsOn(shadowJar)
archiveClassifier = null
doLast {
transformJar(project.path, outputs.files.singleFile)
}
}

val common: Configuration by configurations.creating
Expand Down Expand Up @@ -226,39 +211,6 @@ subprojects {
}
}

fun transformJar(projectPath: String, jar: File) {
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, data) = it
if (name.startsWith("architectury_inject_${"archives_base_name"().lowercase(Locale.ROOT)}_common"))
return@forEach

if (name.endsWith(".json") || name.endsWith(".mcmeta")) {
data = (JsonOutput.toJson(JsonSlurper().parse(data)).toByteArray())
} else if (name.endsWith(".class")) {
data = NumismaticsGradleASM().transformClass(projectPath, data)
}

out.putNextEntry(JarEntry(name))
out.write(data)
out.closeEntry()
}
out.finish()
out.close()
}
}

fun calculateGitHash(): String {
val stdout = ByteArrayOutputStream()
exec {
Expand Down
9 changes: 9 additions & 0 deletions buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ repositories {
mavenCentral()
}

gradlePlugin {
plugins {
create("numismaticsPlugin") {
id = "dev.ithundxr.numismatics.gradle"
implementationClass = "dev.ithundxr.numismaticsgradle.NumismaticsGradlePlugin"
}
}
}

dependencies {
implementation("org.ow2.asm:asm:${"asm_version"()}")
//implementation("org.ow2.asm:asm-analysis:${"asm_version"()}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.numismaticsgradle

import dev.ithundxr.numismaticsgradle.transformer.PostCompileTransformer
import org.gradle.api.Plugin
import org.gradle.api.Project

class NumismaticsGradlePlugin : Plugin<Project> {
override fun apply(project: Project) {
project.tasks.named("remapJar").configure {
doLast {
PostCompileTransformer().transformJar(project.path, outputs.files.singleFile)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.numismaticsgradle.transformer

import dev.ithundxr.numismaticsgradle.asm.NumismaticsGradleASM
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import java.io.File
import java.util.jar.JarEntry
import java.util.jar.JarFile
import java.util.jar.JarOutputStream
import java.util.zip.Deflater

class PostCompileTransformer {
fun transformJar(projectPath: String, jar: File) {
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, data) = it
if (Regex("architectury_inject_.+_common").matches(name))
return@forEach

if (name.endsWith(".json") || name.endsWith(".mcmeta")) {
data = (JsonOutput.toJson(JsonSlurper().parse(data)).toByteArray())
} else if (name.endsWith(".class")) {
data = NumismaticsGradleASM().transformClass(projectPath, data)
}

out.putNextEntry(JarEntry(name))
out.write(data)
out.closeEntry()
}
out.finish()
out.close()
}
}

}

0 comments on commit 84a3b6e

Please sign in to comment.