Read data generation modid from FMJ by default. And code cleanup. (#1008)

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

Closes #999

* Fix #1000
This commit is contained in:
modmuss
2023-12-20 15:52:13 +00:00
committed by GitHub
parent 51e1da7330
commit ecc7e730e9
7 changed files with 41 additions and 87 deletions

View File

@@ -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;
@@ -55,6 +56,8 @@ import org.w3c.dom.NodeList;
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 {
@@ -136,14 +139,10 @@ public abstract class FabricApiExtension {
});
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())
@@ -158,6 +157,20 @@ public abstract class FabricApiExtension {
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);
@@ -168,7 +181,7 @@ public abstract class FabricApiExtension {
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");

View File

@@ -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;
@@ -65,6 +67,8 @@ import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJarConfigura
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;
/**
@@ -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;
@@ -124,8 +126,6 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
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,
@@ -252,7 +252,17 @@ public abstract class LoomGradleExtensionApiImpl implements LoomGradleExtensionA
@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

View File

@@ -1,76 +0,0 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2021 FabricMC
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.fabricmc.loom.extension;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import com.google.gson.JsonObject;
import org.gradle.api.Project;
import org.gradle.api.plugins.JavaPluginExtension;
import net.fabricmc.loom.LoomGradlePlugin;
public class ModVersionParser {
private final Project project;
private String version = null;
public ModVersionParser(Project project) {
this.project = project;
}
public String getModVersion() {
if (version != null) {
return version;
}
File json = locateModJsonFile();
JsonObject jsonObject;
try (var reader = new FileReader(json)) {
jsonObject = LoomGradlePlugin.GSON.fromJson(reader, JsonObject.class);
} catch (IOException e) {
throw new RuntimeException("Failed to read fabric.mod.json file");
}
if (!jsonObject.has("version") || !jsonObject.get("version").isJsonPrimitive()) {
throw new UnsupportedOperationException("Could not find valid version in the fabric.mod.json file");
}
version = jsonObject.get("version").getAsString();
return version;
}
private File locateModJsonFile() {
return project.getExtensions().getByType(JavaPluginExtension.class).getSourceSets()
.getByName("main")
.getResources()
.matching(patternFilterable -> patternFilterable.include("fabric.mod.json"))
.getSingleFile();
}
}

View File

@@ -50,6 +50,10 @@ public abstract sealed class FabricModJson permits FabricModJsonV0, FabricModJso
return readString(jsonObject, "id");
}
public String getModVersion() {
return readString(jsonObject, "version");
}
@Nullable
public abstract JsonElement getCustom(String key);