From 5def8a39ad48d43510ffd8cc23ec0d806fb3efb1 Mon Sep 17 00:00:00 2001 From: MiniDigger | Martin Date: Sat, 21 Oct 2023 12:08:42 +0200 Subject: [PATCH] improve caching --- .../core/taskcontainers/SoftSpoonTasks.kt | 18 ++--- .../tasks/mache/ApplyMachePatches.kt | 43 ---------- .../paperweight/tasks/mache/SetupVanilla.kt | 80 +++++++++++++++++-- .../tasks/softspoon/ApplyPatches.kt | 8 +- 4 files changed, 85 insertions(+), 64 deletions(-) delete mode 100644 paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/mache/ApplyMachePatches.kt diff --git a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/SoftSpoonTasks.kt b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/SoftSpoonTasks.kt index 557e3202..403a2fe7 100644 --- a/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/SoftSpoonTasks.kt +++ b/paperweight-core/src/main/kotlin/io/papermc/paperweight/core/taskcontainers/SoftSpoonTasks.kt @@ -67,6 +67,9 @@ open class SoftSpoonTasks( group = "mache" description = "Setup vanilla source dir." + mache.from(project.configurations.named(MACHE_CONFIG)) + patches.set(layout.cache.resolve(PATCHES_FOLDER)) + inputFile.set(macheDecompileJar.flatMap { it.outputJar }) predicate.set { Files.isRegularFile(it) && it.toString().endsWith(".java")} outputDir.set(layout.cache.resolve(BASE_PROJECT).resolve("sources")) @@ -81,22 +84,11 @@ open class SoftSpoonTasks( outputDir.set(layout.cache.resolve(BASE_PROJECT).resolve("resources")) } - val applyMachePatches by tasks.registering(ApplyMachePatches::class) { - group = "mache" - description = "Applies patches to the vanilla sources" - - mache.from(project.configurations.named(MACHE_CONFIG)) - - input.set(setupMacheSources.flatMap { it.outputDir }) - output.set(layout.cache.resolve(BASE_PROJECT).resolve("sources")) - patches.set(layout.cache.resolve(PATCHES_FOLDER)) - } - val applySourcePatches by tasks.registering(ApplyPatches::class) { group = "softspoon" description = "Applies patches to the vanilla sources" - input.set(applyMachePatches.flatMap { it.output }) + input.set(setupMacheSources.flatMap { it.outputDir }) output.set(project.ext.serverProject.map { it.layout.projectDirectory.dir("src/vanilla/java") }) patches.set(project.layout.projectDirectory.dir("patches/sources")) } @@ -105,7 +97,7 @@ open class SoftSpoonTasks( group = "softspoon" description = "Applies patches to the vanilla sources" - input.set(applyMachePatches.flatMap { it.output }) + input.set(setupMacheSources.flatMap { it.outputDir }) output.set(project.ext.serverProject.map { it.layout.projectDirectory.dir("src/vanilla/java") }) patches.set(project.layout.projectDirectory.dir("patches/sources")) } diff --git a/paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/mache/ApplyMachePatches.kt b/paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/mache/ApplyMachePatches.kt deleted file mode 100644 index 2af1c566..00000000 --- a/paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/mache/ApplyMachePatches.kt +++ /dev/null @@ -1,43 +0,0 @@ -package io.papermc.paperweight.tasks.mache - -import io.papermc.paperweight.tasks.softspoon.ApplyPatches -import io.papermc.paperweight.util.* -import org.eclipse.jgit.api.Git -import org.eclipse.jgit.lib.PersonIdent -import org.gradle.api.file.ConfigurableFileCollection -import org.gradle.api.file.DirectoryProperty -import org.gradle.api.tasks.CacheableTask -import org.gradle.api.tasks.Classpath -import org.gradle.api.tasks.Internal -import org.gradle.api.tasks.UntrackedTask - -@CacheableTask -abstract class ApplyMachePatches : ApplyPatches() { - - @get:Internal - abstract override val patches: DirectoryProperty - - @get:Classpath - abstract val mache: ConfigurableFileCollection - - override fun setup() { - // prepare for patches for patching - val patchesFolder = patches.convertToPath().ensureClean() - - mache.singleFile.toPath().openZip().use { zip -> - zip.getPath("patches").copyRecursivelyTo(patchesFolder) - } - } - - override fun commit() { - val macheIdent = PersonIdent("Mache", "mache@automated.papermc.io") - val git = Git.open(output.convertToPath().toFile()) - git.add().addFilepattern(".").call() - git.tag().setName("mache").setTagger(macheIdent).setSigned(false).call() - git.commit() - .setMessage("Mache") - .setAuthor(macheIdent) - .setSign(false) - .call() - } -} diff --git a/paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/mache/SetupVanilla.kt b/paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/mache/SetupVanilla.kt index f977b519..851eaa9f 100644 --- a/paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/mache/SetupVanilla.kt +++ b/paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/mache/SetupVanilla.kt @@ -2,33 +2,60 @@ package io.papermc.paperweight.tasks.mache import io.papermc.paperweight.tasks.* import io.papermc.paperweight.util.* +import io.papermc.paperweight.util.patches.* +import io.papermc.paperweight.util.patches.JavaPatcher +import io.papermc.paperweight.util.patches.NativePatcher +import io.papermc.paperweight.util.patches.Patcher import java.nio.file.Path import java.util.function.Predicate +import javax.inject.Inject import kotlin.io.path.* import org.eclipse.jgit.api.Git import org.eclipse.jgit.lib.PersonIdent +import org.gradle.api.file.ConfigurableFileCollection import org.gradle.api.file.DirectoryProperty +import org.gradle.api.file.ProjectLayout import org.gradle.api.file.RegularFileProperty import org.gradle.api.provider.Property -import org.gradle.api.tasks.CacheableTask -import org.gradle.api.tasks.InputFile -import org.gradle.api.tasks.Internal -import org.gradle.api.tasks.OutputDirectory -import org.gradle.api.tasks.TaskAction -import org.gradle.api.tasks.UntrackedTask +import org.gradle.api.tasks.* +import org.gradle.process.ExecOperations @CacheableTask abstract class SetupVanilla : BaseTask() { + @get:PathSensitive(PathSensitivity.NONE) @get:InputFile abstract val inputFile: RegularFileProperty - @get:Internal + @get:Input abstract val predicate: Property> @get:OutputDirectory abstract val outputDir: DirectoryProperty + @get:Internal + abstract val patches: DirectoryProperty + + @get:Optional + @get:Classpath + abstract val mache: ConfigurableFileCollection + + @get:Internal + abstract val useNativeDiff: Property + + @get:Internal + abstract val patchExecutable: Property + + @get:Inject + abstract val exec: ExecOperations + + init { + run { + useNativeDiff.convention(false) + patchExecutable.convention("patch") + } + } + @TaskAction fun run() { val path = outputDir.convertToPath().ensureClean() @@ -56,5 +83,44 @@ abstract class SetupVanilla : BaseTask() { .setSign(false) .call() git.tag().setName("vanilla").setTagger(vanillaIdent).setSigned(false).call() + + if (patches.isPresent()) { + // prepare for patches for patching + val patchesFolder = patches.convertToPath().ensureClean() + + mache.singleFile.toPath().openZip().use { zip -> + zip.getPath("patches").copyRecursivelyTo(patchesFolder) + } + + // patch + val result = createPatcher().applyPatches(path, patches.convertToPath(), path, path) + + val macheIdent = PersonIdent("Mache", "mache@automated.papermc.io") + git.add().addFilepattern(".").call() + git.tag().setName("mache").setTagger(macheIdent).setSigned(false).call() + git.commit() + .setMessage("Mache") + .setAuthor(macheIdent) + .setSign(false) + .call() + + if (result is PatchFailure) { + result.failures + .map { "Patch failed: ${it.patch.relativeTo(patches.get().path)}: ${it.details}" } + .forEach { logger.error(it) } + git.close() + throw Exception("Failed to apply ${result.failures.size} patches") + } + } + + git.close() + } + + internal open fun createPatcher(): Patcher { + return if (useNativeDiff.get()) { + NativePatcher(exec, patchExecutable.get()) + } else { + JavaPatcher() + } } } diff --git a/paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/softspoon/ApplyPatches.kt b/paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/softspoon/ApplyPatches.kt index 02b45522..15904337 100644 --- a/paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/softspoon/ApplyPatches.kt +++ b/paperweight-lib/src/main/kotlin/io/papermc/paperweight/tasks/softspoon/ApplyPatches.kt @@ -13,6 +13,8 @@ import org.gradle.api.provider.Property import org.gradle.api.tasks.InputDirectory import org.gradle.api.tasks.Internal import org.gradle.api.tasks.OutputDirectory +import org.gradle.api.tasks.PathSensitive +import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.UntrackedTask import org.gradle.process.ExecOperations @@ -20,12 +22,14 @@ import org.gradle.process.ExecOperations @UntrackedTask(because = "Always apply patches") abstract class ApplyPatches : DefaultTask() { + @get:PathSensitive(PathSensitivity.NONE) @get:InputDirectory abstract val input: DirectoryProperty @get:OutputDirectory abstract val output: DirectoryProperty + @get:PathSensitive(PathSensitivity.NONE) @get:InputDirectory abstract val patches: DirectoryProperty @@ -66,7 +70,8 @@ abstract class ApplyPatches : DefaultTask() { open fun setup() { output.convertToPath().ensureClean() - Git.cloneRepository().setBranch("main").setURI("file://" + input.convertToPath().toString()).setDirectory(output.convertToPath().toFile()).call() + Git.cloneRepository().setBranch("main").setURI("file://" + input.convertToPath().toString()).setDirectory(output.convertToPath().toFile()) + .call().close() } open fun commit() { @@ -79,6 +84,7 @@ abstract class ApplyPatches : DefaultTask() { .setSign(false) .call() git.tag().setName("file").setTagger(ident).setSigned(false).call() + git.close() } internal open fun createPatcher(): Patcher {