Redo DI gradle setup code

This commit is contained in:
Jorge Martín 2025-08-25 08:56:45 +02:00
parent 6027651d85
commit 4ab62a6a38
85 changed files with 215 additions and 259 deletions

View file

@ -1,86 +0,0 @@
/*
* Copyright 2024 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
package extension
import dev.zacsweers.metro.gradle.MetroPluginExtension
import org.gradle.accessors.dm.LibrariesForLibs
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.gradle.kotlin.dsl.the
import org.gradle.plugin.use.PluginDependency
/**
* Setup Anvil plugin with the given configuration.
* @param generateDaggerCode whether to enable general Dagger code generation using Kapt. `false` by default.
* @param generateDaggerFactoriesUsingAnvil whether to generate Dagger factories using Anvil instead of Kapt. `true` by default.
* @param componentMergingStrategy how to perform component merging. This is `ComponentMergingStrategy.NONE` by default, which will prevent component merging
* from running.
*/
fun Project.setupAnvil(
generateDaggerCode: Boolean = false,
generateDaggerFactoriesUsingAnvil: Boolean = true,
componentMergingStrategy: ComponentMergingStrategy = ComponentMergingStrategy.NONE,
) {
val libs = the<LibrariesForLibs>()
// // Add dagger dependency, needed for generated code
// dependencies.implementation(libs.dagger)
// Apply Anvil plugin and configure it
applyPluginIfNeeded(libs.plugins.metro)
applyPluginIfNeeded(libs.plugins.ksp)
// project.pluginManager.withPlugin(libs.plugins.anvil.get().pluginId) {
// // Setup extension
// extensions.configure(AnvilExtension::class.java) {
// this.generateDaggerFactories.set(generateDaggerFactoriesUsingAnvil)
// this.disableComponentMerging.set(componentMergingStrategy == ComponentMergingStrategy.NONE)
//
// useKsp(
// contributesAndFactoryGeneration = true,
// componentMerging = componentMergingStrategy == ComponentMergingStrategy.KSP,
// )
// }
// }
// if (generateDaggerCode) {
// // Needed at the top level since dagger code should be generated at a single point for performance reasons
// dependencies.add("ksp", libs.dagger.compiler)
// }
project.pluginManager.withPlugin(libs.plugins.metro.get().pluginId) {
// Setup extension
extensions.configure(MetroPluginExtension::class.java) {
// debug.set(true)
// generateAssistedFactories.set(true)
// this.generateAssistedFactories.set(generateDaggerFactoriesUsingAnvil)
}
}
// These dependencies are only needed for compose library or application modules
if (project.pluginManager.hasPlugin("io.element.android-compose-library")
|| project.pluginManager.hasPlugin("io.element.android-compose-application")) {
// Annotations to generate DI code for Appyx nodes
dependencies.implementation(project.project(":anvilannotations"))
// Code generator for the annotations above
dependencies.add("ksp", project.project(":anvilcodegen"))
}
}
private fun Project.applyPluginIfNeeded(plugin: Provider<PluginDependency>) {
val pluginId = plugin.get().pluginId
if (!pluginManager.hasPlugin(pluginId)) {
pluginManager.apply(pluginId)
}
}
enum class ComponentMergingStrategy {
NONE,
KAPT,
KSP
}

View file

@ -0,0 +1,49 @@
/*
* Copyright 2024 New Vector Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
* Please see LICENSE files in the repository root for full details.
*/
package extension
import org.gradle.accessors.dm.LibrariesForLibs
import org.gradle.api.Project
import org.gradle.api.provider.Provider
import org.gradle.kotlin.dsl.the
import org.gradle.plugin.use.PluginDependency
/**
* Setup the Metro plugin with the shared configuration.
* @param generateNodeFactories Whether to set up the KSP plugin and dependencies to generate Appyx Node factories.
*/
fun Project.setupDependencyInjection(
generateNodeFactories: Boolean = shouldApplyAppyxCodegen(),
) {
val libs = the<LibrariesForLibs>()
// Apply Metro plugin and configure it
applyPluginIfNeeded(libs.plugins.metro)
if (generateNodeFactories) {
applyPluginIfNeeded(libs.plugins.ksp)
// Annotations to generate DI code for Appyx nodes
dependencies.implementation(project.project(":anvilannotations"))
// Code generator for the annotations above
dependencies.add("ksp", project.project(":anvilcodegen"))
}
}
// These dependencies should only be needed for compose library or application modules
private fun Project.shouldApplyAppyxCodegen(): Boolean {
return project.pluginManager.hasPlugin("io.element.android-compose-library")
|| project.pluginManager.hasPlugin("io.element.android-compose-application")
}
private fun Project.applyPluginIfNeeded(plugin: Provider<PluginDependency>) {
val pluginId = plugin.get().pluginId
if (!pluginManager.hasPlugin(pluginId)) {
pluginManager.apply(pluginId)
}
}