From 6771e5fca3b6519a9a878d18d315ea3838de44b8 Mon Sep 17 00:00:00 2001 From: Silvio Giebl Date: Fri, 12 Jul 2024 14:54:52 +0200 Subject: [PATCH] Move OciImageReferenceSpec and related functions to own file --- ...ciImagesInput.kt => OciImagesInputTask.kt} | 18 +-------------- .../dsl/ResolvableOciImageDependenciesImpl.kt | 2 +- .../resolution/OciImageSpecResolution.kt | 8 +++---- .../oci/metadata/OciImageReferenceSpec.kt | 22 +++++++++++++++++++ 4 files changed, 28 insertions(+), 22 deletions(-) rename src/main/kotlin/io/github/sgtsilvio/gradle/oci/{OciImagesInput.kt => OciImagesInputTask.kt} (91%) create mode 100644 src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciImageReferenceSpec.kt diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/OciImagesInput.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/OciImagesInputTask.kt similarity index 91% rename from src/main/kotlin/io/github/sgtsilvio/gradle/oci/OciImagesInput.kt rename to src/main/kotlin/io/github/sgtsilvio/gradle/oci/OciImagesInputTask.kt index c477894b..8e74e0b3 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/OciImagesInput.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/OciImagesInputTask.kt @@ -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 @@ -29,21 +28,6 @@ data class OciVariantInput( @get:InputFiles @get:PathSensitive(PathSensitivity.NONE) val layerFiles: List, ) -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, @@ -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) } diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/ResolvableOciImageDependenciesImpl.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/ResolvableOciImageDependenciesImpl.kt index fe858dd1..b918348a 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/ResolvableOciImageDependenciesImpl.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/ResolvableOciImageDependenciesImpl.kt @@ -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 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 41993d84..e35a7945 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 @@ -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 @@ -180,4 +180,4 @@ private fun OciVariantNode.collectVariantResultsForPlatform( } private fun Set.normalize(): Set = - if ((size == 1) && (first() == DEFAULT_OCI_REFERENCE_SPEC)) emptySet() else this + if ((size == 1) && contains(DEFAULT_OCI_REFERENCE_SPEC)) emptySet() else this diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciImageReferenceSpec.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciImageReferenceSpec.kt new file mode 100644 index 00000000..f27b76fb --- /dev/null +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/metadata/OciImageReferenceSpec.kt @@ -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() }) +}