diff --git a/common/src/main/java/net/neoforged/gradle/common/tasks/ArtifactFromOutput.java b/common/src/main/java/net/neoforged/gradle/common/tasks/ArtifactFromOutput.java index 80097ee6c..a204eea50 100644 --- a/common/src/main/java/net/neoforged/gradle/common/tasks/ArtifactFromOutput.java +++ b/common/src/main/java/net/neoforged/gradle/common/tasks/ArtifactFromOutput.java @@ -5,8 +5,9 @@ import org.apache.commons.io.FileUtils; import org.gradle.api.file.RegularFileProperty; import org.gradle.api.tasks.*; +import org.gradle.work.DisableCachingByDefault; -@CacheableTask +@DisableCachingByDefault(because = "a simple file-copy is not worthwhile to cache") public abstract class ArtifactFromOutput extends NeoGradleBase implements WithOutput { public ArtifactFromOutput() { 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 872cf636b..c4c00374e 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 @@ -82,7 +82,12 @@ private static void configureMcpRuntimeTaskWithDefaults(NeoFormRuntimeSpecificat @SuppressWarnings("OptionalUsedAsFieldOrParameterType") @Nullable - private static TaskProvider createBuiltIn(final NeoFormRuntimeSpecification spec, NeoFormConfigConfigurationSpecV2 neoFormConfigV2, NeoFormConfigConfigurationSpecV1.Step step, final Map> tasks, final Map> gameArtifactTaskProviders, final Optional> adaptedInput) { + private static TaskProvider createBuiltIn(final NeoFormRuntimeSpecification spec, + NeoFormConfigConfigurationSpecV2 neoFormConfigV2, + NeoFormConfigConfigurationSpecV1.Step step, + final Map> tasks, + final Map> gameArtifactTaskProviders, + final Optional> adaptedInput) { switch (step.getType()) { case "decompile": return createDecompile(spec, step, neoFormConfigV2); @@ -123,7 +128,15 @@ private static TaskProvider createBuiltIn(final NeoFormRun ); }); case "patch": - return spec.getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(spec, step.getName()), Patch.class, task -> task.getInput().fileProvider(NeoFormRuntimeUtils.getTaskInputFor(spec, tasks, step, task))); + return spec.getProject().getTasks().register( + CommonRuntimeUtils.buildTaskName(spec, step.getName()), + Patch.class, + task -> { + task.getInput().fileProvider(NeoFormRuntimeUtils.getTaskInputFor(spec, tasks, step, task)); + task.getPatchArtifact().set(spec.getNeoFormArtifact()); + task.getPatchDirectory().set(neoFormConfigV2.getData("patches", spec.getDistribution().getName())); + } + ); } if (neoFormConfigV2.getSpec() >= 2) { switch (step.getType()) { diff --git a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/Patch.java b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/Patch.java index 326ae7906..e1e581fc9 100644 --- a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/Patch.java +++ b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/Patch.java @@ -4,8 +4,10 @@ import codechicken.diffpatch.cli.PatchOperation; import codechicken.diffpatch.util.LoggingOutputStream; import codechicken.diffpatch.util.PatchMode; +import codechicken.diffpatch.util.archiver.ArchiveFormat; import net.neoforged.gradle.common.runtime.tasks.DefaultRuntime; -import org.gradle.api.file.DirectoryProperty; +import net.neoforged.gradle.dsl.common.util.Artifact; +import net.neoforged.gradle.dsl.common.util.ConfigurationUtils; import org.gradle.api.file.RegularFileProperty; import org.gradle.api.logging.LogLevel; import org.gradle.api.provider.Property; @@ -20,7 +22,6 @@ public abstract class Patch extends DefaultRuntime { public Patch() { super(); - getPatchDirectory().fileProvider(getRuntimeData().flatMap(data -> data.get("patches"))); getRejectsFile().fileProvider(getFileInOutputDirectory("rejects.zip")); getIsVerbose().convention(false); } @@ -31,11 +32,14 @@ public void run() throws Exception { final File output = ensureFileWorkspaceReady(getOutput()); final File rejects = getRejectsFile().get().getAsFile(); + // Resolve the input artifact + File inputArtifact = ConfigurationUtils.getArtifactProvider(getProject(), getPatchArtifact().map(Artifact::getDescriptor)).get(); + PatchOperation.Builder builder = PatchOperation.builder() .logTo(new LoggingOutputStream(getLogger(), LogLevel.LIFECYCLE)) .basePath(input.toPath()) - .patchesPath(getUnpackedMcpZipDirectory().get().getAsFile().toPath()) - .patchesPrefix(getUnpackedMcpZipDirectory().get().getAsFile().toPath().relativize(getPatchDirectory().get().getAsFile().toPath()).toString()) + .patchesPath(inputArtifact.toPath(), ArchiveFormat.ZIP) + .patchesPrefix(getPatchDirectory().get()) .outputPath(output.toPath()) .level(getIsVerbose().get() ? codechicken.diffpatch.util.LogLevel.ALL : codechicken.diffpatch.util.LogLevel.WARN) .mode(PatchMode.OFFSET) @@ -62,9 +66,11 @@ public void run() throws Exception { @PathSensitive(PathSensitivity.NONE) public abstract RegularFileProperty getInput(); - @InputDirectory - @PathSensitive(PathSensitivity.NONE) - public abstract DirectoryProperty getPatchDirectory(); + @Input + public abstract Property getPatchArtifact(); + + @Input + public abstract Property getPatchDirectory(); @OutputFile public abstract RegularFileProperty getRejectsFile(); 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 55d1d8763..2638e2e12 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 @@ -6,8 +6,6 @@ import net.neoforged.gradle.dsl.common.runtime.tasks.RuntimeMultiArguments; import net.neoforged.gradle.util.ZipBuildingFileTreeVisitor; import net.neoforged.gradle.dsl.common.runtime.tasks.Runtime; -import org.gradle.api.Action; -import org.gradle.api.Task; import org.gradle.api.file.ConfigurableFileCollection; import org.gradle.api.file.RegularFileProperty; import org.gradle.api.logging.LogLevel; @@ -25,10 +23,12 @@ import org.gradle.api.tasks.Optional; import org.gradle.api.tasks.PathSensitive; import org.gradle.api.tasks.PathSensitivity; +import org.gradle.api.tasks.TaskAction; import org.gradle.api.tasks.compile.JavaCompile; import org.gradle.jvm.toolchain.JavaLanguageVersion; import org.gradle.jvm.toolchain.JavaToolchainService; import org.gradle.jvm.toolchain.internal.CurrentJvmToolchainSpec; +import org.gradle.work.InputChanges; import java.io.File; import java.io.FileOutputStream; @@ -45,7 +45,7 @@ public abstract class RecompileSourceJar extends JavaCompile implements Runtime public RecompileSourceJar() { super(); - + arguments = getObjectFactory().newInstance(RuntimeArgumentsImpl.class, getProviderFactory()); multiArguments = getObjectFactory().newInstance(RuntimeMultiArgumentsImpl.class, getProviderFactory()); @@ -97,32 +97,30 @@ public RecompileSourceJar() { getOptions().setFork(true); getOptions().setIncremental(true); getOptions().getIncrementalAfterFailure().set(true); + getInputJar().finalizeValueOnRead(); + } - //Leave this as an anon class, so that gradle is aware of this. Lambdas can not be used during task tree analysis. - //noinspection Convert2Lambda - doLast(new Action() { - @Override - public void execute(Task doLast) { - try { - final File outputJar = RecompileSourceJar.this.ensureFileWorkspaceReady(RecompileSourceJar.this.getOutput()); - final FileOutputStream fileOutputStream = new FileOutputStream(outputJar); - final ZipOutputStream outputZipStream = new ZipOutputStream(fileOutputStream); - final ZipBuildingFileTreeVisitor zipBuildingFileTreeVisitor = new ZipBuildingFileTreeVisitor(outputZipStream); - //Add the compiled output. - RecompileSourceJar.this.getDestinationDirectory().getAsFileTree().visit(zipBuildingFileTreeVisitor); - //Add the original resources. - RecompileSourceJar.this.getArchiveOperations().zipTree(RecompileSourceJar.this.getInputJar()).matching(filter -> filter.exclude("**/*.java")).visit(zipBuildingFileTreeVisitor); - outputZipStream.close(); - fileOutputStream.close(); - } catch (IOException e) { - throw new RuntimeException("Failed to create recompiled output jar", e); - } + @Override + @TaskAction + protected void compile(InputChanges inputs) { + super.compile(inputs); + + if (getState().getDidWork()) { + final File outputJar = ensureFileWorkspaceReady(getOutput()); + try (FileOutputStream fileOutputStream = new FileOutputStream(outputJar); + ZipOutputStream outputZipStream = new ZipOutputStream(fileOutputStream)) { + ZipBuildingFileTreeVisitor zipBuildingFileTreeVisitor = new ZipBuildingFileTreeVisitor(outputZipStream); + //Add the compiled output. + getDestinationDirectory().getAsFileTree().visit(zipBuildingFileTreeVisitor); + //Add the original resources. + getArchiveOperations().zipTree(getInputJar()).matching(filter -> filter.exclude("**/*.java")).visit(zipBuildingFileTreeVisitor); + } catch (IOException e) { + throw new RuntimeException("Failed to create recompiled output jar", e); } - }); - - getInputJar().finalizeValueOnRead(); + } } + @Override public RuntimeArguments getArguments() { return arguments; diff --git a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/StripJar.java b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/StripJar.java index 7568e0fec..ad2dde548 100644 --- a/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/StripJar.java +++ b/neoform/src/main/java/net/neoforged/gradle/neoform/runtime/tasks/StripJar.java @@ -55,20 +55,21 @@ protected void run() throws Throwable { } private void strip(File input, File output, boolean whitelist) throws IOException { - JarInputStream is = new JarInputStream(new FileInputStream(input)); - JarOutputStream os = new JarOutputStream(new FileOutputStream(output)); - - // Ignore any entry that's not allowed - JarEntry entry; - while ((entry = is.getNextJarEntry()) != null) { - if (!isEntryValid(entry, whitelist)) continue; - os.putNextEntry(entry); - IOUtils.copyLarge(is, os); - os.closeEntry(); + try (JarInputStream is = new JarInputStream(new FileInputStream(input)); + FileOutputStream fout = new FileOutputStream(output); + JarOutputStream os = new JarOutputStream(fout)) { + + // Ignore any entry that's not allowed + JarEntry entry; + while ((entry = is.getNextJarEntry()) != null) { + if (!isEntryValid(entry, whitelist)) { + continue; + } + os.putNextEntry(entry); + IOUtils.copyLarge(is, os); + os.closeEntry(); + } } - - os.close(); - is.close(); } private boolean isEntryValid(JarEntry entry, boolean whitelist) { diff --git a/userdev/src/main/java/net/neoforged/gradle/userdev/runtime/extension/UserDevRuntimeExtension.java b/userdev/src/main/java/net/neoforged/gradle/userdev/runtime/extension/UserDevRuntimeExtension.java index e3baec0f8..bc215b0b0 100644 --- a/userdev/src/main/java/net/neoforged/gradle/userdev/runtime/extension/UserDevRuntimeExtension.java +++ b/userdev/src/main/java/net/neoforged/gradle/userdev/runtime/extension/UserDevRuntimeExtension.java @@ -102,7 +102,7 @@ public UserDevRuntimeExtension(Project project) { builder.withPreTaskAdapter("decompile", atAndSASAdapter); - builder.withPostTaskAdapter("patch", createPatchAdapter(userDevConfigurationSpec.getSourcePatchesDirectory().get(), unpackedForgeDirectory)); + builder.withPostTaskAdapter("patch", createPatchAdapter(userDevDependency, userDevConfigurationSpec.getSourcePatchesDirectory().get())); builder.withTaskCustomizer("inject", InjectZipContent.class, task -> configureNeoforgeInjects( task, @@ -155,10 +155,13 @@ private TaskTreeAdapter createAccessTransformerAdapter(final String accessTransf }; } - private TaskTreeAdapter createPatchAdapter(final String patchDirectory, final File unpackForgeUserDevDirectory) { + private TaskTreeAdapter createPatchAdapter(Dependency userDevDependency, String patchDirectory) { return (definition, previousTasksOutput, runtimeWorkspace, gameArtifacts, mappingVersionData, dependentTaskConfigurationHandler) -> definition.getSpecification().getProject().getTasks().register(CommonRuntimeUtils.buildTaskName(definition.getSpecification(), "patchUserDev"), Patch.class, task -> { + Artifact userDevArtifact = Artifact.from(userDevDependency); + task.getInput().set(previousTasksOutput.flatMap(WithOutput::getOutput)); - task.getPatchDirectory().fileProvider(definition.getSpecification().getProject().provider(() -> new File(unpackForgeUserDevDirectory, patchDirectory))); + task.getPatchArtifact().set(userDevArtifact); + task.getPatchDirectory().set(patchDirectory); }); }