From 57905fef60b5f33adfc11c2a7e1c1f88471f6b3b Mon Sep 17 00:00:00 2001 From: Rhys <98863820+rhysdh540@users.noreply.github.com> Date: Thu, 1 Aug 2024 23:47:00 -0400 Subject: [PATCH 1/5] stripAnnotations feature and other assorted stuff --- .../unimined/expect/ExpectPlatformAgent.java | 13 ++++++++--- .../expect/ExpectPlatformExtension.kt | 4 ++++ .../expect/task/ExpectPlatformFiles.kt | 7 ++++-- .../unimined/expect/task/ExpectPlatformJar.kt | 10 ++++----- .../expect/transform/ExpectPlatformParams.kt | 4 ++++ .../transform/ExpectPlatformTransform.kt | 6 +++-- .../unimined/expect/TransformPlatform.java | 22 +++++++++++-------- 7 files changed, 44 insertions(+), 22 deletions(-) diff --git a/src/agent/java/xyz/wagyourtail/unimined/expect/ExpectPlatformAgent.java b/src/agent/java/xyz/wagyourtail/unimined/expect/ExpectPlatformAgent.java index a47b009..3169aa6 100644 --- a/src/agent/java/xyz/wagyourtail/unimined/expect/ExpectPlatformAgent.java +++ b/src/agent/java/xyz/wagyourtail/unimined/expect/ExpectPlatformAgent.java @@ -14,14 +14,20 @@ public class ExpectPlatformAgent { private static final String platform = System.getProperty(EXPECT_PLATFORM); private static final String remap = System.getProperty(REMAP); - private static final TransformPlatform transformPlatform = new TransformPlatform(platform, remap); + private static final TransformPlatform transformPlatform = new TransformPlatform(platform, remap, false); public static void premain(String args, Instrumentation inst) { - System.out.println("[ExpectPlatformAgent] Platform: " + platform); - System.out.println("[ExpectPlatformAgent] Remap: " + transformPlatform.getRemap()); if (platform == null) { throw new IllegalStateException("-D" + EXPECT_PLATFORM + " not set"); } + + if(!inst.isRetransformClassesSupported()) { + System.out.println("[ExpectPlatformAgent] ur instrumentation is bad lol"); + } + + System.out.println("[ExpectPlatformAgent] Platform: " + platform); + System.out.println("[ExpectPlatformAgent] Remap: " + transformPlatform.getRemap()); + inst.addTransformer(new ExpectPlatformTransformer(), inst.isRetransformClassesSupported()); } @@ -33,6 +39,7 @@ public static class ExpectPlatformTransformer implements ClassFileTransformer { @Override public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer) { + System.out.println("[ExpectPlatformAgent] Transforming: " + className); ClassReader reader = new ClassReader(classfileBuffer); ClassNode classNode = new ClassNode(); reader.accept(classNode, 0); diff --git a/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt b/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt index 2f72f02..4fdc444 100644 --- a/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt +++ b/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt @@ -9,6 +9,7 @@ import org.gradle.process.JavaExecSpec import org.jetbrains.annotations.VisibleForTesting import xyz.wagyourtail.unimined.expect.transform.ExpectPlatformParams import xyz.wagyourtail.unimined.expect.transform.ExpectPlatformTransform +import xyz.wagyourtail.unimined.expect.utils.FinalizeOnRead abstract class ExpectPlatformExtension(val project: Project) { @@ -18,6 +19,8 @@ abstract class ExpectPlatformExtension(val project: Project) { val annotationsDep by lazy { "xyz.wagyourtail.unimined.expect-platform:expect-platform-annotations:$version" } val agentDep by lazy { "xyz.wagyourtail.unimined.expect-platform:expect-platform-agent:$version" } + val stripAnnotations by FinalizeOnRead(false) + @JvmOverloads fun platform(platformName: String, configuration: Configuration, action: ExpectPlatformParams.() -> Unit = {}) { val expectPlatformAttribute = Attribute.of("expectPlatform.${configuration.name}", Boolean::class.javaObjectType) @@ -40,6 +43,7 @@ abstract class ExpectPlatformExtension(val project: Project) { spec.parameters { it.platformName.set(platformName) + it.stripAnnotations.set(stripAnnotations) it.action() } } diff --git a/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformFiles.kt b/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformFiles.kt index 73a2c28..3b63d67 100644 --- a/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformFiles.kt +++ b/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformFiles.kt @@ -46,7 +46,7 @@ abstract class ExpectPlatformFiles : ConventionTask(), ExpectPlatformParams { } @TaskAction - fun doTranform() { + fun doTransform() { var toTransform = inputCollection.map { it.toPath() }.filter { it.exists() } val fileSystems = mutableSetOf() @@ -70,10 +70,13 @@ abstract class ExpectPlatformFiles : ConventionTask(), ExpectPlatformParams { fs.getPath("/") } } + + val transformer = TransformPlatform(platformName.get(), remap.get(), stripAnnotations.get()) + for (i in toTransform.indices) { val input = toTransform[i] val output = transformed[i] - TransformPlatform(platformName.get(), remap.get()).transform(input, output) + transformer.transform(input, output) } } finally { fileSystems.forEach { it.close() } diff --git a/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformJar.kt b/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformJar.kt index 27a52cf..99d98bb 100644 --- a/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformJar.kt +++ b/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformJar.kt @@ -5,7 +5,7 @@ package xyz.wagyourtail.unimined.expect.task import org.gradle.api.file.FileCollection import org.gradle.api.tasks.InputFiles import org.gradle.api.tasks.TaskAction -import org.gradle.jvm.tasks.Jar +import org.gradle.api.tasks.bundling.Jar import xyz.wagyourtail.unimined.expect.utils.FinalizeOnRead import xyz.wagyourtail.unimined.expect.utils.MustSet import xyz.wagyourtail.unimined.expect.ExpectPlatformExtension @@ -24,19 +24,17 @@ abstract class ExpectPlatformJar : Jar(), ExpectPlatformParams { @TaskAction fun doTransform() { + val transformer = TransformPlatform(platformName.get(), remap.get(), stripAnnotations.get()) for (input in inputFiles) { if (input.isDirectory) { val output = temporaryDir.resolve(input.name + "-expect-platform") - TransformPlatform(platformName.get(), remap.get()).transform(input.toPath(), output.toPath()) + transformer.transform(input.toPath(), output.toPath()) from(output) } else if (input.extension == "jar") { val output = temporaryDir.resolve(input.nameWithoutExtension + "-expect-platform." + input.extension) input.toPath().openZipFileSystem().use { inputFs -> output.toPath().openZipFileSystem(mapOf("create" to true)).use { outputFs -> - TransformPlatform(platformName.get(), remap.get()).transform( - inputFs.getPath("/"), - outputFs.getPath("/") - ) + transformer.transform(inputFs.getPath("/"), outputFs.getPath("/")) } } from(project.zipTree(output)) diff --git a/src/main/kotlin/xyz/wagyourtail/unimined/expect/transform/ExpectPlatformParams.kt b/src/main/kotlin/xyz/wagyourtail/unimined/expect/transform/ExpectPlatformParams.kt index fd31b5b..79f9e43 100644 --- a/src/main/kotlin/xyz/wagyourtail/unimined/expect/transform/ExpectPlatformParams.kt +++ b/src/main/kotlin/xyz/wagyourtail/unimined/expect/transform/ExpectPlatformParams.kt @@ -19,4 +19,8 @@ interface ExpectPlatformParams : TransformParameters { @get:Optional val remap: MapProperty + @get:Input + @get:Optional + val stripAnnotations: Property + } \ No newline at end of file diff --git a/src/main/kotlin/xyz/wagyourtail/unimined/expect/transform/ExpectPlatformTransform.kt b/src/main/kotlin/xyz/wagyourtail/unimined/expect/transform/ExpectPlatformTransform.kt index e03a908..da96a0f 100644 --- a/src/main/kotlin/xyz/wagyourtail/unimined/expect/transform/ExpectPlatformTransform.kt +++ b/src/main/kotlin/xyz/wagyourtail/unimined/expect/transform/ExpectPlatformTransform.kt @@ -16,15 +16,17 @@ abstract class ExpectPlatformTransform : TransformAction { override fun transform(outputs: TransformOutputs) { val platformName = parameters.platformName.get() val remap = parameters.remap.get() + val transformer = TransformPlatform(platformName, remap, parameters.stripAnnotations.get()) + val input = inputArtifact.get().asFile if (input.isDirectory) { val output = outputs.dir(input.name + "-expect-platform") - TransformPlatform(platformName, remap).transform(input.toPath(), output.toPath()) + transformer.transform(input.toPath(), output.toPath()) } else if (input.extension == "jar") { val output = outputs.file(input.nameWithoutExtension + "-expect-platform." + input.extension) input.toPath().openZipFileSystem().use { inputFs -> output.toPath().openZipFileSystem(mapOf("create" to true)).use { outputFs -> - TransformPlatform(platformName, remap).transform(inputFs.getPath("/"), outputFs.getPath("/")) + transformer.transform(inputFs.getPath("/"), outputFs.getPath("/")) } } } else { diff --git a/src/shared/java/xyz/wagyourtail/unimined/expect/TransformPlatform.java b/src/shared/java/xyz/wagyourtail/unimined/expect/TransformPlatform.java index e80e868..08d4645 100644 --- a/src/shared/java/xyz/wagyourtail/unimined/expect/TransformPlatform.java +++ b/src/shared/java/xyz/wagyourtail/unimined/expect/TransformPlatform.java @@ -18,15 +18,18 @@ public class TransformPlatform { private static final int EXPECT_PLATFORM_ACCESS = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC; private final String platformName; + private final boolean stripAnnotations; private final Map remap = new HashMap<>(); - public TransformPlatform(String platformName, String map) { + public TransformPlatform(String platformName, String map, boolean stripAnnotations) { this.platformName = platformName; + this.stripAnnotations = stripAnnotations; stringMapParser(map); } - public TransformPlatform(String platformName, Map map) { + public TransformPlatform(String platformName, Map map, boolean stripAnnotations) { this.platformName = platformName; + this.stripAnnotations = stripAnnotations; remap.putAll(map); } @@ -90,10 +93,16 @@ public ClassNode transform(ClassNode classNode) { for (Map.Entry entry : expectPlatform.entrySet()) { expectPlatform(entry.getKey(), classNode, entry.getValue()); + if (stripAnnotations) { + classNode.invisibleAnnotations.remove(entry.getValue()); + } } for (Map.Entry entry : platformOnly.entrySet()) { platformOnly(entry.getKey(), classNode, entry.getValue()); + if (stripAnnotations) { + classNode.invisibleAnnotations.remove(entry.getValue()); + } } getCurrentTarget(classNode); @@ -130,13 +139,8 @@ private void expectPlatform(MethodNode methodNode, ClassNode classNode, Annotati } if (platformClass == null) { int lastSlash = classNode.name.lastIndexOf('/'); - String pkg; - if (lastSlash == -1) { - pkg = ""; - } else { - pkg = classNode.name.substring(0, lastSlash); - } - String className = classNode.name.substring(lastSlash + 1); + String pkg = lastSlash == -1 ? "" : classNode.name.substring(0, lastSlash); + String className = classNode.name.substring(lastSlash + 1); if (pkg.isEmpty()) { platformClass = platformName + "/" + className + "Impl"; } else { From 805b2dc4fc5416800ca897290a7664d91668e840 Mon Sep 17 00:00:00 2001 From: Rhys <98863820+rhysdh540@users.noreply.github.com> Date: Fri, 2 Aug 2024 09:29:41 -0400 Subject: [PATCH 2/5] more fixes, version bump --- gradle.properties | 2 +- .../expect/ExpectPlatformExtension.kt | 2 +- .../expect/task/ExpectPlatformFiles.kt | 3 +- .../unimined/expect/task/ExpectPlatformJar.kt | 3 +- .../unimined/expect/TransformPlatform.java | 32 +++++++++++++------ 5 files changed, 26 insertions(+), 16 deletions(-) diff --git a/gradle.properties b/gradle.properties index e0e2c0e..3461615 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,5 +1,5 @@ kotlin.code.style=official -version = 1.0.5 +version = 1.1.0 asmVersion=9.7 diff --git a/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt b/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt index 4fdc444..a6f7327 100644 --- a/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt +++ b/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt @@ -43,7 +43,7 @@ abstract class ExpectPlatformExtension(val project: Project) { spec.parameters { it.platformName.set(platformName) - it.stripAnnotations.set(stripAnnotations) + it.stripAnnotations.convention(stripAnnotations) it.action() } } diff --git a/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformFiles.kt b/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformFiles.kt index 3b63d67..9081a27 100644 --- a/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformFiles.kt +++ b/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformFiles.kt @@ -52,7 +52,6 @@ abstract class ExpectPlatformFiles : ConventionTask(), ExpectPlatformParams { val fileSystems = mutableSetOf() try { - outputs.files.forEach { it.deleteRecursively() } val transformed = toTransform.map { temporaryDir.resolve(it.name) }.map { @@ -71,7 +70,7 @@ abstract class ExpectPlatformFiles : ConventionTask(), ExpectPlatformParams { } } - val transformer = TransformPlatform(platformName.get(), remap.get(), stripAnnotations.get()) + val transformer = TransformPlatform(platformName.get(), remap.get(), stripAnnotations.getOrElse(false)) for (i in toTransform.indices) { val input = toTransform[i] diff --git a/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformJar.kt b/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformJar.kt index 99d98bb..ac0b14f 100644 --- a/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformJar.kt +++ b/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformJar.kt @@ -24,7 +24,7 @@ abstract class ExpectPlatformJar : Jar(), ExpectPlatformParams { @TaskAction fun doTransform() { - val transformer = TransformPlatform(platformName.get(), remap.get(), stripAnnotations.get()) + val transformer = TransformPlatform(platformName.get(), remap.get(), stripAnnotations.getOrElse(false)) for (input in inputFiles) { if (input.isDirectory) { val output = temporaryDir.resolve(input.name + "-expect-platform") @@ -40,7 +40,6 @@ abstract class ExpectPlatformJar : Jar(), ExpectPlatformParams { from(project.zipTree(output)) } else if (input.exists()) { throw IllegalStateException("ExpectPlatformJar: $input is not a directory or jar file") - } } diff --git a/src/shared/java/xyz/wagyourtail/unimined/expect/TransformPlatform.java b/src/shared/java/xyz/wagyourtail/unimined/expect/TransformPlatform.java index 08d4645..727b5e1 100644 --- a/src/shared/java/xyz/wagyourtail/unimined/expect/TransformPlatform.java +++ b/src/shared/java/xyz/wagyourtail/unimined/expect/TransformPlatform.java @@ -17,6 +17,13 @@ public class TransformPlatform { private static final int EXPECT_PLATFORM_ACCESS = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC; + private static final String EXPECT_PLATFORM_DESC = "Lxyz/wagyourtail/unimined/expect/annotation/ExpectPlatform;"; + private static final String EXPECT_PLATFORM_TRANSFORMED_DESC = "Lxyz/wagyourtail/unimined/expect/annotation/ExpectPlatform$Transformed"; + private static final String PLATFORM_ONLY_DESC = "Lxyz/wagyourtail/unimined/expect/annotation/PlatformOnly;"; + private static final String TARGET_CLASS = "xyz/wagyourtail/unimined/expect/Target"; + private static final String TARGET_METHOD = "getCurrentTarget"; + + private final String platformName; private final boolean stripAnnotations; private final Map remap = new HashMap<>(); @@ -83,25 +90,29 @@ public ClassNode transform(ClassNode classNode) { for (MethodNode method : classNode.methods) { if (method.invisibleAnnotations == null) continue; for (AnnotationNode annotation : method.invisibleAnnotations) { - if (annotation.desc.equals("Lxyz/wagyourtail/unimined/expect/annotation/ExpectPlatform;")) { + if (annotation.desc.equals(EXPECT_PLATFORM_DESC)) { expectPlatform.put(method, annotation); - } else if (annotation.desc.equals("Lxyz/wagyourtail/unimined/expect/annotation/PlatformOnly;")) { + } else if (annotation.desc.equals(PLATFORM_ONLY_DESC)) { platformOnly.put(method, annotation); } } } for (Map.Entry entry : expectPlatform.entrySet()) { - expectPlatform(entry.getKey(), classNode, entry.getValue()); + MethodNode method = entry.getKey(); + AnnotationNode annotation = entry.getValue(); + expectPlatform(method, classNode, annotation); if (stripAnnotations) { - classNode.invisibleAnnotations.remove(entry.getValue()); + method.invisibleAnnotations.remove(annotation); } } for (Map.Entry entry : platformOnly.entrySet()) { - platformOnly(entry.getKey(), classNode, entry.getValue()); + MethodNode method = entry.getKey(); + AnnotationNode annotation = entry.getValue(); + platformOnly(method, classNode, annotation); if (stripAnnotations) { - classNode.invisibleAnnotations.remove(entry.getValue()); + method.invisibleAnnotations.remove(annotation); } } @@ -115,7 +126,6 @@ private void expectPlatform(MethodNode methodNode, ClassNode classNode, Annotati throw new RuntimeException("ExpectPlatform methods must be public and static"); } - String platformClass = null; List platforms = getAnnotationValue(annotationNode, "platforms"); if (platforms != null) { @@ -171,6 +181,10 @@ private void expectPlatform(MethodNode methodNode, ClassNode classNode, Annotati // recalculate proper maxStack and maxLocals manually so we don't have to recompute anything methodNode.maxStack = Math.max(type.getReturnType().getSize(), stackIndex); methodNode.maxLocals = stackIndex; + + if(!stripAnnotations) { + methodNode.invisibleAnnotations.add(new AnnotationNode(EXPECT_PLATFORM_TRANSFORMED_DESC)); + } } private void platformOnly(MethodNode methodNode, ClassNode classNode, AnnotationNode annotationNode) { @@ -190,9 +204,7 @@ private void getCurrentTarget(ClassNode classNode) { AbstractInsnNode insnNode = iterator.next(); if (insnNode.getOpcode() == Opcodes.INVOKESTATIC) { MethodInsnNode methodInsnNode = (MethodInsnNode) insnNode; - if (methodInsnNode.owner.equals("xyz/wagyourtail/unimined/expect/Target") && - methodInsnNode.name.equals("getCurrentTarget") - ) { + if (methodInsnNode.owner.equals(TARGET_CLASS) && methodInsnNode.name.equals(TARGET_METHOD)) { iterator.set(new LdcInsnNode(platformName)); } } From 30da2f7d0d2e5286cedc214ffc6f176359aa28de Mon Sep 17 00:00:00 2001 From: Rhys <98863820+rhysdh540@users.noreply.github.com> Date: Fri, 2 Aug 2024 10:09:13 -0400 Subject: [PATCH 3/5] fix up tests --- build.gradle.kts | 9 +++++---- expect-platform-test/build.gradle.kts | 10 +++++++--- .../unimined/expect/ExpectPlatformExtension.kt | 3 +++ .../unimined/expect/task/ExpectPlatformFiles.kt | 9 ++------- .../unimined/expect/task/ExpectPlatformJar.kt | 8 ++------ 5 files changed, 19 insertions(+), 20 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 8e5eb61..7df49d9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -70,10 +70,6 @@ dependencies { testImplementation(kotlin("test")) } -tasks.test { - useJUnitPlatform() -} - tasks.jar { from(shared.output) @@ -126,6 +122,11 @@ val agentShadeJar = tasks.register("agentShadowJar") { } } +tasks.test { + useJUnitPlatform() + dependsOn(tasks.jar, annotationJar, agentShadeJar) +} + tasks.assemble { dependsOn(annotationJar) dependsOn(agentShadeJar) diff --git a/expect-platform-test/build.gradle.kts b/expect-platform-test/build.gradle.kts index 0457b18..185ce01 100644 --- a/expect-platform-test/build.gradle.kts +++ b/expect-platform-test/build.gradle.kts @@ -4,6 +4,12 @@ import xyz.wagyourtail.unimined.expect.task.ExpectPlatformJar import xyz.wagyourtail.unimined.expect.ExpectPlatformExtension import java.util.* +val epVersion: String = run { + Properties().apply { + load(file("../gradle.properties").reader()) + }.getProperty("version", "1.1.0") +} + buildscript { repositories { mavenCentral() @@ -12,9 +18,8 @@ buildscript { } } dependencies { - if (!project.hasProperty("runningTest")) { - classpath("xyz.wagyourtail.unimined.expect-platform:expect-platform:1.0.3") + classpath("xyz.wagyourtail.unimined.expect-platform:expect-platform:$epVersion") classpath("org.ow2.asm:asm:9.7") classpath("org.ow2.asm:asm-commons:9.7") classpath("org.ow2.asm:asm-tree:9.7") @@ -35,7 +40,6 @@ plugins { apply(plugin = "xyz.wagyourtail.unimined.expect-platform") - sourceSets { create("a") { compileClasspath += sourceSets.main.get().output diff --git a/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt b/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt index a6f7327..430f243 100644 --- a/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt +++ b/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt @@ -11,6 +11,9 @@ import xyz.wagyourtail.unimined.expect.transform.ExpectPlatformParams import xyz.wagyourtail.unimined.expect.transform.ExpectPlatformTransform import xyz.wagyourtail.unimined.expect.utils.FinalizeOnRead +val Project.expectPlatform: ExpectPlatformExtension + get() = extensions.getByType(ExpectPlatformExtension::class.java) + abstract class ExpectPlatformExtension(val project: Project) { @set:VisibleForTesting diff --git a/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformFiles.kt b/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformFiles.kt index 9081a27..ac2e814 100644 --- a/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformFiles.kt +++ b/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformFiles.kt @@ -7,8 +7,8 @@ import org.gradle.api.tasks.Internal import org.gradle.api.tasks.TaskAction import xyz.wagyourtail.unimined.expect.utils.FinalizeOnRead import xyz.wagyourtail.unimined.expect.utils.MustSet -import xyz.wagyourtail.unimined.expect.ExpectPlatformExtension import xyz.wagyourtail.unimined.expect.TransformPlatform +import xyz.wagyourtail.unimined.expect.expectPlatform import xyz.wagyourtail.unimined.expect.transform.ExpectPlatformParams import xyz.wagyourtail.unimined.expect.utils.openZipFileSystem import java.io.File @@ -19,11 +19,6 @@ import kotlin.io.path.name abstract class ExpectPlatformFiles : ConventionTask(), ExpectPlatformParams { - @get:Internal - protected val ep by lazy { - project.extensions.getByType(ExpectPlatformExtension::class.java) - } - @get:InputFiles var inputCollection: FileCollection by FinalizeOnRead(MustSet()) @@ -70,7 +65,7 @@ abstract class ExpectPlatformFiles : ConventionTask(), ExpectPlatformParams { } } - val transformer = TransformPlatform(platformName.get(), remap.get(), stripAnnotations.getOrElse(false)) + val transformer = TransformPlatform(platformName.get(), remap.get(), stripAnnotations.getOrElse(project.expectPlatform.stripAnnotations)) for (i in toTransform.indices) { val input = toTransform[i] diff --git a/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformJar.kt b/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformJar.kt index ac0b14f..23edff8 100644 --- a/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformJar.kt +++ b/src/main/kotlin/xyz/wagyourtail/unimined/expect/task/ExpectPlatformJar.kt @@ -8,23 +8,19 @@ import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.bundling.Jar import xyz.wagyourtail.unimined.expect.utils.FinalizeOnRead import xyz.wagyourtail.unimined.expect.utils.MustSet -import xyz.wagyourtail.unimined.expect.ExpectPlatformExtension import xyz.wagyourtail.unimined.expect.TransformPlatform +import xyz.wagyourtail.unimined.expect.expectPlatform import xyz.wagyourtail.unimined.expect.transform.ExpectPlatformParams import xyz.wagyourtail.unimined.expect.utils.openZipFileSystem abstract class ExpectPlatformJar : Jar(), ExpectPlatformParams { - private val ep by lazy { - project.extensions.getByType(ExpectPlatformExtension::class.java) - } - @get:InputFiles var inputFiles: FileCollection by FinalizeOnRead(MustSet()) @TaskAction fun doTransform() { - val transformer = TransformPlatform(platformName.get(), remap.get(), stripAnnotations.getOrElse(false)) + val transformer = TransformPlatform(platformName.get(), remap.get(), stripAnnotations.getOrElse(project.expectPlatform.stripAnnotations)) for (input in inputFiles) { if (input.isDirectory) { val output = temporaryDir.resolve(input.name + "-expect-platform") From f847251ee3f8a9b4faa6c848dff5da216f29867d Mon Sep 17 00:00:00 2001 From: Rhys <98863820+rhysdh540@users.noreply.github.com> Date: Fri, 2 Aug 2024 11:05:28 -0400 Subject: [PATCH 4/5] extra qol stuff --- expect-platform-test/build.gradle.kts | 34 +++++++------------ .../wagyourtail/unimined/expect/Target.java | 2 +- .../expect/annotation/PlatformOnly.java | 1 + .../expect/ExpectPlatformExtension.kt | 15 ++++++-- 4 files changed, 27 insertions(+), 25 deletions(-) diff --git a/expect-platform-test/build.gradle.kts b/expect-platform-test/build.gradle.kts index 185ce01..b712023 100644 --- a/expect-platform-test/build.gradle.kts +++ b/expect-platform-test/build.gradle.kts @@ -1,14 +1,10 @@ @file:Suppress("DSL_SCOPE_VIOLATION") import xyz.wagyourtail.unimined.expect.task.ExpectPlatformFiles import xyz.wagyourtail.unimined.expect.task.ExpectPlatformJar -import xyz.wagyourtail.unimined.expect.ExpectPlatformExtension -import java.util.* +import xyz.wagyourtail.unimined.expect.expectPlatform +import java.util.Properties -val epVersion: String = run { - Properties().apply { - load(file("../gradle.properties").reader()) - }.getProperty("version", "1.1.0") -} +val epVersion: String = file("../gradle.properties").let { Properties().apply { load(it.reader()) } }.getProperty("version") buildscript { repositories { @@ -19,7 +15,7 @@ buildscript { } dependencies { if (!project.hasProperty("runningTest")) { - classpath("xyz.wagyourtail.unimined.expect-platform:expect-platform:$epVersion") + classpath("xyz.wagyourtail.unimined.expect-platform:expect-platform:1.1.0") classpath("org.ow2.asm:asm:9.7") classpath("org.ow2.asm:asm-commons:9.7") classpath("org.ow2.asm:asm-tree:9.7") @@ -30,9 +26,7 @@ buildscript { plugins { java if (project.hasProperty("runningTest")) { - plugins { - id("xyz.wagyourtail.unimined.expect-platform") - } + id("xyz.wagyourtail.unimined.expect-platform") } } @@ -58,13 +52,9 @@ repositories { } } -val expectPlatform = project.extensions.getByType(ExpectPlatformExtension::class) -expectPlatform.version = run { - projectDir.parentFile.resolve("gradle.properties").inputStream().use { - val props = Properties() - props.load(it) - props.getProperty("version") as String - } +project.expectPlatform { + version = epVersion + stripAnnotations = true } dependencies { @@ -85,6 +75,7 @@ val aExpectPlatform by tasks.registering(ExpectPlatformFiles::class) { val bExpectPlatform by tasks.registering(ExpectPlatformFiles::class) { platformName = "b" inputCollection = sourceSets.main.get().output + stripAnnotations = false remap = mapOf( "xyz/wagyourtail/unimined/expect/annotation/Environment" to "xyz/wagyourtail/ept/b/OnlyIn", @@ -104,21 +95,21 @@ val cExpectPlatform by tasks.registering(ExpectPlatformFiles::class) { ) } -tasks.register("runA", JavaExec::class) { +tasks.register("runA") { dependsOn(aExpectPlatform) classpath = sourceSets["a"].runtimeClasspath + aExpectPlatform.get().outputCollection mainClass = "xyz.wagyourtail.ept.Main" group = "ept" } -tasks.register("runB", JavaExec::class) { +tasks.register("runB") { dependsOn(bExpectPlatform) classpath = sourceSets["b"].runtimeClasspath + bExpectPlatform.get().outputCollection mainClass = "xyz.wagyourtail.ept.Main" group = "ept" } -tasks.register("runC", JavaExec::class) { +tasks.register("runC") { dependsOn(cExpectPlatform) classpath = sourceSets["c"].runtimeClasspath + cExpectPlatform.get().outputCollection mainClass = "xyz.wagyourtail.ept.Main" @@ -179,6 +170,7 @@ val jarB by tasks.registering(ExpectPlatformJar::class) { inputFiles = sourceSets.main.get().output from(sourceSets["b"].output) archiveFileName = "b.jar" + stripAnnotations = false remap = mapOf( "xyz/wagyourtail/unimined/expect/annotation/Environment" to "xyz/wagyourtail/ept/b/OnlyIn", diff --git a/src/annotations/java/xyz/wagyourtail/unimined/expect/Target.java b/src/annotations/java/xyz/wagyourtail/unimined/expect/Target.java index 6429886..bb7029e 100644 --- a/src/annotations/java/xyz/wagyourtail/unimined/expect/Target.java +++ b/src/annotations/java/xyz/wagyourtail/unimined/expect/Target.java @@ -8,6 +8,6 @@ private Target() { * @return the currently targeted platform */ public static String getCurrentTarget() { - throw new AssertionError("failed to transform method"); + throw new AssertionError("stub method, this shouldn't even be on your runtime classpath!"); } } diff --git a/src/annotations/java/xyz/wagyourtail/unimined/expect/annotation/PlatformOnly.java b/src/annotations/java/xyz/wagyourtail/unimined/expect/annotation/PlatformOnly.java index 70c887d..e7dd1f8 100644 --- a/src/annotations/java/xyz/wagyourtail/unimined/expect/annotation/PlatformOnly.java +++ b/src/annotations/java/xyz/wagyourtail/unimined/expect/annotation/PlatformOnly.java @@ -14,6 +14,7 @@ String[] value(); String FORGE = "forge"; + String NEOFORGE = "neoforge"; String FABRIC = "fabric"; String QUIILT = "quilt"; } diff --git a/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt b/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt index 430f243..1862814 100644 --- a/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt +++ b/src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt @@ -10,6 +10,7 @@ import org.jetbrains.annotations.VisibleForTesting import xyz.wagyourtail.unimined.expect.transform.ExpectPlatformParams import xyz.wagyourtail.unimined.expect.transform.ExpectPlatformTransform import xyz.wagyourtail.unimined.expect.utils.FinalizeOnRead +import java.io.File val Project.expectPlatform: ExpectPlatformExtension get() = extensions.getByType(ExpectPlatformExtension::class.java) @@ -22,7 +23,7 @@ abstract class ExpectPlatformExtension(val project: Project) { val annotationsDep by lazy { "xyz.wagyourtail.unimined.expect-platform:expect-platform-annotations:$version" } val agentDep by lazy { "xyz.wagyourtail.unimined.expect-platform:expect-platform-agent:$version" } - val stripAnnotations by FinalizeOnRead(false) + var stripAnnotations by FinalizeOnRead(false) @JvmOverloads fun platform(platformName: String, configuration: Configuration, action: ExpectPlatformParams.() -> Unit = {}) { @@ -74,12 +75,20 @@ abstract class ExpectPlatformExtension(val project: Project) { @JvmOverloads fun insertAgent(spec: JavaExecSpec, platformName: String, remap: Map = emptyMap()) { - spec.jvmArgs("-javaagent:${agentJar.absolutePath}", "-Dexpect.platform=${platformName}", "-Dexpect.remap=${TransformPlatform.mapToString(remap)}") + spec.jvmArgs( + "-javaagent:${agentJar.absolutePath}", + "-Dexpect.platform=${platformName}", + "-Dexpect.remap=${TransformPlatform.mapToString(remap)}" + ) } - val agentJar by lazy { + val agentJar: File by lazy { val config = project.configurations.detachedConfiguration(project.dependencies.create(agentDep)) config.resolve().first { it.extension == "jar" } } + operator fun invoke(action: ExpectPlatformExtension.() -> Unit) { + action(this) + } + } \ No newline at end of file From 001a0f4bc9ff9b9102a7b1388a55885eabb315af Mon Sep 17 00:00:00 2001 From: Rhys <98863820+rhysdh540@users.noreply.github.com> Date: Fri, 2 Aug 2024 23:17:31 -0400 Subject: [PATCH 5/5] illegal auto-version-getting in tests very silly --- expect-platform-test/build.gradle.kts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/expect-platform-test/build.gradle.kts b/expect-platform-test/build.gradle.kts index b712023..d6c44bd 100644 --- a/expect-platform-test/build.gradle.kts +++ b/expect-platform-test/build.gradle.kts @@ -2,9 +2,10 @@ import xyz.wagyourtail.unimined.expect.task.ExpectPlatformFiles import xyz.wagyourtail.unimined.expect.task.ExpectPlatformJar import xyz.wagyourtail.unimined.expect.expectPlatform -import java.util.Properties -val epVersion: String = file("../gradle.properties").let { Properties().apply { load(it.reader()) } }.getProperty("version") +val epVersion = projectDir.parentFile.resolve("gradle.properties").readText() + .split("\n").find { it.startsWith("version") }!! + .split("=").last().trimStart() buildscript { repositories { @@ -15,7 +16,10 @@ buildscript { } dependencies { if (!project.hasProperty("runningTest")) { - classpath("xyz.wagyourtail.unimined.expect-platform:expect-platform:1.1.0") + val epVersion = projectDir.parentFile.resolve("gradle.properties").readText() + .split("\n").find { it.startsWith("version") }!! + .split("=").last().trimStart() + classpath("xyz.wagyourtail.unimined.expect-platform:expect-platform:${epVersion}") classpath("org.ow2.asm:asm:9.7") classpath("org.ow2.asm:asm-commons:9.7") classpath("org.ow2.asm:asm-tree:9.7")