Fix crash with Gradle 7.7 nightly's in SourceSetHelper

This commit is contained in:
modmuss50
2022-08-28 00:09:22 +01:00
parent 8eb16963f3
commit a8c2bb1c64
2 changed files with 53 additions and 11 deletions

View File

@@ -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<? extends DefaultSourceSetOutput> 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<? /*DirectoryContribution*/> 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<File> getClasspath(ModSettings modSettings, Project project) {
final List<File> files = new ArrayList<>();

View File

@@ -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()