mirror of
https://github.com/architectury/architectury-loom.git
synced 2026-04-01 21:17:46 -05:00
ModMetadataFiles: Only log a warning with broken mods.toml
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package dev.architectury.loom.metadata;
|
||||
|
||||
import net.fabricmc.loom.configuration.ifaceinject.InterfaceInjectionProcessor;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.VisibleForTesting;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* A fallback mod metadata file that represents a non-fatal format error
|
||||
* in another file type. An example of such file type is mods.toml,
|
||||
* which doesn't necessarily need to be parsed – it primarily needs to just exist.
|
||||
*/
|
||||
@VisibleForTesting
|
||||
public final class ErroringModMetadataFile implements ModMetadataFile {
|
||||
private final String fileName;
|
||||
|
||||
ErroringModMetadataFile(String fileName) {
|
||||
this.fileName = fileName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getIds() {
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getAccessWideners() {
|
||||
return Set.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<InterfaceInjectionProcessor.InjectedInterface> getInjectedInterfaces(@Nullable String modId) {
|
||||
return List.of();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileName() {
|
||||
return fileName + " [erroring]";
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getMixinConfigs() {
|
||||
return List.of();
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,11 @@ import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.google.common.collect.ImmutableMap;
|
||||
import org.gradle.api.logging.Logger;
|
||||
import org.gradle.api.logging.Logging;
|
||||
import org.gradle.api.tasks.SourceSet;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -18,12 +21,24 @@ import net.fabricmc.loom.util.gradle.SourceSetHelper;
|
||||
* Utilities for reading mod metadata files.
|
||||
*/
|
||||
public final class ModMetadataFiles {
|
||||
private static final Logger LOGGER = Logging.getLogger(ModMetadataFiles.class);
|
||||
private static final Map<String, Function<byte[], ModMetadataFile>> SINGLE_FILE_METADATA_TYPES = ImmutableMap.<String, Function<byte[], ModMetadataFile>>builder()
|
||||
.put(ArchitecturyCommonJson.FILE_NAME, ArchitecturyCommonJson::of)
|
||||
.put(QuiltModJson.FILE_NAME, QuiltModJson::of)
|
||||
.put(ModsToml.FILE_PATH, ModsToml::of)
|
||||
.put(ModsToml.FILE_PATH, onError(ModsToml::of, "Could not load mods.toml", () -> new ErroringModMetadataFile("mods.toml")))
|
||||
.build();
|
||||
|
||||
private static <A, B> Function<A, B> onError(Function<A, B> fn, String message, Supplier<B> onError) {
|
||||
return a -> {
|
||||
try {
|
||||
return fn.apply(a);
|
||||
} catch (Exception e) {
|
||||
LOGGER.warn(message, e);
|
||||
return onError.get();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the mod metadata file from a jar.
|
||||
*
|
||||
|
||||
@@ -25,12 +25,15 @@
|
||||
package net.fabricmc.loom.test.unit.architectury
|
||||
|
||||
import dev.architectury.loom.metadata.ArchitecturyCommonJson
|
||||
import dev.architectury.loom.metadata.ErroringModMetadataFile
|
||||
import dev.architectury.loom.metadata.ModMetadataFiles
|
||||
import dev.architectury.loom.metadata.QuiltModJson
|
||||
import net.fabricmc.loom.test.unit.forge.ModsTomlTest
|
||||
import net.fabricmc.loom.util.ZipUtils
|
||||
import spock.lang.Specification
|
||||
import spock.lang.TempDir
|
||||
|
||||
import java.nio.file.Files
|
||||
import java.nio.file.Path
|
||||
|
||||
class ModMetadataFilesTest extends Specification {
|
||||
@@ -100,4 +103,15 @@ class ModMetadataFilesTest extends Specification {
|
||||
then:
|
||||
modMetadata instanceof ArchitecturyCommonJson
|
||||
}
|
||||
|
||||
def "read broken mods.toml from directory"() {
|
||||
given:
|
||||
Files.createDirectories(workingDir.resolve('META-INF'))
|
||||
workingDir.resolve('META-INF/mods.toml').text = ModsTomlTest.BROKEN_INPUT
|
||||
when:
|
||||
def modMetadata = ModMetadataFiles.fromDirectory(workingDir)
|
||||
then:
|
||||
modMetadata instanceof ErroringModMetadataFile
|
||||
modMetadata.fileName == 'mods.toml [erroring]'
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,6 +40,11 @@ class ModsTomlTest extends Specification {
|
||||
|[[mods]]
|
||||
|modId="world"
|
||||
'''.stripMargin()
|
||||
public static final String BROKEN_INPUT =
|
||||
'''
|
||||
|[[mods.${MOD_ID}]]
|
||||
|modId = "hello_world"
|
||||
'''.stripMargin()
|
||||
|
||||
@TempDir
|
||||
Path tempDir
|
||||
@@ -81,13 +86,8 @@ class ModsTomlTest extends Specification {
|
||||
}
|
||||
|
||||
def "create from invalid string"() {
|
||||
given:
|
||||
def text = '''
|
||||
[[mods.${MOD_ID}]]
|
||||
modId = "hello_world"
|
||||
'''.stripIndent()
|
||||
when:
|
||||
ModsToml.of(text)
|
||||
ModsToml.of(BROKEN_INPUT)
|
||||
then:
|
||||
def e = thrown(IllegalArgumentException)
|
||||
e.cause instanceof ParsingException
|
||||
|
||||
Reference in New Issue
Block a user