aboutsummaryrefslogtreecommitdiffhomepage
path: root/build.gradle.kts
diff options
context:
space:
mode:
authorJason Penilla <[email protected]>2023-12-05 21:46:41 -0700
committerJason Penilla <[email protected]>2023-12-05 21:46:41 -0700
commit2053d6ace7c71dfd2660efb6f481780bc436b9ea (patch)
tree365ebb01db3cf7fb3e10e0deadd0b1808298784d /build.gradle.kts
parentfc12258f24a68bd0af099972f9567badd9dc518d (diff)
downloadPaper-2053d6ace7c71dfd2660efb6f481780bc436b9ea.tar.gz
Paper-2053d6ace7c71dfd2660efb6f481780bc436b9ea.zip
Improve update helper task
Diffstat (limited to 'build.gradle.kts')
-rw-r--r--build.gradle.kts88
1 files changed, 67 insertions, 21 deletions
diff --git a/build.gradle.kts b/build.gradle.kts
index cb9e7e93b8..5449518dba 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -4,6 +4,7 @@ import org.gradle.api.tasks.testing.logging.TestExceptionFormat
import org.gradle.api.tasks.testing.logging.TestLogEvent
import java.io.ByteArrayOutputStream
import java.nio.file.Path
+import java.util.regex.Pattern
import kotlin.io.path.*
plugins {
@@ -177,37 +178,82 @@ abstract class RebasePatches : BaseTask() {
private fun unapplied(): List<Path> =
unappliedPatches.path.listDirectoryEntries("*.patch").sortedBy { it.name }
+ private fun appliedLoc(patch: Path): Path = appliedPatches.path.resolve(unappliedPatches.path.relativize(patch))
+
+ companion object {
+ val regex = Pattern.compile("Patch failed at ([0-9]{4}) (.*)")
+ const val subjectPrefix = "Subject: [PATCH] "
+ }
+
@TaskAction
fun run() {
- for (patch in unapplied()) {
- val appliedLoc = appliedPatches.path.resolve(unappliedPatches.path.relativize(patch))
- patch.copyTo(appliedLoc)
+ val unapplied = unapplied()
+ for (patch in unapplied) {
+ patch.copyTo(appliedLoc(patch))
+ }
- val out = ByteArrayOutputStream()
+ val out = ByteArrayOutputStream()
+ val proc = ProcessBuilder()
+ .directory(projectDir.path)
+ .command("./gradlew", "applyServerPatches")
+ .redirectErrorStream(true)
+ .start()
+
+ redirect(proc.inputStream, out)
+
+ val exit = proc.waitFor()
+
+ if (exit != 0) {
+ val outStr = String(out.toByteArray())
+ val matcher = regex.matcher(outStr)
+ if (!matcher.find()) error("Could not determine failure point")
+ val failedSubjectFragment = matcher.group(2)
+ val failed = unapplied.single { p ->
+ p.useLines { lines ->
+ val subjectLine = lines.single { it.startsWith(subjectPrefix) }
+ .substringAfter(subjectPrefix)
+ subjectLine.startsWith(failedSubjectFragment)
+ }
+ }
+
+ // delete successful & failure point from unapplied patches dir
+ for (path in unapplied) {
+ path.deleteIfExists()
+ if (path == failed) {
+ break
+ }
+ }
- val proc = ProcessBuilder()
+ // delete failed from patches dir
+ var started = false
+ for (path in unapplied) {
+ if (path == failed) {
+ started = true
+ continue
+ }
+ if (started) {
+ appliedLoc(path).deleteIfExists()
+ }
+ }
+
+ // Apply again to reset the am session (so it ends on the failed patch, to allow us to rebuild after fixing it)
+ val apply2 = ProcessBuilder()
.directory(projectDir.path)
.command("./gradlew", "applyServerPatches")
.redirectErrorStream(true)
.start()
- redirect(proc.inputStream, out)
-
- val exit = proc.waitFor()
+ redirect(apply2.inputStream, System.out)
- patch.deleteIfExists()
-
- if (exit != 0) {
- logger.lifecycle("Patch failed at $patch; Git output:")
- logger.lifecycle(String(out.toByteArray()))
- break
- }
-
- val git = Git(projectDir.path)
- git("add", appliedPatches.path.toString() + "/*").runSilently()
- git("add", unappliedPatches.path.toString() + "/*").runSilently()
-
- logger.lifecycle("Applied $patch")
+ logger.lifecycle(outStr)
+ logger.lifecycle("Patch failed at $failed; See Git output above.")
+ } else {
+ unapplied.forEach { it.deleteIfExists() }
+ logger.lifecycle("All patches applied!")
}
+
+ val git = Git(projectDir.path)
+ git("add", appliedPatches.path.toString() + "/*").runSilently()
+ git("add", unappliedPatches.path.toString() + "/*").runSilently()
}
}