Skip to content

Commit 0d4887e

Browse files
authored
Move Spotless configurations from buildscripts/init.gradle.kts to build.gradle.kts (#1549)
Decouple each project from `buildscripts/init.gradle.kts` Each project currently uses Spotless with `--init-script buildscripts/init.gradle.kts`. The team decided not to configure Spotless in `build.gradle.kts` because Spotless did not yet support Gradle Configuration Cache, which would significantly impact build performance. That means that these projects cannot use the typical Gradle command `./gradlew spotlessCheck`. The flag `--init-script buildscripts/init.gradle.kts` is currently used in this GitHub Actions configuration that is shared by all 6 projects with `uses: ./.github/workflows/build-sample.yml`. https://github.com/android/compose-samples/blob/57913c513a17c39a93d9a5bab4284fbf9c85dc98/.github/workflows/build-sample.yml#L62-L62 Spotless [version 7.0.0 supports configuration cache](https://github.com/diffplug/spotless/blob/main/plugin-gradle/CHANGES.md#700---2025-01-06). From the release notes: "Full, no asterisk support for configuration cache (end of diffplug/spotless#987)" As a first step, we can move the Spotless configuration from `buildscripts/init.gradle.kts` to `build.gradle.kts`. This has the immediate benefit that you can discover and run Spotless. ./gradlew spotlessCheck This configuration uses `ratchetFrom = "origin/main"` which only enforces Spotless checks on files that are different from `origin/main`. In a future step, we can remove this line and fix the Spotless checks for each project. For example, Jetcaster has violations of: `standard:filename`, `standard:property-naming`, `standard:backing-property-naming`, `standard:package-name`. Instead of suppressing or fixing all potential issues for all 6 projects now, we can separate the configuration (right now) and fix each project one-at-a-time (later).
2 parents 57913c5 + 59f2ba2 commit 0d4887e

File tree

19 files changed

+299
-433
lines changed

19 files changed

+299
-433
lines changed

.github/workflows/build-sample.yml

+5-1
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ jobs:
3636
steps:
3737
- name: Checkout
3838
uses: actions/checkout@v4
39+
with:
40+
# https://github.com/diffplug/spotless/issues/710
41+
# Check out full history for Spotless to ensure ratchetFrom can find the ratchet version
42+
fetch-depth: 0
3943

4044
- name: Copy CI gradle.properties
4145
run: mkdir -p ~/.gradle ; cp .github/ci-gradle.properties ~/.gradle/gradle.properties
@@ -59,7 +63,7 @@ jobs:
5963

6064
- name: Check formatting
6165
working-directory: ${{ inputs.path }}
62-
run: ./gradlew --init-script buildscripts/init.gradle.kts spotlessCheck --stacktrace
66+
run: ./gradlew spotlessCheck --stacktrace
6367

6468
- name: Check lint
6569
working-directory: ${{ inputs.path }}

JetLagged/build.gradle.kts

+47
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,53 @@ plugins {
2121
alias(libs.plugins.kotlin.android) apply false
2222
alias(libs.plugins.kotlin.parcelize) apply false
2323
alias(libs.plugins.compose) apply false
24+
alias(libs.plugins.spotless) apply false
2425
}
2526

2627
apply("${project.rootDir}/buildscripts/toml-updater-config.gradle")
28+
29+
subprojects {
30+
apply(plugin = "com.diffplug.spotless")
31+
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
32+
ratchetFrom = "origin/main"
33+
kotlin {
34+
target("**/*.kt")
35+
targetExclude("**/build/**/*.kt")
36+
ktlint().editorConfigOverride(
37+
mapOf(
38+
"ktlint_code_style" to "android_studio",
39+
"ij_kotlin_allow_trailing_comma" to true,
40+
"ktlint_function_naming_ignore_when_annotated_with" to "Composable",
41+
// These rules were introduced in ktlint 0.46.0 and should not be
42+
// enabled without further discussion. They are disabled for now.
43+
// See: https://github.com/pinterest/ktlint/releases/tag/0.46.0
44+
"disabled_rules" to
45+
"filename," +
46+
"annotation,annotation-spacing," +
47+
"argument-list-wrapping," +
48+
"double-colon-spacing," +
49+
"enum-entry-name-case," +
50+
"multiline-if-else," +
51+
"no-empty-first-line-in-method-block," +
52+
"package-name," +
53+
"trailing-comma," +
54+
"spacing-around-angle-brackets," +
55+
"spacing-between-declarations-with-annotations," +
56+
"spacing-between-declarations-with-comments," +
57+
"unary-op-spacing"
58+
)
59+
)
60+
licenseHeaderFile(rootProject.file("spotless/copyright.kt"))
61+
}
62+
format("kts") {
63+
target("**/*.kts")
64+
targetExclude("**/build/**/*.kts")
65+
// Look for the first line that doesn't have a block comment (assumed to be the license)
66+
licenseHeaderFile(rootProject.file("spotless/copyright.kt"), "(^(?![\\/ ]\\*).*$)")
67+
}
68+
kotlinGradle {
69+
target("*.gradle.kts")
70+
ktlint()
71+
}
72+
}
73+
}

JetLagged/buildscripts/init.gradle.kts

-72
This file was deleted.

JetLagged/gradle/libs.versions.toml

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ roborazzi = "1.42.0"
5353
rome = "2.1.0"
5454
room = "2.6.1"
5555
secrets = "2.0.1"
56+
spotless = "7.0.3"
5657
# @keep
5758
targetSdk = "33"
5859
version-catalog-update = "0.8.5"
@@ -171,4 +172,5 @@ kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", versi
171172
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
172173
roborazzi = { id = "io.github.takahirom.roborazzi", version.ref = "roborazzi" }
173174
secrets = { id = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin", version.ref = "secrets" }
175+
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
174176
version-catalog-update = { id = "nl.littlerobots.version-catalog-update", version.ref = "version-catalog-update" }

JetNews/build.gradle.kts

+47
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,53 @@ plugins {
2121
alias(libs.plugins.kotlin.android) apply false
2222
alias(libs.plugins.kotlin.parcelize) apply false
2323
alias(libs.plugins.compose) apply false
24+
alias(libs.plugins.spotless) apply false
2425
}
2526

2627
apply("${project.rootDir}/buildscripts/toml-updater-config.gradle")
28+
29+
subprojects {
30+
apply(plugin = "com.diffplug.spotless")
31+
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
32+
ratchetFrom = "origin/main"
33+
kotlin {
34+
target("**/*.kt")
35+
targetExclude("**/build/**/*.kt")
36+
ktlint().editorConfigOverride(
37+
mapOf(
38+
"ktlint_code_style" to "android_studio",
39+
"ij_kotlin_allow_trailing_comma" to true,
40+
"ktlint_function_naming_ignore_when_annotated_with" to "Composable",
41+
// These rules were introduced in ktlint 0.46.0 and should not be
42+
// enabled without further discussion. They are disabled for now.
43+
// See: https://github.com/pinterest/ktlint/releases/tag/0.46.0
44+
"disabled_rules" to
45+
"filename," +
46+
"annotation,annotation-spacing," +
47+
"argument-list-wrapping," +
48+
"double-colon-spacing," +
49+
"enum-entry-name-case," +
50+
"multiline-if-else," +
51+
"no-empty-first-line-in-method-block," +
52+
"package-name," +
53+
"trailing-comma," +
54+
"spacing-around-angle-brackets," +
55+
"spacing-between-declarations-with-annotations," +
56+
"spacing-between-declarations-with-comments," +
57+
"unary-op-spacing"
58+
)
59+
)
60+
licenseHeaderFile(rootProject.file("spotless/copyright.kt"))
61+
}
62+
format("kts") {
63+
target("**/*.kts")
64+
targetExclude("**/build/**/*.kts")
65+
// Look for the first line that doesn't have a block comment (assumed to be the license)
66+
licenseHeaderFile(rootProject.file("spotless/copyright.kt"), "(^(?![\\/ ]\\*).*$)")
67+
}
68+
kotlinGradle {
69+
target("*.gradle.kts")
70+
ktlint()
71+
}
72+
}
73+
}

JetNews/buildscripts/init.gradle.kts

-72
This file was deleted.

JetNews/gradle/libs.versions.toml

+2
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ roborazzi = "1.42.0"
5353
rome = "2.1.0"
5454
room = "2.6.1"
5555
secrets = "2.0.1"
56+
spotless = "7.0.3"
5657
# @keep
5758
targetSdk = "33"
5859
version-catalog-update = "0.8.5"
@@ -171,4 +172,5 @@ kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", versi
171172
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
172173
roborazzi = { id = "io.github.takahirom.roborazzi", version.ref = "roborazzi" }
173174
secrets = { id = "com.google.android.libraries.mapsplatform.secrets-gradle-plugin", version.ref = "secrets" }
175+
spotless = { id = "com.diffplug.spotless", version.ref = "spotless" }
174176
version-catalog-update = { id = "nl.littlerobots.version-catalog-update", version.ref = "version-catalog-update" }

Jetcaster/build.gradle.kts

+47
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,53 @@ plugins {
2424
alias(libs.plugins.hilt) apply false
2525
alias(libs.plugins.ksp) apply false
2626
alias(libs.plugins.compose) apply false
27+
alias(libs.plugins.spotless) apply false
2728
}
2829

2930
apply("${project.rootDir}/buildscripts/toml-updater-config.gradle")
31+
32+
subprojects {
33+
apply(plugin = "com.diffplug.spotless")
34+
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
35+
ratchetFrom = "origin/main"
36+
kotlin {
37+
target("**/*.kt")
38+
targetExclude("**/build/**/*.kt")
39+
ktlint().editorConfigOverride(
40+
mapOf(
41+
"ktlint_code_style" to "android_studio",
42+
"ij_kotlin_allow_trailing_comma" to true,
43+
"ktlint_function_naming_ignore_when_annotated_with" to "Composable",
44+
// These rules were introduced in ktlint 0.46.0 and should not be
45+
// enabled without further discussion. They are disabled for now.
46+
// See: https://github.com/pinterest/ktlint/releases/tag/0.46.0
47+
"disabled_rules" to
48+
"filename," +
49+
"annotation,annotation-spacing," +
50+
"argument-list-wrapping," +
51+
"double-colon-spacing," +
52+
"enum-entry-name-case," +
53+
"multiline-if-else," +
54+
"no-empty-first-line-in-method-block," +
55+
"package-name," +
56+
"trailing-comma," +
57+
"spacing-around-angle-brackets," +
58+
"spacing-between-declarations-with-annotations," +
59+
"spacing-between-declarations-with-comments," +
60+
"unary-op-spacing"
61+
)
62+
)
63+
licenseHeaderFile(rootProject.file("spotless/copyright.kt"))
64+
}
65+
format("kts") {
66+
target("**/*.kts")
67+
targetExclude("**/build/**/*.kts")
68+
// Look for the first line that doesn't have a block comment (assumed to be the license)
69+
licenseHeaderFile(rootProject.file("spotless/copyright.kt"), "(^(?![\\/ ]\\*).*$)")
70+
}
71+
kotlinGradle {
72+
target("*.gradle.kts")
73+
ktlint()
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)