Skip to content

Commit

Permalink
WIP variants for each platform
Browse files Browse the repository at this point in the history
- Add createPlatformPostfix function
  • Loading branch information
SgtSilvio committed Jun 30, 2024
1 parent 8d40a2d commit 25a66cd
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -263,12 +263,12 @@ internal abstract class OciImageDefinitionImpl @Inject constructor(
}

private fun createMetadataTask(imageDefName: String, taskContainer: TaskContainer, projectLayout: ProjectLayout) =
taskContainer.register<OciMetadataTask>(createOciMetadataClassifier(imageDefName).camelCase() + if (platform == null) "" else "@$platform") {
taskContainer.register<OciMetadataTask>(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<in OciImageDefinition.Bundle.ParentImages>) =
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}")
}
}
}
Expand Down Expand Up @@ -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<OciLayerTask>(createOciLayerClassifier(imageDefName, layerName).camelCase() + platformString) {
) = register<OciLayerTask>(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<OciLayerTask>.contents(configuration: Action<in OciCopySpec>) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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)
}
}
Expand All @@ -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")
Expand All @@ -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)
Expand Down

0 comments on commit 25a66cd

Please sign in to comment.