From 285a1038e88d1a75b90377364327ac73d39ecf3c Mon Sep 17 00:00:00 2001 From: Simon Marquis Date: Thu, 14 Mar 2024 09:11:35 +0000 Subject: [PATCH] feat: decorrelate spotless and ktlint (#1025) --- build-logic/build.gradle.kts | 14 ++++++++++++-- .../kotlin/com/adevinta/spark/SparkProperties.kt | 1 + .../com/adevinta/spark/SparkSpotlessPlugin.kt | 5 +++-- gradle/libs.versions.toml | 3 +++ 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/build-logic/build.gradle.kts b/build-logic/build.gradle.kts index 9b54f3446..e608510bb 100644 --- a/build-logic/build.gradle.kts +++ b/build-logic/build.gradle.kts @@ -20,6 +20,7 @@ * SOFTWARE. */ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile +import kotlin.reflect.KProperty plugins { `kotlin-dsl` @@ -81,6 +82,15 @@ fun NamedDomainObjectContainer.create( this.implementationClass = implementationClass } +private operator fun VersionCatalog.getValue( + thisRef: Any?, + property: KProperty<*>, +) = findVersion(property.name).orElseThrow { + IllegalStateException("Missing catalog version ${property.name}") +} + +val ktlint: VersionConstraint by extensions.getByType().named("libs") + // This block is a copy of SparkSpotlessPlugin since this included build can't use it's own plugins... spotless { val licenseHeader = rootProject.file("./../spotless/spotless.kt") @@ -90,13 +100,13 @@ spotless { } kotlin { target("src/**/*.kt") - ktlint() + ktlint(ktlint.toString()) trimTrailingWhitespace() endWithNewline() licenseHeaderFile(licenseHeader) } kotlinGradle { - ktlint() + ktlint(ktlint.toString()) trimTrailingWhitespace() endWithNewline() licenseHeaderFile( diff --git a/build-logic/src/main/kotlin/com/adevinta/spark/SparkProperties.kt b/build-logic/src/main/kotlin/com/adevinta/spark/SparkProperties.kt index c7c189f83..df4acdfe3 100644 --- a/build-logic/src/main/kotlin/com/adevinta/spark/SparkProperties.kt +++ b/build-logic/src/main/kotlin/com/adevinta/spark/SparkProperties.kt @@ -48,6 +48,7 @@ internal class SparkVersions(catalog: VersionCatalog) { val `compileSdk` by catalog val `kotlin` by catalog val `showkase` by catalog + val `ktlint` by catalog private operator fun VersionCatalog.getValue( thisRef: Any?, diff --git a/build-logic/src/main/kotlin/com/adevinta/spark/SparkSpotlessPlugin.kt b/build-logic/src/main/kotlin/com/adevinta/spark/SparkSpotlessPlugin.kt index 005dd1670..7d9a06f8b 100644 --- a/build-logic/src/main/kotlin/com/adevinta/spark/SparkSpotlessPlugin.kt +++ b/build-logic/src/main/kotlin/com/adevinta/spark/SparkSpotlessPlugin.kt @@ -32,6 +32,7 @@ internal class SparkSpotlessPlugin : Plugin { with(target) { apply(plugin = "com.diffplug.spotless") + val ktlint = spark().versions.ktlint configure { val licenseHeader = rootProject.file("spotless/spotless.kt") format("misc") { @@ -40,14 +41,14 @@ internal class SparkSpotlessPlugin : Plugin { } kotlin { target("src/**/*.kt") - ktlint() + ktlint(ktlint.toString()) trimTrailingWhitespace() endWithNewline() licenseHeaderFile(licenseHeader) targetExclude("spotless/*.kt") } kotlinGradle { - ktlint() + ktlint(ktlint.toString()) trimTrailingWhitespace() endWithNewline() licenseHeaderFile( diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 041cc87ac..2c0d66ec5 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -21,6 +21,7 @@ kotlinx-coroutines = "1.8.0" kotlinx-collections-immutable = "0.3.7" kotlinx-serialization-json = "1.6.3" ksp = "1.9.22-1.0.18" +ktlint = "1.0.1" lint = "31.3.0" minCompileSdk = "24" paparazzi = "1.3.1" @@ -100,6 +101,8 @@ kotlinx-coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-t kotlinx-collections-immutable = { module = "org.jetbrains.kotlinx:kotlinx-collections-immutable", version.ref = "kotlinx-collections-immutable" } kotlinx-serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-json", version.ref = "kotlinx-serialization-json" } +ktlint-bom = { module = "com.pinterest.ktlint:ktlint-bom", version.ref = "ktlint" } + lint = { module = "com.android.tools.lint:lint", version.ref = "lint" } lint-api = { module = "com.android.tools.lint:lint-api", version.ref = "lint" } lint-checks = { module = "com.android.tools.lint:lint-checks", version.ref = "lint" }