Skip to content

Commit 04375f0

Browse files
committed
Add ktlint convention plugin and integrate with build logic
- Create `KtlintConventionPlugin` to configure ktlint with version 1.8.0, console output enabled, and exclusion filters for generated/build directories. - Register the new `ktlint` convention plugin in `build-logic/convention/build.gradle.kts`. - Add a task dependency to automatically install the ktlint format git pre-commit hook during the `preBuild` stage. - Apply the ktlint convention plugin to multiple existing plugins: `CmpFeature`, `CmpLibrary`, `KmpLibrary`, `AndroidApplication`, `AndroidApplicationCompose`, and `CmpApplication`. - Update `libs.versions.toml` to include the `ktlint-gradle` plugin dependency.
1 parent 8c66b65 commit 04375f0

9 files changed

Lines changed: 42 additions & 0 deletions

build-logic/convention/build.gradle.kts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ dependencies {
1616

1717
implementation(libs.buildkonfig.gradlePlugin)
1818
implementation(libs.buildkonfig.compiler)
19+
20+
implementation(libs.ktlint.gradlePlugin)
1921
}
2022

2123
java {
@@ -34,6 +36,11 @@ tasks {
3436
enableStricterValidation = true
3537
failOnWarning = true
3638
}
39+
configureEach {
40+
if (name == "preBuild") {
41+
dependsOn("addKtlintFormatGitPreCommitHook")
42+
}
43+
}
3744
}
3845

3946
gradlePlugin {
@@ -70,5 +77,9 @@ gradlePlugin {
7077
id = "zed.rainxch.convention.room"
7178
implementationClass = "RoomConventionPlugin"
7279
}
80+
register("ktlint") {
81+
id = "zed.rainxch.convention.ktlint"
82+
implementationClass = "KtlintConventionPlugin"
83+
}
7384
}
7485
}

build-logic/convention/src/main/kotlin/AndroidApplicationComposeConventionPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class AndroidApplicationComposeConventionPlugin : Plugin<Project> {
1010
with(pluginManager) {
1111
apply("zed.rainxch.convention.android.application")
1212
apply("org.jetbrains.kotlin.plugin.compose")
13+
apply("zed.rainxch.convention.ktlint")
1314
}
1415

1516
val commonExtension = extensions.getByType<ApplicationExtension>()

build-logic/convention/src/main/kotlin/AndroidApplicationConventionPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class AndroidApplicationConventionPlugin : Plugin<Project> {
1010
with(target) {
1111
with(pluginManager) {
1212
apply("com.android.application")
13+
apply("zed.rainxch.convention.ktlint")
1314
}
1415

1516
extensions.configure<ApplicationExtension> {

build-logic/convention/src/main/kotlin/CmpApplicationConventionPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class CmpApplicationConventionPlugin : Plugin<Project> {
1313
apply("org.jetbrains.kotlin.multiplatform")
1414
apply("org.jetbrains.compose")
1515
apply("org.jetbrains.kotlin.plugin.compose")
16+
apply("zed.rainxch.convention.ktlint")
1617
}
1718

1819
configureAndroidTarget()

build-logic/convention/src/main/kotlin/CmpFeatureConventionPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ class CmpFeatureConventionPlugin : Plugin<Project> {
88
with(target) {
99
with(pluginManager) {
1010
apply("zed.rainxch.convention.cmp.library")
11+
apply("zed.rainxch.convention.ktlint")
1112
}
1213

1314
dependencies {

build-logic/convention/src/main/kotlin/CmpLibraryConventionPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class CmpLibraryConventionPlugin : Plugin<Project> {
1111
apply("zed.rainxch.convention.kmp.library")
1212
apply("org.jetbrains.kotlin.plugin.compose")
1313
apply("org.jetbrains.compose")
14+
apply("zed.rainxch.convention.ktlint")
1415
}
1516

1617
dependencies {

build-logic/convention/src/main/kotlin/KmpLibraryConventionPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class KmpLibraryConventionPlugin : Plugin<Project> {
1515
apply("com.android.library")
1616
apply("org.jetbrains.kotlin.multiplatform")
1717
apply("org.jetbrains.kotlin.plugin.serialization")
18+
apply("zed.rainxch.convention.ktlint")
1819
}
1920

2021
configureKotlinMultiplatform()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import org.gradle.api.Plugin
2+
import org.gradle.api.Project
3+
import org.jlleitschuh.gradle.ktlint.KtlintExtension
4+
5+
class KtlintConventionPlugin : Plugin<Project> {
6+
override fun apply(target: Project) {
7+
with(target) {
8+
pluginManager.apply("org.jlleitschuh.gradle.ktlint")
9+
10+
extensions.configure(KtlintExtension::class.java) {
11+
version.set("1.8.0")
12+
outputToConsole.set(true)
13+
ignoreFailures.set(false)
14+
filter {
15+
exclude("**/generated/**")
16+
exclude("**/build/**")
17+
}
18+
}
19+
}
20+
}
21+
}

gradle/libs.versions.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ jsystemthemedetector = "3.9.1"
4444
work = "2.11.1"
4545
resources="1.10.1"
4646

47+
ktlint-gradle = "12.1.1"
48+
4749
projectApplicationId = "zed.rainxch.githubstore"
4850
projectVersionName = "1.6.2"
4951
projectMinSdkVersion = "26"
@@ -148,6 +150,8 @@ androidx-work-runtime = { module = "androidx.work:work-runtime-ktx", version.ref
148150
# Liquid effect
149151
liquid = { module = "io.github.fletchmckee.liquid:liquid", version.ref = "liquid" }
150152

153+
ktlint-gradlePlugin = { group = "org.jlleitschuh.gradle", name="ktlint-gradle", version.ref = "ktlint-gradle" }
154+
151155
[plugins]
152156

153157
convention-cmp-application = { id = "zed.rainxch.convention.cmp.application", version = "unspecified" }

0 commit comments

Comments
 (0)