Skip to content

Commit

Permalink
Improve resolution code
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Nov 3, 2024
1 parent 69431b9 commit 38ab2dc
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,9 @@ internal abstract class OciImageDependenciesImpl @Inject constructor(
throw e
}
val platformSelector = platformSelectorProvider.orNull
val graphRootAndPlatformsList = selectPlatforms(graph, platformSelector)
val platformToGraphRoots = HashMap<Platform, ArrayList<OciVariantGraphRoot>>()
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) {
Expand Down Expand Up @@ -112,7 +106,7 @@ internal abstract class OciImageDependenciesImpl @Inject constructor(
}
}
val imageInputs = ArrayList<OciImagesTask.ImageInput>()
for ((graphRoot, platforms) in graphRootAndPlatformsList) {
for ((graphRoot, platforms) in selectedPlatformsGraph) {
for (platform in platforms) {
imageInputs += variantSelectorsToImageInput[Pair(platform, graphRoot.variantSelectors)]
?: throw IllegalStateException() // TODO message
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,34 @@ private fun String.decodePlatforms() = split(';').mapTo(LinkedHashSet()) { it.to

// TODO new file from here?

internal fun selectPlatforms(
graph: List<OciVariantGraphRoot>,
internal fun List<OciVariantGraphRoot>.selectPlatforms(
platformSelector: PlatformSelector?,
): List<Pair<OciVariantGraphRoot, Set<Platform>>> = 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<Pair<OciVariantGraphRoot, Set<Platform>>> {
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<Pair<OciVariantGraphRoot, Set<Platform>>>.groupByPlatform(): Map<Platform, List<OciVariantGraphRoot>> {
val platformToGraphRoots = HashMap<Platform, ArrayList<OciVariantGraphRoot>>()
for ((graphRoot, platforms) in this) {
for (platform in platforms) {
platformToGraphRoots.getOrPut(platform) { ArrayList() } += graphRoot
}
}
Pair(graphRoot, platforms)
return platformToGraphRoots
}

// TODO new file from here?
Expand Down

0 comments on commit 38ab2dc

Please sign in to comment.