Skip to content

Commit

Permalink
Add option to configure datagen with the client. (FabricMC#1224)
Browse files Browse the repository at this point in the history
* Add option to configure datagen with the client.

* Revert nightly upgrade
  • Loading branch information
modmuss50 authored Nov 28, 2024
1 parent 38cff6d commit c4e2679
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -112,6 +113,7 @@ public void configureDataGeneration(Action<DataGenerationSettings> action) {
settings.getCreateSourceSet().convention(false);
settings.getStrictValidation().convention(false);
settings.getAddToResources().convention(true);
settings.getClient().convention(false);

action.execute(settings);

Expand Down Expand Up @@ -139,22 +141,25 @@ public void configureDataGeneration(Action<DataGenerationSettings> 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(() -> {
Expand All @@ -181,7 +186,7 @@ public void configureDataGeneration(Action<DataGenerationSettings> 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");
Expand Down Expand Up @@ -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<Boolean> getAddToResources();

/**
* Contains a boolean property indicating whether data generation will be compiled and ran with the client.
*/
Property<Boolean> getClient();
}

private String getDependencyNotation(String moduleName, String fabricApiVersion) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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")

Expand Down Expand Up @@ -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")
Expand All @@ -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
}
}

0 comments on commit c4e2679

Please sign in to comment.