Add loom.forge.useForgeLoggerConfig

Closes #126.
This commit is contained in:
Juuz
2023-04-23 13:35:35 +03:00
parent 9784c5491e
commit b527ed9afe
8 changed files with 182 additions and 2 deletions

View File

@@ -0,0 +1,91 @@
package dev.architectury.loom.util;
import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.StringJoiner;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import org.gradle.api.Project;
import org.jetbrains.annotations.Nullable;
import net.fabricmc.loom.LoomGradleExtension;
import net.fabricmc.loom.util.FileSystemUtil;
public final class ForgeLoggerConfig {
private static final List<ArtifactCoordinates> LOGGER_CONFIG_ARTIFACTS = List.of(
// 1.17 -
new ArtifactCoordinates("net.minecraftforge", "fmlloader", null),
// 1.14 - 1.16
new ArtifactCoordinates("net.minecraftforge", "forge", "launcher")
);
public static void copyToPath(Project project, Path outputFile) {
try {
Files.deleteIfExists(outputFile);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
final JsonArray libraries = LoomGradleExtension.get(project)
.getForgeUserdevProvider()
.getJson()
.getAsJsonArray("libraries");
boolean found = false;
for (JsonElement library : libraries) {
final String notation = library.getAsString();
if (LOGGER_CONFIG_ARTIFACTS.stream().anyMatch(artifact -> artifact.matches(notation))) {
final File libraryFile = project.getConfigurations()
.detachedConfiguration(project.getDependencies().create(notation))
.setTransitive(false)
.getSingleFile();
try (FileSystemUtil.Delegate fs = FileSystemUtil.getJarFileSystem(libraryFile, false)) {
final Path configPath = fs.getPath("log4j2.xml");
Files.copy(configPath, outputFile);
found = true;
break;
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
if (!found) {
StringBuilder sb = new StringBuilder("Could not find Forge dependency with logger config. Tried to find:");
for (ArtifactCoordinates artifact : LOGGER_CONFIG_ARTIFACTS) {
sb.append('\n').append(" - ").append(artifact);
}
throw new RuntimeException(sb.toString());
}
}
private record ArtifactCoordinates(String group, String name, @Nullable String classifier) {
boolean matches(String notation) {
final String[] parts = notation.split(":");
return group.equals(parts[0]) && name.equals(parts[1]) &&
(classifier == null || (parts.length >= 4 && classifier.equals(parts[3])));
}
@Override
public String toString() {
final StringJoiner joiner = new StringJoiner(":");
joiner.add(group);
joiner.add(name);
if (classifier != null) {
joiner.add(classifier);
}
return joiner.toString();
}
}
}

View File

@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2021 FabricMC
* Copyright (c) 2021-2023 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
@@ -104,6 +104,14 @@ public interface ForgeExtensionAPI {
*/
Property<Boolean> getUseCustomMixin();
/**
* If true, Loom will use Forge's Log4J config file instead of its own.
* This is disabled by default.
*
* @return the property
*/
Property<Boolean> getUseForgeLoggerConfig();
/**
* A list of mod IDs for mods applied for data generation.
* The returned list is unmodifiable but not immutable - it will reflect changes done with

View File

@@ -1,7 +1,7 @@
/*
* This file is part of fabric-loom, licensed under the MIT License (MIT).
*
* Copyright (c) 2021 FabricMC
* Copyright (c) 2021-2023 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
@@ -48,6 +48,7 @@ public class ForgeExtensionImpl implements ForgeExtensionAPI {
private final ConfigurableFileCollection accessTransformers;
private final SetProperty<String> mixinConfigs;
private final Property<Boolean> useCustomMixin;
private final Property<Boolean> useForgeLoggerConfig;
private final List<String> dataGenMods = new ArrayList<>(); // not a property because it has custom adding logic
@Inject
@@ -58,6 +59,7 @@ public class ForgeExtensionImpl implements ForgeExtensionAPI {
accessTransformers = project.getObjects().fileCollection();
mixinConfigs = project.getObjects().setProperty(String.class).empty();
useCustomMixin = project.getObjects().property(Boolean.class).convention(true);
useForgeLoggerConfig = project.getObjects().property(Boolean.class).convention(false);
}
@Override
@@ -95,6 +97,11 @@ public class ForgeExtensionImpl implements ForgeExtensionAPI {
return useCustomMixin;
}
@Override
public Property<Boolean> getUseForgeLoggerConfig() {
return useForgeLoggerConfig;
}
@Override
public List<String> getDataGenMods() {
// unmod list prevents uncontrolled additions (we want to create the run config too)

View File

@@ -29,6 +29,7 @@ import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import dev.architectury.loom.util.ForgeLoggerConfig;
import org.gradle.api.tasks.TaskAction;
import net.fabricmc.loom.task.AbstractLoomTask;
@@ -38,6 +39,11 @@ public abstract class GenerateLog4jConfigTask extends AbstractLoomTask {
public void run() {
Path outputFile = getExtension().getFiles().getDefaultLog4jConfigFile().toPath();
if (getExtension().isForge() && getExtension().getForge().getUseForgeLoggerConfig().get()) {
ForgeLoggerConfig.copyToPath(getProject(), outputFile);
return;
}
try (InputStream is = GenerateLog4jConfigTask.class.getClassLoader().getResourceAsStream("log4j2.fabric.xml")) {
Files.deleteIfExists(outputFile);
Files.copy(is, outputFile);

View File

@@ -0,0 +1,37 @@
package net.fabricmc.loom.test.integration.forge
import net.fabricmc.loom.test.util.GradleProjectTestTrait
import spock.lang.Specification
import spock.lang.Unroll
import static net.fabricmc.loom.test.LoomTestConstants.DEFAULT_GRADLE
import static org.gradle.testkit.runner.TaskOutcome.SUCCESS
class FmlLogConfigTest extends Specification implements GradleProjectTestTrait {
@Unroll
def "use fml logger config (minecraft #minecraft, forge #forge)"() {
setup:
def gradle = gradleProject(project: 'forge/loggerConfig', version: DEFAULT_GRADLE)
gradle.gradleProperties << """
minecraft_version=$minecraft
forge_version=$forge
""".stripIndent()
when:
def result = gradle.run(task: 'generateLog4jConfig')
then:
result.task(':generateLog4jConfig').outcome == SUCCESS
def logFile = new File(gradle.projectDir, '.gradle/loom-cache/log4j.xml')
logFile.text.contains('forge.logging')
where:
minecraft | forge
//'1.19.4' | '45.0.43'
//'1.18.1' | '39.0.63'
//'1.17.1' | '37.0.67'
'1.16.5' | '36.2.4'
'1.14.4' | '28.2.23'
}
}

View File

@@ -0,0 +1,20 @@
plugins {
id 'dev.architectury.loom'
}
archivesBaseName = project.archives_base_name
version = project.mod_version
group = project.maven_group
loom {
forge {
useForgeLoggerConfig = true
}
}
dependencies {
// To change the versions see the gradle.properties file
minecraft "com.mojang:minecraft:$project.minecraft_version"
mappings loom.officialMojangMappings()
forge "net.minecraftforge:forge:$project.minecraft_version-$project.forge_version"
}

View File

@@ -0,0 +1,9 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx1G
# Mod Properties
mod_version = 1.0.0
maven_group = com.example
archives_base_name = fabric-example-mod
loom.platform=forge

View File

@@ -0,0 +1,2 @@
rootProject.name = "fabric-example-mod"