From a8c2bb1c64edc1012b0dc1f57280fb1b810d562a Mon Sep 17 00:00:00 2001 From: modmuss50 Date: Sun, 28 Aug 2022 00:09:22 +0100 Subject: [PATCH] Fix crash with Gradle 7.7 nightly's in SourceSetHelper --- .../loom/util/gradle/SourceSetHelper.java | 62 ++++++++++++++++--- .../loom/test/LoomTestConstants.groovy | 2 +- 2 files changed, 53 insertions(+), 11 deletions(-) diff --git a/src/main/java/net/fabricmc/loom/util/gradle/SourceSetHelper.java b/src/main/java/net/fabricmc/loom/util/gradle/SourceSetHelper.java index 1ff7b21e..aec6bc63 100644 --- a/src/main/java/net/fabricmc/loom/util/gradle/SourceSetHelper.java +++ b/src/main/java/net/fabricmc/loom/util/gradle/SourceSetHelper.java @@ -28,6 +28,8 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.UncheckedIOException; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -92,16 +94,7 @@ public final class SourceSetHelper { */ public static Project getSourceSetProject(SourceSet sourceSet) { final DefaultSourceSetOutput sourceSetOutput = (DefaultSourceSetOutput) sourceSet.getOutput(); - final DefaultTaskDependency taskDependency = (DefaultTaskDependency) sourceSetOutput.getClassesContributors(); - Project project = null; - - for (Object object : taskDependency.getMutableValues()) { - if (object instanceof Task task) { - project = task.getProject(); - } else if (object instanceof TaskProvider provider) { - project = provider.get().getProject(); - } - } + final Project project = getProjectFromSourceSetOutput(sourceSetOutput); if (project == null) { throw new NullPointerException("Unable to determine owning project for SourceSet: " + sourceSet.getName()); @@ -110,6 +103,55 @@ public final class SourceSetHelper { return project; } + private static Project getProjectFromSourceSetOutput(SourceSetOutput sourceSetOutput) { + final Class clazz = DefaultSourceSetOutput.class; + + try { + final Method getClassesContributorsMethod = clazz.getMethod("getClassesContributors"); + final Object classesContributors = getClassesContributorsMethod.invoke(sourceSetOutput); + + if (classesContributors instanceof List list) { + // Gradle 7.7 + return getProjectFromDirectoryContributions(list); + } else if (classesContributors instanceof DefaultTaskDependency taskDependency) { + // Pre Gradle 7.7 + return getProjectFromTaskDependency(taskDependency); + } else { + throw new UnsupportedOperationException(); + } + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + // Pre Gradle 7.7 + private static Project getProjectFromTaskDependency(DefaultTaskDependency taskDependency) { + for (Object object : taskDependency.getMutableValues()) { + if (object instanceof Task task) { + return task.getProject(); + } else if (object instanceof TaskProvider provider) { + return provider.get().getProject(); + } + } + + return null; + } + + // Gradle 7.7: https://github.com/gradle/gradle/commit/2797942dc71f0e0e186b7d0c5ba3e09eceea4507#diff-b19ce8fbc4aa4ebaeea74e39609636d65e385bce6990fd42d68581dd829f29b3L153 + private static Project getProjectFromDirectoryContributions(List classesContributions) { + for (Object classesContribution : classesContributions) { + try { + final Method getTask = classesContribution.getClass().getMethod("getTask"); + final TaskProvider taskProvider = (TaskProvider) getTask.invoke(classesContribution); + return taskProvider.get().getProject(); + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException(e); + } + } + + return null; + } + public static List getClasspath(ModSettings modSettings, Project project) { final List files = new ArrayList<>(); diff --git a/src/test/groovy/net/fabricmc/loom/test/LoomTestConstants.groovy b/src/test/groovy/net/fabricmc/loom/test/LoomTestConstants.groovy index 5100f0f1..d7fbc079 100644 --- a/src/test/groovy/net/fabricmc/loom/test/LoomTestConstants.groovy +++ b/src/test/groovy/net/fabricmc/loom/test/LoomTestConstants.groovy @@ -27,7 +27,7 @@ package net.fabricmc.loom.test import org.gradle.util.GradleVersion class LoomTestConstants { - private final static String NIGHTLY_VERSION = "7.7-20220823221227+0000" + private final static String NIGHTLY_VERSION = "7.7-20220827221558+0000" private final static boolean NIGHTLY_EXISTS = nightlyExists(NIGHTLY_VERSION) public final static String DEFAULT_GRADLE = GradleVersion.current().getVersion()