From d2721d628f12f1daf4233d3483c5f893c9c53c91 Mon Sep 17 00:00:00 2001 From: Lulu13022002 <41980282+Lulu13022002@users.noreply.github.com> Date: Mon, 6 Jan 2025 13:23:47 +0100 Subject: [PATCH] split tasks --- paper-api/build.gradle.kts | 4 +- paper-generator/build.gradle.kts | 71 ++++++++++++++++--- .../main/java/io/papermc/generator/Main.java | 61 +++++++++------- .../java/io/papermc/generator/Rewriters.java | 18 ++++- .../registry/RegistryBootstrapper.java | 12 +++- .../generator/BlockStatePropertyTest.java | 2 + .../generator/MobGoalConverterTest.java | 2 + .../generator/RegistryMigrationTest.java | 2 + .../rewriter/OldGeneratedCodeTest.java | 4 +- .../world/entity/ai/goal/Goal.java.patch | 10 +-- 10 files changed, 138 insertions(+), 48 deletions(-) diff --git a/paper-api/build.gradle.kts b/paper-api/build.gradle.kts index 1446fa05cc..5a859f76b5 100644 --- a/paper-api/build.gradle.kts +++ b/paper-api/build.gradle.kts @@ -228,13 +228,13 @@ tasks.compileTestJava { options.compilerArgs.add("-parameters") } -val scanJar = tasks.register("scanJarForBadCalls", io.papermc.paperweight.tasks.ScanJarForBadCalls::class) { +val scanJarForBadCalls by tasks.registering(io.papermc.paperweight.tasks.ScanJarForBadCalls::class) { badAnnotations.add("Lio/papermc/paper/annotation/DoNotUse;") jarToScan.set(tasks.jar.flatMap { it.archiveFile }) classpath.from(configurations.compileClasspath) } tasks.check { - dependsOn(scanJar) + dependsOn(scanJarForBadCalls) } val scanJarForOldGeneratedCode by tasks.registering(io.papermc.paperweight.tasks.ScanJarForOldGeneratedCode::class) { diff --git a/paper-generator/build.gradle.kts b/paper-generator/build.gradle.kts index e6c514f5aa..9efc0412e9 100644 --- a/paper-generator/build.gradle.kts +++ b/paper-generator/build.gradle.kts @@ -1,3 +1,4 @@ +import io.papermc.paperweight.util.capitalized import io.papermc.paperweight.util.defaultJavaLauncher plugins { @@ -28,33 +29,87 @@ dependencies { testRuntimeOnly("org.junit.platform:junit-platform-launcher") } -tasks.registerGenerationTask("generate") { - description = "Generate and rewrite boilerplate content of API and its implementation" - dependsOn(tasks.check) - mainClass.set("io.papermc.generator.Main") +val rewriteApi = tasks.registerGenerationTask("rewriteApi", "paper-api") { + description = "Rewrite boilerplate content of API" + dependsOn("testRewriteApi") + mainClass.set("io.papermc.generator.Main\$Rewriter") classpath(sourceSets.main.map { it.runtimeClasspath }) systemProperty("typewriter.lexer.ignoreMarkdownDocComments", true) } -tasks.registerGenerationTask("scanOldGeneratedSourceCode") { +val rewriteImpl = tasks.registerGenerationTask("rewriteImpl", "paper-server") { + description = "Rewrite boilerplate content of API implementation" + dependsOn("testRewriteImpl") + mainClass.set("io.papermc.generator.Main\$Rewriter") + classpath(sourceSets.main.map { it.runtimeClasspath }) + systemProperty("typewriter.lexer.ignoreMarkdownDocComments", true) +} + +tasks.register("rewrite") { + group = "generation" + description = "Rewrite boilerplate content of API and its implementation" + dependsOn(rewriteApi, rewriteImpl) +} + + +val generateApi = tasks.registerGenerationTask("generateApi", "paper-api") { + description = "Generate boilerplate content of API" + dependsOn("testGenerateApi") + mainClass.set("io.papermc.generator.Main\$Generator") + classpath(sourceSets.main.map { it.runtimeClasspath }) +} + +val generateImpl = tasks.registerGenerationTask("generateImpl", "paper-server") { + description = "Generate boilerplate content of API implementation" + dependsOn("testGenerateImpl") + mainClass.set("io.papermc.generator.Main\$Generator") + classpath(sourceSets.main.map { it.runtimeClasspath }) +} + +tasks.register("generate") { + group = "generation" + description = "Generate boilerplate content of API and its implementation" + dependsOn(generateApi, generateImpl) +} + +tasks.register("scanOldGeneratedSourceCode") { + group = "verification" + javaLauncher = project.javaToolchains.defaultJavaLauncher(project) description = "Scan source code to detect outdated generated code" + args(project(":paper-api").projectDir.toString(), project(":paper-server").projectDir.toString()) mainClass.set("io.papermc.generator.rewriter.OldGeneratedCodeTest") classpath(sourceSets.test.map { it.runtimeClasspath }) } fun TaskContainer.registerGenerationTask( name: String, + vararg args: String, block: JavaExec.() -> Unit ): TaskProvider = register(name) { group = "generation" javaLauncher = project.javaToolchains.defaultJavaLauncher(project) - args(project(":paper-api").projectDir.toString(), project(":paper-server").projectDir.toString()) + if (args.isNotEmpty()) { + val projectDirs = args.map { project.rootProject.findProject(it)?.projectDir } + args(projectDirs.map { it.toString() }) + inputs.files(projectDirs) + } block(this) } -tasks.test { - useJUnitPlatform() +sequenceOf("api", "impl").forEach { side -> + sequenceOf("generate", "rewrite").forEach { type -> + val task = tasks.register("test${type.capitalized()}${side.capitalized()}") { + group = "verification" + javaLauncher = project.javaToolchains.defaultJavaLauncher(project) + useJUnitPlatform { + includeTags.add("${type}-${side}") + } + } + tasks.check { + dependsOn(task) + } + } } group = "io.papermc.paper" diff --git a/paper-generator/src/main/java/io/papermc/generator/Main.java b/paper-generator/src/main/java/io/papermc/generator/Main.java index f1687c4edf..58f5e2486c 100644 --- a/paper-generator/src/main/java/io/papermc/generator/Main.java +++ b/paper-generator/src/main/java/io/papermc/generator/Main.java @@ -35,7 +35,7 @@ import org.jspecify.annotations.NullMarked; import org.slf4j.Logger; @NullMarked -public final class Main { +public class Main { private static final Logger LOGGER = LogUtils.getLogger(); public static final RegistryAccess.Frozen REGISTRY_ACCESS; @@ -72,37 +72,46 @@ public final class Main { private Main() { } - public static void main(String[] args) { - PatternSourceSetRewriter apiSourceSet = new PaperPatternSourceSetRewriter(); - PatternSourceSetRewriter serverSourceSet = new PaperPatternSourceSetRewriter(); - Rewriters.bootstrap(apiSourceSet, serverSourceSet); + public static class Rewriter extends Main { - Path api = Path.of(args[0]); - Path server = Path.of(args[1]); - try { - LOGGER.info("Running API generators..."); - generate(api, Generators.API); - apiSourceSet.apply(api.resolve("src/main/java")); - - LOGGER.info("Running Server generators..."); - generate(server, Generators.SERVER); - serverSourceSet.apply(server.resolve("src/main/java")); - } catch (RuntimeException ex) { - throw ex; - } catch (Exception e) { - throw new RuntimeException(e); + public static void main(String[] args) { + boolean isApi = args[0].endsWith("-api"); + PatternSourceSetRewriter sourceSet = new PaperPatternSourceSetRewriter(); + (isApi ? Rewriters.API : Rewriters.SERVER).accept(sourceSet); + try { + sourceSet.apply(Path.of(args[0], "src/main/java")); + } catch (RuntimeException ex) { + throw ex; + } catch (Exception e) { + throw new RuntimeException(e); + } } } - private static void generate(Path sourceSet, Collection generators) throws IOException { - Path output = sourceSet.resolve("src/main/generated"); - if (Files.exists(output)) { - PathUtils.deleteDirectory(output); + public static class Generator extends Main { + + public static void main(String[] args) { + boolean isApi = args[0].endsWith("-api"); + + try { + generate(Path.of(args[0]), isApi ? Generators.API : Generators.SERVER); + } catch (RuntimeException ex) { + throw ex; + } catch (Exception e) { + throw new RuntimeException(e); + } } - for (SourceGenerator generator : generators) { - generator.writeToFile(output); + private static void generate(Path sourceSet, Collection generators) throws IOException { + Path output = sourceSet.resolve("src/main/generated"); + if (Files.exists(output)) { + PathUtils.deleteDirectory(output); + } + + for (SourceGenerator generator : generators) { + generator.writeToFile(output); + } + LOGGER.info("Files written to {}", output.toAbsolutePath()); } - LOGGER.info("Files written to {}", output.toAbsolutePath()); } } diff --git a/paper-generator/src/main/java/io/papermc/generator/Rewriters.java b/paper-generator/src/main/java/io/papermc/generator/Rewriters.java index b3c734bb22..f0588cb659 100644 --- a/paper-generator/src/main/java/io/papermc/generator/Rewriters.java +++ b/paper-generator/src/main/java/io/papermc/generator/Rewriters.java @@ -28,6 +28,7 @@ import io.papermc.paper.datacomponent.item.consumable.ItemUseAnimation; import io.papermc.typewriter.preset.EnumCloneRewriter; import io.papermc.typewriter.preset.model.EnumValue; import java.util.Map; +import java.util.function.Consumer; import javax.lang.model.SourceVersion; import net.kyori.adventure.text.format.NamedTextColor; import net.minecraft.core.Holder; @@ -84,7 +85,15 @@ import static io.papermc.generator.utils.Formatting.quoted; public final class Rewriters { public static void bootstrap(PatternSourceSetRewriter apiSourceSet, PatternSourceSetRewriter serverSourceSet) { - apiSourceSet + bootstrapApi(apiSourceSet); + bootstrapServer(serverSourceSet); + } + + public static final Consumer API = Rewriters::bootstrapApi; + public static final Consumer SERVER = Rewriters::bootstrapServer; + + private static void bootstrapApi(PatternSourceSetRewriter sourceSet) { + sourceSet .register("PotionType", PotionType.class, new EnumRegistryRewriter<>(Registries.POTION)) .register("EntityType", EntityType.class, new EntityTypeRewriter()) .register("DisplaySlot", DisplaySlot.class, new EnumCloneRewriter<>(net.minecraft.world.scores.DisplaySlot.class) { @@ -185,8 +194,11 @@ public final class Rewriters { .register("FeatureFlag", FeatureFlag.class, new FeatureFlagRewriter()) .register("Tag", Tag.class, new TagRewriter()) .register("MapPalette#colors", MapPalette.class, new MapPaletteRewriter()); + RegistryBootstrapper.bootstrapApi(sourceSet); + } - serverSourceSet + private static void bootstrapServer(PatternSourceSetRewriter sourceSet) { + sourceSet .register("CraftBlockData#MAP", Types.CRAFT_BLOCK_DATA, new CraftBlockDataMapping()) .register("CraftBlockEntityStates", Types.CRAFT_BLOCK_STATES, new CraftBlockEntityStateMapping()) .register(Types.CRAFT_STATISTIC, composite( @@ -198,6 +210,6 @@ public final class Rewriters { holder("CraftPotionUtil#extendable", new CraftPotionUtilRewriter("long")) )) .register("PaperFeatureFlagProviderImpl#FLAGS", Types.PAPER_FEATURE_FLAG_PROVIDER_IMPL, new PaperFeatureFlagMapping()); - RegistryBootstrapper.bootstrap(apiSourceSet, serverSourceSet); + RegistryBootstrapper.bootstrapServer(sourceSet); } } diff --git a/paper-generator/src/main/java/io/papermc/generator/registry/RegistryBootstrapper.java b/paper-generator/src/main/java/io/papermc/generator/registry/RegistryBootstrapper.java index 7e0d7ff7d6..c84fcee518 100644 --- a/paper-generator/src/main/java/io/papermc/generator/registry/RegistryBootstrapper.java +++ b/paper-generator/src/main/java/io/papermc/generator/registry/RegistryBootstrapper.java @@ -31,7 +31,15 @@ public class RegistryBootstrapper { } public static void bootstrap(PatternSourceSetRewriter apiSourceSet, PatternSourceSetRewriter serverSourceSet) { - apiSourceSet.register("RegistryEvents", RegistryEvents.class, new RegistryEventsRewriter()); - serverSourceSet.register("RegistryDefinitions", Types.PAPER_REGISTRIES, new PaperRegistriesRewriter()); + bootstrapApi(apiSourceSet); + bootstrapServer(serverSourceSet); + } + + public static void bootstrapApi(PatternSourceSetRewriter sourceSet) { + sourceSet.register("RegistryEvents", RegistryEvents.class, new RegistryEventsRewriter()); + } + + public static void bootstrapServer(PatternSourceSetRewriter sourceSet) { + sourceSet.register("RegistryDefinitions", Types.PAPER_REGISTRIES, new PaperRegistriesRewriter()); } } diff --git a/paper-generator/src/test/java/io/papermc/generator/BlockStatePropertyTest.java b/paper-generator/src/test/java/io/papermc/generator/BlockStatePropertyTest.java index d0c6c2cfa2..a139306fd7 100644 --- a/paper-generator/src/test/java/io/papermc/generator/BlockStatePropertyTest.java +++ b/paper-generator/src/test/java/io/papermc/generator/BlockStatePropertyTest.java @@ -20,8 +20,10 @@ import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.EnumProperty; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; +@Tag("generate-impl") public class BlockStatePropertyTest { private static Set>> ENUM_PROPERTY_VALUES; diff --git a/paper-generator/src/test/java/io/papermc/generator/MobGoalConverterTest.java b/paper-generator/src/test/java/io/papermc/generator/MobGoalConverterTest.java index 56052d6b58..981f958d28 100644 --- a/paper-generator/src/test/java/io/papermc/generator/MobGoalConverterTest.java +++ b/paper-generator/src/test/java/io/papermc/generator/MobGoalConverterTest.java @@ -7,11 +7,13 @@ import java.util.ArrayList; import java.util.List; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Mob; +import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +@Tag("generate-api") public class MobGoalConverterTest { @Test diff --git a/paper-generator/src/test/java/io/papermc/generator/RegistryMigrationTest.java b/paper-generator/src/test/java/io/papermc/generator/RegistryMigrationTest.java index 61a2648438..c9537b1e16 100644 --- a/paper-generator/src/test/java/io/papermc/generator/RegistryMigrationTest.java +++ b/paper-generator/src/test/java/io/papermc/generator/RegistryMigrationTest.java @@ -11,8 +11,10 @@ import net.minecraft.resources.ResourceKey; import net.minecraft.server.Bootstrap; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Test; +@Tag("rewrite-impl") public class RegistryMigrationTest { @BeforeAll diff --git a/paper-generator/src/test/java/io/papermc/generator/rewriter/OldGeneratedCodeTest.java b/paper-generator/src/test/java/io/papermc/generator/rewriter/OldGeneratedCodeTest.java index 257b64ddb6..4652db0ecc 100644 --- a/paper-generator/src/test/java/io/papermc/generator/rewriter/OldGeneratedCodeTest.java +++ b/paper-generator/src/test/java/io/papermc/generator/rewriter/OldGeneratedCodeTest.java @@ -40,8 +40,8 @@ public class OldGeneratedCodeTest { Rewriters.bootstrap(apiSourceSet, serverSourceSet); - checkOutdated(apiSourceSet, Path.of(args[0])); - checkOutdated(serverSourceSet, Path.of(args[1])); + checkOutdated(apiSourceSet, Path.of(args[0], "src/main/java")); + checkOutdated(serverSourceSet, Path.of(args[1], "src/main/java")); } private static void checkOutdated(PaperPatternSourceSetRewriter sourceSetRewriter, Path sourceSet) throws IOException { diff --git a/paper-server/patches/sources/net/minecraft/world/entity/ai/goal/Goal.java.patch b/paper-server/patches/sources/net/minecraft/world/entity/ai/goal/Goal.java.patch index 8760a911ad..e64289b685 100644 --- a/paper-server/patches/sources/net/minecraft/world/entity/ai/goal/Goal.java.patch +++ b/paper-server/patches/sources/net/minecraft/world/entity/ai/goal/Goal.java.patch @@ -23,13 +23,13 @@ } + // Paper start - Mob goal api -+ private com.destroystokyo.paper.entity.ai.PaperVanillaGoal vanillaGoal; -+ public com.destroystokyo.paper.entity.ai.Goal asPaperVanillaGoal() { -+ if (this.vanillaGoal == null) { -+ this.vanillaGoal = new com.destroystokyo.paper.entity.ai.PaperVanillaGoal<>(this); ++ private com.destroystokyo.paper.entity.ai.PaperGoal paperGoal; ++ public com.destroystokyo.paper.entity.ai.Goal asPaperGoal() { ++ if (this.paperGoal == null) { ++ this.paperGoal = new com.destroystokyo.paper.entity.ai.PaperGoal<>(this); + } + //noinspection unchecked -+ return (com.destroystokyo.paper.entity.ai.Goal) this.vanillaGoal; ++ return (com.destroystokyo.paper.entity.ai.Goal) this.paperGoal; + } + // Paper end - Mob goal api +