Skip to content

Commit

Permalink
Single file patch improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Dec 21, 2024
1 parent 843f5d7 commit 0708c7b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ package io.papermc.paperweight.core.extension

import io.papermc.paperweight.util.*
import javax.inject.Inject
import kotlin.io.path.*
import org.gradle.api.Action
import org.gradle.api.Named
import org.gradle.api.PolymorphicDomainObjectContainer
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.file.ProjectLayout
import org.gradle.api.file.RegularFileProperty
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.ListProperty
Expand Down Expand Up @@ -78,10 +80,15 @@ abstract class UpstreamConfig @Inject constructor(
directoryPatchSets.register(name, RepoPatchSet::class, op)
}

abstract class SingleFilePatchSet {
abstract class SingleFilePatchSet @Inject constructor(
objects: ObjectFactory,
layout: ProjectLayout,
) {
abstract val path: Property<String>
abstract val outputFile: RegularFileProperty
abstract val patchFile: RegularFileProperty
val rejectsFile: RegularFileProperty = objects.fileProperty()
.convention(layout.file(patchFile.map { it.path.resolveSibling(it.path.name + ".rej").toFile() }))
}

abstract class DirectoryPatchSet @Inject constructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class UpstreamConfigTasks(
}
)
outputFile = cfg.outputFile
rejectsFile = cfg.rejectsFile
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ import org.gradle.api.tasks.Nested
import org.gradle.api.tasks.Optional
import org.gradle.api.tasks.OutputFile
import org.gradle.api.tasks.TaskAction
import org.gradle.kotlin.dsl.newInstance
import org.gradle.kotlin.dsl.*

abstract class ApplySingleFilePatches : BaseTask() {

Expand Down Expand Up @@ -82,6 +82,9 @@ abstract class ApplySingleFilePatches : BaseTask() {
@get:OutputFile
abstract val outputFile: RegularFileProperty

@get:OutputFile
abstract val rejectsFile: RegularFileProperty

@get:InputFile
@get:Optional
abstract val patchFile: RegularFileProperty
Expand All @@ -94,55 +97,57 @@ abstract class ApplySingleFilePatches : BaseTask() {

@TaskAction
fun run() {
val tmpWork = temporaryDir.resolve("work").toPath()
val tmpPatch = temporaryDir.resolve("patch").toPath()
val tmpRej = temporaryDir.resolve("rejects").toPath()
tmpRej.deleteRecursive()
val log = temporaryDir.resolve("log.txt").toPath()

ensureDeleted(log)
val error = PrintStream(log.toFile(), Charsets.UTF_8).use { logOut ->
for (patch in patches.get()) {
tmpWork.deleteRecursive()
tmpPatch.deleteRecursive()

val workFile = tmpWork.resolve(patch.path.get()).createParentDirectories()
upstream.path.resolve(patch.path.get())
.copyTo(workFile, true)

if (patch.patchFile.isPresent) {
patch.patchFile.path.copyTo(tmpPatch.resolve(patch.path.get() + ".patch").createParentDirectories(), true)

temporaryDir.toPath().cleanDir()
var fail = false
for ((index, patch) in patches.get().withIndex()) {
val tmp = temporaryDir.toPath().resolve(index.toString()).cleanDir()
val tmpWork = tmp.resolve("work")
val tmpPatch = tmp.resolve("patch")
val tmpRej = tmp.resolve("rejects")
val log = tmp.resolve("log.txt")

val workFile = tmpWork.resolve(patch.path.get()).createParentDirectories()
upstream.path.resolve(patch.path.get())
.copyTo(workFile, true)

if (patch.patchFile.isPresent) {
patch.patchFile.path.copyTo(tmpPatch.resolve(patch.path.get() + ".patch").createParentDirectories(), true)

val result = PrintStream(log.toFile(), Charsets.UTF_8).use { logOut ->
val op = PatchOperation.builder()
.logTo(logOut)
.level(LogLevel.ALL)
.mode(mode.get())
.summary(true)
.summary(false)
.basePath(tmpWork)
.patchesPath(tmpPatch)
.outputPath(tmpWork)
.rejectsPath(tmpRej)
.build()

val result = op.operate()
op.operate()
}

workFile.copyTo(patch.outputFile.path.createParentDirectories(), true)
workFile.copyTo(patch.outputFile.path.createParentDirectories(), true)

if (result.exit != 0) {
return@use "Patch failed on ${patch.patchFile.path}, see log above. Rejects at $tmpRej"
}
val rej = tmpRej.resolve(patch.path.get() + ".patch.rej")
if (rej.exists()) {
rej.copyTo(patch.rejectsFile.path.createParentDirectories(), true)
} else {
upstream.path.resolve(patch.path.get())
.copyTo(patch.outputFile.path.createParentDirectories(), true)
rej.deleteIfExists()
}
}

null
if (result.exit != 0) {
fail = true
logger.error("Patch ${patch.patchFile.path} failed:\n" + log.readText())
}
} else {
upstream.path.resolve(patch.path.get())
.copyTo(patch.outputFile.path.createParentDirectories(), true)
}
}

if (error != null) {
logger.error(log.readText())
throw PaperweightException(error)
if (fail) {
throw PaperweightException("Patch apply failures, review the log above")
}
}
}

0 comments on commit 0708c7b

Please sign in to comment.