|
| 1 | +package dev.codebandits.container.gradle |
| 2 | + |
| 3 | +import dev.codebandits.container.gradle.image.Local |
| 4 | +import dev.codebandits.container.gradle.image.Registry |
| 5 | +import org.gradle.api.Task |
| 6 | +import org.gradle.api.file.RegularFile |
| 7 | +import org.gradle.api.provider.Provider |
| 8 | +import java.net.URLEncoder |
| 9 | +import java.nio.charset.StandardCharsets |
| 10 | + |
| 11 | +public abstract class ContainerTaskExtension(private val task: Task) { |
| 12 | + public fun inputLocalImage(imageReference: String) { |
| 13 | + val trackingFile = task.getLocalImageTrackingFile(imageReference) |
| 14 | + task.inputs.file(trackingFile.map { regularFile -> |
| 15 | + writeLocalImageId(imageReference, regularFile) |
| 16 | + regularFile |
| 17 | + }) |
| 18 | + } |
| 19 | + |
| 20 | + public fun outputLocalImage(imageReference: String) { |
| 21 | + val trackingFile = task.getLocalImageTrackingFile(imageReference) |
| 22 | + task.outputs.file(trackingFile.map { regularFile -> |
| 23 | + writeLocalImageId(imageReference, regularFile) |
| 24 | + regularFile |
| 25 | + }) |
| 26 | + task.doLast { writeLocalImageId(imageReference, trackingFile.get()) } |
| 27 | + } |
| 28 | + |
| 29 | + public fun inputRegistryImage(imageReference: String, autoRefresh: Boolean = false) { |
| 30 | + val trackingFile = task.getRegistryImageTrackingFile(imageReference) |
| 31 | + task.inputs.file(trackingFile.map { regularFile -> |
| 32 | + if (autoRefresh || !regularFile.asFile.exists()) { |
| 33 | + writeRegistryImageDigest(imageReference, regularFile) |
| 34 | + } |
| 35 | + regularFile |
| 36 | + }) |
| 37 | + } |
| 38 | + |
| 39 | + public fun outputRegistryImage(imageReference: String, autoRefresh: Boolean = false) { |
| 40 | + val trackingFile = task.getLocalImageTrackingFile(imageReference) |
| 41 | + task.outputs.file(trackingFile.map { regularFile -> |
| 42 | + if (autoRefresh || !regularFile.asFile.exists()) { |
| 43 | + writeRegistryImageDigest(imageReference, regularFile) |
| 44 | + } |
| 45 | + regularFile |
| 46 | + }) |
| 47 | + task.doLast { |
| 48 | + val regularFile = trackingFile.get() |
| 49 | + if (autoRefresh || !regularFile.asFile.exists()) { |
| 50 | + writeRegistryImageDigest(imageReference, regularFile) |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private fun Task.getLocalImageTrackingFile( |
| 56 | + imageReference: String, |
| 57 | + ): Provider<RegularFile> { |
| 58 | + val fileName = URLEncoder.encode(imageReference, StandardCharsets.UTF_8) |
| 59 | + return project.layout.buildDirectory.file("images/local/$fileName") |
| 60 | + } |
| 61 | + |
| 62 | + private fun Task.getRegistryImageTrackingFile( |
| 63 | + imageReference: String, |
| 64 | + ): Provider<RegularFile> { |
| 65 | + val fileName = URLEncoder.encode(imageReference, StandardCharsets.UTF_8) |
| 66 | + return project.layout.buildDirectory.file("images/registry/$fileName") |
| 67 | + } |
| 68 | + |
| 69 | + private fun writeLocalImageId(imageReference: String, regularFile: RegularFile) { |
| 70 | + val file = regularFile.asFile |
| 71 | + if (!file.parentFile.exists()) { |
| 72 | + file.parentFile.mkdirs() |
| 73 | + } |
| 74 | + val imageId = Local.getImageId(imageReference) |
| 75 | + file.writeText(imageId ?: "") |
| 76 | + } |
| 77 | + |
| 78 | + private fun writeRegistryImageDigest(imageReference: String, regularFile: RegularFile) { |
| 79 | + val file = regularFile.asFile |
| 80 | + if (!file.parentFile.exists()) { |
| 81 | + file.parentFile.mkdirs() |
| 82 | + } |
| 83 | + val imageDigest = Registry.getImageDigest(imageReference) |
| 84 | + file.writeText(imageDigest ?: "") |
| 85 | + } |
| 86 | +} |
0 commit comments