From 6a77b5deaaeb91f90a98f3cc3709a56b4dc6bd5d Mon Sep 17 00:00:00 2001 From: Adrian Siekierka Date: Sun, 30 Dec 2018 12:03:25 +0100 Subject: [PATCH] gradle 5.0 support, fix #38 --- .../net/fabricmc/loom/AbstractPlugin.java | 7 +- .../fabricmc/loom/LoomGradleExtension.java | 87 ++++++++++++++++--- .../loom/mixin/MixinServiceGradle.java | 3 +- .../net/fabricmc/loom/util/ModRemapper.java | 3 +- .../net/fabricmc/loom/util/RunConfig.java | 2 +- 5 files changed, 86 insertions(+), 16 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/AbstractPlugin.java b/src/main/java/net/fabricmc/loom/AbstractPlugin.java index 08dc42f..8c2fd9a 100644 --- a/src/main/java/net/fabricmc/loom/AbstractPlugin.java +++ b/src/main/java/net/fabricmc/loom/AbstractPlugin.java @@ -38,6 +38,7 @@ import org.gradle.api.Project; import org.gradle.api.Task; import org.gradle.api.UnknownTaskException; import org.gradle.api.artifacts.Configuration; +import org.gradle.api.artifacts.dsl.DependencyHandler; import org.gradle.api.artifacts.repositories.MavenArtifactRepository; import org.gradle.api.plugins.JavaPlugin; import org.gradle.api.plugins.JavaPluginConvention; @@ -235,8 +236,12 @@ public class AbstractPlugin implements Plugin { SetupIntelijRunConfigs.setup(project1); } + // add dependencies for mixin annotation processor + DependencyHandler handler = project1.getDependencies(); + handler.add("annotationProcessor", "net.fabricmc:sponge-mixin:" + extension.getMixinVersion()); + handler.add("annotationProcessor", "net.fabricmc:fabric-loom:" + extension.getLoomVersion()); - //Enables the default mod remapper + // Enables the default mod remapper if (extension.remapMod) { AbstractArchiveTask jarTask = (AbstractArchiveTask) project1.getTasks().getByName("jar"); diff --git a/src/main/java/net/fabricmc/loom/LoomGradleExtension.java b/src/main/java/net/fabricmc/loom/LoomGradleExtension.java index df96fbb..345644d 100644 --- a/src/main/java/net/fabricmc/loom/LoomGradleExtension.java +++ b/src/main/java/net/fabricmc/loom/LoomGradleExtension.java @@ -31,14 +31,14 @@ import net.fabricmc.loom.providers.MinecraftProvider; import net.fabricmc.loom.util.LoomDependencyManager; import org.cadixdev.lorenz.MappingSet; import org.gradle.api.Project; +import org.gradle.api.artifacts.Configuration; +import org.gradle.api.artifacts.ConfigurationContainer; import org.gradle.api.artifacts.Dependency; import javax.annotation.Nullable; import java.io.File; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Objects; +import java.util.*; +import java.util.function.BiPredicate; import java.util.function.Supplier; public class LoomGradleExtension { @@ -109,18 +109,85 @@ public class LoomGradleExtension { } @Nullable - public String getMixinVersion() { - for (Dependency dependency : project.getConfigurations().getByName("compile").getDependencies()) { - if (dependency.getName().equalsIgnoreCase("mixin") && dependency.getGroup().equals("org.spongepowered")) { - return dependency.getVersion(); + private Dependency findDependency(Collection configs, BiPredicate groupNameFilter) { + for (String s : configs) { + for (Dependency dependency : project.getConfigurations().getByName(s).getDependencies()) { + if (groupNameFilter.test(dependency.getGroup(), dependency.getName())) { + return dependency; + } + } + } + + return null; + } + + @Nullable + private Dependency findBuildscriptDependency(BiPredicate groupNameFilter) { + for (Configuration config : project.getBuildscript().getConfigurations().getAsMap().values()) { + for (Dependency dependency : config.getDependencies()) { + if (groupNameFilter.test(dependency.getGroup(), dependency.getName())) { + return dependency; + } + } + } + + return null; + } + + @Nullable + public String getLoomVersion() { + Dependency dependency = findBuildscriptDependency((group, name) -> { + if (name.equalsIgnoreCase("fabric-loom")) { + return group.equalsIgnoreCase("net.fabricmc"); } - if (dependency.getName().equals("sponge-mixin") && dependency.getGroup().equals("net.fabricmc")) { + if (name.equalsIgnoreCase("fabric-loom.gradle.plugin")) { + return group.equalsIgnoreCase("fabric-loom"); + } + + return false; + }); + + return dependency != null ? dependency.getVersion() : null; + } + + @Nullable + private Dependency getMixinDependency() { + return findDependency(Collections.singletonList("compile"), (group, name) -> { + if (name.equalsIgnoreCase("mixin") && group.equalsIgnoreCase("org.spongepowered")) { + return true; + } + + if (name.equalsIgnoreCase("sponge-mixin") && group.equalsIgnoreCase("net.fabricmc")) { + return true; + } + + return false; + }); + } + + @Nullable + public String getMixinVersion() { + Dependency dependency = getMixinDependency(); + if (dependency != null) { + return dependency.getVersion(); + } else { + return null; + } + } + + @Nullable + public String getMixinJsonVersion() { + Dependency dependency = getMixinDependency(); + + if (dependency != null) { + if (dependency.getGroup().equalsIgnoreCase("net.fabricmc")) { if (Objects.requireNonNull(dependency.getVersion()).split("\\.").length >= 4) { return dependency.getVersion().substring(0, dependency.getVersion().lastIndexOf('.')) + "-SNAPSHOT"; } - return dependency.getVersion(); } + + return dependency.getVersion(); } return null; diff --git a/src/main/java/net/fabricmc/loom/mixin/MixinServiceGradle.java b/src/main/java/net/fabricmc/loom/mixin/MixinServiceGradle.java index a44bfcf..ea5a814 100644 --- a/src/main/java/net/fabricmc/loom/mixin/MixinServiceGradle.java +++ b/src/main/java/net/fabricmc/loom/mixin/MixinServiceGradle.java @@ -25,7 +25,6 @@ package net.fabricmc.loom.mixin; import com.google.common.io.ByteStreams; -import com.strobel.collections.ImmutableList; import org.spongepowered.asm.lib.ClassReader; import org.spongepowered.asm.lib.tree.ClassNode; import org.spongepowered.asm.mixin.MixinEnvironment; @@ -146,7 +145,7 @@ public class MixinServiceGradle implements IClassBytecodeProvider, IClassProvide @Override public Collection getPlatformAgents() { - return ImmutableList.of("org.spongepowered.asm.launch.platform.MixinPlatformAgentDefault"); + return Collections.singletonList("org.spongepowered.asm.launch.platform.MixinPlatformAgentDefault"); } public byte[] getClassBytes(String name, String transformedName) throws IOException { diff --git a/src/main/java/net/fabricmc/loom/util/ModRemapper.java b/src/main/java/net/fabricmc/loom/util/ModRemapper.java index cf383b4..3c2fcee 100644 --- a/src/main/java/net/fabricmc/loom/util/ModRemapper.java +++ b/src/main/java/net/fabricmc/loom/util/ModRemapper.java @@ -32,7 +32,6 @@ import net.fabricmc.tinyremapper.OutputConsumerPath; import net.fabricmc.tinyremapper.TinyRemapper; import net.fabricmc.tinyremapper.TinyUtils; import org.gradle.api.Project; -import org.gradle.api.artifacts.Dependency; import java.io.File; import java.io.IOException; @@ -103,7 +102,7 @@ public class ModRemapper { } if (extension.refmapName != null && extension.refmapName.length() > 0) { - if (MixinRefmapHelper.addRefmapName(extension.refmapName, extension.getMixinVersion(), modJarOutput)) { + if (MixinRefmapHelper.addRefmapName(extension.refmapName, extension.getMixinJsonVersion(), modJarOutput)) { project.getLogger().debug("Transformed mixin reference maps in output JAR!"); } } diff --git a/src/main/java/net/fabricmc/loom/util/RunConfig.java b/src/main/java/net/fabricmc/loom/util/RunConfig.java index 6c0209e..725dd1c 100644 --- a/src/main/java/net/fabricmc/loom/util/RunConfig.java +++ b/src/main/java/net/fabricmc/loom/util/RunConfig.java @@ -25,10 +25,10 @@ package net.fabricmc.loom.util; import com.google.common.base.Strings; +import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.gson.JsonElement; import com.google.gson.JsonObject; -import com.strobel.collections.ImmutableList; import net.fabricmc.loom.LoomGradleExtension; import net.fabricmc.loom.providers.MinecraftProvider; import org.apache.commons.io.IOUtils;