From b408f371a216aafbe57920e352a948e09512ff02 Mon Sep 17 00:00:00 2001 From: Tony Robalik Date: Wed, 23 Oct 2024 15:40:12 -0700 Subject: [PATCH] fix: only print issue postscript if there's an issue to report. --- .../com/autonomousapps/extension/ReportingHandler.kt | 3 +++ .../advice/ProjectHealthConsoleReportBuilder.kt | 3 ++- .../kotlin/com/autonomousapps/subplugin/RootPlugin.kt | 1 + .../com/autonomousapps/tasks/BuildHealthTask.kt | 11 +++++++++++ .../autonomousapps/tasks/GenerateBuildHealthTask.kt | 11 +++++++++-- 5 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/com/autonomousapps/extension/ReportingHandler.kt b/src/main/kotlin/com/autonomousapps/extension/ReportingHandler.kt index 9c5bdd249..f003fc26c 100644 --- a/src/main/kotlin/com/autonomousapps/extension/ReportingHandler.kt +++ b/src/main/kotlin/com/autonomousapps/extension/ReportingHandler.kt @@ -20,6 +20,9 @@ abstract class ReportingHandler @Inject constructor(objects: ObjectFactory) { internal val postscript: Property = objects.property().convention("") + /** + * A postscript to include in issue reports. Only included when there are issues to report, otherwise ignored. + */ fun postscript(postscript: String) { this.postscript.set(postscript) this.postscript.disallowChanges() diff --git a/src/main/kotlin/com/autonomousapps/internal/advice/ProjectHealthConsoleReportBuilder.kt b/src/main/kotlin/com/autonomousapps/internal/advice/ProjectHealthConsoleReportBuilder.kt index 5b19d3da6..e35ca88e6 100644 --- a/src/main/kotlin/com/autonomousapps/internal/advice/ProjectHealthConsoleReportBuilder.kt +++ b/src/main/kotlin/com/autonomousapps/internal/advice/ProjectHealthConsoleReportBuilder.kt @@ -112,7 +112,8 @@ internal class ProjectHealthConsoleReportBuilder( appendModuleAdvice() - if (postscript.isNotEmpty()) { + // Only print the postscript if there is anything at all to report. + if (isNotEmpty() && postscript.isNotEmpty()) { appendLine() appendLine() appendLine(postscript) diff --git a/src/main/kotlin/com/autonomousapps/subplugin/RootPlugin.kt b/src/main/kotlin/com/autonomousapps/subplugin/RootPlugin.kt index ed6d3a98a..0a3e921c8 100644 --- a/src/main/kotlin/com/autonomousapps/subplugin/RootPlugin.kt +++ b/src/main/kotlin/com/autonomousapps/subplugin/RootPlugin.kt @@ -113,6 +113,7 @@ internal class RootPlugin(private val project: Project) { shouldFail.set(generateBuildHealthTask.flatMap { it.outputFail }) consoleReport.set(generateBuildHealthTask.flatMap { it.consoleOutput }) printBuildHealth.set(printBuildHealth()) + postscript.set(dagpExtension.reportingHandler.postscript) } // Add a dependency from the root project all projects (including itself). diff --git a/src/main/kotlin/com/autonomousapps/tasks/BuildHealthTask.kt b/src/main/kotlin/com/autonomousapps/tasks/BuildHealthTask.kt index babf08192..74645c04f 100644 --- a/src/main/kotlin/com/autonomousapps/tasks/BuildHealthTask.kt +++ b/src/main/kotlin/com/autonomousapps/tasks/BuildHealthTask.kt @@ -31,6 +31,9 @@ abstract class BuildHealthTask : DefaultTask() { @get:Input abstract val printBuildHealth: Property + @get:Input + abstract val postscript: Property + @TaskAction fun action() { val shouldFail = shouldFail.get().asFile.readText().toBoolean() val consoleReportFile = consoleReport.get().asFile @@ -40,7 +43,15 @@ abstract class BuildHealthTask : DefaultTask() { val output = buildString { if (printBuildHealth.get()) { append(consoleReportFile.readText()) + } else { + // If we're not printing the build health report, we should still print the postscript. + val ps = postscript.get() + if (ps.isNotEmpty()) { + appendLine(ps) + appendLine() + } } + // Trailing space so terminal UIs linkify it append("There were dependency violations. See report at ${consoleReportPath.toUri()} ") } diff --git a/src/main/kotlin/com/autonomousapps/tasks/GenerateBuildHealthTask.kt b/src/main/kotlin/com/autonomousapps/tasks/GenerateBuildHealthTask.kt index e937a2c9f..e1a09a6f5 100644 --- a/src/main/kotlin/com/autonomousapps/tasks/GenerateBuildHealthTask.kt +++ b/src/main/kotlin/com/autonomousapps/tasks/GenerateBuildHealthTask.kt @@ -89,7 +89,8 @@ abstract class GenerateBuildHealthTask : DefaultTask() { // console report val report = ProjectHealthConsoleReportBuilder( projectAdvice = projectAdvice, - postscript = postscript.get(), + // For buildHealth, we want to include the postscript only once. + postscript = "", dslKind = dslKind.get(), dependencyMap = dependencyMap.get().toLambda() ).text @@ -135,9 +136,15 @@ abstract class GenerateBuildHealthTask : DefaultTask() { output.bufferWriteJson(buildHealth) outputFail.writeText(shouldFail.toString()) - // This file must always exist, even if empty if (!didWrite) { + // This file must always exist, even if empty consoleOutput.writeText("") + } else { + // Append postscript if it exists + val ps = postscript.get() + if (ps.isNotEmpty()) { + consoleOutput.appendText("\n\n$ps") + } } }