|
| 1 | +package net.thunderbird.gradle.plugin.app.badging |
| 2 | + |
| 3 | +import com.github.difflib.text.DiffRow |
| 4 | +import com.github.difflib.text.DiffRowGenerator |
| 5 | +import org.gradle.api.DefaultTask |
| 6 | +import org.gradle.api.GradleException |
| 7 | +import org.gradle.api.file.DirectoryProperty |
| 8 | +import org.gradle.api.file.RegularFileProperty |
| 9 | +import org.gradle.api.provider.Property |
| 10 | +import org.gradle.api.tasks.CacheableTask |
| 11 | +import org.gradle.api.tasks.Input |
| 12 | +import org.gradle.api.tasks.InputFile |
| 13 | +import org.gradle.api.tasks.Optional |
| 14 | +import org.gradle.api.tasks.OutputDirectory |
| 15 | +import org.gradle.api.tasks.PathSensitive |
| 16 | +import org.gradle.api.tasks.PathSensitivity |
| 17 | +import org.gradle.api.tasks.TaskAction |
| 18 | +import org.gradle.language.base.plugins.LifecycleBasePlugin |
| 19 | + |
| 20 | +@CacheableTask |
| 21 | +abstract class CheckBadgingTask : DefaultTask() { |
| 22 | + |
| 23 | + // In order for the task to be up-to-date when the inputs have not changed, |
| 24 | + // the task must declare an output, even if it's not used. Tasks with no |
| 25 | + // output are always run regardless of whether the inputs changed |
| 26 | + @get:OutputDirectory |
| 27 | + abstract val output: DirectoryProperty |
| 28 | + |
| 29 | + @get:PathSensitive(PathSensitivity.RELATIVE) |
| 30 | + @get:Optional |
| 31 | + @get:InputFile |
| 32 | + abstract val goldenBadging: RegularFileProperty |
| 33 | + |
| 34 | + @get:PathSensitive(PathSensitivity.RELATIVE) |
| 35 | + @get:InputFile |
| 36 | + abstract val generatedBadging: RegularFileProperty |
| 37 | + |
| 38 | + @get:Input |
| 39 | + abstract val updateBadgingTaskName: Property<String> |
| 40 | + |
| 41 | + override fun getGroup(): String = LifecycleBasePlugin.VERIFICATION_GROUP |
| 42 | + |
| 43 | + @TaskAction |
| 44 | + fun taskAction() { |
| 45 | + if (goldenBadging.isPresent.not()) { |
| 46 | + printlnColor( |
| 47 | + ANSI_YELLOW, |
| 48 | + "Golden badging file does not exist!" + |
| 49 | + " If this is the first time running this task," + |
| 50 | + " run ./gradlew ${updateBadgingTaskName.get()}", |
| 51 | + ) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + val goldenBadgingContent = goldenBadging.get().asFile.readText() |
| 56 | + val generatedBadgingContent = generatedBadging.get().asFile.readText() |
| 57 | + if (goldenBadgingContent == generatedBadgingContent) { |
| 58 | + printlnColor(ANSI_YELLOW, "Generated badging is the same as golden badging!") |
| 59 | + return |
| 60 | + } |
| 61 | + |
| 62 | + val diff = performDiff(goldenBadgingContent, generatedBadgingContent) |
| 63 | + printDiff(diff) |
| 64 | + |
| 65 | + throw GradleException( |
| 66 | + """ |
| 67 | + Generated badging is different from golden badging! |
| 68 | +
|
| 69 | + If this change is intended, run ./gradlew ${updateBadgingTaskName.get()} |
| 70 | + """.trimIndent(), |
| 71 | + ) |
| 72 | + } |
| 73 | + |
| 74 | + private fun performDiff(goldenBadgingContent: String, generatedBadgingContent: String): String { |
| 75 | + val generator: DiffRowGenerator = DiffRowGenerator.create() |
| 76 | + .showInlineDiffs(true) |
| 77 | + .mergeOriginalRevised(true) |
| 78 | + .inlineDiffByWord(true) |
| 79 | + .oldTag { _ -> "" } |
| 80 | + .newTag { _ -> "" } |
| 81 | + .build() |
| 82 | + |
| 83 | + return generator.generateDiffRows( |
| 84 | + goldenBadgingContent.lines(), |
| 85 | + generatedBadgingContent.lines(), |
| 86 | + ).filter { row -> row.tag != DiffRow.Tag.EQUAL } |
| 87 | + .joinToString("\n") { row -> |
| 88 | + @Suppress("WHEN_ENUM_CAN_BE_NULL_IN_JAVA") |
| 89 | + when (row.tag) { |
| 90 | + DiffRow.Tag.INSERT -> { |
| 91 | + "+ ${row.newLine}" |
| 92 | + } |
| 93 | + |
| 94 | + DiffRow.Tag.DELETE -> { |
| 95 | + "- ${row.oldLine}" |
| 96 | + } |
| 97 | + |
| 98 | + DiffRow.Tag.CHANGE -> { |
| 99 | + "+ ${row.newLine}" |
| 100 | + "- ${row.oldLine}" |
| 101 | + } |
| 102 | + |
| 103 | + DiffRow.Tag.EQUAL -> "" |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private fun printDiff(diff: String) { |
| 109 | + printlnColor("", null) |
| 110 | + printlnColor(ANSI_YELLOW, "Badging diff:") |
| 111 | + |
| 112 | + diff.lines().forEach { line -> |
| 113 | + val ansiColor = if (line.startsWith("+")) { |
| 114 | + ANSI_GREEN |
| 115 | + } else if (line.startsWith("-")) { |
| 116 | + ANSI_RED |
| 117 | + } else { |
| 118 | + null |
| 119 | + } |
| 120 | + printlnColor(line, ansiColor) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private fun printlnColor(text: String, ansiColor: String?) { |
| 125 | + println( |
| 126 | + if (ansiColor != null) { |
| 127 | + ansiColor + text + ANSI_RESET |
| 128 | + } else { |
| 129 | + text |
| 130 | + }, |
| 131 | + ) |
| 132 | + } |
| 133 | + |
| 134 | + private companion object { |
| 135 | + const val ANSI_RESET = "\u001B[0m" |
| 136 | + const val ANSI_RED = "\u001B[31m" |
| 137 | + const val ANSI_GREEN = "\u001B[32m" |
| 138 | + const val ANSI_YELLOW = "\u001B[33m" |
| 139 | + } |
| 140 | +} |
0 commit comments