Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to limit Dagger factory generation to specific source sets #727

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Next Next commit
Add generateFactoriesPathWhitelist parameter to compiler
  • Loading branch information
matejdro committed Jul 21, 2023
commit c391610318cdb039d0c218fd8237fb738ca873ec
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ public interface AnvilContext {
*/
public val generateFactoriesOnly: Boolean

/**
* List of absolute paths that anvil should generate Factory classes in.
* When empty or ommited, Anvil applies no filtering and
* generates factories in all available files.
*/
public val generateFactoriesPathWhitelist: List<String>

/**
* 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`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ internal const val generateDaggerFactoriesOnlyName = "generate-dagger-factories-
internal val generateDaggerFactoriesOnlyKey =
CompilerConfigurationKey.create<Boolean>("anvil $generateDaggerFactoriesOnlyName")

internal const val generateDaggerFactoriesPathWhitelistName = "generate-dagger-factories-path-whitelist"
internal val generateDaggerFactoriesPathWhitelistKey =
CompilerConfigurationKey.create<List<String>>("anvil $generateDaggerFactoriesPathWhitelistName")

internal const val disableComponentMergingName = "disable-component-merging"
internal val disableComponentMergingKey =
CompilerConfigurationKey.create<Boolean>("anvil $disableComponentMergingName")
Expand Down Expand Up @@ -54,6 +58,15 @@ class AnvilCommandLineProcessor : CommandLineProcessor {
required = false,
allowMultipleOccurrences = false
),
CliOption(
optionName = generateDaggerFactoriesPathWhitelistName,
valueDescription = "Colon separated list",
description = "List of absolute paths that anvil should generate Factory classes in. " +
"When empty or ommited, Anvil applies no filtering and generates factories " +
"in all available files",
required = false,
allowMultipleOccurrences = false
),
CliOption(
optionName = disableComponentMergingName,
valueDescription = "<true|false>",
Expand All @@ -77,6 +90,11 @@ class AnvilCommandLineProcessor : CommandLineProcessor {
configuration.put(generateDaggerFactoriesOnlyKey, value.toBoolean())
disableComponentMergingName ->
configuration.put(disableComponentMergingKey, value.toBoolean())
generateDaggerFactoriesPathWhitelistName ->
configuration.put(
generateDaggerFactoriesPathWhitelistKey,
value.split(":").filter { it.isNotBlank() }
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import org.jetbrains.kotlin.config.CompilerConfiguration
class CommandLineOptions private constructor(
val generateFactories: Boolean,
val generateFactoriesOnly: Boolean,
val generateFactoriesPathWhitelist: List<String>,
val disableComponentMerging: Boolean,
) {
companion object {
val CompilerConfiguration.commandLineOptions: CommandLineOptions
get() = CommandLineOptions(
generateFactories = get(generateDaggerFactoriesKey, false),
generateFactoriesOnly = get(generateDaggerFactoriesOnlyKey, false),
generateFactoriesPathWhitelist = get(generateDaggerFactoriesPathWhitelistKey, emptyList()),
disableComponentMerging = get(disableComponentMergingKey, false)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.squareup.anvil.compiler.codegen.reference.RealAnvilModuleDescriptor
import org.jetbrains.kotlin.analyzer.AnalysisResult
import org.jetbrains.kotlin.analyzer.AnalysisResult.RetryWithAdditionalRoots
import org.jetbrains.kotlin.com.intellij.openapi.project.Project
import org.jetbrains.kotlin.com.intellij.openapi.util.Key
import org.jetbrains.kotlin.com.intellij.psi.PsiManager
import org.jetbrains.kotlin.com.intellij.testFramework.LightVirtualFile
import org.jetbrains.kotlin.container.ComponentProvider
Expand Down Expand Up @@ -93,7 +94,9 @@ internal class CodeGenerationExtension(
content
)

psiManager.findFile(virtualFile)
psiManager.findFile(virtualFile)?.also {
it.putUserData(IS_GENERATED_BY_ANVIL, true)
}
}
.filterIsInstance<KtFile>()
.also { anvilModule.addFiles(it) }
Expand Down Expand Up @@ -160,3 +163,5 @@ internal class CodeGenerationExtension(
)
}
}

val IS_GENERATED_BY_ANVIL = Key<Boolean>("generated_by_anvil")
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor
internal data class RealAnvilContext(
override val generateFactories: Boolean,
override val generateFactoriesOnly: Boolean,
override val generateFactoriesPathWhitelist: List<String>,
override val disableComponentMerging: Boolean,
override val module: ModuleDescriptor
) : AnvilContext
Expand All @@ -16,6 +17,7 @@ internal fun CommandLineOptions.toAnvilContext(
): RealAnvilContext = RealAnvilContext(
generateFactories = generateFactories,
generateFactoriesOnly = generateFactoriesOnly,
generateFactoriesPathWhitelist = generateFactoriesPathWhitelist,
disableComponentMerging = disableComponentMerging,
module = module
)