From 3e0c999ad8eab06070e9481aa824229084f61e20 Mon Sep 17 00:00:00 2001 From: Marc Hermans Date: Sat, 18 Jan 2025 22:50:39 +0100 Subject: [PATCH] Fix an issue with how additional classpath inputs of the recompile task are not being deleted. (#262) --- .../extensions/NeoFormRuntimeExtension.java | 11 ++++--- .../runtime/tasks/RecompileSourceJar.java | 29 ++++++++++--------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/extensions/NeoFormRuntimeExtension.java b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/extensions/NeoFormRuntimeExtension.java index 2b79b0b5..558e5943 100644 --- a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/extensions/NeoFormRuntimeExtension.java +++ b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/extensions/NeoFormRuntimeExtension.java @@ -36,6 +36,7 @@ import net.neoforged.gradle.neoform.util.NeoFormRuntimeUtils; import net.neoforged.gradle.util.TransformerUtils; import org.apache.commons.lang3.StringUtils; +import org.apache.tools.ant.taskdefs.Unpack; import org.gradle.api.GradleException; import org.gradle.api.Project; import org.gradle.api.artifacts.Configuration; @@ -439,16 +440,18 @@ protected void bakeDefinition(NeoFormRuntimeDefinition definition) { extractionSource.flatMap(WithOutput::getOutput) .map(sourceJar -> task.getArchiveOperations().zipTree(sourceJar).matching(sp -> sp.include("**/*.java")).getAsFileTree()) ); - task.getInput().from( - definition.getAdditionalCompileSources() - ); }); unpackSources.configure(neoFormRuntimeTask -> configureMcpRuntimeTaskWithDefaults(spec, neoFormDirectory, symbolicDataSources, neoFormRuntimeTask)); + final TaskProvider unpackAdditionalSources = spec.getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(spec, "unzipAdditionalSources"), UnpackZip.class, task -> { + task.getInput().from(definition.getAdditionalCompileSources()); + }); + unpackAdditionalSources.configure(neoFormRuntimeTask -> configureMcpRuntimeTaskWithDefaults(spec, neoFormDirectory, symbolicDataSources, neoFormRuntimeTask)); + final TaskProvider recompileTask = spec.getProject() .getTasks().register(CommonRuntimeUtils.buildTaskName(spec, "recompile"), RecompileSourceJar.class, task -> { task.setSource(unpackSources.flatMap(UnpackZip::getUnpackingTarget)); - task.getAdditionalInputFiles().from(definition.getAdditionalCompileSources()); + task.getAdditionalInputFileRoot().set(unpackAdditionalSources.flatMap(UnpackZip::getUnpackingTarget)); task.getCompileClasspath().setFrom(recompileDependencies); task.getStepName().set("recompile"); diff --git a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/RecompileSourceJar.java b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/RecompileSourceJar.java index c7b760ad..8cacb107 100644 --- a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/RecompileSourceJar.java +++ b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/RecompileSourceJar.java @@ -9,7 +9,10 @@ import net.neoforged.gradle.dsl.common.runtime.tasks.RuntimeMultiArguments; import org.gradle.api.GradleException; import org.gradle.api.file.ConfigurableFileCollection; +import org.gradle.api.file.DirectoryProperty; import org.gradle.api.file.FileTree; +import org.gradle.api.file.FileVisitDetails; +import org.gradle.api.file.FileVisitor; import org.gradle.api.model.ObjectFactory; import org.gradle.api.plugins.JavaPluginExtension; import org.gradle.api.provider.Property; @@ -82,6 +85,7 @@ public RecompileSourceJar() { getOptions().setFork(true); getOptions().setIncremental(true); getOptions().getIncrementalAfterFailure().set(true); + getOptions().setSourcepath(getProject().files(getAdditionalInputFileRoot())); } @Override @@ -133,7 +137,7 @@ public Property getJavaVersion() { @InputFiles @PathSensitive(PathSensitivity.NONE) - public abstract ConfigurableFileCollection getAdditionalInputFiles(); + public abstract DirectoryProperty getAdditionalInputFileRoot(); @Override protected void compile(InputChanges inputs) { @@ -155,14 +159,13 @@ protected void compile(InputChanges inputs) { private void doCachedCompile(InputChanges inputs) { super.compile(inputs); + final FileTree output = this.getDestinationDirectory().getAsFileTree(); - final FileTree outputTree = getDestinationDirectory().get().getAsFileTree(); - outputTree.matching(pattern -> pattern.include(fileTreeElement -> { - final String relativePath = fileTreeElement.getRelativePath().getPathString(); - if (!relativePath.endsWith(".class")) { - return false; - } + output.visit(details -> { + if (details.isDirectory()) + return; + final String relativePath = details.getRelativePath().getPathString(); final String sourceFilePath; if (!relativePath.contains("$")) { sourceFilePath = relativePath.substring(0, relativePath.length() - ".class".length()) + ".java"; @@ -170,13 +173,11 @@ private void doCachedCompile(InputChanges inputs) { sourceFilePath = relativePath.substring(0, relativePath.indexOf('$')) + ".java"; } - return !getAdditionalInputFiles() - .getAsFileTree() - .matching(sp1 -> sp1.include(sourceFilePath)) - .getFiles().isEmpty(); - })).forEach(file -> { - getLogger().debug("Removing additional source file: {}", file); - file.delete(); + if (!getAdditionalInputFileRoot().getAsFileTree().matching(pattern -> pattern.include(sourceFilePath)) + .isEmpty()) { + getLogger().debug("Deleting additional input file."); + details.getFile().delete(); + } }); } }