From 02e9616622abed6b266ced329b86d419ac31ee88 Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Sun, 7 Apr 2019 15:18:11 +0100 Subject: [PATCH] Start on 0.2.1 --- build.gradle | 2 +- .../net/fabricmc/loom/AbstractPlugin.java | 15 ++- .../net/fabricmc/loom/util/Constants.java | 1 + .../net/fabricmc/loom/util/ModRemapper.java | 4 + .../net/fabricmc/loom/util/NestedJars.java | 118 ++++++++++++++++++ 5 files changed, 134 insertions(+), 6 deletions(-) create mode 100644 src/main/java/net/fabricmc/loom/util/NestedJars.java diff --git a/build.gradle b/build.gradle index 26f409d..27cda46 100644 --- a/build.gradle +++ b/build.gradle @@ -11,7 +11,7 @@ targetCompatibility = 1.8 group = 'net.fabricmc' archivesBaseName = project.name -version = '0.2.0-SNAPSHOT' +version = '0.2.1-SNAPSHOT' def build = "local" def ENV = System.getenv() diff --git a/src/main/java/net/fabricmc/loom/AbstractPlugin.java b/src/main/java/net/fabricmc/loom/AbstractPlugin.java index 8463185..1d1465f 100644 --- a/src/main/java/net/fabricmc/loom/AbstractPlugin.java +++ b/src/main/java/net/fabricmc/loom/AbstractPlugin.java @@ -33,10 +33,7 @@ import net.fabricmc.loom.task.RemapSourcesJar; import net.fabricmc.loom.util.Constants; import net.fabricmc.loom.util.LoomDependencyManager; import net.fabricmc.loom.util.SetupIntelijRunConfigs; -import org.gradle.api.Plugin; -import org.gradle.api.Project; -import org.gradle.api.Task; -import org.gradle.api.UnknownTaskException; +import org.gradle.api.*; import org.gradle.api.artifacts.Configuration; import org.gradle.api.artifacts.dsl.DependencyHandler; import org.gradle.api.artifacts.repositories.MavenArtifactRepository; @@ -65,6 +62,9 @@ public class AbstractPlugin implements Plugin { public void apply(Project target) { this.project = target; + //Done to ensure the child projects are built before the root project + this.project.evaluationDependsOnChildren(); + if(isRootProject(target)){ project.getLogger().lifecycle("Fabric Loom: " + AbstractPlugin.class.getPackage().getImplementationVersion()); } @@ -93,6 +93,9 @@ public class AbstractPlugin implements Plugin { Configuration minecraftConfig = project.getConfigurations().maybeCreate(Constants.MINECRAFT); minecraftConfig.setTransitive(false); + Configuration includeConfig = project.getConfigurations().maybeCreate(Constants.INCLUDE); + includeConfig.setTransitive(false); // Dont get transitive deps + project.getConfigurations().maybeCreate(Constants.MAPPINGS); configureIDEs(); @@ -247,7 +250,7 @@ public class AbstractPlugin implements Plugin { project1.getTasks().getByName("idea").finalizedBy(project1.getTasks().getByName("genIdeaWorkspace")); project1.getTasks().getByName("eclipse").finalizedBy(project1.getTasks().getByName("genEclipseRuns")); - if(extension.autoGenIDERuns){ + if(extension.autoGenIDERuns && isRootProject(project1)){ SetupIntelijRunConfigs.setup(project1); } @@ -265,6 +268,8 @@ public class AbstractPlugin implements Plugin { remapJarTask.doLast(task -> project1.getArtifacts().add("archives", remapJarTask.jar)); remapJarTask.dependsOn(project1.getTasks().getByName("jar")); project1.getTasks().getByName("build").dependsOn(remapJarTask); + //Run all the sub project remap jars tasks before the root projects jar, this is to allow us to include projects + project1.subprojects(subProject -> remapJarTask.dependsOn(subProject.getTasksByName("remapJar", false))); try { AbstractArchiveTask sourcesTask = (AbstractArchiveTask) project1.getTasks().getByName("sourcesJar"); diff --git a/src/main/java/net/fabricmc/loom/util/Constants.java b/src/main/java/net/fabricmc/loom/util/Constants.java index e2765f6..948a57e 100644 --- a/src/main/java/net/fabricmc/loom/util/Constants.java +++ b/src/main/java/net/fabricmc/loom/util/Constants.java @@ -37,6 +37,7 @@ public class Constants { public static final String COMPILE_MODS = "modCompile"; public static final String COMPILE_MODS_MAPPED = "modCompileMapped"; + public static final String INCLUDE = "include"; public static final String MINECRAFT = "minecraft"; public static final String MINECRAFT_DEPENDENCIES = "minecraftLibraries"; public static final String MINECRAFT_INTERMEDIARY = "minecraftIntermediary"; diff --git a/src/main/java/net/fabricmc/loom/util/ModRemapper.java b/src/main/java/net/fabricmc/loom/util/ModRemapper.java index 30943ea..ddbaaed 100644 --- a/src/main/java/net/fabricmc/loom/util/ModRemapper.java +++ b/src/main/java/net/fabricmc/loom/util/ModRemapper.java @@ -117,6 +117,10 @@ public class ModRemapper { project.getLogger().debug("Transformed mixin reference maps in output JAR!"); } + if (NestedJars.addNestedJars(project, modJarOutput)) { + project.getLogger().debug("Added nested jar paths to mod json"); + } + try { if (modJar.exists()) { Files.move(modJar, modJarUnmappedCopy); diff --git a/src/main/java/net/fabricmc/loom/util/NestedJars.java b/src/main/java/net/fabricmc/loom/util/NestedJars.java new file mode 100644 index 0000000..e43f977 --- /dev/null +++ b/src/main/java/net/fabricmc/loom/util/NestedJars.java @@ -0,0 +1,118 @@ +/* + * 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. + */ + +package net.fabricmc.loom.util; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import net.fabricmc.loom.task.RemapJar; +import org.gradle.api.Project; +import org.gradle.api.Task; +import org.gradle.api.artifacts.Configuration; +import org.gradle.api.artifacts.Dependency; +import org.gradle.api.artifacts.DependencySet; +import org.gradle.api.artifacts.ProjectDependency; +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; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.zip.ZipEntry; + +public class NestedJars { + + private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create(); + + public static boolean addNestedJars(Project project, File modJar) { + if (getContainedJars(project).isEmpty()) { + return false; + } + + ZipUtil.addEntries(modJar, getContainedJars(project).stream().map(file -> new FileSource("META-INF/jars/" + file.getName(), file)).toArray(ZipEntrySource[]::new)); + + return ZipUtil.transformEntries(modJar, single(new ZipEntryTransformerEntry("fabric.mod.json", new StringZipEntryTransformer() { + @Override + protected String transform(ZipEntry zipEntry, String input) throws IOException { + JsonObject json = GSON.fromJson(input, JsonObject.class); + JsonArray nestedJars = json.getAsJsonArray("jars"); + if (nestedJars == null || !json.has("jars")) { + nestedJars = new JsonArray(); + } + + for (File file : getContainedJars(project)) { + JsonObject jsonObject = new JsonObject(); + jsonObject.addProperty("file", "META-INF/jars/" + file.getName()); + nestedJars.add(jsonObject); + } + + json.add("jars", nestedJars); + + return GSON.toJson(json); + } + }))); + } + + private static List getContainedJars(Project project) { + + List fileList = new ArrayList<>(); + + Configuration configuration = project.getConfigurations().getByName(Constants.INCLUDE); + DependencySet dependencies = configuration.getDependencies(); + for (Dependency dependency : dependencies) { + if (dependency instanceof ProjectDependency) { + ProjectDependency projectDependency = (ProjectDependency) dependency; + Project dependencyProject = projectDependency.getDependencyProject(); + + //TODO change this to allow just normal jar tasks, so a project can have a none loom sub project + for (Task task : dependencyProject.getTasksByName("remapJar", false)) { + if (task instanceof RemapJar) { + fileList.add(((RemapJar) task).jar); + } + } + } else { + fileList.addAll(configuration.files(dependency)); + } + } + for (File file : fileList) { + if (!file.exists()) { + throw new RuntimeException("Failed to include nested jars, as it could not be found @ " + file.getAbsolutePath()); + } + if (file.isDirectory() || !file.getName().endsWith(".jar")) { + throw new RuntimeException("Failed to include nested jars, as file was not a jar: " + file.getAbsolutePath()); + } + } + return fileList; + } + + private static ZipEntryTransformerEntry[] single(ZipEntryTransformerEntry element) { + return new ZipEntryTransformerEntry[]{element}; + } +}