From 04c99a5a36041b56061a131d474e45efabd6a4ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=99=BD-=E7=99=BD?= Date: Fri, 31 Mar 2023 21:14:32 +0800 Subject: [PATCH] Add ktlint, resolve #999 --- .github/workflows/ci.yml | 3 +++ .github/workflows/pull_request.yml | 3 +++ build.gradle.kts | 4 ++++ ktlint.gradle.kts | 38 ++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+) create mode 100644 ktlint.gradle.kts diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 33ba5506e..0384466a6 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -27,6 +27,9 @@ jobs: run: | echo "y" | sudo ${ANDROID_HOME}/tools/bin/sdkmanager --install "ndk;25.2.9519653" --sdk_root=${ANDROID_SDK_ROOT} &> /dev/null + - name: Ktlint Check + run: ./gradlew ktlintCheck + - name: Gradle Build uses: gradle/gradle-build-action@v2 with: diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 1958f6d1f..54d87d050 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -22,6 +22,9 @@ jobs: run: | echo "y" | sudo ${ANDROID_HOME}/tools/bin/sdkmanager --install "ndk;25.2.9519653" --sdk_root=${ANDROID_SDK_ROOT} &> /dev/null + - name: Ktlint Check + run: ./gradlew ktlintCheck + - name: Gradle Build uses: gradle/gradle-build-action@v2 with: diff --git a/build.gradle.kts b/build.gradle.kts index c5c3c51de..de45f1bdf 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,6 +5,10 @@ plugins { id("com.google.devtools.ksp") version "1.8.10-1.0.9" apply false } +allprojects { + apply(from = "$rootDir/ktlint.gradle.kts") +} + tasks.register("Delete", Delete::class) { delete(rootProject.buildDir) } diff --git a/ktlint.gradle.kts b/ktlint.gradle.kts new file mode 100644 index 000000000..3ba86dacb --- /dev/null +++ b/ktlint.gradle.kts @@ -0,0 +1,38 @@ +val ktlint by configurations.creating + +dependencies { + ktlint("com.pinterest:ktlint:0.48.2") { + attributes { + attribute(Bundling.BUNDLING_ATTRIBUTE, objects.named(Bundling.EXTERNAL)) + } + } + // ktlint(project(":custom-ktlint-ruleset")) // in case of custom ruleset +} + +val ktlintCheck by tasks.registering(JavaExec::class) { + group = LifecycleBasePlugin.VERIFICATION_GROUP + description = "Check Kotlin code style" + classpath = ktlint + mainClass.set("com.pinterest.ktlint.Main") + // see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information + args( + "**/*.kt", + "**/*.kts", + "!**/build/**", + ) +} + +val ktlintFormat by tasks.registering(JavaExec::class) { + group = LifecycleBasePlugin.VERIFICATION_GROUP + description = "Check Kotlin code style and format" + classpath = ktlint + mainClass.set("com.pinterest.ktlint.Main") + jvmArgs("--add-opens=java.base/java.lang=ALL-UNNAMED") + // see https://pinterest.github.io/ktlint/install/cli/#command-line-usage for more information + args( + "-F", + "**/*.kt", + "**/*.kts", + "!**/build/**", + ) +}