From 5ae8095ef1ed2ae9f98486f9148e933c77a28692 Mon Sep 17 00:00:00 2001 From: AntsyLich <59261191+AntsyLich@users.noreply.github.com> Date: Mon, 19 Aug 2024 18:11:14 +0600 Subject: [PATCH] Add spotless (with ktlint) (#1136) --- .github/workflows/build_pull_request.yml | 2 +- .github/workflows/build_push.yml | 2 +- buildSrc/build.gradle.kts | 1 + ...hon.android.application.compose.gradle.kts | 2 ++ .../mihon.android.application.gradle.kts | 2 ++ .../main/kotlin/mihon.benchmark.gradle.kts | 2 ++ .../main/kotlin/mihon.code.lint.gradle.kts | 30 +++++++++++++++++++ .../kotlin/mihon.library.compose.gradle.kts | 2 ++ .../src/main/kotlin/mihon.library.gradle.kts | 2 ++ gradle/libs.versions.toml | 5 ++++ 10 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 buildSrc/src/main/kotlin/mihon.code.lint.gradle.kts diff --git a/.github/workflows/build_pull_request.yml b/.github/workflows/build_pull_request.yml index 2cae6e891..11ecb58ec 100644 --- a/.github/workflows/build_pull_request.yml +++ b/.github/workflows/build_pull_request.yml @@ -38,7 +38,7 @@ jobs: uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 - name: Build app and run unit tests - run: ./gradlew assembleStandardRelease testReleaseUnitTest testStandardReleaseUnitTest + run: ./gradlew spotlessCheck assembleStandardRelease testReleaseUnitTest testStandardReleaseUnitTest - name: Upload APK uses: actions/upload-artifact@v4 diff --git a/.github/workflows/build_push.yml b/.github/workflows/build_push.yml index 207b166a2..077bfad01 100644 --- a/.github/workflows/build_push.yml +++ b/.github/workflows/build_push.yml @@ -36,7 +36,7 @@ jobs: uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 - name: Build app and run unit tests - run: ./gradlew assembleStandardRelease testReleaseUnitTest testStandardReleaseUnitTest + run: ./gradlew spotlessCheck assembleStandardRelease testReleaseUnitTest testStandardReleaseUnitTest - name: Upload APK uses: actions/upload-artifact@v4 diff --git a/buildSrc/build.gradle.kts b/buildSrc/build.gradle.kts index b825b4172..5ed94c26b 100644 --- a/buildSrc/build.gradle.kts +++ b/buildSrc/build.gradle.kts @@ -6,6 +6,7 @@ dependencies { implementation(androidx.gradle) implementation(kotlinx.gradle) implementation(kotlinx.compose.compiler.gradle) + implementation(libs.spotless.gradle) implementation(gradleApi()) implementation(files(libs.javaClass.superclass.protectionDomain.codeSource.location)) diff --git a/buildSrc/src/main/kotlin/mihon.android.application.compose.gradle.kts b/buildSrc/src/main/kotlin/mihon.android.application.compose.gradle.kts index 6b330b1d9..767f758cb 100644 --- a/buildSrc/src/main/kotlin/mihon.android.application.compose.gradle.kts +++ b/buildSrc/src/main/kotlin/mihon.android.application.compose.gradle.kts @@ -3,6 +3,8 @@ import mihon.buildlogic.configureCompose plugins { id("com.android.application") kotlin("android") + + id("mihon.code.lint") } android { diff --git a/buildSrc/src/main/kotlin/mihon.android.application.gradle.kts b/buildSrc/src/main/kotlin/mihon.android.application.gradle.kts index 7ba9c60fb..080f9ca64 100644 --- a/buildSrc/src/main/kotlin/mihon.android.application.gradle.kts +++ b/buildSrc/src/main/kotlin/mihon.android.application.gradle.kts @@ -5,6 +5,8 @@ import mihon.buildlogic.configureTest plugins { id("com.android.application") kotlin("android") + + id("mihon.code.lint") } android { diff --git a/buildSrc/src/main/kotlin/mihon.benchmark.gradle.kts b/buildSrc/src/main/kotlin/mihon.benchmark.gradle.kts index 3a43a2aa1..50c47f280 100644 --- a/buildSrc/src/main/kotlin/mihon.benchmark.gradle.kts +++ b/buildSrc/src/main/kotlin/mihon.benchmark.gradle.kts @@ -4,6 +4,8 @@ import mihon.buildlogic.configureTest plugins { id("com.android.test") kotlin("android") + + id("mihon.code.lint") } android { diff --git a/buildSrc/src/main/kotlin/mihon.code.lint.gradle.kts b/buildSrc/src/main/kotlin/mihon.code.lint.gradle.kts new file mode 100644 index 000000000..9dfa7eef2 --- /dev/null +++ b/buildSrc/src/main/kotlin/mihon.code.lint.gradle.kts @@ -0,0 +1,30 @@ +import org.gradle.accessors.dm.LibrariesForLibs + +plugins { + id("com.diffplug.spotless") +} + +val libs = the() + +spotless { + kotlin { + target("**/*.kt", "**/*.kts") + targetExclude("**/build/**/*.kt") + ktlint(libs.ktlint.core.get().version) + .editorConfigOverride(mapOf( + "ktlint_function_naming_ignore_when_annotated_with" to "Composable", + "ktlint_standard_class-signature" to "disabled", + "ktlint_standard_discouraged-comment-location" to "disabled", + "ktlint_standard_function-expression-body" to "disabled", + "ktlint_standard_function-signature" to "disabled", + )) + trimTrailingWhitespace() + endWithNewline() + } + format("xml") { + target("**/*.xml") + targetExclude("**/build/**/*.xml") + trimTrailingWhitespace() + endWithNewline() + } +} diff --git a/buildSrc/src/main/kotlin/mihon.library.compose.gradle.kts b/buildSrc/src/main/kotlin/mihon.library.compose.gradle.kts index 0c37a3256..7b377ce4c 100644 --- a/buildSrc/src/main/kotlin/mihon.library.compose.gradle.kts +++ b/buildSrc/src/main/kotlin/mihon.library.compose.gradle.kts @@ -2,6 +2,8 @@ import mihon.buildlogic.configureCompose plugins { id("com.android.library") + + id("mihon.code.lint") } android { diff --git a/buildSrc/src/main/kotlin/mihon.library.gradle.kts b/buildSrc/src/main/kotlin/mihon.library.gradle.kts index 743bf07e3..4cf5f6efc 100644 --- a/buildSrc/src/main/kotlin/mihon.library.gradle.kts +++ b/buildSrc/src/main/kotlin/mihon.library.gradle.kts @@ -3,6 +3,8 @@ import mihon.buildlogic.configureTest plugins { id("com.android.library") + + id("mihon.code.lint") } android { diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 648faf7f3..3ee860883 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -8,6 +8,8 @@ shizuku_version = "12.2.0" sqldelight = "2.0.2" sqlite = "2.4.0" voyager = "1.0.0" +spotless = "6.25.0" +ktlint-core = "1.3.1" [libraries] desugar = "com.android.tools:desugar_jdk_libs:2.0.4" @@ -96,6 +98,9 @@ voyager-screenmodel = { module = "cafe.adriel.voyager:voyager-screenmodel", vers voyager-tab-navigator = { module = "cafe.adriel.voyager:voyager-tab-navigator", version.ref = "voyager" } voyager-transitions = { module = "cafe.adriel.voyager:voyager-transitions", version.ref = "voyager" } +spotless-gradle = { group = "com.diffplug.spotless", name = "spotless-plugin-gradle", version.ref = "spotless" } +ktlint-core = { module = "com.pinterest.ktlint:ktlint-cli", version.ref = "ktlint-core" } + [bundles] okhttp = ["okhttp-core", "okhttp-logging", "okhttp-brotli", "okhttp-dnsoverhttps"] js-engine = ["quickjs-android"]