[Gradle] Simplify LazyResolvedConfiguration
By: - Removing unused `moduleArtifacts` function - Removing bad documentation (replaced by simpler function names)+ - Using .withClosure util function KT-49933
This commit is contained in:
committed by
Space Team
parent
674e7aa480
commit
3162769b96
+1
-1
@@ -231,7 +231,7 @@ internal class GranularMetadataTransformation(
|
||||
|
||||
val compositeMetadataArtifact = params
|
||||
.resolvedMetadataConfiguration
|
||||
.dependencyArtifacts(dependency)
|
||||
.getArtifacts(dependency)
|
||||
.singleOrNull()
|
||||
// Make sure that resolved metadata artifact is actually Multiplatform one
|
||||
?.takeIf { it.variant.attributes.containsMultiplatformAttributes }
|
||||
|
||||
+1
-1
@@ -139,7 +139,7 @@ open class MetadataDependencyTransformationTask
|
||||
is MetadataDependencyResolution.ChooseVisibleSourceSets ->
|
||||
objectFactory.transformMetadataLibrariesForBuild(resolution, outputsDir, true)
|
||||
is MetadataDependencyResolution.KeepOriginalDependency ->
|
||||
transformationParameters.resolvedMetadataConfiguration.componentArtifacts(resolution.dependency).map { it.file }
|
||||
transformationParameters.resolvedMetadataConfiguration.getArtifacts(resolution.dependency).map { it.file }
|
||||
is MetadataDependencyResolution.Exclude -> emptyList()
|
||||
}
|
||||
}
|
||||
|
||||
+2
-2
@@ -216,7 +216,7 @@ constructor(
|
||||
.allDependencies
|
||||
.filterIsInstance<ResolvedDependencyResult>()
|
||||
.forEach {
|
||||
val dependencyFiles = exportLibrariesResolvedGraph.dependencyArtifacts(it).map { it.file }.filterKlibsPassedToCompiler()
|
||||
val dependencyFiles = exportLibrariesResolvedGraph.getArtifacts(it).map { it.file }.filterKlibsPassedToCompiler()
|
||||
if (!apiFiles.files.containsAll(dependencyFiles)) {
|
||||
failed.add(it)
|
||||
}
|
||||
@@ -226,7 +226,7 @@ constructor(
|
||||
val failedDependenciesList = failed.joinToString(separator = "\n") {
|
||||
val componentId = it.selected.id
|
||||
when (componentId) {
|
||||
is ModuleComponentIdentifier -> "|Files: ${exportLibrariesResolvedGraph.dependencyArtifacts(it).map { it.file }}"
|
||||
is ModuleComponentIdentifier -> "|Files: ${exportLibrariesResolvedGraph.getArtifacts(it).map { it.file }}"
|
||||
is ProjectComponentIdentifier -> "|Project ${componentId.projectPath}"
|
||||
else -> "|${componentId.displayName}"
|
||||
}
|
||||
|
||||
+2
-2
@@ -784,7 +784,7 @@ internal class CacheBuilder(
|
||||
.filterIsInstance<ResolvedDependencyResult>()
|
||||
.forEach { ensureDependencyPrecached(it, visitedDependencies) }
|
||||
|
||||
val artifactsToAddToCache = dependencyArtifacts(dependency).filter { needCache(it.file.absolutePath) }
|
||||
val artifactsToAddToCache = getArtifacts(dependency).filter { needCache(it.file.absolutePath) }
|
||||
|
||||
if (artifactsToAddToCache.isEmpty()) return
|
||||
|
||||
@@ -848,7 +848,7 @@ internal class CacheBuilder(
|
||||
args += "-Xcache-directory=${it.absolutePath}"
|
||||
}
|
||||
getAllDependencies(dependency)
|
||||
.flatMap { dependencyArtifacts(it) }
|
||||
.flatMap { getArtifacts(it) }
|
||||
.map { it.file }
|
||||
.filterKlibsPassedToCompiler()
|
||||
.forEach {
|
||||
|
||||
+29
-59
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.gradle.utils
|
||||
import org.gradle.api.artifacts.ArtifactCollection
|
||||
import org.gradle.api.artifacts.Configuration
|
||||
import org.gradle.api.artifacts.ResolveException
|
||||
import org.gradle.api.artifacts.ResolvedConfiguration
|
||||
import org.gradle.api.artifacts.result.*
|
||||
import org.gradle.api.file.FileCollection
|
||||
import org.jetbrains.kotlin.tooling.core.withClosure
|
||||
@@ -20,8 +21,7 @@ import org.jetbrains.kotlin.tooling.core.withClosure
|
||||
*
|
||||
* Has similar API as non-configuration cache friendly Gradle's [ResolvedConfiguration]
|
||||
*/
|
||||
internal class LazyResolvedConfiguration
|
||||
private constructor(
|
||||
internal class LazyResolvedConfiguration private constructor(
|
||||
private val resolvedComponentsRootProvider: Lazy<ResolvedComponentResult>,
|
||||
private val artifactCollection: ArtifactCollection,
|
||||
private val configurationName: String,
|
||||
@@ -35,80 +35,50 @@ private constructor(
|
||||
configurationName = configuration.name
|
||||
)
|
||||
|
||||
val root get() = resolvedComponentsRootProvider.value
|
||||
val files: FileCollection get() = artifactCollection.artifactFiles
|
||||
val artifacts get() = artifactCollection.artifacts
|
||||
|
||||
private val artifactsByComponentId by TransientLazy { artifacts.groupBy { it.id.componentIdentifier } }
|
||||
val root by resolvedComponentsRootProvider
|
||||
|
||||
val allDependencies: List<DependencyResult> get() {
|
||||
val visited = mutableSetOf<DependencyResult>()
|
||||
fun DependencyResult.allDependenciesRecursive(): List<DependencyResult> {
|
||||
if (this in visited) return emptyList()
|
||||
visited += this
|
||||
return if (this is ResolvedDependencyResult) {
|
||||
listOf(this) + selected.dependencies.flatMap { it.allDependenciesRecursive() }
|
||||
} else {
|
||||
listOf(this)
|
||||
}
|
||||
private val resolvedArtifacts: Set<ResolvedArtifactResult> get() = artifactCollection.artifacts
|
||||
|
||||
private val artifactsByComponentId by TransientLazy { resolvedArtifacts.groupBy { it.id.componentIdentifier } }
|
||||
|
||||
val allDependencies: Set<DependencyResult> by TransientLazy {
|
||||
root.dependencies.withClosure<DependencyResult> {
|
||||
if (it is ResolvedDependencyResult) it.selected.dependencies
|
||||
else emptyList()
|
||||
}
|
||||
|
||||
return root.dependencies.flatMap { it.allDependenciesRecursive() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns artifacts of a given dependency walking through external variants (the ones that referenced in `available-at`)
|
||||
* For example given a MPP library:
|
||||
* lib
|
||||
* / \
|
||||
* lib-jvm lib-iosX64
|
||||
*
|
||||
* And some requested dependency on `lib` in `jvmMain` source set. In this case it should be resolved to `lib-jvm`
|
||||
* so requesting [dependencyArtifacts] for `lib` should actually return a jvm artifact from `lib-jvm`
|
||||
*
|
||||
* If you need to avoid such behavior see [moduleArtifacts]
|
||||
*/
|
||||
fun dependencyArtifacts(dependency: ResolvedDependencyResult): List<ResolvedArtifactResult> {
|
||||
val componentId = dependency.resolvedVariant.findNonExternalVariant().owner
|
||||
fun getArtifacts(dependency: ResolvedDependencyResult): List<ResolvedArtifactResult> {
|
||||
val componentId = dependency.resolvedVariant.lastExternalVariantOrSelf().owner
|
||||
return artifactsByComponentId[componentId] ?: emptyList()
|
||||
}
|
||||
|
||||
fun componentArtifacts(component: ResolvedComponentResult): List<ResolvedArtifactResult> {
|
||||
val componentIds = component.variants.map { it.findNonExternalVariant().owner }
|
||||
fun getArtifacts(component: ResolvedComponentResult): List<ResolvedArtifactResult> {
|
||||
val componentIds = component.variants.map { it.lastExternalVariantOrSelf().owner }
|
||||
return componentIds.flatMap { artifactsByComponentId[it].orEmpty() }
|
||||
}
|
||||
|
||||
private fun ResolvedVariantResult.findNonExternalVariant(): ResolvedVariantResult =
|
||||
if (externalVariant.isPresent) {
|
||||
externalVariant.get().findNonExternalVariant()
|
||||
} else {
|
||||
this
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns own artifacts of a given dependency.
|
||||
* Acts in the same way as [org.gradle.api.artifacts.ResolvedDependency.getModuleArtifacts].
|
||||
*
|
||||
* To get artifacts from External Variants (aka `available-at`) use [dependencyArtifacts]
|
||||
*/
|
||||
fun moduleArtifacts(dependency: ResolvedDependencyResult): List<ResolvedArtifactResult> {
|
||||
val componentId = dependency.resolvedVariant.owner
|
||||
return artifactsByComponentId[componentId] ?: emptyList()
|
||||
private tailrec fun ResolvedVariantResult.lastExternalVariantOrSelf(): ResolvedVariantResult {
|
||||
return if (externalVariant.isPresent) externalVariant.get().lastExternalVariantOrSelf() else this
|
||||
}
|
||||
|
||||
override fun toString(): String = "ResolvedDependencyGraph(configuration='$configurationName')"
|
||||
override fun toString(): String = "LazyResolvedConfiguration(configuration='$configurationName')"
|
||||
}
|
||||
|
||||
internal val LazyResolvedConfiguration.allResolvedDependencies: Set<ResolvedDependencyResult> get() = allDependencies
|
||||
.filterIsInstance<ResolvedDependencyResult>()
|
||||
.toSet()
|
||||
internal val LazyResolvedConfiguration.allResolvedDependencies: Set<ResolvedDependencyResult>
|
||||
get() = allDependencies
|
||||
.filterIsInstance<ResolvedDependencyResult>()
|
||||
.toSet()
|
||||
|
||||
/**
|
||||
* Same as [LazyResolvedConfiguration.dependencyArtifacts] except it returns null for cases when dependency is resolved
|
||||
* Same as [LazyResolvedConfiguration.getArtifacts] except it returns null for cases when dependency is resolved
|
||||
* but artifact is not available. For example when host-specific part of the library is not yet published
|
||||
*/
|
||||
internal fun LazyResolvedConfiguration.dependencyArtifactsOrNull(dependency: ResolvedDependencyResult): List<ResolvedArtifactResult>? = try {
|
||||
dependencyArtifacts(dependency)
|
||||
} catch (_: ResolveException) {
|
||||
null
|
||||
}
|
||||
internal fun LazyResolvedConfiguration.dependencyArtifactsOrNull(dependency: ResolvedDependencyResult): List<ResolvedArtifactResult>? =
|
||||
try {
|
||||
getArtifacts(dependency)
|
||||
} catch (_: ResolveException) {
|
||||
null
|
||||
}
|
||||
+2
-2
@@ -51,7 +51,7 @@ private fun computeDependenciesHash(dependency: ResolvedDependencyResult, resolv
|
||||
if (partialLinkage) append("#__PL__#")
|
||||
|
||||
(listOf(dependency) + getAllDependencies(dependency))
|
||||
.flatMap { resolvedConfiguration.dependencyArtifacts(it) }
|
||||
.flatMap { resolvedConfiguration.getArtifacts(it) }
|
||||
.map { it.file.absolutePath }
|
||||
.distinct()
|
||||
.sortedBy { it }
|
||||
@@ -72,7 +72,7 @@ internal fun getDependenciesCacheDirectories(
|
||||
): List<File>? {
|
||||
return getAllDependencies(dependency)
|
||||
.flatMap { childDependency ->
|
||||
resolvedConfiguration.dependencyArtifacts(childDependency).map {
|
||||
resolvedConfiguration.getArtifacts(childDependency).map {
|
||||
if (libraryFilter(it)) {
|
||||
val cacheDirectory = getCacheDirectory(
|
||||
rootCacheDirectory = rootCacheDirectory,
|
||||
|
||||
Reference in New Issue
Block a user