Skip to content

Commit

Permalink
Clean up apply and rebuild logging
Browse files Browse the repository at this point in the history
use paperweight.verboseApplyPatches to see all the Git output, even on
success (when the task's output is enabled)
  • Loading branch information
jpenilla committed Dec 8, 2023
1 parent f154896 commit 8230d7d
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ open class AllTasks(
if (project.isBaseExecution) {
doNotTrackState("$name should always run when requested as part of the base execution.")
}
printOutput.set(project.printApplyPatchesOutput())
printOutput.set(project.isBaseExecution)

branch.set("HEAD")
upstreamBranch.set("upstream")
Expand Down Expand Up @@ -110,7 +110,7 @@ open class AllTasks(
if (project.isBaseExecution) {
doNotTrackState("$name should always run when requested as part of the base execution.")
}
printOutput.set(project.printApplyPatchesOutput())
printOutput.set(project.isBaseExecution)

patchDir.set(extension.paper.spigotServerPatchDir)
remappedSource.set(remapSpigotSources.flatMap { it.sourcesOutputZip })
Expand Down Expand Up @@ -140,7 +140,6 @@ open class AllTasks(
baseRef.set("base")

patchDir.set(extension.paper.spigotApiPatchDir)
printOutput.set(project.printRebuildPatchesOutput())
}

val rebuildServerPatches by tasks.registering<RebuildGitPatches> {
Expand All @@ -150,7 +149,6 @@ open class AllTasks(
baseRef.set("base")

patchDir.set(extension.paper.spigotServerPatchDir)
printOutput.set(project.printRebuildPatchesOutput())
}

@Suppress("unused")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,13 @@ abstract class ApplyGitPatches : ControllableOutputTask() {
@get:Inject
abstract val providers: ProviderFactory

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

override fun init() {
printOutput.convention(false).finalizeValueOnRead()
ignoreGitIgnore.convention(Git.ignoreProperty(providers)).finalizeValueOnRead()
verbose.convention(providers.verboseApplyPatches())
}

@TaskAction
Expand All @@ -91,7 +95,7 @@ abstract class ApplyGitPatches : ControllableOutputTask() {
val target = outputPath.name

if (printOutput.get()) {
println(" Resetting $target to ${upstream.path.name}...")
logger.lifecycle("Resetting $target to ${upstream.path.name}...")
}

val rootPatchDir = patchDir.pathOrNull ?: patchZip.path.let { unzip(it, findOutputDir(it)) }
Expand All @@ -102,14 +106,14 @@ abstract class ApplyGitPatches : ControllableOutputTask() {

if (unneededFiles.isPresent && unneededFiles.get().size > 0) {
unneededFiles.get().forEach { path -> outputDir.path.resolve(path).deleteRecursive() }
git(*Git.add(ignoreGitIgnore, ".")).setupOut().run()
git("commit", "-m", "Initial", "--author=Initial Source <[email protected]>").setupOut().run()
git(*Git.add(ignoreGitIgnore, ".")).executeSilently()
git("commit", "-m", "Initial", "--author=Initial Source <[email protected]>").executeSilently()
}

git("tag", "-d", "base").runSilently(silenceErr = true)
git("tag", "base").executeSilently(silenceErr = true)

applyGitPatches(git, target, outputDir.path, rootPatchDir, printOutput.get())
applyGitPatches(git, target, outputDir.path, rootPatchDir, printOutput.get(), verbose.get())
}
} finally {
if (rootPatchDir != patchDir.pathOrNull) {
Expand All @@ -124,10 +128,11 @@ fun ControllableOutputTask.applyGitPatches(
target: String,
outputDir: Path,
patchDir: Path?,
printOutput: Boolean
printOutput: Boolean,
verbose: Boolean,
) {
if (printOutput) {
println(" Applying patches to $target...")
logger.lifecycle("Applying patches to $target...")
}

val statusFile = outputDir.resolve(".git/patch-apply-failed")
Expand All @@ -138,7 +143,7 @@ fun ControllableOutputTask.applyGitPatches(
val patches = patchDir?.useDirectoryEntries("*.patch") { it.toMutableList() } ?: mutableListOf()
if (patches.isEmpty()) {
if (printOutput) {
println("No patches found")
logger.lifecycle("No patches found")
}
return
}
Expand All @@ -155,11 +160,12 @@ fun ControllableOutputTask.applyGitPatches(
patch.copyTo(mailDir.resolve(patch.fileName))
}

val result = git("am", "--3way", "--ignore-whitespace", tempDir.absolutePathString()).captureOut()
val gitOut = printOutput && verbose
val result = git("am", "--3way", "--ignore-whitespace", tempDir.absolutePathString()).captureOut(gitOut)
if (result.exit != 0) {
statusFile.writeText("1")

if (!printOutput) {
if (!gitOut) {
// Log the output anyway on failure
logger.lifecycle(result.out)
}
Expand All @@ -170,7 +176,7 @@ fun ControllableOutputTask.applyGitPatches(
} else {
statusFile.deleteForcefully()
if (printOutput) {
println(" Patches applied cleanly to $target")
logger.lifecycle("${patches.size} patches applied cleanly to $target")
}
}
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,13 @@ abstract class ApplyPaperPatches : ControllableOutputTask() {
@get:OutputDirectory
abstract val mcDevSources: DirectoryProperty

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

override fun init() {
upstreamBranch.convention("master")
ignoreGitIgnore.convention(Git.ignoreProperty(providers)).finalizeValueOnRead()
verbose.convention(providers.verboseApplyPatches())
}

@TaskAction
Expand All @@ -114,7 +118,7 @@ abstract class ApplyPaperPatches : ControllableOutputTask() {
val target = outputFile.name

if (printOutput.get()) {
println(" Creating $target from remapped source...")
logger.lifecycle("Creating $target from remapped source...")
}

Git(outputFile).let { git ->
Expand Down Expand Up @@ -161,7 +165,7 @@ abstract class ApplyPaperPatches : ControllableOutputTask() {
git("tag", "-d", "base").runSilently(silenceErr = true)
git("tag", "base").executeSilently()

applyGitPatches(git, target, outputFile, patchDir.path, printOutput.get())
applyGitPatches(git, target, outputFile, patchDir.path, printOutput.get(), verbose.get())

makeMcDevSrc(layout.cache, sourceMcDevJar.path, mcDevSources.path, outputDir.path, sourceDir, mcDataDir)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
package io.papermc.paperweight.tasks

import io.papermc.paperweight.util.*
import java.io.ByteArrayOutputStream
import java.io.OutputStream
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Console

Expand All @@ -49,21 +47,4 @@ abstract class ControllableOutputTask : BaseTask() {
setup(UselessOutputStream, System.out)
}
}

class Result(val exit: Int, val out: String)

fun Command.captureOut(): Result = run {
val out = ByteArrayOutputStream()
if (printOutput.get()) {
val combined = System.out + out
setup(combined, combined)
} else {
setup(out, out)
}
Result(run(), String(out.toByteArray()))
}

private operator fun OutputStream.plus(out: OutputStream): OutputStream {
return DelegatingOutputStream(this, out)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ abstract class RebuildGitPatches : ControllableOutputTask() {
}

if (printOutput.get()) {
println("Formatting patches for $what...")
logger.lifecycle("Formatting patches for $what...")
}

if (inputDir.path.resolve(".git/rebase-apply").exists()) {
// in middle of a rebase, be smarter
if (printOutput.get()) {
println("REBASE DETECTED - PARTIAL SAVE")
logger.lifecycle("REBASE DETECTED - PARTIAL SAVE")
val last = inputDir.path.resolve(".git/rebase-apply/last").readText().trim().toInt()
val next = inputDir.path.resolve(".git/rebase-apply/next").readText().trim().toInt()
val orderedFiles = patchFolder.useDirectoryEntries("*.patch") { it.toMutableList() }
Expand Down Expand Up @@ -106,10 +106,12 @@ abstract class RebuildGitPatches : ControllableOutputTask() {

if (filterPatches.get()) {
cleanupPatches(patchDirGit)
}
} else {
if (printOutput.get()) {
val saved = patchFolder.listDirectoryEntries("*.patch").size

if (printOutput.get()) {
println(" Patches saved for $what to ${patchFolder.name}/")
logger.lifecycle("Saved $saved patches for $what to ${layout.projectDirectory.path.relativize(patchFolder)}/")
}
}
}

Expand Down Expand Up @@ -153,9 +155,9 @@ abstract class RebuildGitPatches : ControllableOutputTask() {
}

if (printOutput.get()) {
for (patch in patchFiles) {
println(patch.name)
}
val saved = patchFiles.size - noChangesPatches.size
val relDir = layout.projectDirectory.path.relativize(patchDir.path)
logger.lifecycle("Saved modified patches ($saved/${patchFiles.size}) for ${inputDir.path.name} to $relDir/")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ object McDev {
if (dataTargetDir != null) {
logger.log(
if (printOutput) LogLevel.LIFECYCLE else LogLevel.DEBUG,
"Importing {} data files from vanilla data...",
"Importing {} data files from vanilla...",
dataImportMcDev.size
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ import org.gradle.api.Task

const val PAPERWEIGHT_EXTENSION = "paperweight"
const val PAPERWEIGHT_DEBUG = "paperweight.debug"
const val PAPERWEIGHT_PRINT_APPLY_PATCHES_OUTPUT = "paperweight.printBaseExecutionApplyPatchesOutput"
const val PAPERWEIGHT_PRINT_REBUILD_PATCHES_OUTPUT = "paperweight.printRebuildPatchesOutput"
const val PAPERWEIGHT_VERBOSE_APPLY_PATCHES = "paperweight.verboseApplyPatches"

const val MC_LIBRARY_URL = "https://libraries.minecraft.net/"

Expand Down
13 changes: 13 additions & 0 deletions paperweight-lib/src/main/kotlin/io/papermc/paperweight/util/git.kt
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,17 @@ class Command(private val processBuilder: ProcessBuilder, private val command: S
setup(out, System.err)
return if (run() == 0) String(out.toByteArray(), Charset.defaultCharset()) else null
}

class Result(val exit: Int, val out: String)

fun captureOut(logOut: Boolean): Result = run {
val out = ByteArrayOutputStream()
if (logOut) {
val combined = DelegatingOutputStream(System.out, out)
setup(combined, combined)
} else {
setup(out, out)
}
Result(run(), String(out.toByteArray()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,29 +124,18 @@ fun commentRegex(): Regex {
return Regex("\\s*#.*")
}

val Project.isBaseExecutionProvider: Provider<Boolean>
get() = providers.gradleProperty(PAPERWEIGHT_DOWNSTREAM_FILE_PROPERTY)
val ProviderFactory.isBaseExecution: Provider<Boolean>
get() = gradleProperty(PAPERWEIGHT_DOWNSTREAM_FILE_PROPERTY)
.orElse(provider { "false" })
.map { it == "false" }

val Project.isBaseExecution: Boolean
get() = isBaseExecutionProvider.get()
get() = providers.isBaseExecution.get()

fun Project.printApplyPatchesOutput(): Provider<Boolean> {
val base = isBaseExecutionProvider
val showOutput = providers.gradleProperty(PAPERWEIGHT_PRINT_APPLY_PATCHES_OUTPUT)
fun ProviderFactory.verboseApplyPatches(): Provider<Boolean> =
gradleProperty(PAPERWEIGHT_VERBOSE_APPLY_PATCHES)
.map { it.toBoolean() }
.orElse(false)
return base.zip(showOutput) { baseExecution, showOutputProp ->
return@zip baseExecution && showOutputProp
}
}

fun Project.printRebuildPatchesOutput(): Provider<Boolean> {
return providers.gradleProperty(PAPERWEIGHT_PRINT_REBUILD_PATCHES_OUTPUT)
.map { it.toBoolean() }
.orElse(false)
}

val redirectThreadCount: AtomicLong = AtomicLong(0)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ class PaperweightPatcher : Plugin<Project> {
if (isBaseExecution) {
doNotTrackState("$name should always run when requested as part of the base execution.")
}
printOutput.set(printApplyPatchesOutput())
printOutput.set(isBaseExecution)

val (cloneTask, upstreamDataTask) = upstreamTaskPair
dependsOn(upstreamDataTask)
Expand Down Expand Up @@ -295,7 +295,6 @@ class PaperweightPatcher : Plugin<Project> {
patchDir.convention(config.patchDir)
inputDir.convention(config.outputDir)
baseRef.convention("base")
printOutput.set(project.printRebuildPatchesOutput())
}

rebuildPatches {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ abstract class PatcherApplyGitPatches : ControllableOutputTask() {
@get:OutputDirectory
abstract val mcDevSources: DirectoryProperty

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

override fun init() {
upstreamBranch.convention("master")
importMcDev.convention(false)
printOutput.convention(true).finalizeValueOnRead()
ignoreGitIgnore.convention(Git.ignoreProperty(providers)).finalizeValueOnRead()
verbose.convention(providers.verboseApplyPatches())
}

@TaskAction
Expand All @@ -112,7 +116,7 @@ abstract class PatcherApplyGitPatches : ControllableOutputTask() {
val target = output.name

if (printOutput.get()) {
println(" Creating $target from patch source...")
logger.lifecycle("Creating $target from patch source...")
}

if (bareDirectory.get()) {
Expand Down Expand Up @@ -157,7 +161,7 @@ abstract class PatcherApplyGitPatches : ControllableOutputTask() {
git("tag", "-d", "base").runSilently(silenceErr = true)
git("tag", "base").executeSilently()

applyGitPatches(git, target, output, patchDir.pathOrNull, printOutput.get())
applyGitPatches(git, target, output, patchDir.pathOrNull, printOutput.get(), verbose.get())

makeMcDevSrc(layout.cache, sourceMcDevJar.path, mcDevSources.path, outputDir.path, srcDir, dataDir)
}
Expand Down

0 comments on commit 8230d7d

Please sign in to comment.