From 3ae105df92d4bd000d1950cb79837d201d5b229b Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Tue, 1 Jun 2021 15:05:05 +0530 Subject: Configure Spotless for all projects and tweak Dagger flags (#1427) --- build.gradle.kts | 2 +- buildSrc/src/main/java/KaptConfigs.kt | 34 +++++++++++++++++++++++++++ buildSrc/src/main/java/PasswordStorePlugin.kt | 9 ++++--- buildSrc/src/main/java/Spotless.kt | 32 +++++++++++++++++-------- 4 files changed, 61 insertions(+), 16 deletions(-) create mode 100644 buildSrc/src/main/java/KaptConfigs.kt diff --git a/build.gradle.kts b/build.gradle.kts index 3365e7b0..a04020c8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,4 +7,4 @@ plugins { `aps-plugin` } -allprojects { apply(plugin = "com.diffplug.spotless") } +allprojects { configureSpotless() } diff --git a/buildSrc/src/main/java/KaptConfigs.kt b/buildSrc/src/main/java/KaptConfigs.kt new file mode 100644 index 00000000..db1c6d5a --- /dev/null +++ b/buildSrc/src/main/java/KaptConfigs.kt @@ -0,0 +1,34 @@ +import org.gradle.api.Project +import org.gradle.kotlin.dsl.configure +import org.jetbrains.kotlin.gradle.plugin.KaptExtension + +/** Apply default kapt configs to the [Project]. */ +internal fun Project.configureKapt() { + extensions.configure { + javacOptions { + if (hasDaggerCompilerDependency) { + // https://dagger.dev/dev-guide/compiler-options#fastinit-mode + option("-Adagger.fastInit=enabled") + // Enable the better, experimental error messages + // https://github.com/google/dagger/commit/0d2505a727b54f47b8677f42dd4fc5c1924e37f5 + option("-Adagger.experimentalDaggerErrorMessages=enabled") + // Share test components for when we start leveraging Hilt for tests + // https://github.com/google/dagger/releases/tag/dagger-2.34 + option("-Adagger.hilt.shareTestComponents=true") + // KAPT nests errors causing real issues to be suppressed in CI logs + option("-Xmaxerrs", 500) + // Enables per-module validation for faster error detection + // https://github.com/google/dagger/commit/325b516ac6a53d3fc973d247b5231fafda9870a2 + option("-Adagger.moduleBindingValidation=ERROR") + } + } + } + // disable kapt tasks for unit tests + tasks + .matching { it.name.startsWith("kapt") && it.name.endsWith("UnitTestKotlin") } + .configureEach { enabled = false } +} + +private val Project.hasDaggerCompilerDependency: Boolean + get() = + configurations.any { it.dependencies.any { dependency -> dependency.name == "hilt-compiler" } } diff --git a/buildSrc/src/main/java/PasswordStorePlugin.kt b/buildSrc/src/main/java/PasswordStorePlugin.kt index 5db8c893..0705de82 100644 --- a/buildSrc/src/main/java/PasswordStorePlugin.kt +++ b/buildSrc/src/main/java/PasswordStorePlugin.kt @@ -7,8 +7,6 @@ import com.android.build.gradle.TestedExtension import com.android.build.gradle.internal.dsl.BaseAppModuleExtension import com.android.build.gradle.internal.plugins.AppPlugin import com.android.build.gradle.internal.plugins.LibraryPlugin -import com.diffplug.gradle.spotless.SpotlessExtension -import com.diffplug.gradle.spotless.SpotlessPlugin import org.gradle.api.Plugin import org.gradle.api.Project import org.gradle.api.plugins.JavaLibraryPlugin @@ -20,6 +18,7 @@ import org.gradle.kotlin.dsl.withType import org.gradle.plugins.signing.SigningExtension import org.gradle.plugins.signing.SigningPlugin import org.jetbrains.kotlin.gradle.dsl.KotlinProjectExtension +import org.jetbrains.kotlin.gradle.internal.Kapt3GradleSubplugin import org.jetbrains.kotlin.gradle.plugin.KotlinPluginWrapper import org.jetbrains.kotlin.gradle.tasks.KotlinCompile @@ -53,15 +52,15 @@ class PasswordStorePlugin : Plugin { project.extensions.getByType().configureBuildSigning(project) project.extensions.getByType().configureCommonAndroidOptions() } - is SpotlessPlugin -> { - project.extensions.getByType().configureSpotless() - } is SigningPlugin -> { project.extensions.getByType().configureBuildSigning() } is KotlinPluginWrapper -> { project.configureExplicitApi() } + is Kapt3GradleSubplugin -> { + project.configureKapt() + } } } } diff --git a/buildSrc/src/main/java/Spotless.kt b/buildSrc/src/main/java/Spotless.kt index 859faf73..bab6f58d 100644 --- a/buildSrc/src/main/java/Spotless.kt +++ b/buildSrc/src/main/java/Spotless.kt @@ -4,16 +4,28 @@ */ import com.diffplug.gradle.spotless.SpotlessExtension +import org.gradle.api.Project +import org.gradle.kotlin.dsl.apply +import org.gradle.kotlin.dsl.configure -internal fun SpotlessExtension.configureSpotless() { - kotlin { - ktfmt().googleStyle() - target("src/**/*.kt", "**/*.kts") - } - format("xml") { - target("**/*.xml") - trimTrailingWhitespace() - indentWithSpaces() - endWithNewline() +fun Project.configureSpotless() { + apply(plugin = "com.diffplug.spotless") + configure { + kotlin { + ktfmt().googleStyle() + target("**/*.kt") + targetExclude("**/build/") + } + kotlinGradle { + ktfmt().googleStyle() + target("**/*.kts") + } + format("xml") { + target("**/*.xml") + targetExclude("**/build/", ".idea/") + trimTrailingWhitespace() + indentWithSpaces() + endWithNewline() + } } } -- cgit v1.2.3