Skip to content

Commit

Permalink
Move functions to OciVariantGraphPlatformSelection.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Nov 4, 2024
1 parent 8dc75b7 commit 136a3ed
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,49 +6,10 @@ import io.github.sgtsilvio.gradle.oci.internal.gradle.toVariantSelector
import io.github.sgtsilvio.gradle.oci.metadata.DEFAULT_OCI_IMAGE_REFERENCE_SPEC
import io.github.sgtsilvio.gradle.oci.metadata.OciImageReferenceSpec
import io.github.sgtsilvio.gradle.oci.metadata.toOciImageReferenceSpec
import io.github.sgtsilvio.gradle.oci.platform.Platform
import io.github.sgtsilvio.gradle.oci.platform.PlatformSelector
import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.artifacts.result.ResolvedDependencyResult
import org.gradle.api.artifacts.result.ResolvedVariantResult

// TODO new file from here?

internal fun OciVariantGraph.selectPlatforms(
platformSelector: PlatformSelector?,
): List<Pair<OciVariantGraphRoot, Set<Platform>>> {
val selectedPlatformsGraph = ArrayList<Pair<OciVariantGraphRoot, Set<Platform>>>(size)
val graphRootsWithEmptySelection = ArrayList<OciVariantGraphRoot>()
for (graphRoot in this) {
val supportedPlatforms = graphRoot.node.supportedPlatforms
val platforms = platformSelector?.select(supportedPlatforms) ?: supportedPlatforms.set
if (platforms.isEmpty()) {
graphRootsWithEmptySelection += graphRoot
} else {
selectedPlatformsGraph += Pair(graphRoot, platforms)
}
}
if (graphRootsWithEmptySelection.isNotEmpty()) {
val errorMessage = graphRootsWithEmptySelection.joinToString("\n") {
"no platforms can be selected for variant ${it.node.variant} (supported platforms: ${it.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
}
}
return platformToGraphRoots
}

// TODO new file from here?

internal class OciImageSpec(val variants: List<ResolvedVariantResult>, val selectors: Set<VariantSelector>) // TODO

internal fun collectOciImageSpecs(rootComponent: ResolvedComponentResult): List<OciImageSpec> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.github.sgtsilvio.gradle.oci.internal.resolution

import io.github.sgtsilvio.gradle.oci.platform.Platform
import io.github.sgtsilvio.gradle.oci.platform.PlatformSelector

internal typealias OciVariantGraphWithSelectedPlatforms = List<Pair<OciVariantGraphRoot, Set<Platform>>>

internal fun OciVariantGraph.selectPlatforms(platformSelector: PlatformSelector?): OciVariantGraphWithSelectedPlatforms {
val selectedPlatformsGraph = ArrayList<Pair<OciVariantGraphRoot, Set<Platform>>>(size)
val graphRootsWithEmptySelection = ArrayList<OciVariantGraphRoot>()
for (graphRoot in this) {
val supportedPlatforms = graphRoot.node.supportedPlatforms
val platforms = platformSelector?.select(supportedPlatforms) ?: supportedPlatforms.set
if (platforms.isEmpty()) {
graphRootsWithEmptySelection += graphRoot
} else {
selectedPlatformsGraph += Pair(graphRoot, platforms)
}
}
if (graphRootsWithEmptySelection.isNotEmpty()) {
val errorMessage = graphRootsWithEmptySelection.joinToString("\n") {
"no platforms can be selected for variant ${it.node.variant} (supported platforms: ${it.node.supportedPlatforms}, platform selector: $platformSelector)"
}
throw IllegalStateException(errorMessage)
}
return selectedPlatformsGraph
}

internal fun OciVariantGraphWithSelectedPlatforms.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
}
}
return platformToGraphRoots
}

0 comments on commit 136a3ed

Please sign in to comment.