summaryrefslogtreecommitdiff
path: root/build-logic/kotlin-plugins/src/main
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2022-10-29 07:29:46 +0530
committerHarsh Shandilya <me@msfjarvis.dev>2022-10-29 08:00:28 +0530
commitfd20480f554060805acba3124cb251be7824c4d2 (patch)
tree5632081000dd955d51c04397733b0faeabd16b04 /build-logic/kotlin-plugins/src/main
parent505c2fa705fca1aaec7aae80c50a939bfd607716 (diff)
feat(build): add a homebrew ktfmt plugin
The general idea of the implementation is borrowed from https://github.com/cortinico/ktfmt-gradle
Diffstat (limited to 'build-logic/kotlin-plugins/src/main')
-rw-r--r--build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/KtfmtPlugin.kt32
-rw-r--r--build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtCheckTask.kt68
-rw-r--r--build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffEntry.kt3
-rw-r--r--build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffer.kt35
-rw-r--r--build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtFormatTask.kt56
5 files changed, 194 insertions, 0 deletions
diff --git a/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/KtfmtPlugin.kt b/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/KtfmtPlugin.kt
new file mode 100644
index 00000000..74e8ba95
--- /dev/null
+++ b/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/KtfmtPlugin.kt
@@ -0,0 +1,32 @@
+package app.passwordstore.gradle
+
+import app.passwordstore.gradle.ktfmt.KtfmtCheckTask
+import app.passwordstore.gradle.ktfmt.KtfmtFormatTask
+import org.gradle.api.Plugin
+import org.gradle.api.Project
+import org.gradle.kotlin.dsl.register
+
+class KtfmtPlugin : Plugin<Project> {
+
+ override fun apply(target: Project) {
+ target.tasks.register<KtfmtFormatTask>("ktfmtFormat") {
+ source =
+ project.layout.projectDirectory.asFileTree
+ .filter { file ->
+ file.extension == "kt" ||
+ file.extension == "kts" && !file.canonicalPath.contains("build")
+ }
+ .asFileTree
+ }
+ target.tasks.register<KtfmtCheckTask>("ktfmtCheck") {
+ source =
+ project.layout.projectDirectory.asFileTree
+ .filter { file ->
+ file.extension == "kt" ||
+ file.extension == "kts" && !file.canonicalPath.contains("build")
+ }
+ .asFileTree
+ projectDirectory.set(target.layout.projectDirectory)
+ }
+ }
+}
diff --git a/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtCheckTask.kt b/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtCheckTask.kt
new file mode 100644
index 00000000..a50c8494
--- /dev/null
+++ b/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtCheckTask.kt
@@ -0,0 +1,68 @@
+package app.passwordstore.gradle.ktfmt
+
+import com.facebook.ktfmt.format.Formatter
+import com.facebook.ktfmt.format.FormattingOptions
+import java.io.File
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.async
+import kotlinx.coroutines.awaitAll
+import kotlinx.coroutines.coroutineScope
+import kotlinx.coroutines.runBlocking
+import org.gradle.api.file.DirectoryProperty
+import org.gradle.api.file.FileCollection
+import org.gradle.api.tasks.IgnoreEmptyDirectories
+import org.gradle.api.tasks.InputFiles
+import org.gradle.api.tasks.Internal
+import org.gradle.api.tasks.PathSensitive
+import org.gradle.api.tasks.PathSensitivity
+import org.gradle.api.tasks.SourceTask
+import org.gradle.api.tasks.TaskAction
+
+@OptIn(ExperimentalCoroutinesApi::class)
+abstract class KtfmtCheckTask : SourceTask() {
+
+ @get:PathSensitive(PathSensitivity.RELATIVE)
+ @get:InputFiles
+ @get:IgnoreEmptyDirectories
+ protected val inputFiles: FileCollection
+ get() = super.getSource()
+
+ @get:Internal abstract val projectDirectory: DirectoryProperty
+
+ @TaskAction
+ fun execute() {
+ runBlocking(Dispatchers.IO.limitedParallelism(PARALLEL_TASK_LIMIT)) {
+ 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")
+ }
+ }
+ }
+ }
+
+ private fun checkFile(input: File): Pair<Boolean, List<KtfmtDiffEntry>> {
+ val originCode = input.readText()
+ val formattedCode =
+ Formatter.format(
+ FormattingOptions(
+ style = FormattingOptions.Style.GOOGLE,
+ maxWidth = 100,
+ continuationIndent = 2,
+ ),
+ originCode
+ )
+ val pathNormalizer = { file: File -> file.toRelativeString(projectDirectory.asFile.get()) }
+ return (originCode != formattedCode) to
+ KtfmtDiffer.computeDiff(input, formattedCode, pathNormalizer)
+ }
+
+ companion object {
+
+ private const val PARALLEL_TASK_LIMIT = 4
+ }
+}
diff --git a/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffEntry.kt b/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffEntry.kt
new file mode 100644
index 00000000..44d1a967
--- /dev/null
+++ b/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffEntry.kt
@@ -0,0 +1,3 @@
+package app.passwordstore.gradle.ktfmt
+
+data class KtfmtDiffEntry(val input: String, val lineNumber: Int, val message: String)
diff --git a/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffer.kt b/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffer.kt
new file mode 100644
index 00000000..936596cd
--- /dev/null
+++ b/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtDiffer.kt
@@ -0,0 +1,35 @@
+package app.passwordstore.gradle.ktfmt
+
+import com.github.difflib.DiffUtils
+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(
+ inputFile: File,
+ formattedCode: String,
+ pathNormalizer: (File) -> String
+ ): List<KtfmtDiffEntry> {
+ val originCode = inputFile.readText()
+ return DiffUtils.diff(originCode, formattedCode, null).deltas.map {
+ val line = it.source.position + 1
+ val message: String =
+ when (it) {
+ is ChangeDelta -> "Line changed: ${it.source.lines.first()}"
+ is DeleteDelta -> "Line deleted"
+ is InsertDelta -> "Line added"
+ else -> ""
+ }
+ KtfmtDiffEntry(pathNormalizer(inputFile), line, message)
+ }
+ }
+
+ fun printDiff(entries: List<KtfmtDiffEntry>, logger: Logger) {
+ entries.forEach { entry ->
+ logger.error("${entry.input}:${entry.lineNumber} - ${entry.message}")
+ }
+ }
+}
diff --git a/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtFormatTask.kt b/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtFormatTask.kt
new file mode 100644
index 00000000..82ce4ca3
--- /dev/null
+++ b/build-logic/kotlin-plugins/src/main/kotlin/app/passwordstore/gradle/ktfmt/KtfmtFormatTask.kt
@@ -0,0 +1,56 @@
+package app.passwordstore.gradle.ktfmt
+
+import com.facebook.ktfmt.format.Formatter
+import com.facebook.ktfmt.format.FormattingOptions
+import java.io.File
+import kotlinx.coroutines.Dispatchers
+import kotlinx.coroutines.ExperimentalCoroutinesApi
+import kotlinx.coroutines.async
+import kotlinx.coroutines.awaitAll
+import kotlinx.coroutines.coroutineScope
+import kotlinx.coroutines.runBlocking
+import org.gradle.api.file.FileCollection
+import org.gradle.api.tasks.IgnoreEmptyDirectories
+import org.gradle.api.tasks.InputFiles
+import org.gradle.api.tasks.PathSensitive
+import org.gradle.api.tasks.PathSensitivity
+import org.gradle.api.tasks.SourceTask
+import org.gradle.api.tasks.TaskAction
+
+@OptIn(ExperimentalCoroutinesApi::class)
+abstract class KtfmtFormatTask : SourceTask() {
+
+ @get:PathSensitive(PathSensitivity.RELATIVE)
+ @get:InputFiles
+ @get:IgnoreEmptyDirectories
+ protected val inputFiles: FileCollection
+ get() = super.getSource()
+
+ @TaskAction
+ fun execute() {
+ runBlocking(Dispatchers.IO.limitedParallelism(PARALLEL_TASK_LIMIT)) {
+ coroutineScope { inputFiles.map { async { formatFile(it) } }.awaitAll() }
+ }
+ }
+
+ private fun formatFile(input: File) {
+ val originCode = input.readText()
+ val formattedCode =
+ Formatter.format(
+ FormattingOptions(
+ style = FormattingOptions.Style.GOOGLE,
+ maxWidth = 100,
+ continuationIndent = 2,
+ ),
+ originCode
+ )
+ if (originCode != formattedCode) {
+ input.writeText(formattedCode)
+ }
+ }
+
+ companion object {
+
+ private const val PARALLEL_TASK_LIMIT = 4
+ }
+}