mirror of
https://github.com/architectury/architectury-loom.git
synced 2026-04-03 05:57:42 -05:00
Support JarJar (#93)
* Support JarJar Signed-off-by: shedaniel <daniel@shedaniel.me> * Use @Input instead @Internal and remove random lines Signed-off-by: shedaniel <daniel@shedaniel.me> * Resolve reviews Signed-off-by: shedaniel <daniel@shedaniel.me> * Turn them into records Signed-off-by: shedaniel <daniel@shedaniel.me>
This commit is contained in:
@@ -159,8 +159,6 @@ public interface LoomGradleExtension extends LoomGradleExtensionAPI {
|
||||
return isForge() && !getMcpConfigProvider().isOfficial();
|
||||
}
|
||||
|
||||
boolean supportsInclude();
|
||||
|
||||
DependencyProviders getDependencyProviders();
|
||||
|
||||
void setDependencyProviders(DependencyProviders dependencyProviders);
|
||||
|
||||
@@ -26,11 +26,16 @@ package net.fabricmc.loom.build.nesting;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Set;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import com.google.common.collect.Sets;
|
||||
import com.google.common.hash.Hashing;
|
||||
@@ -47,6 +52,7 @@ import org.gradle.api.artifacts.ResolvedDependency;
|
||||
import org.gradle.api.file.ConfigurableFileCollection;
|
||||
import org.gradle.api.plugins.JavaPlugin;
|
||||
import org.gradle.api.provider.Provider;
|
||||
import org.gradle.api.tasks.TaskDependency;
|
||||
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
@@ -54,6 +60,7 @@ import net.fabricmc.loom.LoomGradleExtension;
|
||||
import net.fabricmc.loom.LoomGradlePlugin;
|
||||
import net.fabricmc.loom.task.RemapTaskConfiguration;
|
||||
import net.fabricmc.loom.util.ModUtils;
|
||||
import net.fabricmc.loom.util.Pair;
|
||||
import net.fabricmc.loom.util.ZipUtils;
|
||||
|
||||
public final class IncludedJarFactory {
|
||||
@@ -67,16 +74,34 @@ public final class IncludedJarFactory {
|
||||
return project.provider(() -> {
|
||||
final ConfigurableFileCollection files = project.files();
|
||||
final Set<String> visited = Sets.newHashSet();
|
||||
final Set<Task> builtBy = Sets.newHashSet();
|
||||
|
||||
files.from(getProjectDeps(configuration, visited));
|
||||
files.from(getFileDeps(configuration, visited));
|
||||
files.from(getProjectDeps(configuration, visited, builtBy).stream().map(LazyNestedFile::file).toArray());
|
||||
files.from(getFileDeps(configuration, visited, builtBy).stream().map(LazyNestedFile::file).toArray());
|
||||
files.builtBy(configuration.getBuildDependencies());
|
||||
return files;
|
||||
});
|
||||
}
|
||||
|
||||
private ConfigurableFileCollection getFileDeps(Configuration configuration, Set<String> visited) {
|
||||
final ConfigurableFileCollection files = project.files();
|
||||
public Provider<Pair<List<LazyNestedFile>, TaskDependency>> getForgeNestedJars(final Configuration configuration) {
|
||||
return project.provider(() -> {
|
||||
final List<LazyNestedFile> files = new ArrayList<>();
|
||||
final Set<String> visited = Sets.newHashSet();
|
||||
final Set<Task> builtBy = Sets.newHashSet();
|
||||
|
||||
files.addAll(getProjectDeps(configuration, visited, builtBy));
|
||||
files.addAll(getFileDeps(configuration, visited, builtBy));
|
||||
return new Pair<>(files, task -> {
|
||||
TaskDependency dependencies = configuration.getBuildDependencies();
|
||||
Set<Task> tasks = new HashSet<>(dependencies.getDependencies(task));
|
||||
tasks.addAll(builtBy);
|
||||
return tasks;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
private List<LazyNestedFile> getFileDeps(Configuration configuration, Set<String> visited, Set<Task> builtBy) {
|
||||
final List<LazyNestedFile> files = new ArrayList<>();
|
||||
|
||||
final ResolvedConfiguration resolvedConfiguration = configuration.getResolvedConfiguration();
|
||||
final Set<ResolvedDependency> dependencies = resolvedConfiguration.getFirstLevelModuleDependencies();
|
||||
@@ -94,15 +119,15 @@ public final class IncludedJarFactory {
|
||||
artifact.getClassifier()
|
||||
);
|
||||
|
||||
files.from(project.provider(() -> getNestableJar(artifact.getFile(), metadata)));
|
||||
files.add(new LazyNestedFile(project, metadata, () -> getNestableJar(artifact.getFile(), metadata)));
|
||||
}
|
||||
}
|
||||
|
||||
return files;
|
||||
}
|
||||
|
||||
private ConfigurableFileCollection getProjectDeps(Configuration configuration, Set<String> visited) {
|
||||
final ConfigurableFileCollection files = project.files();
|
||||
private List<LazyNestedFile> getProjectDeps(Configuration configuration, Set<String> visited, Set<Task> builtBy) {
|
||||
final List<LazyNestedFile> files = new ArrayList<>();
|
||||
|
||||
for (Dependency dependency : configuration.getDependencies()) {
|
||||
if (dependency instanceof ProjectDependency projectDependency) {
|
||||
@@ -130,8 +155,8 @@ public final class IncludedJarFactory {
|
||||
);
|
||||
|
||||
Provider<File> provider = archiveTask.getArchiveFile().map(regularFile -> getNestableJar(regularFile.getAsFile(), metadata));
|
||||
files.from(provider);
|
||||
files.builtBy(task);
|
||||
files.add(new LazyNestedFile(metadata, provider));
|
||||
builtBy.add(task);
|
||||
} else {
|
||||
throw new UnsupportedOperationException("Cannot nest none AbstractArchiveTask task: " + task.getName());
|
||||
}
|
||||
@@ -201,7 +226,7 @@ public final class IncludedJarFactory {
|
||||
return LoomGradlePlugin.GSON.toJson(jsonObject);
|
||||
}
|
||||
|
||||
private record Metadata(String group, String name, String version, @Nullable String classifier) {
|
||||
public record Metadata(String group, String name, String version, @Nullable String classifier) implements Serializable {
|
||||
@Override
|
||||
public String classifier() {
|
||||
if (classifier == null) {
|
||||
@@ -211,4 +236,16 @@ public final class IncludedJarFactory {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public record NestedFile(Metadata metadata, File file) implements Serializable { }
|
||||
|
||||
public record LazyNestedFile(Metadata metadata, Provider<File> file) implements Serializable {
|
||||
public LazyNestedFile(Project project, Metadata metadata, Supplier<File> file) {
|
||||
this(metadata, project.provider(file::get));
|
||||
}
|
||||
|
||||
public NestedFile resolve() {
|
||||
return new NestedFile(metadata, file.get());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,6 +28,7 @@ import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.nio.file.Files;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -38,13 +39,15 @@ import com.google.gson.JsonObject;
|
||||
import org.gradle.api.UncheckedIOException;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import net.fabricmc.loom.LoomGradlePlugin;
|
||||
import net.fabricmc.loom.build.nesting.IncludedJarFactory.NestedFile;
|
||||
import net.fabricmc.loom.util.ModPlatform;
|
||||
import net.fabricmc.loom.util.ModUtils;
|
||||
import net.fabricmc.loom.util.Pair;
|
||||
import net.fabricmc.loom.util.ZipUtils;
|
||||
|
||||
public class JarNester {
|
||||
public static void nestJars(Collection<File> jars, File modJar, ModPlatform platform, Logger logger) {
|
||||
public static void nestJars(Collection<File> jars, List<NestedFile> forgeJars, File modJar, ModPlatform platform, Logger logger) {
|
||||
if (jars.isEmpty()) {
|
||||
logger.debug("Nothing to nest into " + modJar.getName());
|
||||
return;
|
||||
@@ -61,6 +64,11 @@ public class JarNester {
|
||||
}
|
||||
}).collect(Collectors.toList()));
|
||||
|
||||
if (platform == ModPlatform.FORGE) {
|
||||
handleForgeJarJar(forgeJars, modJar, logger);
|
||||
return;
|
||||
}
|
||||
|
||||
int count = ZipUtils.transformJson(JsonObject.class, modJar.toPath(), Stream.of(platform == ModPlatform.FABRIC ? new Pair<>("fabric.mod.json", json -> {
|
||||
JsonArray nestedJars = json.getAsJsonArray("jars");
|
||||
|
||||
@@ -132,4 +140,42 @@ public class JarNester {
|
||||
throw new java.io.UncheckedIOException("Failed to nest jars into " + modJar.getName(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private static void handleForgeJarJar(List<NestedFile> forgeJars, File modJar, Logger logger) throws IOException {
|
||||
JsonObject json = new JsonObject();
|
||||
JsonArray nestedJars = new JsonArray();
|
||||
|
||||
for (NestedFile nestedFile : forgeJars) {
|
||||
IncludedJarFactory.Metadata metadata = nestedFile.metadata();
|
||||
File file = nestedFile.file();
|
||||
String nestedJarPath = "META-INF/jars/" + file.getName();
|
||||
Preconditions.checkArgument(ModUtils.isMod(file, ModPlatform.FORGE), "Cannot nest none mod jar: " + file.getName());
|
||||
|
||||
for (JsonElement nestedJar : nestedJars) {
|
||||
JsonObject jsonObject = nestedJar.getAsJsonObject();
|
||||
|
||||
if (jsonObject.has("path") && jsonObject.get("path").getAsString().equals(nestedJarPath)) {
|
||||
throw new IllegalStateException("Cannot nest 2 jars at the same path: " + nestedJarPath);
|
||||
}
|
||||
}
|
||||
|
||||
JsonObject jsonObject = new JsonObject();
|
||||
JsonObject identifierObject = new JsonObject();
|
||||
JsonObject versionObject = new JsonObject();
|
||||
identifierObject.addProperty("group", metadata.group());
|
||||
identifierObject.addProperty("artifact", metadata.name());
|
||||
versionObject.addProperty("range", "[" + metadata.version() + ",)");
|
||||
versionObject.addProperty("artifactVersion", metadata.version());
|
||||
jsonObject.add("identifier", identifierObject);
|
||||
jsonObject.add("version", versionObject);
|
||||
jsonObject.addProperty("path", nestedJarPath);
|
||||
nestedJars.add(jsonObject);
|
||||
|
||||
logger.debug("Nested " + nestedJarPath + " into " + modJar.getName());
|
||||
}
|
||||
|
||||
json.add("jars", nestedJars);
|
||||
|
||||
ZipUtils.add(modJar.toPath(), "META-INF/jarjar/metadata.json", LoomGradlePlugin.GSON.toJson(json));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -135,9 +135,7 @@ public final class CompileConfiguration {
|
||||
extendsFrom(JavaPlugin.TEST_RUNTIME_CLASSPATH_CONFIGURATION_NAME, Constants.Configurations.FORGE_EXTRA, project);
|
||||
}
|
||||
|
||||
if (extension.supportsInclude()) {
|
||||
extension.createLazyConfiguration(Constants.Configurations.INCLUDE, configuration -> configuration.setTransitive(false)); // Dont get transitive deps
|
||||
}
|
||||
extension.createLazyConfiguration(Constants.Configurations.INCLUDE, configuration -> configuration.setTransitive(false)); // Dont get transitive deps
|
||||
|
||||
extension.createLazyConfiguration(Constants.Configurations.MAPPING_CONSTANTS);
|
||||
extension.createLazyConfiguration(Constants.Configurations.NAMED_ELEMENTS, configuration -> {
|
||||
|
||||
@@ -58,7 +58,6 @@ import net.fabricmc.loom.configuration.providers.minecraft.mapped.IntermediaryMi
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.mapped.NamedMinecraftProvider;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.mapped.SrgMinecraftProvider;
|
||||
import net.fabricmc.loom.util.ModPlatform;
|
||||
import net.fabricmc.loom.util.function.LazyBool;
|
||||
|
||||
public class LoomGradleExtensionImpl extends LoomGradleExtensionApiImpl implements LoomGradleExtension {
|
||||
private final Project project;
|
||||
@@ -84,8 +83,6 @@ public class LoomGradleExtensionImpl extends LoomGradleExtensionApiImpl implemen
|
||||
// +-------------------+
|
||||
// | Architectury Loom |
|
||||
// +-------------------+
|
||||
private static final String INCLUDE_PROPERTY = "loom.forge.include";
|
||||
private final LazyBool supportsInclude;
|
||||
private DependencyProviders dependencyProviders;
|
||||
|
||||
public LoomGradleExtensionImpl(Project project, LoomFiles files) {
|
||||
@@ -96,7 +93,6 @@ public class LoomGradleExtensionImpl extends LoomGradleExtensionApiImpl implemen
|
||||
this.loomFiles = files;
|
||||
this.unmappedMods = project.files();
|
||||
this.forgeExtension = Suppliers.memoize(() -> isForge() ? project.getObjects().newInstance(ForgeExtensionImpl.class, project, this) : null);
|
||||
this.supportsInclude = new LazyBool(() -> Boolean.parseBoolean(Objects.toString(project.findProperty(INCLUDE_PROPERTY))));
|
||||
|
||||
// Setup the default intermediate mappings provider.
|
||||
setIntermediateMappingsProvider(IntermediaryMappingsProvider.class, provider -> {
|
||||
@@ -282,11 +278,6 @@ public class LoomGradleExtensionImpl extends LoomGradleExtensionApiImpl implemen
|
||||
return forgeExtension.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean supportsInclude() {
|
||||
return !isForge() || supportsInclude.getAsBoolean();
|
||||
}
|
||||
|
||||
@Override
|
||||
public DependencyProviders getDependencyProviders() {
|
||||
return dependencyProviders;
|
||||
|
||||
@@ -42,6 +42,7 @@ import java.nio.file.Path;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
@@ -74,12 +75,14 @@ import org.gradle.api.plugins.JavaPlugin;
|
||||
import org.gradle.api.provider.ListProperty;
|
||||
import org.gradle.api.provider.MapProperty;
|
||||
import org.gradle.api.provider.Property;
|
||||
import org.gradle.api.provider.Provider;
|
||||
import org.gradle.api.provider.SetProperty;
|
||||
import org.gradle.api.tasks.Input;
|
||||
import org.gradle.api.tasks.InputFiles;
|
||||
import org.gradle.api.tasks.Internal;
|
||||
import org.gradle.api.tasks.SourceSet;
|
||||
import org.gradle.api.tasks.TaskAction;
|
||||
import org.gradle.api.tasks.TaskDependency;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -91,6 +94,8 @@ import net.fabricmc.loom.LoomGradleExtension;
|
||||
import net.fabricmc.loom.LoomGradlePlugin;
|
||||
import net.fabricmc.loom.build.MixinRefmapHelper;
|
||||
import net.fabricmc.loom.build.nesting.IncludedJarFactory;
|
||||
import net.fabricmc.loom.build.nesting.IncludedJarFactory.LazyNestedFile;
|
||||
import net.fabricmc.loom.build.nesting.IncludedJarFactory.NestedFile;
|
||||
import net.fabricmc.loom.build.nesting.JarNester;
|
||||
import net.fabricmc.loom.configuration.accesswidener.AccessWidenerFile;
|
||||
import net.fabricmc.loom.configuration.providers.minecraft.MinecraftSourceSets;
|
||||
@@ -117,6 +122,9 @@ public abstract class RemapJarTask extends AbstractRemapJarTask {
|
||||
@InputFiles
|
||||
public abstract ConfigurableFileCollection getNestedJars();
|
||||
|
||||
@Input
|
||||
public abstract ListProperty<NestedFile> getForgeNestedJars();
|
||||
|
||||
@Input
|
||||
public abstract Property<Boolean> getAddNestedDependencies();
|
||||
|
||||
@@ -166,9 +174,19 @@ public abstract class RemapJarTask extends AbstractRemapJarTask {
|
||||
getReadMixinConfigsFromManifest().convention(LoomGradleExtension.get(getProject()).isForge()).finalizeValueOnRead();
|
||||
getInjectAccessWidener().convention(false);
|
||||
|
||||
if (LoomGradleExtension.get(getProject()).supportsInclude()) {
|
||||
Configuration includeConfiguration = getProject().getConfigurations().getByName(Constants.Configurations.INCLUDE);
|
||||
getNestedJars().from(new IncludedJarFactory(getProject()).getNestedJars(includeConfiguration));
|
||||
Configuration includeConfiguration = getProject().getConfigurations().getByName(Constants.Configurations.INCLUDE);
|
||||
IncludedJarFactory factory = new IncludedJarFactory(getProject());
|
||||
|
||||
if (!LoomGradleExtension.get(getProject()).isForge()) {
|
||||
getNestedJars().from(factory.getNestedJars(includeConfiguration));
|
||||
} else {
|
||||
Provider<Pair<List<LazyNestedFile>, TaskDependency>> forgeNestedJars = factory.getForgeNestedJars(includeConfiguration);
|
||||
getForgeNestedJars().value(forgeNestedJars.map(Pair::left).map(pairs -> {
|
||||
return pairs.stream()
|
||||
.map(LazyNestedFile::resolve)
|
||||
.toList();
|
||||
}));
|
||||
getNestedJars().builtBy(forgeNestedJars.map(Pair::right));
|
||||
}
|
||||
|
||||
setupPreparationTask();
|
||||
@@ -195,8 +213,12 @@ public abstract class RemapJarTask extends AbstractRemapJarTask {
|
||||
final LoomGradleExtension extension = LoomGradleExtension.get(getProject());
|
||||
|
||||
submitWork(RemapAction.class, params -> {
|
||||
if (extension.supportsInclude() && getAddNestedDependencies().get()) {
|
||||
if (getAddNestedDependencies().get()) {
|
||||
params.getNestedJars().from(getNestedJars());
|
||||
|
||||
if (extension.isForge()) {
|
||||
params.getForgeNestedJars().set(getForgeNestedJars());
|
||||
}
|
||||
}
|
||||
|
||||
params.getJarManifestService().set(JarManifestService.get(getProject()));
|
||||
@@ -336,6 +358,9 @@ public abstract class RemapJarTask extends AbstractRemapJarTask {
|
||||
|
||||
public interface RemapParams extends AbstractRemapParams {
|
||||
ConfigurableFileCollection getNestedJars();
|
||||
|
||||
ListProperty<NestedFile> getForgeNestedJars();
|
||||
|
||||
ConfigurableFileCollection getRemapClasspath();
|
||||
|
||||
Property<ModPlatform> getPlatform();
|
||||
@@ -527,13 +552,16 @@ public abstract class RemapJarTask extends AbstractRemapJarTask {
|
||||
|
||||
private void addNestedJars() {
|
||||
FileCollection nestedJars = getParameters().getNestedJars();
|
||||
ListProperty<NestedFile> forgeNestedJars = getParameters().getForgeNestedJars();
|
||||
|
||||
if (nestedJars.isEmpty()) {
|
||||
if (nestedJars.isEmpty() && (!forgeNestedJars.isPresent() || forgeNestedJars.get().isEmpty())) {
|
||||
LOGGER.info("No jars to nest");
|
||||
return;
|
||||
}
|
||||
|
||||
JarNester.nestJars(nestedJars.getFiles(), outputFile.toFile(), getParameters().getPlatform().get(), LOGGER);
|
||||
Set<File> jars = new HashSet<>(nestedJars.getFiles());
|
||||
jars.addAll(forgeNestedJars.get().stream().map(NestedFile::file).toList());
|
||||
JarNester.nestJars(jars, forgeNestedJars.getOrElse(List.of()), outputFile.toFile(), getParameters().getPlatform().get(), LOGGER);
|
||||
}
|
||||
|
||||
private void modifyJarManifest() throws IOException {
|
||||
|
||||
@@ -24,5 +24,7 @@
|
||||
|
||||
package net.fabricmc.loom.util;
|
||||
|
||||
public record Pair<L, R>(L left, R right) {
|
||||
import java.io.Serializable;
|
||||
|
||||
public record Pair<L, R>(L left, R right) implements Serializable {
|
||||
}
|
||||
|
||||
@@ -1,52 +0,0 @@
|
||||
/*
|
||||
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) 2020-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.util.function;
|
||||
|
||||
import java.util.Objects;
|
||||
import java.util.function.BooleanSupplier;
|
||||
|
||||
/**
|
||||
* A lazily computed boolean value.
|
||||
*
|
||||
* @author Juuz
|
||||
*/
|
||||
public final class LazyBool implements BooleanSupplier {
|
||||
private BooleanSupplier supplier;
|
||||
private Boolean value;
|
||||
|
||||
public LazyBool(BooleanSupplier supplier) {
|
||||
this.supplier = Objects.requireNonNull(supplier, "supplier");
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getAsBoolean() {
|
||||
if (value == null) {
|
||||
value = supplier.getAsBoolean();
|
||||
supplier = null; // Release the supplier
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user