From 024dc9ec347d8445f73c89b3694c158e8a2644a3 Mon Sep 17 00:00:00 2001 From: James Rich <2199651+jamesarich@users.noreply.github.com> Date: Mon, 21 Jul 2025 18:31:35 -0500 Subject: [PATCH] chore(spotless): apply code style improvements and formatting (#2459) Signed-off-by: James Rich <2199651+jamesarich@users.noreply.github.com> --- .github/workflows/reusable-android-build.yml | 8 +- app/build.gradle.kts | 128 +++++++++++++------ config/copyright.txt | 16 +++ gradle/libs.versions.toml | 2 + 4 files changed, 108 insertions(+), 46 deletions(-) create mode 100644 config/copyright.txt diff --git a/.github/workflows/reusable-android-build.yml b/.github/workflows/reusable-android-build.yml index 0f0905286..77f4abe93 100644 --- a/.github/workflows/reusable-android-build.yml +++ b/.github/workflows/reusable-android-build.yml @@ -64,8 +64,8 @@ jobs: - name: Expose Version Code as Environment Variable run: echo "VERSION_CODE=${{ steps.calculate_version_code.outputs.versionCode }}" >> $GITHUB_ENV - - name: Run Detekt, Build, Lint, and Local Tests - run: ./gradlew :app:detekt :app:lintFdroidDebug :app:lintGoogleDebug :app:assembleDebug :app:testFdroidDebug :app:testGoogleDebug --configuration-cache --scan + - name: Run Spotless, Detekt, Build, Lint, and Local Tests + run: ./gradlew :app:spotlessCheck :app:detekt :app:lintFdroidDebug :app:lintGoogleDebug :app:assembleDebug :app:testFdroidDebug :app:testGoogleDebug --configuration-cache --scan env: VERSION_CODE: ${{ env.VERSION_CODE }} - name: Upload F-Droid debug artifact @@ -82,11 +82,11 @@ jobs: name: googleDebug path: app/build/outputs/apk/google/debug/app-google-debug.apk retention-days: 14 - - name: Upload build and Detekt reports + - name: Upload reports if: ${{ inputs.upload_artifacts }} uses: actions/upload-artifact@v4 with: - name: build-and-detekt-reports + name: upload-reports path: | app/build/reports **/build/reports/detekt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index f7b306d85..e167a42cb 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -28,10 +28,12 @@ plugins { alias(libs.plugins.protobuf) alias(libs.plugins.devtools.ksp) alias(libs.plugins.detekt) + alias(libs.plugins.spotless) } val keystorePropertiesFile = rootProject.file("keystore.properties") val keystoreProperties = Properties() + if (keystorePropertiesFile.exists()) { FileInputStream(keystorePropertiesFile).use { keystoreProperties.load(it) } } @@ -52,42 +54,65 @@ android { applicationId = Configs.APPLICATION_ID minSdk = Configs.MIN_SDK_VERSION targetSdk = Configs.TARGET_SDK - versionCode = System.getenv("VERSION_CODE")?.toIntOrNull() - ?: 30630 + versionCode = System.getenv("VERSION_CODE")?.toIntOrNull() ?: 30630 testInstrumentationRunner = "com.geeksville.mesh.TestRunner" buildConfigField("String", "MIN_FW_VERSION", "\"${Configs.MIN_FW_VERSION}\"") buildConfigField("String", "ABS_MIN_FW_VERSION", "\"${Configs.ABS_MIN_FW_VERSION}\"") // per https://developer.android.com/studio/write/vector-asset-studio vectorDrawables.useSupportLibrary = true - // We have to list all translated languages here, because some of our libs have bogus languages that google play + // We have to list all translated languages here, + // because some of our libs have bogus languages that google play // doesn't like and we need to strip them (gr) @Suppress("UnstableApiUsage") androidResources.localeFilters.addAll( listOf( - "bg", "ca", "cs", "de", - "el", "en", "es", "et", - "fi", "fr", "fr-rHT", "ga", - "gl", "hr", "hu", "is", - "it", "iw", "ja", "ko", - "lt", "nl", "nb", "pl", - "pt", "pt-rBR", "ro", - "ru", "sk", "sl", "sq", - "sr", "sv", "tr", "zh-rCN", - "zh-rTW", "uk" + "bg", + "ca", + "cs", + "de", + "el", + "en", + "es", + "et", + "fi", + "fr", + "fr-rHT", + "ga", + "gl", + "hr", + "hu", + "is", + "it", + "iw", + "ja", + "ko", + "lt", + "nl", + "nb", + "pl", + "pt", + "pt-rBR", + "ro", + "ru", + "sk", + "sl", + "sq", + "sr", + "sv", + "tr", + "zh-rCN", + "zh-rTW", + "uk", ) ) - ndk { - abiFilters += listOf("armeabi-v7a", "arm64-v8a", "x86", "x86_64") - } + ndk { abiFilters += listOf("armeabi-v7a", "arm64-v8a", "x86", "x86_64") } } flavorDimensions.add("default") productFlavors { val versionCode = defaultConfig.versionCode create("fdroid") { dimension = "default" - dependenciesInfo { - includeInApk = false - } + dependenciesInfo { includeInApk = false } versionName = "${Configs.VERSION_NAME_BASE} ($versionCode) fdroid" } create("google") { @@ -107,18 +132,12 @@ android { isShrinkResources = true proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), - "proguard-rules.pro" + "proguard-rules.pro", ) } - named("debug") { - isPseudoLocalesEnabled = true - } - } - bundle { - language { - enableSplit = false - } + named("debug") { isPseudoLocalesEnabled = true } } + bundle { language { enableSplit = false } } buildFeatures { viewBinding = true compose = true @@ -142,16 +161,14 @@ kotlin { "-opt-in=kotlin.RequiresOptIn", "-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi", "-Xcontext-receivers", - "-Xannotation-default-target=param-property" + "-Xannotation-default-target=param-property", ) } } // per protobuf-gradle-plugin docs, this is recommended for android protobuf { - protoc { - artifact = libs.protobuf.protoc.get().toString() - } + protoc { artifact = libs.protobuf.protoc.get().toString() } generateProtoTasks { all().forEach { task -> task.builtins { @@ -167,9 +184,7 @@ androidComponents { onVariants(selector().all()) { variant -> project.afterEvaluate { val capName = variant.name.replaceFirstChar { it.uppercase() } - tasks.named("ksp${capName}Kotlin") { - dependsOn("generate${capName}Proto") - } + tasks.named("ksp${capName}Kotlin") { dependsOn("generate${capName}Proto") } } } } @@ -238,13 +253,11 @@ dependencies { } ksp { -// arg("room.generateKotlin", "true") + // arg("room.generateKotlin", "true") arg("room.schemaLocation", "$projectDir/schemas") } -repositories { - maven { url = uri("https://jitpack.io") } -} +repositories { maven { url = uri("https://jitpack.io") } } detekt { config.setFrom("../config/detekt/detekt.yml") @@ -252,13 +265,44 @@ detekt { } val googleServiceKeywords = listOf("crashlytics", "google") + tasks.configureEach { if ( - googleServiceKeywords.any { - name.contains(it, ignoreCase = true) - } && name.contains("fdroid", ignoreCase = true) + googleServiceKeywords.any { name.contains(it, ignoreCase = true) } && + name.contains("fdroid", ignoreCase = true) ) { project.logger.lifecycle("Disabling task for F-Droid: $name") enabled = false } } + +spotless { + ratchetFrom("origin/main") + kotlin { + target("src/*/kotlin/**/*.kt", "src/*/java/**/*.kt") + targetExclude("**/build/**/*.kt") + ktlint() + ktfmt().kotlinlangStyle() + licenseHeaderFile(rootProject.file("config/copyright.txt")) + } + kotlinGradle { + target("**/*.gradle.kts") + ktlint() + ktfmt().kotlinlangStyle() + } +} + +tasks.register("createSpotlessPreCommitHook") { + val gitHooksDirectory = rootProject.file(".git/hooks") + if (!gitHooksDirectory.exists()) gitHooksDirectory.mkdirs() + val preCommitHook = rootProject.file(".git/hooks/pre-commit") + preCommitHook.writeText( + """ + #!/bin/bash + echo "Running spotless check" + ./gradlew spotlessApply + """ + .trimIndent() + ) + preCommitHook.setExecutable(true) +} diff --git a/config/copyright.txt b/config/copyright.txt new file mode 100644 index 000000000..9f680ab62 --- /dev/null +++ b/config/copyright.txt @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2025 Meshtastic LLC + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ \ No newline at end of file diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 59415d24d..82b2f5cec 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -47,6 +47,7 @@ usb-serial-android = "3.9.0" work-runtime-ktx = "2.10.2" zxing-android-embedded = "4.3.0" zxing-core = "3.5.3" +spotless = "7.1.0" [libraries] agp = { group = "com.android.tools.build", name = "gradle", version.ref = "agp" } @@ -200,3 +201,4 @@ protobuf = { id = "com.google.protobuf" } android-library = { id = "com.android.library" } google-services = { id = "com.google.gms.google-services" } firebase-crashlytics = { id = "com.google.firebase.crashlytics" } +spotless = { id = "com.diffplug.spotless", version .ref= "spotless" }