From 25a66cdfc38760ba852d00f31fd383345f7e04f8 Mon Sep 17 00:00:00 2001 From: Silvio Giebl Date: Sun, 30 Jun 2024 17:23:35 +0200 Subject: [PATCH] WIP variants for each platform - Add createPlatformPostfix function --- .../sgtsilvio/gradle/oci/internal/OciNaming.kt | 6 ++++-- .../oci/internal/dsl/OciImageDefinitionImpl.kt | 16 ++++++++-------- .../internal/registry/OciRepositoryHandler.kt | 18 +++++++----------- 3 files changed, 19 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/OciNaming.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/OciNaming.kt index e5bb961e..d2618ac3 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/OciNaming.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/OciNaming.kt @@ -10,13 +10,15 @@ internal fun createOciVariantName(variantName: String): String = variantName.mainToEmpty().camelCase().concatCamelCase("ociImage") internal fun createOciVariantName(variantName: String, platform: Platform): String = - variantName.mainToEmpty().camelCase().concatCamelCase("ociImage") + '@' + platform + variantName.mainToEmpty().camelCase().concatCamelCase("ociImage") + createPlatformPostfix(platform) internal fun createOciVariantInternalName(variantName: String, platform: Platform): String = - variantName.mainToEmpty().camelCase().concatCamelCase("ociImageInternal") + '@' + platform + variantName.mainToEmpty().camelCase().concatCamelCase("ociImageInternal") + createPlatformPostfix(platform) internal fun createOciMetadataClassifier(variantName: String): String = variantName.mainToEmpty().kebabCase().concatKebabCase("oci-metadata") internal fun createOciLayerClassifier(variantName: String, layerName: String): String = variantName.mainToEmpty().kebabCase().concatKebabCase(layerName.kebabCase()).concatKebabCase("oci-layer") + +internal fun createPlatformPostfix(platform: Platform?) = if (platform == null) "" else "@$platform" diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDefinitionImpl.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDefinitionImpl.kt index d922ea9f..5700a316 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDefinitionImpl.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/dsl/OciImageDefinitionImpl.kt @@ -263,12 +263,12 @@ internal abstract class OciImageDefinitionImpl @Inject constructor( } private fun createMetadataTask(imageDefName: String, taskContainer: TaskContainer, projectLayout: ProjectLayout) = - taskContainer.register(createOciMetadataClassifier(imageDefName).camelCase() + if (platform == null) "" else "@$platform") { + taskContainer.register(createOciMetadataClassifier(imageDefName).camelCase() + createPlatformPostfix(platform)) { group = TASK_GROUP_NAME description = "Assembles the metadata json file of the '$imageDefName' OCI image" + if (platform == null) "." else " for the platform $platform" encodedMetadata.set(metadata.map { it.encodeToJsonString() }) destinationDirectory.set(projectLayout.buildDirectory.dir("oci/images/$imageDefName")) - classifier.set(createOciMetadataClassifier(imageDefName) + if (platform == null) "" else "@$platform") + classifier.set(createOciMetadataClassifier(imageDefName) + createPlatformPostfix(platform)) } final override fun parentImages(configuration: Action) = @@ -387,7 +387,7 @@ internal abstract class OciImageDefinitionImpl @Inject constructor( var task = task val bundleScopeConfigurations = bundleScopeConfigurations if ((task == null) || (bundleScopeConfigurations != null)) { - task = taskContainer.createLayerTask(imageDefName, name, if (platform == null) "" else "@$platform", projectLayout) + task = taskContainer.createLayerTask(imageDefName, name, createPlatformPostfix(platform), projectLayout) this.task = task if (bundleScopeConfigurations != null) { this.bundleScopeConfigurations = null @@ -517,11 +517,11 @@ internal abstract class OciImageDefinitionImpl @Inject constructor( fun onAfterEvaluate() { val capabilities = imageDefinition.configuration.outgoing.capabilities if (capabilities.isEmpty()) { - configuration.outgoing.capability("${project.group}:${project.name}@$platform:${project.version}") + configuration.outgoing.capability("${project.group}:${project.name}${createPlatformPostfix(platform)}:${project.version}") } else { for (capability in capabilities) { externalConfiguration.outgoing.capability(capability) - configuration.outgoing.capability("${capability.group}:${capability.name}@$platform:${capability.version}") + configuration.outgoing.capability("${capability.group}:${capability.name}${createPlatformPostfix(platform)}:${capability.version}") } } } @@ -627,13 +627,13 @@ internal abstract class OciImageDefinitionImpl @Inject constructor( private fun TaskContainer.createLayerTask( imageDefName: String, layerName: String, - platformString: String, + platformPostfix: String, projectLayout: ProjectLayout, -) = register(createOciLayerClassifier(imageDefName, layerName).camelCase() + platformString) { +) = register(createOciLayerClassifier(imageDefName, layerName).camelCase() + platformPostfix) { group = TASK_GROUP_NAME description = "Assembles the layer '$layerName' of the '$imageDefName' OCI image." destinationDirectory.set(projectLayout.buildDirectory.dir("oci/images/$imageDefName")) - classifier.set(createOciLayerClassifier(imageDefName, layerName) + platformString) + classifier.set(createOciLayerClassifier(imageDefName, layerName) + platformPostfix) } private fun TaskProvider.contents(configuration: Action) = diff --git a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciRepositoryHandler.kt b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciRepositoryHandler.kt index a1ed9e17..bb7e768f 100644 --- a/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciRepositoryHandler.kt +++ b/src/main/kotlin/io/github/sgtsilvio/gradle/oci/internal/registry/OciRepositoryHandler.kt @@ -5,11 +5,8 @@ import com.github.benmanes.caffeine.cache.Caffeine import io.github.sgtsilvio.gradle.oci.attributes.* import io.github.sgtsilvio.gradle.oci.component.VersionedCoordinates import io.github.sgtsilvio.gradle.oci.component.encodeToJsonString +import io.github.sgtsilvio.gradle.oci.internal.* import io.github.sgtsilvio.gradle.oci.internal.cache.getMono -import io.github.sgtsilvio.gradle.oci.internal.createOciLayerClassifier -import io.github.sgtsilvio.gradle.oci.internal.createOciMetadataClassifier -import io.github.sgtsilvio.gradle.oci.internal.createOciVariantInternalName -import io.github.sgtsilvio.gradle.oci.internal.createOciVariantName import io.github.sgtsilvio.gradle.oci.internal.json.addArray import io.github.sgtsilvio.gradle.oci.internal.json.addArrayIfNotEmpty import io.github.sgtsilvio.gradle.oci.internal.json.addObject @@ -195,7 +192,7 @@ internal class OciRepositoryHandler( addString("org.gradle.status", "release") } } - val fileNamePrefix = "${componentId.name}-${componentId.version}" + val fileNamePrefix = "${componentId.name}-${componentId.version}-" addArray("variants") { for ((variantName, capabilities, metadataList) in variantMetadataList) { addObject { @@ -227,7 +224,7 @@ internal class OciRepositoryHandler( addArray("requestedCapabilities", capabilities) { capability -> addObject { addString("group", capability.group) - addString("name", capability.name + '@' + platform) + addString("name", capability.name + createPlatformPostfix(platform)) addString("version", capability.version) } } @@ -264,7 +261,7 @@ internal class OciRepositoryHandler( addArray("requestedCapabilities", capabilities) { capability -> addObject { addString("group", capability.group) - addString("name", capability.name + '@' + platform) + addString("name", capability.name + createPlatformPostfix(platform)) addString("version", capability.version) } } @@ -282,14 +279,14 @@ internal class OciRepositoryHandler( addArray("capabilities", capabilities) { capability -> addObject { addString("group", capability.group) - addString("name", capability.name + '@' + platform) + addString("name", capability.name + createPlatformPostfix(platform)) addString("version", capability.version) } } addArray("files") { addObject { val metadataJson = metadata.encodeToJsonString().toByteArray() - val metadataName = "$fileNamePrefix-${createOciMetadataClassifier(variantName)}@$platform.json" + val metadataName = fileNamePrefix + createOciMetadataClassifier(variantName) + createPlatformPostfix(platform) + ".json" val escapedImageReference = metadata.imageReference.toString().escapePathSegment() addString("name", metadataName) addString("url", "$escapedImageReference/$digest/$size/$metadataName") @@ -304,11 +301,10 @@ internal class OciRepositoryHandler( addObject { val algorithmId = layerDigest.algorithm.id val encodedHash = layerDigest.encodedHash - val classifier = createOciLayerClassifier( + val layerName = fileNamePrefix + createOciLayerClassifier( "main", algorithmId + '!' + encodedHash.take(5) + ".." + encodedHash.takeLast(5), ) - val layerName = "$fileNamePrefix-$classifier" addString("name", layerName + mapLayerMediaTypeToExtension(mediaType)) addString("url", "$escapedImageName/$layerDigest/$layerSize/$layerName") addNumber("size", layerSize)