Skip to content

Commit

Permalink
Read data generation modid from FMJ by default. And code cleanup. (#1008
Browse files Browse the repository at this point in the history
)

* Read data generation modid from FMJ by default. And code cleanup.

Closes #999

* Fix #1000
  • Loading branch information
modmuss50 authored Dec 20, 2023
1 parent 51e1da7 commit ecc7e73
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 87 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package net.fabricmc.loom.configuration;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.Collections;
import java.util.HashMap;
Expand Down Expand Up @@ -55,6 +56,8 @@

import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.util.download.DownloadException;
import net.fabricmc.loom.util.fmj.FabricModJson;
import net.fabricmc.loom.util.fmj.FabricModJsonFactory;
import net.fabricmc.loom.util.gradle.SourceSetHelper;

public abstract class FabricApiExtension {
Expand Down Expand Up @@ -136,14 +139,10 @@ public void configureDataGeneration(Action<DataGenerationSettings> action) {
});

if (settings.getCreateSourceSet().get()) {
if (!settings.getModId().isPresent()) {
throw new IllegalStateException("DataGenerationSettings.getModId() must be set when using split sources.");
}

SourceSetContainer sourceSets = SourceSetHelper.getSourceSets(getProject());

// Create the new datagen sourceset, depend on the main sourceset.
sourceSets.create(DATAGEN_SOURCESET_NAME, sourceSet -> {
SourceSet dataGenSourceSet = sourceSets.create(DATAGEN_SOURCESET_NAME, sourceSet -> {
sourceSet.setCompileClasspath(
sourceSet.getCompileClasspath()
.plus(mainSourceSet.getOutput())
Expand All @@ -158,6 +157,20 @@ public void configureDataGeneration(Action<DataGenerationSettings> action) {
extendsFrom(getProject(), sourceSet.getRuntimeClasspathConfigurationName(), mainSourceSet.getRuntimeClasspathConfigurationName());
});

settings.getModId().convention(getProject().provider(() -> {
try {
final FabricModJson fabricModJson = FabricModJsonFactory.createFromSourceSetsNullable(dataGenSourceSet);

if (fabricModJson == null) {
throw new RuntimeException("Could not find a fabric.mod.json file in the data source set or a value for DataGenerationSettings.getModId()");
}

return fabricModJson.getId();
} catch (IOException e) {
throw new org.gradle.api.UncheckedIOException("Failed to read mod id from the datagen source set.", e);
}
}));

extension.getMods().create(settings.getModId().get(), mod -> {
// Create a classpath group for this mod. Assume that the main sourceset is already in a group.
mod.sourceSet(DATAGEN_SOURCESET_NAME);
Expand All @@ -168,7 +181,7 @@ public void configureDataGeneration(Action<DataGenerationSettings> action) {

if (settings.getCreateRunConfiguration().get()) {
extension.getRunConfigs().create("datagen", run -> {
run.name("Data Generation");
run.setConfigName("Data Generation");
run.inherit(extension.getRunConfigs().getByName("server"));

run.property("fabric-api.datagen");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
package net.fabricmc.loom.extension;

import java.io.File;
import java.io.IOException;
import java.util.Set;

import org.gradle.api.Action;
import org.gradle.api.NamedDomainObjectContainer;
import org.gradle.api.NamedDomainObjectList;
import org.gradle.api.Project;
import org.gradle.api.UncheckedIOException;
import org.gradle.api.artifacts.Dependency;
import org.gradle.api.file.ConfigurableFileCollection;
import org.gradle.api.file.RegularFileProperty;
Expand Down Expand Up @@ -65,6 +67,8 @@
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftSourceSets;
import net.fabricmc.loom.task.GenerateSourcesTask;
import net.fabricmc.loom.util.DeprecationHelper;
import net.fabricmc.loom.util.fmj.FabricModJson;
import net.fabricmc.loom.util.fmj.FabricModJsonFactory;
import net.fabricmc.loom.util.gradle.SourceSetHelper;

/**
Expand All @@ -88,8 +92,6 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
private final Property<Boolean> splitEnvironmentalSourceSet;
private final InterfaceInjectionExtensionAPI interfaceInjectionExtension;

private final ModVersionParser versionParser;

private final NamedDomainObjectContainer<RunConfigSettings> runConfigs;
private final NamedDomainObjectContainer<DecompilerOptions> decompilers;
private final NamedDomainObjectContainer<ModSettings> mods;
Expand Down Expand Up @@ -124,8 +126,6 @@ protected LoomGradleExtensionApiImpl(Project project, LoomFiles directories) {
this.intermediateMappingsProvider = project.getObjects().property(IntermediateMappingsProvider.class);
this.intermediateMappingsProvider.finalizeValueOnRead();

this.versionParser = new ModVersionParser(project);

this.deprecationHelper = new DeprecationHelper.ProjectBased(project);

this.runConfigs = project.container(RunConfigSettings.class,
Expand Down Expand Up @@ -252,7 +252,17 @@ public SetProperty<String> getKnownIndyBsms() {

@Override
public String getModVersion() {
return versionParser.getModVersion();
try {
final FabricModJson fabricModJson = FabricModJsonFactory.createFromSourceSetsNullable(SourceSetHelper.getMainSourceSet(getProject()));

if (fabricModJson == null) {
throw new RuntimeException("Could not find a fabric.mod.json file in the main sourceset");
}

return fabricModJson.getModVersion();
} catch (IOException e) {
throw new UncheckedIOException("Failed to read mod version from main sourceset.", e);
}
}

@Override
Expand Down
76 changes: 0 additions & 76 deletions src/main/java/net/fabricmc/loom/extension/ModVersionParser.java

This file was deleted.

4 changes: 4 additions & 0 deletions src/main/java/net/fabricmc/loom/util/fmj/FabricModJson.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public String getId() {
return readString(jsonObject, "id");
}

public String getModVersion() {
return readString(jsonObject, "version");
}

@Nullable
public abstract JsonElement getCustom(String key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class FabricModJsonV0Test extends Specification {
def fmj = FabricModJsonFactory.create(JSON_OBJECT, mockSource)
then:
fmj.version == 0
fmj.modVersion == "1.0.0"
}

def "id"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class FabricModJsonV1Test extends Specification {
def fmj = FabricModJsonFactory.create(JSON_OBJECT, mockSource)
then:
fmj.version == 1
fmj.modVersion == "1.0.0"
}

def "id"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ class FabricModJsonV2Test extends Specification {
def fmj = FabricModJsonFactory.create(JSON_OBJECT, mockSource)
then:
fmj.version == 2
fmj.modVersion == "1.0.0"
}

def "id"() {
Expand Down

0 comments on commit ecc7e73

Please sign in to comment.