Refactor the dependency graph resolver to be more readable
This commit is contained in:
+18
-17
@@ -48,23 +48,21 @@ class GradleKotlinDependencyGraphResolver(
|
|||||||
|
|
||||||
val nodeByModuleId = mutableMapOf<KotlinModuleIdentifier, GradleDependencyGraphNode>()
|
val nodeByModuleId = mutableMapOf<KotlinModuleIdentifier, GradleDependencyGraphNode>()
|
||||||
|
|
||||||
fun nodeFromComponent(component: ResolvedComponentResult, isRoot: Boolean /*refactor me*/): GradleDependencyGraphNode {
|
fun getKotlinModuleFromComponentResult(component: ResolvedComponentResult): KotlinModule =
|
||||||
val id = component.toSingleModuleIdentifier()
|
moduleResolver.resolveDependency(requestingModule, component.toModuleDependency())
|
||||||
return nodeByModuleId.getOrPut(id) {
|
?: buildSyntheticPlainModule(component, component.variants.singleOrNull()?.displayName ?: "default")
|
||||||
val module = if (isRoot)
|
|
||||||
requestingModule
|
|
||||||
else moduleResolver.resolveDependency(requestingModule, component.toModuleDependency())
|
|
||||||
.takeIf { component !in excludeLegacyMetadataModulesFromResult }
|
|
||||||
?: buildSyntheticModule(component, component.variants.singleOrNull()?.displayName ?: "default")
|
|
||||||
|
|
||||||
|
fun nodeFromModule(componentResult: ResolvedComponentResult, kotlinModule: KotlinModule): GradleDependencyGraphNode {
|
||||||
|
val id = kotlinModule.moduleIdentifier
|
||||||
|
return nodeByModuleId.getOrPut(id) {
|
||||||
val metadataSourceComponent =
|
val metadataSourceComponent =
|
||||||
(module as? ExternalImportedKotlinModule)
|
(kotlinModule as? ExternalImportedKotlinModule)
|
||||||
?.takeIf { it.hasLegacyMetadataModule }
|
?.takeIf { it.hasLegacyMetadataModule }
|
||||||
?.let { (component.dependencies.singleOrNull() as? ResolvedDependencyResult)?.selected }
|
?.let { (componentResult.dependencies.singleOrNull() as? ResolvedDependencyResult)?.selected }
|
||||||
?: component
|
?: componentResult
|
||||||
|
|
||||||
val dependenciesRequestedByModule =
|
val dependenciesRequestedByModule =
|
||||||
module.fragments.flatMap { fragment -> fragment.declaredModuleDependencies.map { it.moduleIdentifier } }.toSet()
|
kotlinModule.fragments.flatMap { fragment -> fragment.declaredModuleDependencies.map { it.moduleIdentifier } }.toSet()
|
||||||
|
|
||||||
val resolvedComponentDependencies = metadataSourceComponent.dependencies
|
val resolvedComponentDependencies = metadataSourceComponent.dependencies
|
||||||
.filterIsInstance<ResolvedDependencyResult>()
|
.filterIsInstance<ResolvedDependencyResult>()
|
||||||
@@ -74,15 +72,18 @@ class GradleKotlinDependencyGraphResolver(
|
|||||||
.flatMap { dependency -> dependency.requested.toModuleIdentifiers().map { id -> id to dependency.selected } }
|
.flatMap { dependency -> dependency.requested.toModuleIdentifiers().map { id -> id to dependency.selected } }
|
||||||
.toMap()
|
.toMap()
|
||||||
|
|
||||||
val fragmentDependencies = module.fragments.associateWith { it.declaredModuleDependencies }
|
val fragmentDependencies = kotlinModule.fragments.associateWith { it.declaredModuleDependencies }
|
||||||
|
|
||||||
val nodeDependenciesMap = fragmentDependencies.mapValues { (_, deps) ->
|
val nodeDependenciesMap = fragmentDependencies.mapValues { (_, deps) ->
|
||||||
deps.mapNotNull { resolvedComponentDependencies[it.moduleIdentifier] }.map { nodeFromComponent(it, isRoot = false) }
|
deps.mapNotNull { resolvedComponentDependencies[it.moduleIdentifier] }.map {
|
||||||
|
val dependencyModule = getKotlinModuleFromComponentResult(it)
|
||||||
|
nodeFromModule(it, dependencyModule)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
GradleDependencyGraphNode(
|
GradleDependencyGraphNode(
|
||||||
module,
|
kotlinModule,
|
||||||
component,
|
componentResult,
|
||||||
metadataSourceComponent,
|
metadataSourceComponent,
|
||||||
nodeDependenciesMap
|
nodeDependenciesMap
|
||||||
)
|
)
|
||||||
@@ -91,7 +92,7 @@ class GradleKotlinDependencyGraphResolver(
|
|||||||
|
|
||||||
return GradleDependencyGraph(
|
return GradleDependencyGraph(
|
||||||
requestingModule,
|
requestingModule,
|
||||||
nodeFromComponent(configurationToResolve(requestingModule).incoming.resolutionResult.root, isRoot = true)
|
nodeFromModule(configurationToResolve(requestingModule).incoming.resolutionResult.root, requestingModule)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user