Skip to content

Commit

Permalink
Merge pull request #2 from rhysdh540/master
Browse files Browse the repository at this point in the history
publish annotations sources, strengthen annotation parsing
  • Loading branch information
wagyourtail authored Jul 14, 2024
2 parents 13a1900 + 81121d5 commit 769e646
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ build/
### IntelliJ IDEA ###
.idea/
out/
*.iml
!**/src/main/**/out/
!**/src/test/**/out/

Expand Down
6 changes: 0 additions & 6 deletions ExpectPlatform.iml

This file was deleted.

4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pluginManagement {
and then add the plugin to your `build.gradle` file:
```gradle
plugins {
id 'xyz.wagyourtail.unimined.expect-platform' version '1.0.3'
id 'xyz.wagyourtail.unimined.expect-platform' version '1.0.4'
}
```

Expand Down Expand Up @@ -68,7 +68,7 @@ tasks.create("PlatformJar", xyz.wagyourtail.unimined.expect.ExpectPlatformJar) {
```

if you need the agent on a runner that isn't implementing JavaExecSpec (ie, the one in unimined)
if you need the agent on a runner that isn't implementing JavaExecSpec (i.e. the one in unimined 1.2)
check [the implementation](src/main/kotlin/xyz/wagyourtail/unimined/expect/ExpectPlatformExtension.kt#L69) for how it actually applies the agent to a JavaExec task.

### Environment
Expand Down
15 changes: 12 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
@file:Suppress("HasPlatformType")

import xyz.wagyourtail.gradle.shadow.ShadowJar
import java.net.URI

Expand All @@ -14,7 +16,7 @@ base {
archivesName.set("expect-platform")
}

val annotations by sourceSets.creating {}
val annotations by sourceSets.creating

val shared by sourceSets.creating {
compileClasspath += sourceSets.main.get().compileClasspath
Expand Down Expand Up @@ -97,6 +99,12 @@ val annotationJar = tasks.register<Jar>("annotationJar") {
}
}

val annotationSources = tasks.register<Jar>("annotationSources") {
archiveBaseName.set("expect-platform-annotations")
archiveClassifier.set("sources")
from(annotations.allSource)
}

val agentShadeJar = tasks.register<ShadowJar>("agentShadowJar") {
archiveBaseName.set("expect-platform-agent")

Expand Down Expand Up @@ -157,15 +165,16 @@ publishing {
artifactId = "expect-platform-agent"
version = project.version.toString()

artifact(agentShadeJar) {}
artifact(agentShadeJar)
}

create<MavenPublication>("annotations") {
groupId = "xyz.wagyourtail.unimined.expect-platform"
artifactId = "expect-platform-annotations"
version = project.version.toString()

artifact(annotationJar) {}
artifact(annotationJar)
artifact(annotationSources)
}
}
}
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.3
version = 1.0.4

asmVersion=9.7
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.stream.Stream;

public class TransformPlatform {
private static final int EXPECT_PLATFORM_ACCESS = Opcodes.ACC_PUBLIC | Opcodes.ACC_STATIC;

private final String platformName;
private final Map<String, String> remap = new HashMap<>();

Expand Down Expand Up @@ -95,14 +97,15 @@ public ClassNode transform(ClassNode classNode) {
}

private void expectPlatform(MethodNode methodNode, ClassNode classNode, AnnotationNode annotationNode) {
if (((methodNode.access & Opcodes.ACC_PUBLIC) == 0) || ((methodNode.access & Opcodes.ACC_STATIC) == 0)) {
if ((methodNode.access & EXPECT_PLATFORM_ACCESS) != EXPECT_PLATFORM_ACCESS) {
throw new RuntimeException("ExpectPlatform methods must be public and static");
}


String platformClass = null;
if (annotationNode.values != null) {
for (AnnotationNode platform : (List<AnnotationNode>) annotationNode.values.get(1)) {
List<AnnotationNode> platforms = getAnnotationValue(annotationNode, "platforms");
if (platforms != null) {
for (AnnotationNode platform : platforms) {
String name = null;
String target = null;
for (int i = 0; i < platform.values.size(); i += 2) {
Expand All @@ -114,8 +117,7 @@ private void expectPlatform(MethodNode methodNode, ClassNode classNode, Annotati
target = (String) value;
}
}
assert name != null;
if (name.equals(platformName)) {
if (platformName.equals(name) && target != null) {
platformClass = target;
break;
}
Expand Down Expand Up @@ -163,9 +165,9 @@ private void expectPlatform(MethodNode methodNode, ClassNode classNode, Annotati
}

private void platformOnly(MethodNode methodNode, ClassNode classNode, AnnotationNode annotationNode) {
List<String> platforms = (List<String>) annotationNode.values.get(1);
List<String> platforms = getAnnotationValue(annotationNode, "value");

if (!platforms.contains(platformName)) {
if (platforms != null && !platforms.contains(platformName)) {
classNode.methods.remove(methodNode);
}
}
Expand Down Expand Up @@ -208,4 +210,15 @@ public static String mapToString(Map<String, String> map) {
return sb.toString();
}

@SuppressWarnings("unchecked")
private <T> T getAnnotationValue(AnnotationNode annotation, String key) {
if(annotation.values == null) return null;
for (int i = 0; i < annotation.values.size(); i += 2) {
if (annotation.values.get(i).equals(key)) {
return (T) annotation.values.get(i + 1);
}
}
return null;
}

}

0 comments on commit 769e646

Please sign in to comment.