Skip to content

Commit

Permalink
Gate dagger processing options behind a property instead
Browse files Browse the repository at this point in the history
  • Loading branch information
ZacSweers committed Jan 27, 2025
1 parent f7993f3 commit 7636811
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ Changelog
**Unreleased**
--------------

- Gate Dagger's processing options on a new `foundry.dagger.options` property. The structure of this is a comma-separated list of key=value pairs. i.e. `foundry.dagger.options=dagger.useBindingGraphFix=ENABLED,dagger.ignoreProvisionKeyWildcards=ENABLED`.

0.24.0
------

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,16 @@ internal interface AptOptionsConfig {
* ```
*/
internal abstract class BasicAptOptionsConfig : AptOptionsConfig {
private val rawAptCompilerOptions by lazy {
globalOptions.map { (option, value) -> "-A$option=$value" }
}

open val name: String = this::class.java.simpleName
open val globalOptions: Map<String, String> = emptyMap()

val javaCompileAptAction =
Action<JavaCompile> { options.compilerArgs.addAll(rawAptCompilerOptions) }
open fun globalOptions(properties: FoundryProperties): Map<String, String> = emptyMap()

fun javaCompileAptAction(foundryProperties: FoundryProperties) =
Action<JavaCompile> {
options.compilerArgs.addAll(
globalOptions(foundryProperties).map { (option, value) -> "-A$option=$value" }
)
}

final override fun newConfigurer(project: Project): AptOptionsConfigurer {
return newConfigurer(project, BasicAptOptionsConfigurer(project, this))
Expand All @@ -128,26 +129,33 @@ internal abstract class BasicAptOptionsConfig : AptOptionsConfig {
"${baseConfig.name}: Adding javac apt options to android project " +
"${project.path} at buildType $name"
)
baseConfig.globalOptions.forEach { (key, value) ->
baseConfig.globalOptions(project.foundryProperties).forEach { (key, value) ->
javaCompileOptions.annotationProcessorOptions.arguments[key] = value
}
}

private val javaLibraryAction =
private fun javaLibraryAction(foundryProperties: FoundryProperties) =
Action<Any> {
// Implicitly not using Kotlin because we would have to use Kapt
project.logger.debug(
"${baseConfig.name}: Adding javac apt options to android project ${project.path}"
)
project.tasks.withType(JavaCompile::class.java, baseConfig.javaCompileAptAction)
project.tasks.withType(
JavaCompile::class.java,
baseConfig.javaCompileAptAction(foundryProperties),
)
}

override fun configure(configurationContext: ConfigurationContext) =
with(configurationContext.project) {
if (configurationContext.isKaptConfiguration) {
logger.debug("${baseConfig.name}: Adding kapt arguments to $path")
configure<KaptExtension> {
arguments { baseConfig.globalOptions.forEach { (key, value) -> arg(key, value) } }
arguments {
baseConfig.globalOptions(foundryProperties).forEach { (key, value) ->
arg(key, value)
}
}
}
} else {
project.pluginManager.withPlugin("com.android.application") {
Expand All @@ -162,8 +170,8 @@ internal abstract class BasicAptOptionsConfig : AptOptionsConfig {
)
configure<LibraryExtension> { buildTypes.configureEach(baseBuildTypeAction) }
}
project.pluginManager.withPlugin("java", javaLibraryAction)
project.pluginManager.withPlugin("java-library", javaLibraryAction)
project.pluginManager.withPlugin("java", javaLibraryAction(foundryProperties))
project.pluginManager.withPlugin("java-library", javaLibraryAction(foundryProperties))
}
}
}
Expand All @@ -184,7 +192,11 @@ internal object AptOptionsConfigs {

object Dagger : BasicAptOptionsConfig() {
override val targetDependency: String = "dagger-compiler"
override val globalOptions: Map<String, String> =

override fun globalOptions(foundryProperties: FoundryProperties): Map<String, String> =
foundryProperties.daggerOptions.getOrElse(DEFAULT_ARGS)

private val DEFAULT_ARGS =
mapOf(
"dagger.warnIfInjectionFactoryNotGeneratedUpstream" to "enabled",
// New error messages. Feedback should go to https://github.com/google/dagger/issues/1769
Expand All @@ -201,7 +213,8 @@ internal object AptOptionsConfigs {

object Moshi : BasicAptOptionsConfig() {
override val targetDependency: String = "moshi-kotlin-codegen"
override val globalOptions: Map<String, String> =

override fun globalOptions(foundryProperties: FoundryProperties): Map<String, String> =
mapOf("moshi.generated" to "javax.annotation.Generated")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,19 @@ internal constructor(
AnvilMode.valueOf(it.uppercase(Locale.US))
}

/**
* Defines Dagger processing options. The structure of this is a comma-separated list of key=value
* pairs. i.e.
*
* `foundry.dagger.options=dagger.useBindingGraphFix=ENABLED,dagger.ignoreProvisionKeyWildcards=ENABLED`
*/
public val daggerOptions: Provider<Map<String, String>>
get() =
resolver.optionalStringProvider("foundry.dagger.options", defaultValue = "").map { value ->
if (value.isBlank()) return@map emptyMap<String, String>()
value.splitToSequence(',').associate { kv -> kv.trim().split('=').let { it[0] to it[1] } }
}

/** Overrides the kotlin language version if present. */
public val kaptLanguageVersion: Provider<KotlinVersion>
get() =
Expand Down

0 comments on commit 7636811

Please sign in to comment.