Skip to content

Commit

Permalink
Add applyOrMove task for file patches
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jan 25, 2025
1 parent b05d734 commit 8babfab
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -196,11 +196,9 @@ abstract class PaperweightCore : Plugin<Project> {

if (coreExt.updatingMinecraft.oldPaperCommit.isPresent) {
tasks.paperPatchingTasks.applySourcePatches.configure {
additionalRemote.set(
layout.cache.resolve(
"paperweight/oldPaper/${coreExt.updatingMinecraft.oldPaperCommit.get()}/paper-server/src/minecraft/java"
).absolutePathString()
)
additionalRemote = layout.cache.resolve(
"$OLD_PAPER_PATH/${coreExt.updatingMinecraft.oldPaperCommit.get()}/paper-server/src/minecraft/java"
).absolutePathString()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ abstract class PaperweightCoreExtension @Inject constructor(objects: ObjectFacto
val macheRepo: Property<String> = objects.property<String>().convention(PAPER_MAVEN_REPO_URL)

val gitFilePatches: Property<Boolean> = objects.property<Boolean>().convention(false)
val filterPatches: Property<Boolean> = objects.property<Boolean>().convention(true)

val vanillaJarIncludes: ListProperty<String> = objects.listProperty<String>().convention(
listOf("/*.class", "/net/minecraft/**", "/com/mojang/math/**")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ class CoreTasks(
setupMacheSources.flatMap { it.outputDir },
setupMacheResources.flatMap { it.outputDir },
project.coreExt.gitFilePatches,
project.coreExt.filterPatches,
paperOutputRoot,
)

Expand Down Expand Up @@ -212,6 +213,7 @@ class CoreTasks(
upstreamTasks.first.applyFeaturePatches.flatMap { it.repo },
upstreamTasks.first.applyResourcePatches.flatMap { it.output },
project.coreExt.gitFilePatches,
project.coreExt.filterPatches,
outputRoot,
)

Expand All @@ -233,6 +235,7 @@ class CoreTasks(
"upstream server patching"
},
project.coreExt.gitFilePatches,
project.coreExt.filterPatches,
null,
upstreamTasks.second,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class MinecraftPatchingTasks(
private val baseSources: Provider<Directory>,
private val baseResources: Provider<Directory>,
private val gitFilePatches: Provider<Boolean>,
private val filterPatches: Provider<Boolean>,
outputRoot: Path,
private val outputSrc: Path = outputRoot.resolve("src/minecraft/java"),
private val outputResources: Path = outputRoot.resolve("src/minecraft/resources"),
Expand Down Expand Up @@ -237,6 +238,7 @@ class MinecraftPatchingTasks(
inputDir.set(outputSrc)
patchDir.set(featurePatchDir)
baseRef.set("file")
filterPatches.set(this@MinecraftPatchingTasks.filterPatches)
}

val rebuildPatches = tasks.register<Task>(rebuildPatchesName) {
Expand All @@ -260,5 +262,13 @@ class MinecraftPatchingTasks(
repo.set(outputResources)
upstream.set("upstream/main")
}

val applyOrMoveSourcePatches = tasks.register<ApplyFilePatches>("applyOrMove${namePart}SourcePatches") {
configureApplyFilePatches()
description = "Applies $configName file patches to the Minecraft sources as Git patches, moving any failed patches to the rejects dir. " +
"Useful when updating to a new Minecraft version."
gitFilePatches = true
moveFailedGitPatchesToRejects = true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ class PatchingTasks(
private val featurePatchDir: DirectoryProperty,
private val baseDir: Provider<Directory>,
private val gitFilePatches: Provider<Boolean>,
private val filterPatches: Provider<Boolean>,
private val outputDir: Path,
private val tasks: TaskContainer = project.tasks,
) {
Expand Down Expand Up @@ -146,12 +147,21 @@ class PatchingTasks(
inputDir.set(outputDir)
patchDir.set(featurePatchDir)
baseRef.set("file")
filterPatches.set(this@PatchingTasks.filterPatches)
}

val rebuildPatches = tasks.register<Task>(rebuildPatchesName) {
group = taskGroup
description = "Rebuilds all $patchSetName patches"
dependsOn(rebuildFilePatches, rebuildFeaturePatches)
}

val applyOrMoveFilePatches = tasks.register<ApplyFilePatches>("applyOrMove${namePart}FilePatches") {
configureApplyFilePatches()
description = "Applies $patchSetName file patches as Git patches, moving any failed patches to the rejects dir. " +
"Useful when updating to a new Minecraft version."
gitFilePatches = true
moveFailedGitPatchesToRejects = true
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class UpstreamConfigTasks(
private val readOnly: Boolean,
private val taskGroup: String,
private val gitFilePatches: Provider<Boolean>,
private val filterPatches: Provider<Boolean>,
private val setupUpstream: TaskProvider<out RunNestedBuild>?,
private val upstreamTasks: UpstreamConfigTasks?,
) {
Expand Down Expand Up @@ -132,6 +133,7 @@ class UpstreamConfigTasks(
cfg.featurePatchDir,
base,
gitFilePatches,
filterPatches,
cfg.outputDir.path,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import io.papermc.paperweight.PaperweightException
import io.papermc.paperweight.core.util.ApplySourceATs
import io.papermc.paperweight.tasks.*
import io.papermc.paperweight.util.*
import io.papermc.paperweight.util.constants.*
import java.nio.file.Path
import java.util.concurrent.TimeUnit
import java.util.function.Predicate
Expand Down Expand Up @@ -199,8 +200,8 @@ abstract class SetupMinecraftSources : JavaLauncherTask() {
logger.lifecycle("Setting up Paper commit ${oldPaperCommit.get()} to use as base for constructing Git repo...")

val rootProjectDir = layout.projectDirectory.dir("../").path
val oldPaperDir = layout.cache.resolve("paperweight/oldPaper/${oldPaperCommit.get()}")
val oldPaperLog = layout.cache.resolve("paperweight/oldPaper/${oldPaperCommit.get()}.log")
val oldPaperDir = layout.cache.resolve("$OLD_PAPER_PATH/${oldPaperCommit.get()}")
val oldPaperLog = layout.cache.resolve("$OLD_PAPER_PATH/${oldPaperCommit.get()}.log")

val oldPaperGit: Git
if (oldPaperDir.exists()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import codechicken.diffpatch.cli.PatchOperation
import codechicken.diffpatch.match.FuzzyLineMatcher
import codechicken.diffpatch.util.LoggingOutputStream
import codechicken.diffpatch.util.PatchMode
import io.papermc.paperweight.PaperweightException
import io.papermc.paperweight.tasks.*
import io.papermc.paperweight.util.*
import java.io.PrintStream
Expand Down Expand Up @@ -85,11 +86,15 @@ abstract class ApplyFilePatches : BaseTask() {
@get:Input
abstract val additionalRemoteName: Property<String>

@get:Input
abstract val moveFailedGitPatchesToRejects: Property<Boolean>

init {
run {
verbose.convention(false)
gitFilePatches.convention(false)
additionalRemoteName.convention("old")
moveFailedGitPatchesToRejects.convention(false)
}
}

Expand Down Expand Up @@ -161,15 +166,36 @@ abstract class ApplyFilePatches : BaseTask() {

private fun applyWithGit(outputPath: Path): Int {
val git = Git(outputPath)
val patches = patches.path.filesMatchingRecursive("*.patch")
val patchStrings = patches.map { outputPath.relativize(it).pathString }
patchStrings.chunked(12).forEach {
git("apply", "--3way", *it.toTypedArray()).executeSilently(silenceOut = !verbose.get(), silenceErr = !verbose.get())
val patchFiles = patches.path.filesMatchingRecursive("*.patch")
if (moveFailedGitPatchesToRejects.get() && rejects.isPresent) {
patchFiles.forEach { patch ->
val patchPathFromGit = outputPath.relativize(patch)
val responseCode =
git("apply", "--3way", patchPathFromGit.pathString).runSilently(silenceOut = !verbose.get(), silenceErr = !verbose.get())
when {
responseCode == 0 -> {}
responseCode > 1 -> throw PaperweightException("Failed to apply patch $patch: $responseCode")
responseCode == 1 -> {
val relativePatch = patches.path.relativize(patch)
val failedFile = relativePatch.parent.resolve(relativePatch.fileName.toString().substringBeforeLast(".patch"))
git("reset", "--", failedFile.pathString).executeSilently(silenceOut = !verbose.get(), silenceErr = !verbose.get())
git("restore", failedFile.pathString).executeSilently(silenceOut = !verbose.get(), silenceErr = !verbose.get())

val rejectFile = rejects.path.resolve(relativePatch)
patch.moveTo(rejectFile.createParentDirectories(), overwrite = true)
}
}
}
} else {
val patchStrings = patchFiles.map { outputPath.relativize(it).pathString }
patchStrings.chunked(12).forEach {
git("apply", "--3way", *it.toTypedArray()).executeSilently(silenceOut = !verbose.get(), silenceErr = !verbose.get())
}
}

commit()

return patches.size
return patchFiles.size
}

private fun applyWithDiffPatch(): Int {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ abstract class PaperweightPatcher : Plugin<Project> {
checkoutTask.flatMap { it.outputDir },
!isBaseExecution,
"patching",
provider { false }, // TODO
patcher.gitFilePatches,
patcher.filterPatches,
applyUpstream,
null,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,15 @@ import org.gradle.api.Action
import org.gradle.api.NamedDomainObjectContainer
import org.gradle.api.NamedDomainObjectProvider
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.kotlin.dsl.*

@Suppress("unused", "MemberVisibilityCanBePrivate")
abstract class PaperweightPatcherExtension @Inject constructor(private val objects: ObjectFactory) {

val gitFilePatches: Property<Boolean> = objects.property<Boolean>().convention(false)
val filterPatches: Property<Boolean> = objects.property<Boolean>().convention(true)

val upstreams: NamedDomainObjectContainer<UpstreamConfig> = objects.domainObjectContainer(UpstreamConfig::class) {
objects.newInstance(it, true)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ const val DOWNLOAD_SERVICE_NAME = "paperweightDownloadService"

private const val MACHE_PATH = "$PAPER_PATH/mache"
const val BASE_PROJECT = "$MACHE_PATH/base"
const val OLD_PAPER_PATH = "$PAPER_PATH/oldPaper"

fun Task.paperTaskOutput(ext: String? = null) = paperTaskOutput(name, ext)
fun paperTaskOutput(name: String, ext: String? = null) = "$TASK_CACHE/$name" + (ext?.let { ".$it" } ?: "")
Expand Down

0 comments on commit 8babfab

Please sign in to comment.