Skip to content

Commit 84bcf72

Browse files
authored
Merge pull request #790 from square/lazily_evaluate_dependency_guard_configs
Support late/lazy configuration creation in dependency guard
2 parents d0a5342 + c37f60f commit 84bcf72

File tree

1 file changed

+30
-23
lines changed

1 file changed

+30
-23
lines changed
Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,38 @@
11
import com.android.build.gradle.TestedExtension
22
import com.dropbox.gradle.plugins.dependencyguard.DependencyGuardPluginExtension
33

4-
val configurationNames = when {
5-
// record the root project's *build* classpath
6-
project == rootProject -> listOf("classpath")
7-
// Android variants and their configurations are added later in the configuration phase,
8-
// so we can't look them up now using the `configurations` property.
9-
// Instead we can just hard-code "releaseRuntimeClasspath" for any module which has AGP applied.
10-
// This is actually pretty robust, since if this configuration ever changes,
11-
// dependency-guard will fail when trying to look it up.
12-
extensions.findByType<TestedExtension>() != null -> listOf("releaseRuntimeClasspath")
13-
// If we got here, we're either in an empty "parent" module without a build plugin
14-
// (and no configurations), or we're in a vanilla Kotlin module. In this case, we can just look
15-
// at configuration names.
16-
else -> configurations
17-
.map { it.name }
18-
// anything ending with 'runtimeClasspath' but not 'testRuntimeClasspath'
19-
.filter { it.matches("^(?!test)\\w*[rR]untimeClasspath$".toRegex()) }
20-
}
4+
// We have to use `afterEvaluate { ... }` because both KMP and AGP create their configurations later
5+
// in the configuration phase. If we were to try looking up those configurations eagerly as soon
6+
// as this convention plugin is applied, there would be nothing there.
7+
afterEvaluate {
8+
val configurationNames = when {
9+
// record the root project's *build* classpath
10+
project == rootProject -> listOf("classpath")
11+
12+
// For Android modules, just hard-code `releaseRuntimeClasspath` for the release variant.
13+
// This is actually pretty robust, since if this configuration ever changes, dependency-guard
14+
// will fail when trying to look it up.
15+
extensions.findByType<TestedExtension>() != null -> listOf("releaseRuntimeClasspath")
16+
17+
// If we got here, we're either in an empty "parent" module without a build plugin
18+
// (and no configurations), or we're in a vanilla Kotlin module. In this case, we can just look
19+
// at configuration names.
20+
else -> configurations
21+
.map { it.name }
22+
.filter {
23+
it.endsWith("runtimeClasspath", ignoreCase = true) &&
24+
!it.endsWith("testRuntimeClasspath", ignoreCase = true)
25+
}
26+
}
2127

22-
if (configurationNames.isNotEmpty()) {
23-
apply(plugin = "com.dropbox.dependency-guard")
28+
if (configurationNames.isNotEmpty()) {
29+
apply(plugin = "com.dropbox.dependency-guard")
2430

25-
configure<DependencyGuardPluginExtension> {
26-
configurationNames.forEach { configName ->
27-
// Tell dependency-guard to check the `configName` configuration's dependencies.
28-
configuration(configName)
31+
configure<DependencyGuardPluginExtension> {
32+
configurationNames.forEach { configName ->
33+
// Tell dependency-guard to check the `configName` configuration's dependencies.
34+
configuration(configName)
35+
}
2936
}
3037
}
3138
}

0 commit comments

Comments
 (0)