Skip to content

Commit

Permalink
Move OciImageReferenceSpec and related functions to own file
Browse files Browse the repository at this point in the history
  • Loading branch information
SgtSilvio committed Jul 12, 2024
1 parent 146ec2e commit 6771e5f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.gradle.api.DefaultTask
import org.gradle.api.tasks.*
import org.gradle.kotlin.dsl.setProperty
import java.io.File
import java.io.Serializable

/**
* @author Silvio Giebl
Expand All @@ -29,21 +28,6 @@ data class OciVariantInput(
@get:InputFiles @get:PathSensitive(PathSensitivity.NONE) val layerFiles: List<File>,
)

data class OciImageReferenceSpec(val name: String?, val tag: String?) : Serializable {
override fun toString() = (name ?: "") + ":" + (tag ?: "")
}

internal fun String.toOciImageReferenceSpec(): OciImageReferenceSpec {
val parts = split(':')
if (parts.size != 2) {
throw IllegalArgumentException("'$this' must contain exactly one ':' character")
}
return OciImageReferenceSpec(parts[0].takeIf { it.isNotEmpty() }, parts[1].takeIf { it.isNotEmpty() })
}

// TODO factory method for OciImageReferenceSpec that returns DEFAULT_OCI_REFERENCE_SPEC if both are null
internal val DEFAULT_OCI_REFERENCE_SPEC = OciImageReferenceSpec(null, null)

internal class OciMultiArchImage(
val index: OciData,
val platformToImage: Map<Platform, OciImage>,
Expand Down Expand Up @@ -111,7 +95,7 @@ abstract class OciImagesInputTask : DefaultTask() {
val defaultImageReference = variants.last().metadata.imageReference
// imageReferences set is linked because it will be iterated
val imageReferences = imageInput.referenceSpecs.mapTo(LinkedHashSet()) {
OciImageReference(it.name ?: defaultImageReference.name, it.tag ?: defaultImageReference.tag)
it.materialize(defaultImageReference)
}.ifEmpty { setOf(defaultImageReference) }
Pair(image, imageReferences)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package io.github.sgtsilvio.gradle.oci.internal.dsl

import io.github.sgtsilvio.gradle.oci.OciImageReferenceSpec
import io.github.sgtsilvio.gradle.oci.attributes.*
import io.github.sgtsilvio.gradle.oci.dsl.ResolvableOciImageDependencies
import io.github.sgtsilvio.gradle.oci.dsl.ResolvableOciImageDependencies.Nameable
import io.github.sgtsilvio.gradle.oci.dsl.ResolvableOciImageDependencies.Taggable
import io.github.sgtsilvio.gradle.oci.internal.gradle.attribute
import io.github.sgtsilvio.gradle.oci.internal.gradle.zipAbsentAsNull
import io.github.sgtsilvio.gradle.oci.metadata.OciImageReferenceSpec
import org.gradle.api.artifacts.ConfigurationContainer
import org.gradle.api.artifacts.ModuleDependency
import org.gradle.api.artifacts.dsl.DependencyHandler
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package io.github.sgtsilvio.gradle.oci.internal.resolution

import io.github.sgtsilvio.gradle.oci.DEFAULT_OCI_REFERENCE_SPEC
import io.github.sgtsilvio.gradle.oci.OciImageReferenceSpec
import io.github.sgtsilvio.gradle.oci.attributes.MULTIPLE_PLATFORMS_ATTRIBUTE_VALUE
import io.github.sgtsilvio.gradle.oci.attributes.OCI_IMAGE_REFERENCE_ATTRIBUTE
import io.github.sgtsilvio.gradle.oci.attributes.PLATFORM_ATTRIBUTE
import io.github.sgtsilvio.gradle.oci.attributes.UNIVERSAL_PLATFORM_ATTRIBUTE_VALUE
import io.github.sgtsilvio.gradle.oci.metadata.DEFAULT_OCI_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.toPlatform
import io.github.sgtsilvio.gradle.oci.toOciImageReferenceSpec
import org.gradle.api.artifacts.result.ResolvedComponentResult
import org.gradle.api.artifacts.result.ResolvedDependencyResult
import org.gradle.api.artifacts.result.ResolvedVariantResult
Expand Down Expand Up @@ -180,4 +180,4 @@ private fun OciVariantNode.collectVariantResultsForPlatform(
}

private fun Set<OciImageReferenceSpec>.normalize(): Set<OciImageReferenceSpec> =
if ((size == 1) && (first() == DEFAULT_OCI_REFERENCE_SPEC)) emptySet() else this
if ((size == 1) && contains(DEFAULT_OCI_REFERENCE_SPEC)) emptySet() else this
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.sgtsilvio.gradle.oci.metadata

import java.io.Serializable

/**
* @author Silvio Giebl
*/
data class OciImageReferenceSpec(val name: String?, val tag: String?) : Serializable {
override fun toString() = (name ?: "") + ":" + (tag ?: "")
}

// TODO factory method for OciImageReferenceSpec that returns DEFAULT_OCI_REFERENCE_SPEC if both are null
internal val DEFAULT_OCI_REFERENCE_SPEC = OciImageReferenceSpec(null, null)

internal fun OciImageReferenceSpec.materialize(default: OciImageReference) =
OciImageReference(name ?: default.name, tag ?: default.tag)

internal fun String.toOciImageReferenceSpec(): OciImageReferenceSpec {
val parts = split(':')
require(parts.size == 2) { "'$this' must contain exactly one ':' character" }
return OciImageReferenceSpec(parts[0].takeIf { it.isNotEmpty() }, parts[1].takeIf { it.isNotEmpty() })
}

0 comments on commit 6771e5f

Please sign in to comment.