2019-04-07 14:18:11 +00:00
|
|
|
/*
|
|
|
|
* This file is part of fabric-loom, licensed under the MIT License (MIT).
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016, 2017, 2018 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.
|
|
|
|
*/
|
|
|
|
|
2020-12-24 20:58:30 +00:00
|
|
|
package net.fabricmc.loom.build;
|
2019-04-07 14:18:11 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Collection;
|
2021-02-02 23:03:57 +00:00
|
|
|
import java.util.Collections;
|
2020-09-04 18:12:04 +00:00
|
|
|
import java.util.HashSet;
|
2019-11-02 20:23:27 +00:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
|
|
|
import java.util.Set;
|
2020-07-26 20:08:48 +00:00
|
|
|
import java.util.stream.Collectors;
|
2019-11-02 20:23:27 +00:00
|
|
|
import java.util.zip.ZipEntry;
|
|
|
|
|
2019-04-07 14:18:11 +00:00
|
|
|
import com.google.gson.JsonArray;
|
|
|
|
import com.google.gson.JsonObject;
|
2019-04-08 10:24:12 +00:00
|
|
|
import org.apache.commons.io.FileUtils;
|
2020-12-24 20:58:30 +00:00
|
|
|
import org.gradle.api.Project;
|
|
|
|
import org.gradle.api.Task;
|
2020-07-26 20:08:48 +00:00
|
|
|
import org.gradle.api.artifacts.Configuration;
|
|
|
|
import org.gradle.api.artifacts.Dependency;
|
2020-12-24 20:58:30 +00:00
|
|
|
import org.gradle.api.artifacts.DependencySet;
|
2020-07-26 20:08:48 +00:00
|
|
|
import org.gradle.api.artifacts.ProjectDependency;
|
|
|
|
import org.gradle.api.artifacts.ResolvedArtifact;
|
2020-12-24 20:58:30 +00:00
|
|
|
import org.gradle.api.artifacts.ResolvedConfiguration;
|
|
|
|
import org.gradle.api.artifacts.ResolvedDependency;
|
|
|
|
import org.gradle.api.tasks.bundling.AbstractArchiveTask;
|
2019-11-02 20:23:27 +00:00
|
|
|
import org.zeroturnaround.zip.FileSource;
|
|
|
|
import org.zeroturnaround.zip.ZipEntrySource;
|
|
|
|
import org.zeroturnaround.zip.ZipUtil;
|
|
|
|
import org.zeroturnaround.zip.transform.StringZipEntryTransformer;
|
|
|
|
import org.zeroturnaround.zip.transform.ZipEntryTransformerEntry;
|
2019-04-07 14:18:11 +00:00
|
|
|
|
2019-11-02 20:23:27 +00:00
|
|
|
import net.fabricmc.loom.LoomGradleExtension;
|
2020-12-27 16:25:30 +00:00
|
|
|
import net.fabricmc.loom.LoomGradlePlugin;
|
2019-11-02 20:23:27 +00:00
|
|
|
import net.fabricmc.loom.task.RemapJarTask;
|
2020-12-24 20:58:30 +00:00
|
|
|
import net.fabricmc.loom.util.Constants;
|
2019-04-07 14:18:11 +00:00
|
|
|
|
|
|
|
public class NestedJars {
|
2019-05-10 22:53:50 +00:00
|
|
|
public static boolean addNestedJars(Project project, Path modJarPath) {
|
2020-01-24 21:59:59 +00:00
|
|
|
List<File> containedJars = getContainedJars(project);
|
|
|
|
|
|
|
|
if (containedJars.isEmpty()) {
|
2019-04-07 14:18:11 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-05-10 22:53:50 +00:00
|
|
|
File modJar = modJarPath.toFile();
|
|
|
|
|
2020-01-24 21:59:59 +00:00
|
|
|
ZipUtil.addOrReplaceEntries(modJar, containedJars.stream().map(file -> new FileSource("META-INF/jars/" + file.getName(), file)).toArray(ZipEntrySource[]::new));
|
2019-04-07 14:18:11 +00:00
|
|
|
|
|
|
|
return ZipUtil.transformEntries(modJar, single(new ZipEntryTransformerEntry("fabric.mod.json", new StringZipEntryTransformer() {
|
|
|
|
@Override
|
2020-07-26 20:32:10 +00:00
|
|
|
protected String transform(ZipEntry zipEntry, String input) {
|
2020-12-27 16:25:30 +00:00
|
|
|
JsonObject json = LoomGradlePlugin.GSON.fromJson(input, JsonObject.class);
|
2019-04-07 14:18:11 +00:00
|
|
|
JsonArray nestedJars = json.getAsJsonArray("jars");
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-04-07 14:18:11 +00:00
|
|
|
if (nestedJars == null || !json.has("jars")) {
|
|
|
|
nestedJars = new JsonArray();
|
|
|
|
}
|
|
|
|
|
2020-01-24 21:59:59 +00:00
|
|
|
for (File file : containedJars) {
|
2019-04-07 14:18:11 +00:00
|
|
|
JsonObject jsonObject = new JsonObject();
|
|
|
|
jsonObject.addProperty("file", "META-INF/jars/" + file.getName());
|
|
|
|
nestedJars.add(jsonObject);
|
|
|
|
}
|
|
|
|
|
|
|
|
json.add("jars", nestedJars);
|
|
|
|
|
2020-12-27 16:25:30 +00:00
|
|
|
return LoomGradlePlugin.GSON.toJson(json);
|
2019-04-07 14:18:11 +00:00
|
|
|
}
|
|
|
|
})));
|
|
|
|
}
|
|
|
|
|
|
|
|
private static List<File> getContainedJars(Project project) {
|
|
|
|
List<File> fileList = new ArrayList<>();
|
|
|
|
|
2020-09-21 19:22:31 +00:00
|
|
|
Configuration configuration = project.getConfigurations().getByName(Constants.Configurations.INCLUDE);
|
2020-09-04 18:12:04 +00:00
|
|
|
ResolvedConfiguration resolvedConfiguration = configuration.getResolvedConfiguration();
|
|
|
|
Set<ResolvedDependency> dependencies = resolvedConfiguration.getFirstLevelModuleDependencies();
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2020-09-04 18:12:04 +00:00
|
|
|
// Bit ugly doing this, id guess there is a better way but this works.
|
|
|
|
Set<String> projectDeps = new HashSet<>();
|
|
|
|
|
|
|
|
for (Dependency dependency : configuration.getDependencies()) {
|
2019-04-07 14:18:11 +00:00
|
|
|
if (dependency instanceof ProjectDependency) {
|
|
|
|
ProjectDependency projectDependency = (ProjectDependency) dependency;
|
|
|
|
Project dependencyProject = projectDependency.getDependencyProject();
|
|
|
|
|
2020-09-04 18:12:04 +00:00
|
|
|
projectDeps.add(dependency.getGroup() + ":" + dependency.getName() + ":" + dependency.getVersion());
|
|
|
|
|
2020-07-26 20:32:10 +00:00
|
|
|
// TODO change this to allow just normal jar tasks, so a project can have a none loom sub project
|
2019-05-10 22:53:50 +00:00
|
|
|
Collection<Task> remapJarTasks = dependencyProject.getTasksByName("remapJar", false);
|
|
|
|
Collection<Task> jarTasks = dependencyProject.getTasksByName("jar", false);
|
|
|
|
|
|
|
|
for (Task task : remapJarTasks.isEmpty() ? jarTasks : remapJarTasks) {
|
|
|
|
if (task instanceof RemapJarTask) {
|
2021-02-02 23:03:57 +00:00
|
|
|
fileList.addAll(prepareForNesting(
|
|
|
|
Collections.singleton(((RemapJarTask) task).getArchivePath()),
|
|
|
|
projectDependency,
|
|
|
|
new ProjectDependencyMetaExtractor(),
|
|
|
|
project
|
|
|
|
));
|
2019-05-10 22:53:50 +00:00
|
|
|
} else if (task instanceof AbstractArchiveTask) {
|
2021-02-02 23:03:57 +00:00
|
|
|
fileList.addAll(prepareForNesting(
|
|
|
|
Collections.singleton(((AbstractArchiveTask) task).getArchivePath()),
|
|
|
|
projectDependency,
|
|
|
|
new ProjectDependencyMetaExtractor(),
|
|
|
|
project
|
|
|
|
));
|
2019-04-07 14:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-04 18:12:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (ResolvedDependency dependency : dependencies) {
|
|
|
|
if (projectDeps.contains(dependency.getModuleGroup() + ":" + dependency.getModuleName() + ":" + dependency.getModuleVersion())) {
|
|
|
|
continue;
|
2019-04-07 14:18:11 +00:00
|
|
|
} else {
|
2020-07-26 20:08:48 +00:00
|
|
|
fileList.addAll(prepareForNesting(
|
|
|
|
dependency
|
|
|
|
.getModuleArtifacts()
|
|
|
|
.stream()
|
|
|
|
.map(ResolvedArtifact::getFile)
|
|
|
|
.collect(Collectors.toSet()),
|
2021-02-02 23:03:57 +00:00
|
|
|
dependency,
|
|
|
|
new ResolvedDependencyMetaExtractor(),
|
|
|
|
project
|
|
|
|
));
|
2019-04-07 14:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-04-07 14:18:11 +00:00
|
|
|
for (File file : fileList) {
|
|
|
|
if (!file.exists()) {
|
|
|
|
throw new RuntimeException("Failed to include nested jars, as it could not be found @ " + file.getAbsolutePath());
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-04-07 14:18:11 +00:00
|
|
|
if (file.isDirectory() || !file.getName().endsWith(".jar")) {
|
|
|
|
throw new RuntimeException("Failed to include nested jars, as file was not a jar: " + file.getAbsolutePath());
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-04-07 14:18:11 +00:00
|
|
|
return fileList;
|
|
|
|
}
|
|
|
|
|
2020-07-26 20:32:10 +00:00
|
|
|
// Looks for any deps that require a sub project to be built first
|
2019-11-02 20:23:27 +00:00
|
|
|
public static List<RemapJarTask> getRequiredTasks(Project project) {
|
2019-05-10 22:53:50 +00:00
|
|
|
List<RemapJarTask> remapTasks = new ArrayList<>();
|
2019-04-07 19:38:10 +00:00
|
|
|
|
2020-09-21 19:22:31 +00:00
|
|
|
Configuration configuration = project.getConfigurations().getByName(Constants.Configurations.INCLUDE);
|
2019-04-07 19:38:10 +00:00
|
|
|
DependencySet dependencies = configuration.getDependencies();
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-04-07 19:38:10 +00:00
|
|
|
for (Dependency dependency : dependencies) {
|
|
|
|
if (dependency instanceof ProjectDependency) {
|
|
|
|
ProjectDependency projectDependency = (ProjectDependency) dependency;
|
|
|
|
Project dependencyProject = projectDependency.getDependencyProject();
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-04-07 19:38:10 +00:00
|
|
|
for (Task task : dependencyProject.getTasksByName("remapJar", false)) {
|
2019-05-10 22:53:50 +00:00
|
|
|
if (task instanceof RemapJarTask) {
|
|
|
|
remapTasks.add((RemapJarTask) task);
|
2019-04-07 19:38:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-04-07 19:38:10 +00:00
|
|
|
return remapTasks;
|
|
|
|
}
|
|
|
|
|
2019-04-08 10:24:12 +00:00
|
|
|
//This is a good place to do pre-nesting operations, such as adding a fabric.mod.json to a library
|
2021-02-02 23:03:57 +00:00
|
|
|
private static <D> List<File> prepareForNesting(Set<File> files, D dependency, DependencyMetaExtractor<D> metaExtractor, Project project) {
|
2019-04-08 10:24:12 +00:00
|
|
|
List<File> fileList = new ArrayList<>();
|
2019-11-02 20:23:27 +00:00
|
|
|
|
|
|
|
for (File file : files) {
|
2019-04-08 10:24:12 +00:00
|
|
|
//A lib that doesnt have a mod.json, we turn it into a fake mod
|
2019-11-02 20:23:27 +00:00
|
|
|
if (!ZipUtil.containsEntry(file, "fabric.mod.json")) {
|
2019-04-08 10:24:12 +00:00
|
|
|
LoomGradleExtension extension = project.getExtensions().getByType(LoomGradleExtension.class);
|
|
|
|
File tempDir = new File(extension.getUserCache(), "temp/modprocessing");
|
2019-11-02 20:23:27 +00:00
|
|
|
|
|
|
|
if (!tempDir.exists()) {
|
2019-04-08 10:24:12 +00:00
|
|
|
tempDir.mkdirs();
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-04-08 10:24:12 +00:00
|
|
|
File tempFile = new File(tempDir, file.getName());
|
2019-11-02 20:23:27 +00:00
|
|
|
|
|
|
|
if (tempFile.exists()) {
|
2019-04-08 10:24:12 +00:00
|
|
|
tempFile.delete();
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-04-08 10:24:12 +00:00
|
|
|
try {
|
|
|
|
FileUtils.copyFile(file, tempFile);
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException("Failed to copy file", e);
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2021-02-02 23:03:57 +00:00
|
|
|
ZipUtil.addEntry(tempFile, "fabric.mod.json", getMod(dependency, metaExtractor).getBytes());
|
2019-04-08 10:24:12 +00:00
|
|
|
fileList.add(tempFile);
|
|
|
|
} else {
|
2020-07-26 20:32:10 +00:00
|
|
|
// Default copy the jar right in
|
2019-04-08 10:24:12 +00:00
|
|
|
fileList.add(file);
|
|
|
|
}
|
|
|
|
}
|
2019-11-02 20:23:27 +00:00
|
|
|
|
2019-04-08 10:24:12 +00:00
|
|
|
return fileList;
|
|
|
|
}
|
|
|
|
|
2020-07-26 20:32:10 +00:00
|
|
|
// Generates a barebones mod for a dependency
|
2021-02-02 23:03:57 +00:00
|
|
|
private static <D> String getMod(D dependency, DependencyMetaExtractor<D> metaExtractor) {
|
2019-04-08 10:24:12 +00:00
|
|
|
JsonObject jsonObject = new JsonObject();
|
|
|
|
jsonObject.addProperty("schemaVersion", 1);
|
2021-02-02 23:03:57 +00:00
|
|
|
jsonObject.addProperty("id", (metaExtractor.group(dependency) + "_" + metaExtractor.name(dependency)).replaceAll("\\.", "_").toLowerCase(Locale.ENGLISH));
|
|
|
|
jsonObject.addProperty("version", metaExtractor.version(dependency));
|
|
|
|
jsonObject.addProperty("name", metaExtractor.name(dependency));
|
2019-04-08 10:24:12 +00:00
|
|
|
|
2020-10-08 19:42:07 +00:00
|
|
|
JsonObject custom = new JsonObject();
|
|
|
|
custom.addProperty("fabric-loom:generated", true);
|
|
|
|
jsonObject.add("custom", custom);
|
|
|
|
|
2020-12-27 16:25:30 +00:00
|
|
|
return LoomGradlePlugin.GSON.toJson(jsonObject);
|
2019-04-08 10:24:12 +00:00
|
|
|
}
|
|
|
|
|
2019-04-07 14:18:11 +00:00
|
|
|
private static ZipEntryTransformerEntry[] single(ZipEntryTransformerEntry element) {
|
|
|
|
return new ZipEntryTransformerEntry[]{element};
|
|
|
|
}
|
2021-02-02 23:03:57 +00:00
|
|
|
|
|
|
|
private interface DependencyMetaExtractor<D> {
|
|
|
|
String group(D dependency);
|
|
|
|
|
|
|
|
String version(D dependency);
|
|
|
|
|
|
|
|
String name(D dependency);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final class ProjectDependencyMetaExtractor implements DependencyMetaExtractor<ProjectDependency> {
|
|
|
|
@Override
|
|
|
|
public String group(ProjectDependency dependency) {
|
|
|
|
return dependency.getGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String version(ProjectDependency dependency) {
|
|
|
|
return dependency.getVersion();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String name(ProjectDependency dependency) {
|
|
|
|
return dependency.getName();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final class ResolvedDependencyMetaExtractor implements DependencyMetaExtractor<ResolvedDependency> {
|
|
|
|
@Override
|
|
|
|
public String group(ResolvedDependency dependency) {
|
|
|
|
return dependency.getModuleGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String version(ResolvedDependency dependency) {
|
|
|
|
return dependency.getModuleVersion();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String name(ResolvedDependency dependency) {
|
|
|
|
return dependency.getModuleName();
|
|
|
|
}
|
|
|
|
}
|
2019-04-07 14:18:11 +00:00
|
|
|
}
|