mirror of
https://github.com/architectury/architectury-loom.git
synced 2026-03-30 21:05:58 -05:00
Fix Intelij download sources hook. (#1006)
* Fix Intelij download sources hook. * Cleanup and performance improvements
This commit is contained in:
@@ -222,8 +222,10 @@ public abstract class CompileConfiguration implements Runnable {
|
||||
private void configureDecompileTasks(ConfigContext configContext) {
|
||||
final LoomGradleExtension extension = configContext.extension();
|
||||
|
||||
extension.getMinecraftJarConfiguration().get().getDecompileConfigurationBiFunction()
|
||||
.apply(configContext, extension.getNamedMinecraftProvider()).afterEvaluation();
|
||||
extension.getMinecraftJarConfiguration().get()
|
||||
.getDecompileConfigurationBiFunction()
|
||||
.apply(configContext.project(), extension.getNamedMinecraftProvider())
|
||||
.afterEvaluation();
|
||||
}
|
||||
|
||||
private Path getLockFile() {
|
||||
|
||||
@@ -30,8 +30,8 @@ import org.gradle.api.Project;
|
||||
import org.gradle.api.artifacts.ConfigurationContainer;
|
||||
|
||||
import net.fabricmc.loom.LoomGradleExtension;
|
||||
import net.fabricmc.loom.configuration.ConfigContext;
|
||||
import net.fabricmc.loom.configuration.providers.mappings.MappingConfiguration;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJar;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.mapped.MappedMinecraftProvider;
|
||||
import net.fabricmc.loom.task.GenerateSourcesTask;
|
||||
import net.fabricmc.loom.util.Constants;
|
||||
@@ -42,13 +42,15 @@ public abstract class DecompileConfiguration<T extends MappedMinecraftProvider>
|
||||
protected final LoomGradleExtension extension;
|
||||
protected final MappingConfiguration mappingConfiguration;
|
||||
|
||||
public DecompileConfiguration(ConfigContext configContext, T minecraftProvider) {
|
||||
this.project = configContext.project();
|
||||
public DecompileConfiguration(Project project, T minecraftProvider) {
|
||||
this.project = project;
|
||||
this.minecraftProvider = minecraftProvider;
|
||||
this.extension = configContext.extension();
|
||||
this.extension = LoomGradleExtension.get(project);
|
||||
this.mappingConfiguration = extension.getMappingConfiguration();
|
||||
}
|
||||
|
||||
public abstract String getTaskName(MinecraftJar.Type type);
|
||||
|
||||
public abstract void afterEvaluation();
|
||||
|
||||
protected final void configureUnpick(GenerateSourcesTask task, File unpickOutputJar) {
|
||||
|
||||
@@ -27,16 +27,22 @@ package net.fabricmc.loom.configuration.decompile;
|
||||
import java.io.File;
|
||||
import java.util.List;
|
||||
|
||||
import org.gradle.api.Project;
|
||||
|
||||
import net.fabricmc.loom.LoomGradleExtension;
|
||||
import net.fabricmc.loom.configuration.ConfigContext;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJar;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.mapped.MappedMinecraftProvider;
|
||||
import net.fabricmc.loom.task.GenerateSourcesTask;
|
||||
import net.fabricmc.loom.util.Constants;
|
||||
|
||||
public class SingleJarDecompileConfiguration extends DecompileConfiguration<MappedMinecraftProvider> {
|
||||
public SingleJarDecompileConfiguration(ConfigContext configContext, MappedMinecraftProvider minecraftProvider) {
|
||||
super(configContext, minecraftProvider);
|
||||
public SingleJarDecompileConfiguration(Project project, MappedMinecraftProvider minecraftProvider) {
|
||||
super(project, minecraftProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTaskName(MinecraftJar.Type type) {
|
||||
return "genSources";
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -44,10 +50,11 @@ public class SingleJarDecompileConfiguration extends DecompileConfiguration<Mapp
|
||||
final List<MinecraftJar> minecraftJars = minecraftProvider.getMinecraftJars();
|
||||
assert minecraftJars.size() == 1;
|
||||
final MinecraftJar minecraftJar = minecraftJars.get(0);
|
||||
final String taskBaseName = getTaskName(minecraftJar.getType());
|
||||
|
||||
LoomGradleExtension.get(project).getDecompilerOptions().forEach(options -> {
|
||||
final String decompilerName = options.getFormattedName();
|
||||
String taskName = "genSourcesWith" + decompilerName;
|
||||
String taskName = "%sWith%s".formatted(taskBaseName, decompilerName);
|
||||
// Decompiler will be passed to the constructor of GenerateSourcesTask
|
||||
project.getTasks().register(taskName, GenerateSourcesTask.class, options).configure(task -> {
|
||||
task.getInputJarName().set(minecraftJar.getName());
|
||||
@@ -64,7 +71,7 @@ public class SingleJarDecompileConfiguration extends DecompileConfiguration<Mapp
|
||||
});
|
||||
});
|
||||
|
||||
project.getTasks().register("genSources", task -> {
|
||||
project.getTasks().register(taskBaseName, task -> {
|
||||
task.setDescription("Decompile minecraft using the default decompiler.");
|
||||
task.setGroup(Constants.TaskGroup.FABRIC);
|
||||
|
||||
|
||||
@@ -27,19 +27,28 @@ package net.fabricmc.loom.configuration.decompile;
|
||||
import java.io.File;
|
||||
|
||||
import org.gradle.api.Action;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.Task;
|
||||
import org.gradle.api.tasks.TaskProvider;
|
||||
|
||||
import net.fabricmc.loom.api.decompilers.DecompilerOptions;
|
||||
import net.fabricmc.loom.configuration.ConfigContext;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJar;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.mapped.MappedMinecraftProvider;
|
||||
import net.fabricmc.loom.task.GenerateSourcesTask;
|
||||
import net.fabricmc.loom.util.Constants;
|
||||
|
||||
public final class SplitDecompileConfiguration extends DecompileConfiguration<MappedMinecraftProvider.Split> {
|
||||
public SplitDecompileConfiguration(ConfigContext configContext, MappedMinecraftProvider.Split minecraftProvider) {
|
||||
super(configContext, minecraftProvider);
|
||||
public SplitDecompileConfiguration(Project project, MappedMinecraftProvider.Split minecraftProvider) {
|
||||
super(project, minecraftProvider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getTaskName(MinecraftJar.Type type) {
|
||||
return switch (type) {
|
||||
case COMMON -> "genCommonSources";
|
||||
case CLIENT_ONLY -> "genClientSources";
|
||||
default -> throw new AssertionError();
|
||||
};
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,145 @@
|
||||
/*
|
||||
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) 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
|
||||
* 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.configuration.ide.idea;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.Task;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import net.fabricmc.loom.LoomGradleExtension;
|
||||
import net.fabricmc.loom.configuration.mods.dependency.LocalMavenHelper;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJar;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.mapped.NamedMinecraftProvider;
|
||||
|
||||
// See: https://github.com/JetBrains/intellij-community/blob/a09b1b84ab64a699794c860bc96774766dd38958/plugins/gradle/java/src/util/GradleAttachSourcesProvider.java
|
||||
record DownloadSourcesHook(Project project, Task task) {
|
||||
public static final String INIT_SCRIPT_NAME = "ijDownloadSources";
|
||||
private static final Pattern NOTATION_PATTERN = Pattern.compile("dependencyNotation = '(?<notation>.*)'");
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(DownloadSourcesHook.class);
|
||||
|
||||
public static boolean hasInitScript(Project project) {
|
||||
List<File> initScripts = project.getGradle().getStartParameter().getInitScripts();
|
||||
|
||||
for (File initScript : initScripts) {
|
||||
if (initScript.getName().contains(INIT_SCRIPT_NAME)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void tryHook() {
|
||||
List<File> initScripts = project.getGradle().getStartParameter().getInitScripts();
|
||||
|
||||
for (File initScript : initScripts) {
|
||||
if (!initScript.getName().contains(INIT_SCRIPT_NAME)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
try {
|
||||
final String script = Files.readString(initScript.toPath(), StandardCharsets.UTF_8);
|
||||
final String notation = parseInitScript(script);
|
||||
|
||||
if (notation == null) {
|
||||
LOGGER.debug("failed to parse init script dependency");
|
||||
continue;
|
||||
}
|
||||
|
||||
final MinecraftJar.Type jarType = getJarType(notation);
|
||||
|
||||
if (jarType == null) {
|
||||
LOGGER.debug("init script is trying to download sources for another Minecraft jar ({}) not used by this project ({})", notation, project.getPath());
|
||||
continue;
|
||||
}
|
||||
|
||||
String sourcesTaskName = getGenSourcesTaskName(jarType);
|
||||
task.dependsOn(project.getTasks().named(sourcesTaskName));
|
||||
|
||||
LOGGER.info("Running genSources task: {} in project: {} for {}", sourcesTaskName, project.getPath(), notation);
|
||||
break;
|
||||
} catch (IOException e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private String parseInitScript(String script) {
|
||||
if (!script.contains("IjDownloadTask")) {
|
||||
// Failed some basic sanity checks.
|
||||
return null;
|
||||
}
|
||||
|
||||
// A little gross but should do the job nicely.
|
||||
final Matcher matcher = NOTATION_PATTERN.matcher(script);
|
||||
|
||||
if (matcher.find()) {
|
||||
return matcher.group("notation");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private String getGenSourcesTaskName(MinecraftJar.Type jarType) {
|
||||
LoomGradleExtension extension = LoomGradleExtension.get(project);
|
||||
return extension.getMinecraftJarConfiguration().get()
|
||||
.getDecompileConfigurationBiFunction()
|
||||
.apply(project, extension.getNamedMinecraftProvider())
|
||||
.getTaskName(jarType);
|
||||
}
|
||||
|
||||
// Return the jar type, or null when this jar isnt used by the project
|
||||
@Nullable
|
||||
private MinecraftJar.Type getJarType(String name) {
|
||||
final LoomGradleExtension extension = LoomGradleExtension.get(project);
|
||||
final NamedMinecraftProvider<?> minecraftProvider = extension.getNamedMinecraftProvider();
|
||||
final List<MinecraftJar.Type> dependencyTypes = minecraftProvider.getDependencyTypes();
|
||||
|
||||
if (dependencyTypes.isEmpty()) {
|
||||
throw new IllegalStateException();
|
||||
}
|
||||
|
||||
for (MinecraftJar.Type type : dependencyTypes) {
|
||||
final LocalMavenHelper mavenHelper = minecraftProvider.getMavenHelper(type).withClassifier("sources");
|
||||
|
||||
if (mavenHelper.getNotation().equals(name)) {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -24,40 +24,27 @@
|
||||
|
||||
package net.fabricmc.loom.configuration.ide.idea;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.nio.file.Files;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.inject.Inject;
|
||||
|
||||
import org.gradle.StartParameter;
|
||||
import org.gradle.TaskExecutionRequest;
|
||||
import org.gradle.api.Project;
|
||||
import org.gradle.api.Task;
|
||||
import org.gradle.api.tasks.TaskProvider;
|
||||
import org.gradle.internal.DefaultTaskExecutionRequest;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import net.fabricmc.loom.LoomGradleExtension;
|
||||
import net.fabricmc.loom.configuration.ide.RunConfigSettings;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJarConfiguration;
|
||||
import net.fabricmc.loom.task.LoomTasks;
|
||||
import net.fabricmc.loom.util.gradle.GradleUtils;
|
||||
|
||||
public abstract class IdeaConfiguration implements Runnable {
|
||||
private static final String INIT_SCRIPT_NAME = "ijmiscinit";
|
||||
private static final Pattern NOTATION_PATTERN = Pattern.compile("'net\\.minecraft:(?<name>.*):(.*):sources'");
|
||||
|
||||
@Inject
|
||||
protected abstract Project getProject();
|
||||
|
||||
public void run() {
|
||||
TaskProvider<IdeaSyncTask> ideaSyncTask = getProject().getTasks().register("ideaSyncTask", IdeaSyncTask.class, task -> {
|
||||
getProject().getTasks().register("ideaSyncTask", IdeaSyncTask.class, task -> {
|
||||
if (LoomGradleExtension.get(getProject()).getRunConfigs().stream().anyMatch(RunConfigSettings::isIdeConfigGenerated)) {
|
||||
task.dependsOn(LoomTasks.getIDELaunchConfigureTaskName(getProject()));
|
||||
} else {
|
||||
@@ -65,11 +52,7 @@ public abstract class IdeaConfiguration implements Runnable {
|
||||
}
|
||||
});
|
||||
|
||||
getProject().getTasks().configureEach(task -> {
|
||||
if (task.getName().equals("DownloadSources")) {
|
||||
hookDownloadSources(getProject(), task);
|
||||
}
|
||||
});
|
||||
hookDownloadSources();
|
||||
|
||||
if (!IdeaUtils.isIdeaSync()) {
|
||||
return;
|
||||
@@ -82,62 +65,27 @@ public abstract class IdeaConfiguration implements Runnable {
|
||||
startParameter.setTaskRequests(taskRequests);
|
||||
}
|
||||
|
||||
/*
|
||||
"Parse" the init script enough to figure out what jar we are talking about.
|
||||
private void hookDownloadSources() {
|
||||
LoomGradleExtension extension = LoomGradleExtension.get(getProject());
|
||||
|
||||
Intelij code: https://github.com/JetBrains/intellij-community/blob/a09b1b84ab64a699794c860bc96774766dd38958/plugins/gradle/java/src/util/GradleAttachSourcesProvider.java
|
||||
*/
|
||||
private static void hookDownloadSources(Project project, Task task) {
|
||||
List<File> initScripts = project.getGradle().getStartParameter().getInitScripts();
|
||||
if (!extension.isRootProject()) {
|
||||
return;
|
||||
}
|
||||
|
||||
for (File initScript : initScripts) {
|
||||
if (!initScript.getName().contains(INIT_SCRIPT_NAME)) {
|
||||
continue;
|
||||
if (!DownloadSourcesHook.hasInitScript(getProject())) {
|
||||
return;
|
||||
}
|
||||
|
||||
getProject().getTasks().configureEach(task -> {
|
||||
if (task.getName().startsWith(DownloadSourcesHook.INIT_SCRIPT_NAME)) {
|
||||
getProject().allprojects(subProject -> {
|
||||
if (!GradleUtils.isLoomProject(subProject)) {
|
||||
return;
|
||||
}
|
||||
|
||||
new DownloadSourcesHook(subProject, task).tryHook();
|
||||
});
|
||||
}
|
||||
|
||||
try {
|
||||
final String script = Files.readString(initScript.toPath(), StandardCharsets.UTF_8);
|
||||
final String notation = parseInitScript(project, script);
|
||||
|
||||
if (notation != null) {
|
||||
task.dependsOn(getGenSourcesTaskName(LoomGradleExtension.get(project), notation));
|
||||
}
|
||||
} catch (IOException e) {
|
||||
// Ignore
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String parseInitScript(Project project, String script) {
|
||||
if (!script.contains("Attempt to download sources from")
|
||||
|| !script.contains("downloadSources_")
|
||||
|| !script.contains("'%s'".formatted(project.getPath()))) {
|
||||
// Failed some basic sanity checks.
|
||||
return null;
|
||||
}
|
||||
|
||||
// A little gross but should do the job nicely.
|
||||
final Matcher matcher = NOTATION_PATTERN.matcher(script);
|
||||
|
||||
if (matcher.find()) {
|
||||
return matcher.group("name");
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private static String getGenSourcesTaskName(LoomGradleExtension extension, String notation) {
|
||||
final MinecraftJarConfiguration configuration = extension.getMinecraftJarConfiguration().get();
|
||||
|
||||
if (configuration == MinecraftJarConfiguration.SPLIT) {
|
||||
if (notation.toLowerCase(Locale.ROOT).contains("minecraft-clientonly")) {
|
||||
return "genClientOnlySources";
|
||||
}
|
||||
|
||||
return "genCommonSources";
|
||||
}
|
||||
|
||||
return "genSources";
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -34,22 +34,7 @@ import java.nio.file.StandardCopyOption;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public final class LocalMavenHelper {
|
||||
private final String group;
|
||||
private final String name;
|
||||
private final String version;
|
||||
@Nullable
|
||||
private final String baseClassifier;
|
||||
private final Path root;
|
||||
|
||||
public LocalMavenHelper(String group, String name, String version, @Nullable String classifier, Path root) {
|
||||
this.group = group;
|
||||
this.name = name;
|
||||
this.version = version;
|
||||
this.baseClassifier = classifier;
|
||||
this.root = root;
|
||||
}
|
||||
|
||||
public record LocalMavenHelper(String group, String name, String version, @Nullable String baseClassifier, Path root) {
|
||||
public Path copyToMaven(Path artifact, @Nullable String classifier) throws IOException {
|
||||
if (!artifact.getFileName().toString().endsWith(".jar")) {
|
||||
throw new UnsupportedOperationException();
|
||||
@@ -108,4 +93,8 @@ public final class LocalMavenHelper {
|
||||
: String.format("%s-%s-%s.jar", name, version, classifier);
|
||||
return getDirectory().resolve(fileName);
|
||||
}
|
||||
|
||||
public LocalMavenHelper withClassifier(String classifier) {
|
||||
return new LocalMavenHelper(group, name, version, classifier, root);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,14 +31,14 @@ import java.util.Objects;
|
||||
public abstract sealed class MinecraftJar permits MinecraftJar.Client, MinecraftJar.ClientOnly, MinecraftJar.Common, MinecraftJar.Merged, MinecraftJar.Server {
|
||||
private final Path path;
|
||||
private final boolean merged, client, server;
|
||||
private final String name;
|
||||
private final Type type;
|
||||
|
||||
protected MinecraftJar(Path path, boolean merged, boolean client, boolean server, String name) {
|
||||
protected MinecraftJar(Path path, boolean merged, boolean client, boolean server, Type type) {
|
||||
this.path = Objects.requireNonNull(path);
|
||||
this.merged = merged;
|
||||
this.client = client;
|
||||
this.server = server;
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Path getPath() {
|
||||
@@ -62,16 +62,18 @@ public abstract sealed class MinecraftJar permits MinecraftJar.Client, Minecraft
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
return type.toString();
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public abstract MinecraftJar forPath(Path path);
|
||||
|
||||
public static final class Merged extends MinecraftJar {
|
||||
public static final String NAME = "merged";
|
||||
|
||||
public Merged(Path path) {
|
||||
super(path, true, true, true, NAME);
|
||||
super(path, true, true, true, Type.MERGED);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -81,10 +83,8 @@ public abstract sealed class MinecraftJar permits MinecraftJar.Client, Minecraft
|
||||
}
|
||||
|
||||
public static final class Common extends MinecraftJar {
|
||||
public static final String NAME = "common";
|
||||
|
||||
public Common(Path path) {
|
||||
super(path, false, false, true, NAME);
|
||||
super(path, false, false, true, Type.COMMON);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -94,10 +94,8 @@ public abstract sealed class MinecraftJar permits MinecraftJar.Client, Minecraft
|
||||
}
|
||||
|
||||
public static final class Server extends MinecraftJar {
|
||||
public static final String NAME = "server";
|
||||
|
||||
public Server(Path path) {
|
||||
super(path, false, false, true, NAME);
|
||||
super(path, false, false, true, Type.SERVER);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -108,10 +106,8 @@ public abstract sealed class MinecraftJar permits MinecraftJar.Client, Minecraft
|
||||
|
||||
// Un-split client jar
|
||||
public static final class Client extends MinecraftJar {
|
||||
public static final String NAME = "client";
|
||||
|
||||
public Client(Path path) {
|
||||
super(path, false, true, false, NAME);
|
||||
super(path, false, true, false, Type.CLIENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -122,10 +118,8 @@ public abstract sealed class MinecraftJar permits MinecraftJar.Client, Minecraft
|
||||
|
||||
// Split client jar
|
||||
public static final class ClientOnly extends MinecraftJar {
|
||||
public static final String NAME = "clientOnly";
|
||||
|
||||
public ClientOnly(Path path) {
|
||||
super(path, false, true, false, NAME);
|
||||
super(path, false, true, false, Type.CLIENT_ONLY);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -133,4 +127,28 @@ public abstract sealed class MinecraftJar permits MinecraftJar.Client, Minecraft
|
||||
return new ClientOnly(path);
|
||||
}
|
||||
}
|
||||
|
||||
public enum Type {
|
||||
// Merged jar
|
||||
MERGED("merged"),
|
||||
|
||||
// Regular jars, not merged or split
|
||||
SERVER("server"),
|
||||
CLIENT("client"),
|
||||
|
||||
// Split jars
|
||||
COMMON("common"),
|
||||
CLIENT_ONLY("clientOnly");
|
||||
|
||||
private final String name;
|
||||
|
||||
Type(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ public enum MinecraftJarConfiguration {
|
||||
private final BiFunction<Project, MinecraftProvider, IntermediaryMinecraftProvider<?>> intermediaryMinecraftProviderBiFunction;
|
||||
private final BiFunction<Project, MinecraftProvider, NamedMinecraftProvider<?>> namedMinecraftProviderBiFunction;
|
||||
private final BiFunction<NamedMinecraftProvider<?>, MinecraftJarProcessorManager, ProcessedNamedMinecraftProvider<?, ?>> processedNamedMinecraftProviderBiFunction;
|
||||
private final BiFunction<ConfigContext, MappedMinecraftProvider, DecompileConfiguration<?>> decompileConfigurationBiFunction;
|
||||
private final BiFunction<Project, MappedMinecraftProvider, DecompileConfiguration<?>> decompileConfigurationBiFunction;
|
||||
private final List<String> supportedEnvironments;
|
||||
|
||||
@SuppressWarnings("unchecked") // Just a bit of a generic mess :)
|
||||
@@ -87,14 +87,14 @@ public enum MinecraftJarConfiguration {
|
||||
BiFunction<Project, M, IntermediaryMinecraftProvider<M>> intermediaryMinecraftProviderBiFunction,
|
||||
BiFunction<Project, M, P> namedMinecraftProviderBiFunction,
|
||||
BiFunction<P, MinecraftJarProcessorManager, ProcessedNamedMinecraftProvider<M, P>> processedNamedMinecraftProviderBiFunction,
|
||||
BiFunction<ConfigContext, Q, DecompileConfiguration<?>> decompileConfigurationBiFunction,
|
||||
BiFunction<Project, Q, DecompileConfiguration<?>> decompileConfigurationBiFunction,
|
||||
List<String> supportedEnvironments
|
||||
) {
|
||||
this.minecraftProviderFunction = (Function<ConfigContext, MinecraftProvider>) minecraftProviderFunction;
|
||||
this.intermediaryMinecraftProviderBiFunction = (BiFunction<Project, MinecraftProvider, IntermediaryMinecraftProvider<?>>) (Object) intermediaryMinecraftProviderBiFunction;
|
||||
this.namedMinecraftProviderBiFunction = (BiFunction<Project, MinecraftProvider, NamedMinecraftProvider<?>>) namedMinecraftProviderBiFunction;
|
||||
this.processedNamedMinecraftProviderBiFunction = (BiFunction<NamedMinecraftProvider<?>, MinecraftJarProcessorManager, ProcessedNamedMinecraftProvider<?, ?>>) (Object) processedNamedMinecraftProviderBiFunction;
|
||||
this.decompileConfigurationBiFunction = (BiFunction<ConfigContext, MappedMinecraftProvider, DecompileConfiguration<?>>) decompileConfigurationBiFunction;
|
||||
this.decompileConfigurationBiFunction = (BiFunction<Project, MappedMinecraftProvider, DecompileConfiguration<?>>) decompileConfigurationBiFunction;
|
||||
this.supportedEnvironments = supportedEnvironments;
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ public enum MinecraftJarConfiguration {
|
||||
return processedNamedMinecraftProviderBiFunction;
|
||||
}
|
||||
|
||||
public BiFunction<ConfigContext, MappedMinecraftProvider, DecompileConfiguration<?>> getDecompileConfigurationBiFunction() {
|
||||
public BiFunction<Project, MappedMinecraftProvider, DecompileConfiguration<?>> getDecompileConfigurationBiFunction() {
|
||||
return decompileConfigurationBiFunction;
|
||||
}
|
||||
|
||||
|
||||
@@ -45,7 +45,7 @@ public abstract sealed class MinecraftSourceSets permits MinecraftSourceSets.Sin
|
||||
return LoomGradleExtension.get(project).areEnvironmentSourceSetsSplit() ? Split.INSTANCE : Single.INSTANCE;
|
||||
}
|
||||
|
||||
public abstract void applyDependencies(BiConsumer<String, String> consumer, List<String> targets);
|
||||
public abstract void applyDependencies(BiConsumer<String, MinecraftJar.Type> consumer, List<MinecraftJar.Type> targets);
|
||||
|
||||
public abstract String getSourceSetForEnv(String env);
|
||||
|
||||
@@ -100,8 +100,8 @@ public abstract sealed class MinecraftSourceSets permits MinecraftSourceSets.Sin
|
||||
private static final Single INSTANCE = new Single();
|
||||
|
||||
@Override
|
||||
public void applyDependencies(BiConsumer<String, String> consumer, List<String> targets) {
|
||||
for (String target : targets) {
|
||||
public void applyDependencies(BiConsumer<String, MinecraftJar.Type> consumer, List<MinecraftJar.Type> targets) {
|
||||
for (MinecraftJar.Type target : targets) {
|
||||
consumer.accept(MINECRAFT_NAMED.compile(), target);
|
||||
consumer.accept(MINECRAFT_NAMED.runtime(), target);
|
||||
}
|
||||
@@ -150,15 +150,15 @@ public abstract sealed class MinecraftSourceSets permits MinecraftSourceSets.Sin
|
||||
private static final Split INSTANCE = new Split();
|
||||
|
||||
@Override
|
||||
public void applyDependencies(BiConsumer<String, String> consumer, List<String> targets) {
|
||||
public void applyDependencies(BiConsumer<String, MinecraftJar.Type> consumer, List<MinecraftJar.Type> targets) {
|
||||
Preconditions.checkArgument(targets.size() == 2);
|
||||
Preconditions.checkArgument(targets.contains(MinecraftJar.Common.NAME));
|
||||
Preconditions.checkArgument(targets.contains(MinecraftJar.ClientOnly.NAME));
|
||||
Preconditions.checkArgument(targets.contains(MinecraftJar.Type.COMMON));
|
||||
Preconditions.checkArgument(targets.contains(MinecraftJar.Type.CLIENT_ONLY));
|
||||
|
||||
consumer.accept(MINECRAFT_COMMON_NAMED.runtime(), MinecraftJar.Common.NAME);
|
||||
consumer.accept(MINECRAFT_CLIENT_ONLY_NAMED.runtime(), MinecraftJar.ClientOnly.NAME);
|
||||
consumer.accept(MINECRAFT_COMMON_NAMED.compile(), MinecraftJar.Common.NAME);
|
||||
consumer.accept(MINECRAFT_CLIENT_ONLY_NAMED.compile(), MinecraftJar.ClientOnly.NAME);
|
||||
consumer.accept(MINECRAFT_COMMON_NAMED.runtime(), MinecraftJar.Type.COMMON);
|
||||
consumer.accept(MINECRAFT_CLIENT_ONLY_NAMED.runtime(), MinecraftJar.Type.CLIENT_ONLY);
|
||||
consumer.accept(MINECRAFT_COMMON_NAMED.compile(), MinecraftJar.Type.COMMON);
|
||||
consumer.accept(MINECRAFT_CLIENT_ONLY_NAMED.compile(), MinecraftJar.Type.CLIENT_ONLY);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -28,22 +28,22 @@ import java.nio.file.Path;
|
||||
import java.util.function.Function;
|
||||
|
||||
public enum SingleJarEnvType {
|
||||
CLIENT(MinecraftJar.Client::new, MinecraftJar.Client.NAME),
|
||||
SERVER(MinecraftJar.Server::new, MinecraftJar.Server.NAME);
|
||||
CLIENT(MinecraftJar.Client::new, MinecraftJar.Type.CLIENT),
|
||||
SERVER(MinecraftJar.Server::new, MinecraftJar.Type.SERVER);
|
||||
|
||||
private final Function<Path, MinecraftJar> jarFunction;
|
||||
private final String name;
|
||||
private final MinecraftJar.Type type;
|
||||
|
||||
SingleJarEnvType(Function<Path, MinecraftJar> jarFunction, String name) {
|
||||
SingleJarEnvType(Function<Path, MinecraftJar> jarFunction, MinecraftJar.Type type) {
|
||||
this.jarFunction = jarFunction;
|
||||
this.name = name;
|
||||
this.type = type;
|
||||
}
|
||||
|
||||
public Function<Path, MinecraftJar> getJar() {
|
||||
return jarFunction;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
public MinecraftJar.Type getType() {
|
||||
return type;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,7 +68,8 @@ public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvide
|
||||
|
||||
public abstract List<RemappedJars> getRemappedJars();
|
||||
|
||||
public List<String> getDependencyTargets() {
|
||||
// Returns a list of MinecraftJar.Type's that this provider exports to be used as a dependency
|
||||
public List<MinecraftJar.Type> getDependencyTypes() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
@@ -87,11 +88,11 @@ public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvide
|
||||
}
|
||||
|
||||
if (context.applyDependencies()) {
|
||||
final List<String> dependencyTargets = getDependencyTargets();
|
||||
final List<MinecraftJar.Type> dependencyTargets = getDependencyTypes();
|
||||
|
||||
if (!dependencyTargets.isEmpty()) {
|
||||
MinecraftSourceSets.get(getProject()).applyDependencies(
|
||||
(configuration, name) -> getProject().getDependencies().add(configuration, getDependencyNotation(name)),
|
||||
(configuration, type) -> getProject().getDependencies().add(configuration, getDependencyNotation(type)),
|
||||
dependencyTargets
|
||||
);
|
||||
}
|
||||
@@ -109,8 +110,8 @@ public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvide
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path getJar(String name) {
|
||||
return getMavenHelper(name).getOutputFile(null);
|
||||
public Path getJar(MinecraftJar.Type type) {
|
||||
return getMavenHelper(type).getOutputFile(null);
|
||||
}
|
||||
|
||||
public enum MavenScope {
|
||||
@@ -132,16 +133,16 @@ public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvide
|
||||
|
||||
public abstract MavenScope getMavenScope();
|
||||
|
||||
public LocalMavenHelper getMavenHelper(String name) {
|
||||
return new LocalMavenHelper("net.minecraft", getName(name), getVersion(), null, getMavenScope().getRoot(extension));
|
||||
public LocalMavenHelper getMavenHelper(MinecraftJar.Type type) {
|
||||
return new LocalMavenHelper("net.minecraft", getName(type), getVersion(), null, getMavenScope().getRoot(extension));
|
||||
}
|
||||
|
||||
protected String getName(String name) {
|
||||
protected String getName(MinecraftJar.Type type) {
|
||||
final String intermediateName = extension.getIntermediateMappingsProvider().getName();
|
||||
|
||||
var sj = new StringJoiner("-");
|
||||
sj.add("minecraft");
|
||||
sj.add(name);
|
||||
sj.add(type.toString());
|
||||
|
||||
// Include the intermediate mapping name if it's not the default intermediary
|
||||
if (!intermediateName.equals(IntermediaryMappingsProvider.NAME)) {
|
||||
@@ -159,13 +160,13 @@ public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvide
|
||||
return "%s-%s".formatted(extension.getMinecraftProvider().minecraftVersion(), extension.getMappingConfiguration().mappingsIdentifier());
|
||||
}
|
||||
|
||||
protected String getDependencyNotation(String name) {
|
||||
return "net.minecraft:%s:%s".formatted(getName(name), getVersion());
|
||||
protected String getDependencyNotation(MinecraftJar.Type type) {
|
||||
return "net.minecraft:%s:%s".formatted(getName(type), getVersion());
|
||||
}
|
||||
|
||||
private boolean areOutputsValid(List<RemappedJars> remappedJars) {
|
||||
for (RemappedJars remappedJar : remappedJars) {
|
||||
if (!getMavenHelper(remappedJar.name()).exists(null)) {
|
||||
if (!getMavenHelper(remappedJar.type()).exists(null)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -209,7 +210,7 @@ public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvide
|
||||
remapper.finish();
|
||||
}
|
||||
|
||||
getMavenHelper(remappedJars.name()).savePom();
|
||||
getMavenHelper(remappedJars.type()).savePom();
|
||||
}
|
||||
|
||||
protected void configureRemapper(RemappedJars remappedJars, TinyRemapper.Builder tinyRemapperBuilder) {
|
||||
@@ -248,5 +249,9 @@ public abstract class AbstractMappedMinecraftProvider<M extends MinecraftProvide
|
||||
public String name() {
|
||||
return outputJar().getName();
|
||||
}
|
||||
|
||||
public MinecraftJar.Type type() {
|
||||
return outputJar().getType();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -38,14 +38,12 @@ public interface MappedMinecraftProvider {
|
||||
List<MinecraftJar> getMinecraftJars();
|
||||
|
||||
interface ProviderImpl extends MappedMinecraftProvider {
|
||||
Path getJar(String name);
|
||||
Path getJar(MinecraftJar.Type type);
|
||||
}
|
||||
|
||||
interface Merged extends ProviderImpl {
|
||||
String MERGED = MinecraftJar.Merged.NAME;
|
||||
|
||||
default MinecraftJar getMergedJar() {
|
||||
return new MinecraftJar.Merged(getJar(MERGED));
|
||||
return new MinecraftJar.Merged(getJar(MinecraftJar.Type.MERGED));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -55,15 +53,12 @@ public interface MappedMinecraftProvider {
|
||||
}
|
||||
|
||||
interface Split extends ProviderImpl {
|
||||
String COMMON = MinecraftJar.Common.NAME;
|
||||
String CLIENT_ONLY = MinecraftJar.ClientOnly.NAME;
|
||||
|
||||
default MinecraftJar getCommonJar() {
|
||||
return new MinecraftJar.Common(getJar(COMMON));
|
||||
return new MinecraftJar.Common(getJar(MinecraftJar.Type.COMMON));
|
||||
}
|
||||
|
||||
default MinecraftJar getClientOnlyJar() {
|
||||
return new MinecraftJar.ClientOnly(getJar(CLIENT_ONLY));
|
||||
return new MinecraftJar.ClientOnly(getJar(MinecraftJar.Type.CLIENT_ONLY));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -75,12 +70,12 @@ public interface MappedMinecraftProvider {
|
||||
interface SingleJar extends ProviderImpl {
|
||||
SingleJarEnvType env();
|
||||
|
||||
default String envName() {
|
||||
return env().getName();
|
||||
default MinecraftJar.Type envType() {
|
||||
return env().getType();
|
||||
}
|
||||
|
||||
default MinecraftJar getEnvOnlyJar() {
|
||||
return env().getJar().apply(getJar(envName()));
|
||||
return env().getJar().apply(getJar(env().getType()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.gradle.api.Project;
|
||||
|
||||
import net.fabricmc.loom.api.mappings.layered.MappingsNamespace;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.MergedMinecraftProvider;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftJar;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftProvider;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.SingleJarEnvType;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.SingleJarMinecraftProvider;
|
||||
@@ -64,8 +65,8 @@ public abstract class NamedMinecraftProvider<M extends MinecraftProvider> extend
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getDependencyTargets() {
|
||||
return List.of(MERGED);
|
||||
public List<MinecraftJar.Type> getDependencyTypes() {
|
||||
return List.of(MinecraftJar.Type.MERGED);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -88,8 +89,8 @@ public abstract class NamedMinecraftProvider<M extends MinecraftProvider> extend
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getDependencyTargets() {
|
||||
return List.of(CLIENT_ONLY, COMMON);
|
||||
public List<MinecraftJar.Type> getDependencyTypes() {
|
||||
return List.of(MinecraftJar.Type.CLIENT_ONLY, MinecraftJar.Type.COMMON);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,8 +118,8 @@ public abstract class NamedMinecraftProvider<M extends MinecraftProvider> extend
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getDependencyTargets() {
|
||||
return List.of(envName());
|
||||
public List<MinecraftJar.Type> getDependencyTypes() {
|
||||
return List.of(envType());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -88,7 +88,7 @@ public abstract class ProcessedNamedMinecraftProvider<M extends MinecraftProvide
|
||||
final MinecraftJar outputJar = entry.getValue();
|
||||
deleteSimilarJars(outputJar.getPath());
|
||||
|
||||
final LocalMavenHelper mavenHelper = getMavenHelper(minecraftJar.getName());
|
||||
final LocalMavenHelper mavenHelper = getMavenHelper(minecraftJar.getType());
|
||||
final Path outputPath = mavenHelper.copyToMaven(minecraftJar.getPath(), null);
|
||||
|
||||
assert outputJar.getPath().equals(outputPath);
|
||||
@@ -97,8 +97,13 @@ public abstract class ProcessedNamedMinecraftProvider<M extends MinecraftProvide
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<MinecraftJar.Type> getDependencyTypes() {
|
||||
return parentMinecraftProvider.getDependencyTypes();
|
||||
}
|
||||
|
||||
private void applyDependencies() {
|
||||
final List<String> dependencyTargets = parentMinecraftProvider.getDependencyTargets();
|
||||
final List<MinecraftJar.Type> dependencyTargets = getDependencyTypes();
|
||||
|
||||
if (dependencyTargets.isEmpty()) {
|
||||
return;
|
||||
@@ -125,13 +130,13 @@ public abstract class ProcessedNamedMinecraftProvider<M extends MinecraftProvide
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getName(String name) {
|
||||
protected String getName(MinecraftJar.Type type) {
|
||||
// Hash the cache value so that we don't have to process the same JAR multiple times for many projects
|
||||
return "minecraft-%s-%s".formatted(name, jarProcessorManager.getJarHash());
|
||||
return "minecraft-%s-%s".formatted(type.toString(), jarProcessorManager.getJarHash());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Path getJar(String name) {
|
||||
public Path getJar(MinecraftJar.Type type) {
|
||||
// Something has gone wrong if this gets called.
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
@@ -153,7 +158,7 @@ public abstract class ProcessedNamedMinecraftProvider<M extends MinecraftProvide
|
||||
}
|
||||
|
||||
private Path getProcessedPath(MinecraftJar minecraftJar) {
|
||||
final LocalMavenHelper mavenHelper = getMavenHelper(minecraftJar.getName());
|
||||
final LocalMavenHelper mavenHelper = getMavenHelper(minecraftJar.getType());
|
||||
return mavenHelper.getOutputFile(null);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user