diff --git a/build.gradle.kts b/build.gradle.kts index 183f7090..3d292914 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -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 @@ -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 @@ -79,6 +66,7 @@ allprojects { subprojects { apply(plugin = "dev.architectury.loom") + apply(plugin = "dev.ithundxr.numismatics.gradle" ) val capitalizedName = project.name.capitalized() @@ -160,9 +148,6 @@ subprojects { injectAccessWidener = true dependsOn(shadowJar) archiveClassifier = null - doLast { - transformJar(project.path, outputs.files.singleFile) - } } val common: Configuration by configurations.creating @@ -226,39 +211,6 @@ subprojects { } } -fun transformJar(projectPath: String, jar: File) { - val contents = linkedMapOf() - 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 { diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index 34c63dcf..6208bf5a 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -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"()}") diff --git a/buildSrc/src/main/kotlin/dev/ithundxr/numismaticsgradle/NumismaticsGradlePlugin.kt b/buildSrc/src/main/kotlin/dev/ithundxr/numismaticsgradle/NumismaticsGradlePlugin.kt new file mode 100644 index 00000000..4adc1d5f --- /dev/null +++ b/buildSrc/src/main/kotlin/dev/ithundxr/numismaticsgradle/NumismaticsGradlePlugin.kt @@ -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 . + */ + +package dev.ithundxr.numismaticsgradle + +import dev.ithundxr.numismaticsgradle.transformer.PostCompileTransformer +import org.gradle.api.Plugin +import org.gradle.api.Project + +class NumismaticsGradlePlugin : Plugin { + override fun apply(project: Project) { + project.tasks.named("remapJar").configure { + doLast { + PostCompileTransformer().transformJar(project.path, outputs.files.singleFile) + } + } + } +} \ No newline at end of file diff --git a/buildSrc/src/main/kotlin/dev/ithundxr/numismaticsgradle/transformer/PostCompileTransformer.kt b/buildSrc/src/main/kotlin/dev/ithundxr/numismaticsgradle/transformer/PostCompileTransformer.kt new file mode 100644 index 00000000..4aee2a30 --- /dev/null +++ b/buildSrc/src/main/kotlin/dev/ithundxr/numismaticsgradle/transformer/PostCompileTransformer.kt @@ -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 . + */ + +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() + 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() + } + } + +} \ No newline at end of file