Skip to content

Commit

Permalink
Merge pull request #3 from rhysdh540/master
Browse files Browse the repository at this point in the history
`stripAnnotations` option, and extra fixes and stuff
  • Loading branch information
wagyourtail authored Aug 4, 2024
2 parents ca97e97 + 001a0f4 commit 701e711
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 69 deletions.
9 changes: 5 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ dependencies {
testImplementation(kotlin("test"))
}

tasks.test {
useJUnitPlatform()
}

tasks.jar {
from(shared.output)

Expand Down Expand Up @@ -126,6 +122,11 @@ val agentShadeJar = tasks.register<ShadowJar>("agentShadowJar") {
}
}

tasks.test {
useJUnitPlatform()
dependsOn(tasks.jar, annotationJar, agentShadeJar)
}

tasks.assemble {
dependsOn(annotationJar)
dependsOn(agentShadeJar)
Expand Down
36 changes: 18 additions & 18 deletions expect-platform-test/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
@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

val epVersion = projectDir.parentFile.resolve("gradle.properties").readText()
.split("\n").find { it.startsWith("version") }!!
.split("=").last().trimStart()

buildscript {
repositories {
Expand All @@ -12,9 +15,11 @@ buildscript {
}
}
dependencies {

if (!project.hasProperty("runningTest")) {
classpath("xyz.wagyourtail.unimined.expect-platform:expect-platform:1.0.3")
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")
Expand All @@ -25,17 +30,14 @@ buildscript {
plugins {
java
if (project.hasProperty("runningTest")) {
plugins {
id("xyz.wagyourtail.unimined.expect-platform")
}
id("xyz.wagyourtail.unimined.expect-platform")
}

}


apply(plugin = "xyz.wagyourtail.unimined.expect-platform")


sourceSets {
create("a") {
compileClasspath += sourceSets.main.get().output
Expand All @@ -54,13 +56,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 {
Expand All @@ -81,6 +79,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",
Expand All @@ -100,21 +99,21 @@ val cExpectPlatform by tasks.registering(ExpectPlatformFiles::class) {
)
}

tasks.register("runA", JavaExec::class) {
tasks.register<JavaExec>("runA") {
dependsOn(aExpectPlatform)
classpath = sourceSets["a"].runtimeClasspath + aExpectPlatform.get().outputCollection
mainClass = "xyz.wagyourtail.ept.Main"
group = "ept"
}

tasks.register("runB", JavaExec::class) {
tasks.register<JavaExec>("runB") {
dependsOn(bExpectPlatform)
classpath = sourceSets["b"].runtimeClasspath + bExpectPlatform.get().outputCollection
mainClass = "xyz.wagyourtail.ept.Main"
group = "ept"
}

tasks.register("runC", JavaExec::class) {
tasks.register<JavaExec>("runC") {
dependsOn(cExpectPlatform)
classpath = sourceSets["c"].runtimeClasspath + cExpectPlatform.get().outputCollection
mainClass = "xyz.wagyourtail.ept.Main"
Expand Down Expand Up @@ -175,6 +174,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",
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
kotlin.code.style=official

version = 1.0.5
version = 1.1.0

asmVersion=9.7
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
String[] value();

String FORGE = "forge";
String NEOFORGE = "neoforge";
String FABRIC = "fabric";
String QUIILT = "quilt";
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ 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
import java.io.File

val Project.expectPlatform: ExpectPlatformExtension
get() = extensions.getByType(ExpectPlatformExtension::class.java)

abstract class ExpectPlatformExtension(val project: Project) {

Expand All @@ -18,6 +23,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" }

var stripAnnotations by FinalizeOnRead(false)

@JvmOverloads
fun platform(platformName: String, configuration: Configuration, action: ExpectPlatformParams.() -> Unit = {}) {
val expectPlatformAttribute = Attribute.of("expectPlatform.${configuration.name}", Boolean::class.javaObjectType)
Expand All @@ -40,6 +47,7 @@ abstract class ExpectPlatformExtension(val project: Project) {

spec.parameters {
it.platformName.set(platformName)
it.stripAnnotations.convention(stripAnnotations)
it.action()
}
}
Expand Down Expand Up @@ -67,12 +75,20 @@ abstract class ExpectPlatformExtension(val project: Project) {

@JvmOverloads
fun insertAgent(spec: JavaExecSpec, platformName: String, remap: Map<String, String> = 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)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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())

Expand All @@ -46,13 +41,12 @@ abstract class ExpectPlatformFiles : ConventionTask(), ExpectPlatformParams {
}

@TaskAction
fun doTranform() {
fun doTransform() {
var toTransform = inputCollection.map { it.toPath() }.filter { it.exists() }

val fileSystems = mutableSetOf<FileSystem>()

try {

outputs.files.forEach { it.deleteRecursively() }

val transformed = toTransform.map { temporaryDir.resolve(it.name) }.map {
Expand All @@ -70,10 +64,13 @@ abstract class ExpectPlatformFiles : ConventionTask(), ExpectPlatformParams {
fs.getPath("/")
}
}

val transformer = TransformPlatform(platformName.get(), remap.get(), stripAnnotations.getOrElse(project.expectPlatform.stripAnnotations))

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() }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,37 @@ 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
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(project.expectPlatform.stripAnnotations))
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))
} else if (input.exists()) {
throw IllegalStateException("ExpectPlatformJar: $input is not a directory or jar file")

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,8 @@ interface ExpectPlatformParams : TransformParameters {
@get:Optional
val remap: MapProperty<String, String>

@get:Input
@get:Optional
val stripAnnotations: Property<Boolean>

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ abstract class ExpectPlatformTransform : TransformAction<ExpectPlatformParams> {
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 {
Expand Down
Loading

0 comments on commit 701e711

Please sign in to comment.