Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gradle-plugin/api/gradle-plugin.api
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public final class com/squareup/anvil/plugin/AndroidVariantFilter : com/squareup
}

public abstract class com/squareup/anvil/plugin/AnvilExtension {
public fun <init> (Lorg/gradle/api/model/ObjectFactory;)V
public fun <init> (Lorg/gradle/api/model/ObjectFactory;Lorg/gradle/api/provider/ProviderFactory;)V
public final fun getAddOptionalAnnotations ()Lorg/gradle/api/provider/Property;
public final fun getDisableComponentMerging ()Lorg/gradle/api/provider/Property;
public final fun getGenerateDaggerFactories ()Lorg/gradle/api/provider/Property;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
package com.squareup.anvil.plugin

import com.rickbusarow.kase.Kase4
import com.rickbusarow.kase.gradle.GradleDependencyVersion
import com.rickbusarow.kase.gradle.GradleKotlinTestVersions
import com.rickbusarow.kase.gradle.GradleProjectBuilder
import com.rickbusarow.kase.gradle.HasGradleDependencyVersion
import com.rickbusarow.kase.gradle.HasKotlinDependencyVersion
import com.rickbusarow.kase.gradle.KotlinDependencyVersion
import com.rickbusarow.kase.kase
import com.rickbusarow.kase.times
import io.kotest.matchers.string.shouldContain
import org.junit.jupiter.api.DynamicNode
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.TestFactory
import java.util.stream.Stream

class AnvilExtensionTest : BaseGradleTest() {

@Nested
inner class `gradle property to extension property mapping` {

val properties = listOf(
kase(a1 = "generateDaggerFactories", false),
kase(a1 = "generateDaggerFactoriesOnly", false),
kase(a1 = "disableComponentMerging", false),
kase(a1 = "syncGeneratedSources", false),
kase(a1 = "addOptionalAnnotations", false),
)

fun GradleProjectBuilder.gradlePropertiesFile(propertyName: String, value: Any) {
val props = buildList {
if (propertyName == "generateDaggerFactoriesOnly") {
// The "__FactoriesOnly" toggle requires the "__Factories" toggle to be enabled.
add("com.squareup.anvil.generateDaggerFactories=true")
}
add("com.squareup.anvil.$propertyName=$value")
}
gradlePropertiesFile(props.joinToString("\n"))
}

@TestFactory
fun `extension properties are their default value when not set anywhere`() =
tests { versions, propertyName, default ->

rootProject.buildFile(
"""
plugins {
kotlin("jvm") version "${versions.kotlinVersion}"
id("com.squareup.anvil") version "$anvilVersion"
}

tasks.register("printProperty") {
doLast {
println("$propertyName: ${'$'}{anvil.$propertyName.get()}")
}
}
""".trimIndent(),
)

shouldSucceed("printProperty") {
output shouldContain "$propertyName: $default"
}
}

@TestFactory
fun `the extension property is not default when only set in gradle properties`() =
tests { versions, propertyName, default ->
rootProject {
buildFile(
"""
plugins {
kotlin("jvm") version "${versions.kotlinVersion}"
id("com.squareup.anvil") version "$anvilVersion"
}

tasks.register("printProperty") {
doLast {
println("$propertyName: ${'$'}{anvil.$propertyName.get()}")
}
}
""".trimIndent(),
)

gradlePropertiesFile(propertyName, !default)
}

shouldSucceed("printProperty") {
output shouldContain "$propertyName: ${!default}"
}
}

@TestFactory
fun `the extension property is default when explicitly set to it in gradle properties`() =
tests { versions, propertyName, default ->
rootProject {
buildFile(
"""
plugins {
kotlin("jvm") version "${versions.kotlinVersion}"
id("com.squareup.anvil") version "$anvilVersion"
}

tasks.register("printProperty") {
doLast {
println("$propertyName: ${'$'}{anvil.$propertyName.get()}")
}
}
""".trimIndent(),
)

gradlePropertiesFile(propertyName, default)
}

shouldSucceed("printProperty") {
output shouldContain "$propertyName: $default"
}
}

@TestFactory
fun `the extension property overrides the gradle properties value when set`() =
tests { versions, propertyName, default ->

rootProject {
val extensionText = if (propertyName == "generateDaggerFactoriesOnly") {
"""
anvil {
generateDaggerFactories = true
$propertyName = ${!default}
}
""".trimIndent()
} else {
"""
anvil {
$propertyName = ${!default}
}
""".trimIndent()
}
buildFile(
"""
plugins {
kotlin("jvm") version "${versions.kotlinVersion}"
id("com.squareup.anvil") version "$anvilVersion"
}

tasks.register("printProperty") {
doLast {
println("$propertyName: ${'$'}{anvil.$propertyName.get()}")
}
}

$extensionText
""".trimIndent(),
)

gradlePropertiesFile(propertyName, default)
}

shouldSucceed("printProperty") {
output shouldContain "$propertyName: ${!default}"
}
}

inline fun tests(
crossinline action: AnvilGradleTestEnvironment.(
GradleKotlinTestVersions,
String,
Boolean,
) -> Unit,
): Stream<out DynamicNode> = params.asContainers { versions ->
properties.times(listOf(versions), instanceFactory = ::PropertyKase)
.asTests { kase -> action(versions, kase.propertyName, kase.default) }
}
}
}

@PublishedApi
internal class PropertyKase(
val propertyName: String,
val default: Boolean,
override val gradle: GradleDependencyVersion,
override val kotlin: KotlinDependencyVersion,
) : Kase4<GradleDependencyVersion, KotlinDependencyVersion, String, Boolean> by kase(
displayName = "property: $propertyName | default: $default",
a1 = gradle,
a2 = kotlin,
a3 = propertyName,
a4 = default,
),
GradleKotlinTestVersions,
HasGradleDependencyVersion by HasGradleDependencyVersion(gradle),
HasKotlinDependencyVersion by HasKotlinDependencyVersion(kotlin)
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ package com.squareup.anvil.plugin
import org.gradle.api.Action
import org.gradle.api.model.ObjectFactory
import org.gradle.api.provider.Property
import org.gradle.api.provider.ProviderFactory
import javax.inject.Inject

public abstract class AnvilExtension @Inject constructor(objects: ObjectFactory) {
public abstract class AnvilExtension @Inject constructor(
objects: ObjectFactory,
private val providers: ProviderFactory,
) {
/**
* Allows you to use Anvil to generate Factory classes that usually the Dagger annotation
* processor would generate for @Provides methods, @Inject constructors and @Inject fields.
Expand All @@ -20,42 +24,74 @@ public abstract class AnvilExtension @Inject constructor(objects: ObjectFactory)
* Java sources either.
*
* By default this feature is disabled.
*
* This property can also be set via a Gradle property:
*
* ```properties
* com.squareup.anvil.generateDaggerFactories=true
* ```
*/
public val generateDaggerFactories: Property<Boolean> = objects.property(Boolean::class.java)
.convention(false)
.conventionFromProperty("com.squareup.anvil.generateDaggerFactories", false)

/**
* There are occasions where consumers of Anvil are only interested in generating Dagger
* factories to speed up build times and don't want to make use of the other features. With this
* flag Anvil will only generate Dagger factories and skip all other steps. If this flag is set
* to `true`, then also [generateDaggerFactories] must be set to `true`.
*
* This property can also be set via a Gradle property:
*
* ```properties
* com.squareup.anvil.generateDaggerFactoriesOnly=true
* ```
*/
public val generateDaggerFactoriesOnly: Property<Boolean> = objects.property(Boolean::class.java)
.convention(false)
.conventionFromProperty("com.squareup.anvil.generateDaggerFactoriesOnly", false)

/**
* Enabling this indicates that only code generation should run and no component merging should
* run. This is useful for cases where you want to use `@ContributesTo`, `@ContributesBinding`,
* or similar annotations but _not_ `@MergeComponent` or `@MergeSubcomponent` functionality.
* This allows for anvil use in projects with kapt enabled but _not_ require disabling
* incremental compilation in kapt stub generation tasks.
*
* This property can also be set via a Gradle property:
*
* ```properties
* com.squareup.anvil.disableComponentMerging=true
* ```
*/
public val disableComponentMerging: Property<Boolean> = objects.property(Boolean::class.java)
.convention(false)
.conventionFromProperty("com.squareup.anvil.disableComponentMerging", false)

/**
* Add Anvil generated source directories to sourceSets in Gradle for indexing visibility in the
* IDE. This can be useful in debugging and is disabled by default.
*
* This property can also be set via a Gradle property:
*
* ```properties
* com.squareup.anvil.syncGeneratedSources=true
* ```
*/
public val syncGeneratedSources: Property<Boolean> = objects.property(Boolean::class.java)
.convention(false)
.conventionFromProperty("com.squareup.anvil.syncGeneratedSources", false)

/**
* Add the `annotations-optional` artifact as a dependency to make annotations like `@SingleIn` and `@ForScope`
* available to use. These are annotations that are not strictly required but which we've found to be helpful with managing larger dependency graphs.
* Add the `annotations-optional` artifact as a dependency to make annotations
* like `@SingleIn` and `@ForScope` available to use. These are annotations
* that are not strictly required but which we've found to be helpful with
* managing larger dependency graphs.
*
* This property can also be set via a Gradle property:
*
* ```properties
* com.squareup.anvil.addOptionalAnnotations=true
* ```
*/
public val addOptionalAnnotations: Property<Boolean> = objects.property(Boolean::class.java)
.convention(false)
.conventionFromProperty("com.squareup.anvil.addOptionalAnnotations", false)

@Suppress("PropertyName")
internal var _variantFilter: Action<VariantFilter>? = null
Expand All @@ -67,4 +103,13 @@ public abstract class AnvilExtension @Inject constructor(objects: ObjectFactory)
public fun variantFilter(action: Action<VariantFilter>) {
_variantFilter = action
}

private fun Property<Boolean>.conventionFromProperty(
name: String,
default: Boolean,
): Property<Boolean> = convention(
providers.gradleProperty(name)
.map(String::toBoolean)
.orElse(default),
)
}