"""
This folder contains labels used to collect release artifacts.
"""

load("@com_github_ash2k_bazel_tools//multirun:def.bzl", "multirun")
load("@rules_oci//oci:defs.bzl", "oci_push")
load("//bazel/oci:containers.bzl", "container_reponame", "containers")
load("//bazel/oci:pin.bzl", "oci_sum", "oci_sum_merge")

[
    container_reponame(
        name = container["name"] + "_reponame",
        container_name = container["image_name"],
    )
    for container in containers()
]

[
    genrule(
        name = container["name"] + "_repotag",
        srcs = [
            "//bazel/release:" + container["name"] + "_reponame",
            "//bazel/settings:tag",
        ],
        outs = [container["repotag_file"]],
        cmd = "echo -n ':' | cat $(location //bazel/release:" + container["name"] + "_reponame) - $(location //bazel/settings:tag) > $@",
        visibility = ["//visibility:public"],
    )
    for container in containers()
]

# TODO(3u13r): re-enable target once https://github.com/bazel-contrib/rules_oci/issues/184 is fixed
# [
#     oci_tarball(
#         name = container["name"] + "_tar",
#         image = container["oci"],
#         repotag_file = container["repotag_file"],
#     )
#     for container in containers()
# ]

[
    oci_sum(
        name = container["name"] + "_sum",
        image_name = container["image_name"],
        oci = container["oci"],
        repotag_file = container["repotag_file"],
    )
    for container in containers()
]

oci_sum_merge(
    name = "container_sums",
    sums = [
        ":%s_sum" % container["name"]
        for container in containers()
    ],
    visibility = ["//visibility:public"],
)

[
    oci_push(
        name = container["name"] + "_push",
        image = container["oci"],
        repotags = container["repotag_file"],
    )
    for container in containers()
]

multirun(
    name = "push",
    commands = [
        ":" + container["name"] + "_push"
        for container in containers()
    ],
    jobs = 0,  # execute in parallel
    visibility = ["//visibility:public"],
)

multirun(
    name = "build_and_push",
    commands = [
        "//bazel/release:push",
        "//bazel/devbuild:devbuild",
    ],
    visibility = ["//visibility:public"],
)
