Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit 1c1b04d

Browse files
committed
refactor: simplify KtfmtCheckTask
1 parent 8a0e987 commit 1c1b04d

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtCheckTask.kt

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import kotlinx.coroutines.async
99
import kotlinx.coroutines.awaitAll
1010
import kotlinx.coroutines.coroutineScope
1111
import kotlinx.coroutines.runBlocking
12+
import org.gradle.api.GradleException
1213
import org.gradle.api.file.DirectoryProperty
1314
import org.gradle.api.file.FileCollection
1415
import org.gradle.api.tasks.IgnoreEmptyDirectories
@@ -36,10 +37,12 @@ abstract class KtfmtCheckTask : SourceTask() {
3637
coroutineScope {
3738
val results = inputFiles.map { async { checkFile(it) } }.awaitAll()
3839
if (results.any { (notFormatted, _) -> notFormatted }) {
39-
results
40-
.map { (_, diffs) -> diffs }
41-
.forEach { diffs -> KtfmtDiffer.printDiff(diffs, logger) }
42-
error("[ktfmt] Found unformatted files")
40+
val prettyDiff =
41+
results
42+
.map { (_, diffs) -> diffs }
43+
.flatten()
44+
.joinToString(separator = "\n") { diff -> diff.toString() }
45+
throw GradleException("[ktfmt] Found unformatted files\n${prettyDiff}")
4346
}
4447
}
4548
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
package app.passwordstore.gradle.ktfmt
22

3-
data class KtfmtDiffEntry(val input: String, val lineNumber: Int, val message: String)
3+
data class KtfmtDiffEntry(val input: String, val lineNumber: Int, val message: String) {
4+
5+
override fun toString(): String {
6+
return "$input:$lineNumber - $message"
7+
}
8+
}

build-logic/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffer.kt

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.github.difflib.patch.ChangeDelta
55
import com.github.difflib.patch.DeleteDelta
66
import com.github.difflib.patch.InsertDelta
77
import java.io.File
8-
import org.gradle.api.logging.Logger
98

109
object KtfmtDiffer {
1110
fun computeDiff(
@@ -26,10 +25,4 @@ object KtfmtDiffer {
2625
KtfmtDiffEntry(pathNormalizer(inputFile), line, message)
2726
}
2827
}
29-
30-
fun printDiff(entries: List<KtfmtDiffEntry>, logger: Logger) {
31-
entries.forEach { entry ->
32-
logger.error("${entry.input}:${entry.lineNumber} - ${entry.message}")
33-
}
34-
}
3528
}

0 commit comments

Comments
 (0)