From 1c1b04dad9965ba7d3483e5b061f8b50406d1bf7 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Thu, 8 Dec 2022 21:31:17 +0530 Subject: refactor: simplify `KtfmtCheckTask` --- .../kotlin/app/passwordstore/gradle/ktfmt/KtfmtCheckTask.kt | 11 +++++++---- .../kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffEntry.kt | 7 ++++++- .../main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffer.kt | 7 ------- 3 files changed, 13 insertions(+), 12 deletions(-) (limited to 'build-logic') diff --git a/build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtCheckTask.kt b/build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtCheckTask.kt index a50c8494..72dd4111 100644 --- a/build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtCheckTask.kt +++ b/build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtCheckTask.kt @@ -9,6 +9,7 @@ import kotlinx.coroutines.async import kotlinx.coroutines.awaitAll import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.runBlocking +import org.gradle.api.GradleException import org.gradle.api.file.DirectoryProperty import org.gradle.api.file.FileCollection import org.gradle.api.tasks.IgnoreEmptyDirectories @@ -36,10 +37,12 @@ abstract class KtfmtCheckTask : SourceTask() { coroutineScope { val results = inputFiles.map { async { checkFile(it) } }.awaitAll() if (results.any { (notFormatted, _) -> notFormatted }) { - results - .map { (_, diffs) -> diffs } - .forEach { diffs -> KtfmtDiffer.printDiff(diffs, logger) } - error("[ktfmt] Found unformatted files") + val prettyDiff = + results + .map { (_, diffs) -> diffs } + .flatten() + .joinToString(separator = "\n") { diff -> diff.toString() } + throw GradleException("[ktfmt] Found unformatted files\n${prettyDiff}") } } } diff --git a/build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffEntry.kt b/build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffEntry.kt index 44d1a967..ca01b5e9 100644 --- a/build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffEntry.kt +++ b/build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffEntry.kt @@ -1,3 +1,8 @@ package app.passwordstore.gradle.ktfmt -data class KtfmtDiffEntry(val input: String, val lineNumber: Int, val message: String) +data class KtfmtDiffEntry(val input: String, val lineNumber: Int, val message: String) { + + override fun toString(): String { + return "$input:$lineNumber - $message" + } +} diff --git a/build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffer.kt b/build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffer.kt index 936596cd..6eb76eab 100644 --- a/build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffer.kt +++ b/build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffer.kt @@ -5,7 +5,6 @@ import com.github.difflib.patch.ChangeDelta import com.github.difflib.patch.DeleteDelta import com.github.difflib.patch.InsertDelta import java.io.File -import org.gradle.api.logging.Logger object KtfmtDiffer { fun computeDiff( @@ -26,10 +25,4 @@ object KtfmtDiffer { KtfmtDiffEntry(pathNormalizer(inputFile), line, message) } } - - fun printDiff(entries: List, logger: Logger) { - entries.forEach { entry -> - logger.error("${entry.input}:${entry.lineNumber} - ${entry.message}") - } - } } -- cgit v1.2.3