build: switch to Spotless for formatting

Signed-off-by: Harsh Shandilya <me@msfjarvis.dev>
This commit is contained in:
Harsh Shandilya 2021-05-07 16:27:55 +05:30
parent 00e0881e47
commit 7050f0ea40
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
11 changed files with 73 additions and 44 deletions

View file

@ -20,7 +20,7 @@
## :pencil: Checklist ## :pencil: Checklist
<!--- Put an `x` in the boxes that apply --> <!--- Put an `x` in the boxes that apply -->
- [ ] I formatted the code with the IDE's reformat action (Ctrl + Shift + L/Cmd + Shift + L) - [ ] I formatted the code `./gradlew spotlessApply`
- [ ] I reviewed submitted code - [ ] I reviewed submitted code
- [ ] I added a [CHANGELOG](CHANGELOG.md) entry if applicable - [ ] I added a [CHANGELOG](CHANGELOG.md) entry if applicable

View file

@ -44,6 +44,8 @@ jobs:
- name: Checkout repository - name: Checkout repository
if: ${{ steps.service-changed.outputs.result == 'true' }} if: ${{ steps.service-changed.outputs.result == 'true' }}
uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f
with:
fetch-depth: 0
#with: #with:
# ref: refs/pull/${{ github.event.pull_request.number }}/merge # ref: refs/pull/${{ github.event.pull_request.number }}/merge
@ -55,7 +57,7 @@ jobs:
if: ${{ steps.service-changed.outputs.result == 'true' }} if: ${{ steps.service-changed.outputs.result == 'true' }}
uses: burrunan/gradle-cache-action@03c71a8ba93d670980695505f48f49daf43704a6 uses: burrunan/gradle-cache-action@03c71a8ba93d670980695505f48f49daf43704a6
with: with:
arguments: apiCheck testFreeDebug lintFreeDebug ktfmtCheck arguments: apiCheck testFreeDebug lintFreeDebug spotlessCheck
- name: Run instrumentation tests - name: Run instrumentation tests
if: ${{ steps.service-changed.outputs.result == 'true' }} if: ${{ steps.service-changed.outputs.result == 'true' }}

View file

@ -9,4 +9,4 @@ plugins {
id("dev.zacsweers.kgp-150-leak-patcher") version "1.0.1" id("dev.zacsweers.kgp-150-leak-patcher") version "1.0.1"
} }
allprojects { apply(plugin = "com.ncorti.ktfmt.gradle") } allprojects { apply(plugin = "com.diffplug.spotless") }

View file

@ -3,10 +3,7 @@
* SPDX-License-Identifier: GPL-3.0-only * SPDX-License-Identifier: GPL-3.0-only
*/ */
plugins { plugins { `kotlin-dsl` }
`kotlin-dsl`
id("com.ncorti.ktfmt.gradle") version "0.5.0"
}
repositories { repositories {
google() google()
@ -26,11 +23,6 @@ repositories {
} }
} }
ktfmt {
googleStyle()
maxWidth.set(120)
}
gradlePlugin { gradlePlugin {
plugins { plugins {
register("aps") { register("aps") {
@ -50,13 +42,14 @@ gradlePlugin {
dependencies { dependencies {
implementation("com.android.tools.build:gradle:4.2.0") implementation("com.android.tools.build:gradle:4.2.0")
implementation("org.jetbrains.kotlinx:binary-compatibility-validator:0.5.0") implementation("com.diffplug.spotless:spotless-plugin-gradle:5.12.4")
implementation("org.jetbrains.dokka:dokka-gradle-plugin:1.4.32")
implementation("de.undercouch:gradle-download-task:4.1.1")
implementation("com.google.dagger:hilt-android-gradle-plugin:2.35.1") implementation("com.google.dagger:hilt-android-gradle-plugin:2.35.1")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0") implementation("com.squareup.okhttp3:okhttp:4.9.0")
implementation("com.ncorti.ktfmt.gradle:plugin:0.5.0")
implementation("com.vanniktech:gradle-maven-publish-plugin:0.15.1") implementation("com.vanniktech:gradle-maven-publish-plugin:0.15.1")
implementation("com.squareup.okhttp3:okhttp:4.9.0") implementation("com.squareup.okhttp3:okhttp:4.9.0")
implementation("com.vdurmont:semver4j:3.1.0") implementation("com.vdurmont:semver4j:3.1.0")
implementation("de.undercouch:gradle-download-task:4.1.1")
implementation("org.jetbrains.dokka:dokka-gradle-plugin:1.4.32")
implementation("org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.0")
implementation("org.jetbrains.kotlinx:binary-compatibility-validator:0.5.0")
} }

View file

@ -7,7 +7,6 @@ import com.android.build.gradle.TestedExtension
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import org.gradle.api.JavaVersion import org.gradle.api.JavaVersion
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.tasks.Delete
import org.gradle.api.tasks.testing.Test import org.gradle.api.tasks.testing.Test
import org.gradle.api.tasks.testing.logging.TestLogEvent import org.gradle.api.tasks.testing.logging.TestLogEvent
import org.gradle.api.tasks.wrapper.Wrapper import org.gradle.api.tasks.wrapper.Wrapper
@ -21,8 +20,6 @@ import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
* `buildscript` block in the top-level build.gradle.kts file. * `buildscript` block in the top-level build.gradle.kts file.
*/ */
internal fun Project.configureForRootProject() { internal fun Project.configureForRootProject() {
// register task for cleaning the build directory in the root project
tasks.register("clean", Delete::class.java) { delete(rootProject.buildDir) }
tasks.withType<Wrapper> { tasks.withType<Wrapper> {
gradleVersion = "7.0" gradleVersion = "7.0"
distributionType = Wrapper.DistributionType.ALL distributionType = Wrapper.DistributionType.ALL
@ -78,7 +75,8 @@ fun Project.isSnapshot(): Boolean {
/** Apply configurations for app module */ /** Apply configurations for app module */
@Suppress("UnstableApiUsage") @Suppress("UnstableApiUsage")
internal fun BaseAppModuleExtension.configureAndroidApplicationOptions(project: Project) { internal fun BaseAppModuleExtension.configureAndroidApplicationOptions(project: Project) {
val minifySwitch = project.providers.environmentVariable("DISABLE_MINIFY").forUseAtConfigurationTime() val minifySwitch =
project.providers.environmentVariable("DISABLE_MINIFY").forUseAtConfigurationTime()
adbOptions.installOptions("--user 0") adbOptions.installOptions("--user 0")

View file

@ -18,7 +18,8 @@ import org.w3c.dom.Document
private const val EXCEPTION_MESSAGE = private const val EXCEPTION_MESSAGE =
"""Applying `crowdin-plugin` requires a projectName to be configured via the "crowdin" extension.""" """Applying `crowdin-plugin` requires a projectName to be configured via the "crowdin" extension."""
private const val CROWDIN_BUILD_API_URL = "https://api.crowdin.com/api/project/%s/export?login=%s&account-key=%s" private const val CROWDIN_BUILD_API_URL =
"https://api.crowdin.com/api/project/%s/export?login=%s&account-key=%s"
class CrowdinDownloadPlugin : Plugin<Project> { class CrowdinDownloadPlugin : Plugin<Project> {
@ -69,7 +70,10 @@ class CrowdinDownloadPlugin : Plugin<Project> {
doLast { doLast {
val sourceSets = arrayOf("main", "nonFree") val sourceSets = arrayOf("main", "nonFree")
for (sourceSet in sourceSets) { for (sourceSet in sourceSets) {
val stringFiles = File("${projectDir}/src/$sourceSet").walkTopDown().filter { it.name == "strings.xml" } val stringFiles =
File("${projectDir}/src/$sourceSet").walkTopDown().filter {
it.name == "strings.xml"
}
val sourceFile = val sourceFile =
stringFiles.firstOrNull { it.path.endsWith("values/strings.xml") } stringFiles.firstOrNull { it.path.endsWith("values/strings.xml") }
?: throw GradleException("No root strings.xml found in '$sourceSet' sourceSet") ?: throw GradleException("No root strings.xml found in '$sourceSet' sourceSet")

View file

@ -1,11 +0,0 @@
/*
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
import com.ncorti.ktfmt.gradle.KtfmtExtension
fun KtfmtExtension.configureKtfmt() {
googleStyle()
maxWidth.set(120)
}

View file

@ -7,8 +7,8 @@ import com.android.build.gradle.TestedExtension
import com.android.build.gradle.internal.dsl.BaseAppModuleExtension import com.android.build.gradle.internal.dsl.BaseAppModuleExtension
import com.android.build.gradle.internal.plugins.AppPlugin import com.android.build.gradle.internal.plugins.AppPlugin
import com.android.build.gradle.internal.plugins.LibraryPlugin import com.android.build.gradle.internal.plugins.LibraryPlugin
import com.ncorti.ktfmt.gradle.KtfmtExtension import com.diffplug.gradle.spotless.SpotlessExtension
import com.ncorti.ktfmt.gradle.KtfmtPlugin import com.diffplug.gradle.spotless.SpotlessPlugin
import org.gradle.api.Plugin import org.gradle.api.Plugin
import org.gradle.api.Project import org.gradle.api.Project
import org.gradle.api.plugins.JavaLibraryPlugin import org.gradle.api.plugins.JavaLibraryPlugin
@ -45,12 +45,15 @@ class PasswordStorePlugin : Plugin<Project> {
project.configureExplicitApi() project.configureExplicitApi()
} }
is AppPlugin -> { is AppPlugin -> {
project.extensions.getByType<BaseAppModuleExtension>().configureAndroidApplicationOptions(project) project
.extensions
.getByType<BaseAppModuleExtension>()
.configureAndroidApplicationOptions(project)
project.extensions.getByType<BaseAppModuleExtension>().configureBuildSigning(project) project.extensions.getByType<BaseAppModuleExtension>().configureBuildSigning(project)
project.extensions.getByType<TestedExtension>().configureCommonAndroidOptions() project.extensions.getByType<TestedExtension>().configureCommonAndroidOptions()
} }
is KtfmtPlugin -> { is SpotlessPlugin -> {
project.extensions.getByType<KtfmtExtension>().configureKtfmt() project.extensions.getByType<SpotlessExtension>().configureSpotless()
} }
is SigningPlugin -> { is SigningPlugin -> {
project.extensions.getByType<SigningExtension>().configureBuildSigning() project.extensions.getByType<SigningExtension>().configureBuildSigning()

View file

@ -0,0 +1,20 @@
/*
* Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
* SPDX-License-Identifier: GPL-3.0-only
*/
import com.diffplug.gradle.spotless.SpotlessExtension
internal fun SpotlessExtension.configureSpotless() {
ratchetFrom = "origin/develop"
kotlin {
ktfmt().googleStyle()
target("src/**/*.kt", "**/*.kts")
}
format("xml") {
target("**/*.xml")
trimTrailingWhitespace()
indentWithSpaces()
endWithNewline()
}
}

View file

@ -69,16 +69,36 @@ class VersioningPlugin : Plugin<Project> {
doLast { version.withClearedSuffix().writeForAndroid(propFile.asFile.outputStream()) } doLast { version.withClearedSuffix().writeForAndroid(propFile.asFile.outputStream()) }
} }
tasks.register("bumpMajor") { tasks.register("bumpMajor") {
doLast { version.withIncMajor().withClearedSuffix().writeForAndroid(propFile.asFile.outputStream()) } doLast {
version
.withIncMajor()
.withClearedSuffix()
.writeForAndroid(propFile.asFile.outputStream())
}
} }
tasks.register("bumpMinor") { tasks.register("bumpMinor") {
doLast { version.withIncMinor().withClearedSuffix().writeForAndroid(propFile.asFile.outputStream()) } doLast {
version
.withIncMinor()
.withClearedSuffix()
.writeForAndroid(propFile.asFile.outputStream())
}
} }
tasks.register("bumpPatch") { tasks.register("bumpPatch") {
doLast { version.withIncPatch().withClearedSuffix().writeForAndroid(propFile.asFile.outputStream()) } doLast {
version
.withIncPatch()
.withClearedSuffix()
.writeForAndroid(propFile.asFile.outputStream())
}
} }
tasks.register("bumpSnapshot") { tasks.register("bumpSnapshot") {
doLast { version.withIncMinor().withSuffix("SNAPSHOT").writeForAndroid(propFile.asFile.outputStream()) } doLast {
version
.withIncMinor()
.withSuffix("SNAPSHOT")
.writeForAndroid(propFile.asFile.outputStream())
}
} }
} }
} }

View file

@ -14,10 +14,10 @@ dokkaPlugin = "org.jetbrains.dokka:dokka-gradle-plugin:1.4.32"
downloadTaskPlugin = "de.undercouch:gradle-download-task:4.1.1" downloadTaskPlugin = "de.undercouch:gradle-download-task:4.1.1"
hiltGradlePlugin = { module = "com.google.dagger:hilt-android-gradle-plugin", version.ref = "hilt" } hiltGradlePlugin = { module = "com.google.dagger:hilt-android-gradle-plugin", version.ref = "hilt" }
kotlinGradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" } kotlinGradlePlugin = { module = "org.jetbrains.kotlin:kotlin-gradle-plugin", version.ref = "kotlin" }
ktfmtGradlePlugin = "com.ncorti.ktfmt.gradle:plugin:0.5.0"
mavenPublishPlugin = "com.vanniktech:gradle-maven-publish-plugin:0.15.1" mavenPublishPlugin = "com.vanniktech:gradle-maven-publish-plugin:0.15.1"
okhttp = "com.squareup.okhttp3:okhttp:4.9.0" okhttp = "com.squareup.okhttp3:okhttp:4.9.0"
semver4j = "com.vdurmont:semver4j:3.1.0" semver4j = "com.vdurmont:semver4j:3.1.0"
spotlessGradlePlugin = "com.diffplug.spotless:spotless-plugin-gradle:5.12.4"
# Kotlin dependencies # Kotlin dependencies
kotlin-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" } kotlin-coroutines-android = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-android", version.ref = "coroutines" }