Skip to content

Commit f72628b

Browse files
committed
Upgrade Spotless to Spotless 7
1 parent 1da1d9d commit f72628b

File tree

6 files changed

+119
-53
lines changed

6 files changed

+119
-53
lines changed

.github/workflows/apply_spotless.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
java-version: '17'
4343

4444
- name: Run spotlessApply
45-
run: ./gradlew spotlessApply --init-script gradle/init.gradle.kts --no-configuration-cache --stacktrace
45+
run: ./gradlew spotlessApply --stacktrace
4646

4747
- name: Auto-commit if spotlessApply has changes
4848
uses: stefanzweifel/git-auto-commit-action@v5

build.gradle.kts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,89 @@ plugins {
1313
alias(libs.plugins.kotlin.multiplatform) apply false
1414
alias(libs.plugins.android.kotlin.multiplatform.library) apply false
1515
alias(libs.plugins.android.lint) apply false
16+
alias(libs.plugins.spotless) apply false
17+
}
18+
19+
subprojects {
20+
apply(plugin = "com.diffplug.spotless")
21+
extensions.configure<com.diffplug.gradle.spotless.SpotlessExtension> {
22+
kotlin {
23+
target("**/*.kt")
24+
targetExclude("**/build/**/*.kt")
25+
26+
val disabledRules = arrayOf(
27+
// These rules were introduced in ktlint 0.46.0 and should not be
28+
// enabled without further discussion. They are disabled for now.
29+
// See: https://github.com/pinterest/ktlint/releases/tag/0.46.0
30+
"filename",
31+
"annotation",
32+
"annotation-spacing",
33+
"argument-list-wrapping",
34+
"double-colon-spacing",
35+
"enum-entry-name-case",
36+
"multiline-if-else",
37+
"no-empty-first-line-in-method-block",
38+
"package-name",
39+
"trailing-comma",
40+
"spacing-around-angle-brackets",
41+
"spacing-between-declarations-with-annotations",
42+
"spacing-between-declarations-with-comments",
43+
"unary-op-spacing",
44+
"no-trailing-spaces",
45+
"max-line-length",
46+
// Disabled rules that were introduced or changed between 0.46.0 ~ 1.50.0
47+
"class-signature",
48+
"trailing-comma-on-call-site",
49+
"trailing-comma-on-declaration-site",
50+
"comment-wrapping",
51+
"function-literal",
52+
"function-signature",
53+
"function-expression-body",
54+
"function-start-of-body-spacing",
55+
"multiline-expression-wrapping",
56+
)
57+
58+
ktlint(libs.versions.ktlint.get()).editorConfigOverride(
59+
mapOf(
60+
"android" to "true",
61+
"ktlint_code_style" to "android_studio",
62+
"ij_kotlin_allow_trailing_comma" to "true",
63+
) + disabledRules.map { Pair("ktlint_standard_$it", "disabled") }
64+
)
65+
66+
// ktlint 7.0.0 introduces lints, which existing snippets do not satisfy
67+
val kotlinSuppressLints = arrayOf(
68+
"standard:function-naming",
69+
"standard:property-naming",
70+
"standard:class-naming",
71+
"standard:max-line-length",
72+
"standard:comment-wrapping",
73+
"standard:import-ordering",
74+
"standard:filename",
75+
"standard:backing-property-naming",
76+
)
77+
for (lint in kotlinSuppressLints) {
78+
suppressLintsFor {
79+
step = "ktlint"
80+
shortCode = lint
81+
}
82+
}
83+
84+
licenseHeaderFile(rootProject.file("spotless/copyright.kt"))
85+
}
86+
kotlinGradle {
87+
target("**/*.kts")
88+
targetExclude("**/build/**/*.kts")
89+
// Look for the first line that doesn't have a block comment (assumed to be the license)
90+
licenseHeaderFile(rootProject.file("spotless/copyright.kts"), "(^(?![\\/ ]\\*).*$)")
91+
}
92+
format("xml") {
93+
target("**/*.xml")
94+
targetExclude("**/build/**/*.xml")
95+
// Look for the root tag or a tag that is a snippet
96+
licenseHeaderFile(rootProject.file("spotless/copyright.xml"), "(<[a-zA-Z])|(<!--\\s+(//\\s*)?\\[START)").skipLinesMatching(".*START.*")
97+
}
98+
}
1699
}
17100

18101
apply("${project.rootDir}/buildscripts/toml-updater-config.gradle")

gradle/init.gradle.kts

Lines changed: 0 additions & 52 deletions
This file was deleted.

gradle/libs.versions.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ guava = "33.4.8-jre"
4747
hilt = "2.57"
4848
horologist = "0.8.1-alpha"
4949
junit = "4.13.2"
50+
ktlint = "1.5.0"
5051
kotlin = "2.2.10"
5152
kotlinCoroutinesOkhttp = "1.0"
5253
kotlinxCoroutinesGuava = "1.10.2"
@@ -64,6 +65,7 @@ okHttp = "5.1.0"
6465
playServicesWearable = "19.0.0"
6566
protolayout = "1.3.0"
6667
recyclerview = "1.4.0"
68+
spotless = "7.0.2"
6769
targetSdk = "35"
6870
tiles = "1.5.0"
6971
tracing = "1.3.0"
@@ -207,3 +209,4 @@ kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", versi
207209
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
208210
version-catalog-update = { id = "nl.littlerobots.version-catalog-update", version.ref = "version-catalog-update" }
209211
android-lint = { id = "com.android.lint", version.ref = "androidGradlePlugin" }
212+
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }

spotless/copyright.kts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright $YEAR The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+

spotless/copyright.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright $YEAR The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
https://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->

0 commit comments

Comments
 (0)