From 38ab2dca0358d2b291d7646848f4150be89dd0a9 Mon Sep 17 00:00:00 2001 From: Silvio Giebl Date: Sun, 3 Nov 2024 23:38:21 +0100 Subject: [PATCH] Improve resolution code --- .../internal/dsl/OciImageDependenciesImpl.kt | 12 ++----- .../resolution/OciImageSpecResolution.kt | 34 ++++++++++++++----- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDependenciesImpl.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDependenciesImpl.kt index 2ed6bffb..6df95dc4 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDependenciesImpl.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDependenciesImpl.kt @@ -65,15 +65,9 @@ internal abstract class OciImageDependenciesImpl @Inject constructor( throw e } val platformSelector = platformSelectorProvider.orNull - val graphRootAndPlatformsList = selectPlatforms(graph, platformSelector) - val platformToGraphRoots = HashMap>() - for ((graphRoot, platforms) in graphRootAndPlatformsList) { - for (platform in platforms) { - platformToGraphRoots.getOrPut(platform) { ArrayList() } += graphRoot - } - } + val selectedPlatformsGraph = graph.selectPlatforms(platformSelector) val allDependencies = allDependencies.value - val platformToConfiguration = platformToGraphRoots.mapValues { (platform, graphRoots) -> + val platformToConfiguration = selectedPlatformsGraph.groupByPlatform().mapValues { (platform, graphRoots) -> val platformConfigurationName = "${indexConfiguration.name}@$platform" + if (platformSelector == null) "" else "($platformSelector)" platformConfigurations.getOrPut(platformConfigurationName) { @@ -112,7 +106,7 @@ internal abstract class OciImageDependenciesImpl @Inject constructor( } } val imageInputs = ArrayList() - for ((graphRoot, platforms) in graphRootAndPlatformsList) { + for ((graphRoot, platforms) in selectedPlatformsGraph) { for (platform in platforms) { imageInputs += variantSelectorsToImageInput[Pair(platform, graphRoot.variantSelectors)] ?: throw IllegalStateException() // TODO message diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/resolution/OciImageSpecResolution.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/resolution/OciImageSpecResolution.kt index 3cd0c238..ebf40f5c 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/resolution/OciImageSpecResolution.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/resolution/OciImageSpecResolution.kt @@ -87,16 +87,34 @@ private fun String.decodePlatforms() = split(';').mapTo(LinkedHashSet()) { it.to // TODO new file from here? -internal fun selectPlatforms( - graph: List, +internal fun List.selectPlatforms( platformSelector: PlatformSelector?, -): List>> = graph.map { graphRoot -> - val rootNode = graphRoot.node - val platforms = platformSelector?.select(rootNode.supportedPlatforms) ?: rootNode.supportedPlatforms.set - if (platforms.isEmpty()) { // TODO defer exception, empty set is failed - throw IllegalStateException("no platforms can be selected for variant ${rootNode.variant} (supported platforms: ${rootNode.supportedPlatforms}, platform selector: $platformSelector)") +): List>> { + var hasEmptySelection = false + val selectedPlatformsGraph = map { graphRoot -> + val supportedPlatforms = graphRoot.node.supportedPlatforms + val platforms = platformSelector?.select(supportedPlatforms) ?: supportedPlatforms.set + if (platforms.isEmpty()) { + hasEmptySelection = true + } + Pair(graphRoot, platforms) + } + if (hasEmptySelection) { + val errorMessage = selectedPlatformsGraph.filter { (_, platforms) -> platforms.isEmpty() } + .joinToString("\n") { (graphRoot) -> "no platforms can be selected for variant ${graphRoot.node.variant} (supported platforms: ${graphRoot.node.supportedPlatforms}, platform selector: $platformSelector)" } + throw IllegalStateException(errorMessage) + } + return selectedPlatformsGraph +} + +internal fun List>>.groupByPlatform(): Map> { + val platformToGraphRoots = HashMap>() + for ((graphRoot, platforms) in this) { + for (platform in platforms) { + platformToGraphRoots.getOrPut(platform) { ArrayList() } += graphRoot + } } - Pair(graphRoot, platforms) + return platformToGraphRoots } // TODO new file from here?