From d76693c5dbe524d70fcb52040235debcbdce2e70 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Wolf-Martell=20Montw=C3=A9?= Date: Tue, 16 Jan 2024 14:30:49 +0100 Subject: [PATCH] Add badging diff and colored output --- build-plugin/build.gradle.kts | 2 +- .../thunderbird.quality.badging.gradle.kts | 94 +++++++++++++++++-- gradle/libs.versions.toml | 1 + 3 files changed, 86 insertions(+), 11 deletions(-) diff --git a/build-plugin/build.gradle.kts b/build-plugin/build.gradle.kts index b57ac9c8b..10e0b0818 100644 --- a/build-plugin/build.gradle.kts +++ b/build-plugin/build.gradle.kts @@ -16,8 +16,8 @@ dependencies { implementation(plugin(libs.plugins.detekt)) implementation(plugin(libs.plugins.dependency.check)) + implementation(libs.diff.utils) compileOnly(libs.android.tools.common) - compileOnly(libs.assertk) } fun plugin(provider: Provider) = with(provider.get()) { diff --git a/build-plugin/src/main/kotlin/thunderbird.quality.badging.gradle.kts b/build-plugin/src/main/kotlin/thunderbird.quality.badging.gradle.kts index 4c011c436..e1e6cbc7c 100644 --- a/build-plugin/src/main/kotlin/thunderbird.quality.badging.gradle.kts +++ b/build-plugin/src/main/kotlin/thunderbird.quality.badging.gradle.kts @@ -1,7 +1,7 @@ -import assertk.assertThat -import assertk.assertions.isEqualTo import com.android.SdkConstants import com.android.build.api.artifact.SingleArtifact +import com.github.difflib.text.DiffRow +import com.github.difflib.text.DiffRowGenerator import org.gradle.configurationcache.extensions.capitalized /** @@ -119,10 +119,11 @@ abstract class CheckBadgingTask : DefaultTask() { @TaskAction fun taskAction() { if (goldenBadging.isPresent.not()) { - logger.error( - "Golden badging file does not exist! " + - "If this is the first time running this task, " + - "run ./gradlew ${updateBadgingTaskName.get()}", + printlnColor( + ANSI_YELLOW, + "Golden badging file does not exist!" + + " If this is the first time running this task," + + " run ./gradlew ${updateBadgingTaskName.get()}", ) return } @@ -130,12 +131,85 @@ abstract class CheckBadgingTask : DefaultTask() { val goldenBadgingContent = goldenBadging.get().asFile.readText() val generatedBadgingContent = generatedBadging.get().asFile.readText() if (goldenBadgingContent == generatedBadgingContent) { - logger.info("Generated badging is the same as golden badging!") + printlnColor(ANSI_YELLOW, "Generated badging is the same as golden badging!") return } - assertThat(generatedBadgingContent).isEqualTo(goldenBadgingContent) + val diff = performDiff(goldenBadgingContent, generatedBadgingContent) + printDiff(diff) + + throw GradleException( + """ + Generated badging is different from golden badging! + + If this change is intended, run ./gradlew ${updateBadgingTaskName.get()} + """.trimIndent(), + ) + } + + private fun performDiff(goldenBadgingContent: String, generatedBadgingContent: String): String { + val generator: DiffRowGenerator = DiffRowGenerator.create() + .showInlineDiffs(true) + .mergeOriginalRevised(true) + .inlineDiffByWord(true) + .oldTag { _ -> "" } + .newTag { _ -> "" } + .build() + + return generator.generateDiffRows( + goldenBadgingContent.lines(), + generatedBadgingContent.lines(), + ).filter { row -> row.tag != DiffRow.Tag.EQUAL } + .joinToString("\n") { row -> + when (row.tag) { + DiffRow.Tag.INSERT -> { + "+ ${row.newLine}" + } + + DiffRow.Tag.DELETE -> { + "- ${row.oldLine}" + } + + DiffRow.Tag.CHANGE -> { + "+ ${row.newLine}" + "- ${row.oldLine}" + } + + DiffRow.Tag.EQUAL -> "" + } + } + } + + private fun printDiff(diff: String) { + printlnColor("", null) + printlnColor(ANSI_YELLOW, "Badging diff:") + + diff.lines().forEach { line -> + val ansiColor = if (line.startsWith("+")) { + ANSI_GREEN + } else if (line.startsWith("-")) { + ANSI_RED + } else { + null + } + printlnColor(line, ansiColor) + } + } + + private fun printlnColor(text: String, ansiColor: String?) { + println( + if (ansiColor != null) { + ansiColor + text + ANSI_RESET + } else { + text + }, + ) + } + + private companion object { + const val ANSI_RESET = "\u001B[0m" + const val ANSI_RED = "\u001B[31m" + const val ANSI_GREEN = "\u001B[32m" + const val ANSI_YELLOW = "\u001B[33m" } } - - diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 8e1958a48..98bd685ba 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -61,6 +61,7 @@ dependency-guard = { id = "com.dropbox.dependency-guard", version.ref = "pluginD [libraries] desugar = "com.android.tools:desugar_jdk_libs:2.0.4" android-tools-common = { group = "com.android.tools", name = "common", version.ref = "androidTools" } +diff-utils = "io.github.java-diff-utils:java-diff-utils:4.12" kotlin-test = { module = "org.jetbrains.kotlin:kotlin-test", version.ref = "kotlin" } kotlin-reflect = { module = "org.jetbrains.kotlin:kotlin-reflect", version.ref = "kotlin" }