From c4e2679e242b03130d68db63dd66b0a90fe075ca Mon Sep 17 00:00:00 2001 From: modmuss Date: Thu, 28 Nov 2024 20:18:25 +0000 Subject: [PATCH] Add option to configure datagen with the client. (#1224) * Add option to configure datagen with the client. * Revert nightly upgrade --- .../configuration/FabricApiExtension.java | 22 ++- .../integration/DataGenerationTest.groovy | 137 +++++++++++++++--- 2 files changed, 135 insertions(+), 24 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/configuration/FabricApiExtension.java b/src/main/java/net/fabricmc/loom/configuration/FabricApiExtension.java index fa97e7ea6..a1eefff4d 100644 --- a/src/main/java/net/fabricmc/loom/configuration/FabricApiExtension.java +++ b/src/main/java/net/fabricmc/loom/configuration/FabricApiExtension.java @@ -55,6 +55,7 @@ import org.w3c.dom.NodeList; import net.fabricmc.loom.LoomGradleExtension; +import net.fabricmc.loom.configuration.providers.minecraft.MinecraftSourceSets; import net.fabricmc.loom.util.download.DownloadException; import net.fabricmc.loom.util.fmj.FabricModJson; import net.fabricmc.loom.util.fmj.FabricModJsonFactory; @@ -112,6 +113,7 @@ public void configureDataGeneration(Action action) { settings.getCreateSourceSet().convention(false); settings.getStrictValidation().convention(false); settings.getAddToResources().convention(true); + settings.getClient().convention(false); action.execute(settings); @@ -139,22 +141,25 @@ public void configureDataGeneration(Action action) { }); if (settings.getCreateSourceSet().get()) { + final boolean isClientAndSplit = extension.areEnvironmentSourceSetsSplit() && settings.getClient().get(); + final SourceSet targetSourceSet = isClientAndSplit ? SourceSetHelper.getSourceSetByName(MinecraftSourceSets.Split.CLIENT_ONLY_SOURCE_SET_NAME, getProject()) : mainSourceSet; + SourceSetContainer sourceSets = SourceSetHelper.getSourceSets(getProject()); - // Create the new datagen sourceset, depend on the main sourceset. + // Create the new datagen sourceset, depend on the main or client sourceset. SourceSet dataGenSourceSet = sourceSets.create(DATAGEN_SOURCESET_NAME, sourceSet -> { sourceSet.setCompileClasspath( sourceSet.getCompileClasspath() - .plus(mainSourceSet.getOutput()) + .plus(targetSourceSet.getOutput()) ); sourceSet.setRuntimeClasspath( sourceSet.getRuntimeClasspath() - .plus(mainSourceSet.getOutput()) + .plus(targetSourceSet.getOutput()) ); - extendsFrom(getProject(), sourceSet.getCompileClasspathConfigurationName(), mainSourceSet.getCompileClasspathConfigurationName()); - extendsFrom(getProject(), sourceSet.getRuntimeClasspathConfigurationName(), mainSourceSet.getRuntimeClasspathConfigurationName()); + extendsFrom(getProject(), sourceSet.getCompileClasspathConfigurationName(), targetSourceSet.getCompileClasspathConfigurationName()); + extendsFrom(getProject(), sourceSet.getRuntimeClasspathConfigurationName(), targetSourceSet.getRuntimeClasspathConfigurationName()); }); settings.getModId().convention(getProject().provider(() -> { @@ -181,7 +186,7 @@ public void configureDataGeneration(Action action) { if (settings.getCreateRunConfiguration().get()) { extension.getRunConfigs().create("datagen", run -> { - run.inherit(extension.getRunConfigs().getByName("server")); + run.inherit(extension.getRunConfigs().getByName(settings.getClient().get() ? "client" : "server")); run.setConfigName("Data Generation"); run.property("fabric-api.datagen"); @@ -235,6 +240,11 @@ public interface DataGenerationSettings { * Contains a boolean property indicating whether the generated resources will be automatically added to the main sourceset. */ Property getAddToResources(); + + /** + * Contains a boolean property indicating whether data generation will be compiled and ran with the client. + */ + Property getClient(); } private String getDependencyNotation(String moduleName, String fabricApiVersion) { diff --git a/src/test/groovy/net/fabricmc/loom/test/integration/DataGenerationTest.groovy b/src/test/groovy/net/fabricmc/loom/test/integration/DataGenerationTest.groovy index 66919c0ff..94e9d293a 100644 --- a/src/test/groovy/net/fabricmc/loom/test/integration/DataGenerationTest.groovy +++ b/src/test/groovy/net/fabricmc/loom/test/integration/DataGenerationTest.groovy @@ -29,10 +29,20 @@ import spock.lang.Unroll import net.fabricmc.loom.test.util.GradleProjectTestTrait +import static net.fabricmc.loom.test.LoomTestConstants.PRE_RELEASE_GRADLE import static net.fabricmc.loom.test.LoomTestConstants.STANDARD_TEST_VERSIONS import static org.gradle.testkit.runner.TaskOutcome.SUCCESS class DataGenerationTest extends Specification implements GradleProjectTestTrait { + private static String DEPENDENCIES = """ + dependencies { + minecraft "com.mojang:minecraft:1.20.2" + mappings "net.fabricmc:yarn:1.20.2+build.4:v2" + modImplementation "net.fabricmc:fabric-loader:0.14.23" + modImplementation "net.fabricmc.fabric-api:fabric-api:0.90.0+1.20.2" + } + """ + @Unroll def "dataGeneration (gradle #version)"() { setup: @@ -41,14 +51,7 @@ class DataGenerationTest extends Specification implements GradleProjectTestTrait fabricApi { configureDataGeneration() } - - dependencies { - minecraft "com.mojang:minecraft:1.20.2" - mappings "net.fabricmc:yarn:1.20.2+build.4:v2" - modImplementation "net.fabricmc:fabric-loader:0.14.23" - modImplementation "net.fabricmc.fabric-api:fabric-api:0.90.0+1.20.2" - } - ''' + ''' + DEPENDENCIES when: def result = gradle.run(task: "runDatagen") @@ -80,17 +83,8 @@ class DataGenerationTest extends Specification implements GradleProjectTestTrait } } - dependencies { - minecraft "com.mojang:minecraft:1.20.2" - mappings "net.fabricmc:yarn:1.20.2+build.4:v2" - modImplementation "net.fabricmc:fabric-loader:0.14.23" - modImplementation "net.fabricmc.fabric-api:fabric-api:0.90.0+1.20.2" - - modDatagenImplementation fabricApi.module("fabric-data-generation-api-v1", "0.90.0+1.20.2") - } - println("%%" + loom.runs.datagen.configName + "%%") - ''' + ''' + DEPENDENCIES when: def result = gradle.run(task: "runDatagen") @@ -101,4 +95,111 @@ class DataGenerationTest extends Specification implements GradleProjectTestTrait where: version << STANDARD_TEST_VERSIONS } + + @Unroll + def "client dataGeneration (gradle #version)"() { + setup: + def gradle = gradleProject(project: "minimalBase", version: PRE_RELEASE_GRADLE) + gradle.buildGradle << ''' + fabricApi { + configureDataGeneration { + client = true + } + } + ''' + DEPENDENCIES + when: + def result = gradle.run(task: "runDatagen") + + then: + result.task(":runDatagen").outcome == SUCCESS + } + + @Unroll + def "client dataGeneration sourceset (gradle #version)"() { + setup: + def gradle = gradleProject(project: "minimalBase", version: PRE_RELEASE_GRADLE) + gradle.buildGradle << ''' + // Must configure the main mod + loom.mods { + "example" { + sourceSet sourceSets.main + } + } + + fabricApi { + configureDataGeneration { + createSourceSet = true + createRunConfiguration = true + modId = "example-datagen" + strictValidation = true + client = true + } + } + ''' + DEPENDENCIES + when: + def result = gradle.run(task: "runDatagen") + + then: + result.task(":runDatagen").outcome == SUCCESS + } + + @Unroll + def "split client dataGeneration (gradle #version)"() { + setup: + def gradle = gradleProject(project: "minimalBase", version: PRE_RELEASE_GRADLE) + gradle.buildGradle << ''' + loom { + splitEnvironmentSourceSets() + mods { + "example" { + sourceSet sourceSets.main + sourceSet sourceSets.client + } + } + } + + fabricApi { + configureDataGeneration { + client = true + } + } + ''' + DEPENDENCIES + when: + def result = gradle.run(task: "runDatagen") + + then: + result.task(":runDatagen").outcome == SUCCESS + } + + @Unroll + def "split client dataGeneration sourceset (gradle #version)"() { + setup: + def gradle = gradleProject(project: "minimalBase", version: PRE_RELEASE_GRADLE) + gradle.buildGradle << ''' + loom { + splitEnvironmentSourceSets() + mods { + "example" { + sourceSet sourceSets.main + sourceSet sourceSets.client + } + } + } + + fabricApi { + configureDataGeneration { + createSourceSet = true + createRunConfiguration = true + modId = "example-datagen" + strictValidation = true + client = true + } + } + ''' + DEPENDENCIES + when: + def result = gradle.run(task: "runDatagen") + + then: + result.task(":runDatagen").outcome == SUCCESS + } }