refactor: simplify KtfmtCheckTask
This commit is contained in:
parent
8a0e987743
commit
1c1b04dad9
3 changed files with 13 additions and 12 deletions
|
@ -9,6 +9,7 @@ import kotlinx.coroutines.async
|
||||||
import kotlinx.coroutines.awaitAll
|
import kotlinx.coroutines.awaitAll
|
||||||
import kotlinx.coroutines.coroutineScope
|
import kotlinx.coroutines.coroutineScope
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
|
import org.gradle.api.GradleException
|
||||||
import org.gradle.api.file.DirectoryProperty
|
import org.gradle.api.file.DirectoryProperty
|
||||||
import org.gradle.api.file.FileCollection
|
import org.gradle.api.file.FileCollection
|
||||||
import org.gradle.api.tasks.IgnoreEmptyDirectories
|
import org.gradle.api.tasks.IgnoreEmptyDirectories
|
||||||
|
@ -36,10 +37,12 @@ abstract class KtfmtCheckTask : SourceTask() {
|
||||||
coroutineScope {
|
coroutineScope {
|
||||||
val results = inputFiles.map { async { checkFile(it) } }.awaitAll()
|
val results = inputFiles.map { async { checkFile(it) } }.awaitAll()
|
||||||
if (results.any { (notFormatted, _) -> notFormatted }) {
|
if (results.any { (notFormatted, _) -> notFormatted }) {
|
||||||
|
val prettyDiff =
|
||||||
results
|
results
|
||||||
.map { (_, diffs) -> diffs }
|
.map { (_, diffs) -> diffs }
|
||||||
.forEach { diffs -> KtfmtDiffer.printDiff(diffs, logger) }
|
.flatten()
|
||||||
error("[ktfmt] Found unformatted files")
|
.joinToString(separator = "\n") { diff -> diff.toString() }
|
||||||
|
throw GradleException("[ktfmt] Found unformatted files\n${prettyDiff}")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,8 @@
|
||||||
package app.passwordstore.gradle.ktfmt
|
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"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -5,7 +5,6 @@ import com.github.difflib.patch.ChangeDelta
|
||||||
import com.github.difflib.patch.DeleteDelta
|
import com.github.difflib.patch.DeleteDelta
|
||||||
import com.github.difflib.patch.InsertDelta
|
import com.github.difflib.patch.InsertDelta
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import org.gradle.api.logging.Logger
|
|
||||||
|
|
||||||
object KtfmtDiffer {
|
object KtfmtDiffer {
|
||||||
fun computeDiff(
|
fun computeDiff(
|
||||||
|
@ -26,10 +25,4 @@ object KtfmtDiffer {
|
||||||
KtfmtDiffEntry(pathNormalizer(inputFile), line, message)
|
KtfmtDiffEntry(pathNormalizer(inputFile), line, message)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun printDiff(entries: List<KtfmtDiffEntry>, logger: Logger) {
|
|
||||||
entries.forEach { entry ->
|
|
||||||
logger.error("${entry.input}:${entry.lineNumber} - ${entry.message}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue