Centralise the DI code generation logic (#3562)
* Create Anvil extension helper * Use the helper everywhere
This commit is contained in:
committed by
GitHub
parent
3ee686f250
commit
2efdb3ae45
59
plugins/src/main/kotlin/extension/AnvilExtensions.kt
Normal file
59
plugins/src/main/kotlin/extension/AnvilExtensions.kt
Normal file
@@ -0,0 +1,59 @@
|
||||
/*
|
||||
* Copyright 2024 New Vector Ltd.
|
||||
*
|
||||
* SPDX-License-Identifier: AGPL-3.0-only
|
||||
* Please see LICENSE in the repository root for full details.
|
||||
*/
|
||||
|
||||
package extension
|
||||
|
||||
import com.squareup.anvil.plugin.AnvilExtension
|
||||
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
|
||||
* @param generateDaggerFactoriesUsingAnvil whether to generate Dagger factories using Anvil instead of Kapt
|
||||
*/
|
||||
fun Project.setupAnvil(
|
||||
generateDaggerCode: Boolean = false,
|
||||
generateDaggerFactoriesUsingAnvil: Boolean = true,
|
||||
) {
|
||||
val libs = the<LibrariesForLibs>()
|
||||
// Apply plugins and dependencies
|
||||
applyPluginIfNeeded(libs.plugins.anvil)
|
||||
|
||||
if (generateDaggerCode) {
|
||||
applyPluginIfNeeded(libs.plugins.kapt)
|
||||
// Needed at the top level since dagger code should be generated at a single point for performance
|
||||
dependencies.implementation(libs.dagger)
|
||||
dependencies.add("kapt", libs.dagger.compiler)
|
||||
}
|
||||
|
||||
// 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("anvil", project.project(":anvilcodegen"))
|
||||
}
|
||||
|
||||
project.pluginManager.withPlugin(libs.plugins.anvil.get().pluginId) {
|
||||
// Setup extension
|
||||
extensions.configure(AnvilExtension::class.java) {
|
||||
this.generateDaggerFactories.set(generateDaggerFactoriesUsingAnvil)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun Project.applyPluginIfNeeded(plugin: Provider<PluginDependency>) {
|
||||
val pluginId = plugin.get().pluginId
|
||||
if (!pluginManager.hasPlugin(pluginId)) {
|
||||
pluginManager.apply(pluginId)
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import ModulesConfig
|
||||
import org.gradle.accessors.dm.LibrariesForLibs
|
||||
import org.gradle.api.Action
|
||||
import org.gradle.api.artifacts.ExternalModuleDependency
|
||||
import org.gradle.api.artifacts.dsl.DependencyHandler
|
||||
import org.gradle.api.logging.Logger
|
||||
import org.gradle.kotlin.dsl.DependencyHandlerScope
|
||||
import org.gradle.kotlin.dsl.closureOf
|
||||
@@ -19,6 +20,7 @@ import org.gradle.kotlin.dsl.project
|
||||
import java.io.File
|
||||
|
||||
private fun DependencyHandlerScope.implementation(dependency: Any) = dependencies.add("implementation", dependency)
|
||||
internal fun DependencyHandler.implementation(dependency: Any) = add("implementation", dependency)
|
||||
|
||||
// Implementation + config block
|
||||
private fun DependencyHandlerScope.implementation(
|
||||
|
||||
Reference in New Issue
Block a user