package(default_visibility = ["//visibility:public"])

load("@io_k8s_repo_infra//defs:pkg.bzl", "pkg_tar")
load("//build:helm.bzl", "helm_pkg")

pkg_tar(
    name = "release-tar",
    srcs = [
        ":cert-manager",
    ],
    extension = "tar.gz",
    mode = "0644",
    package_dir = "chart",
    strip_prefix = ".",
)

helm_pkg(
    name = "cert-manager",
    srcs = ["//deploy/charts/cert-manager/templates:chart-srcs"],
    chart_name = "cert-manager",
    chart_yaml = ":Chart.yaml",
    readme_file = ":README.md",
    tpl_files = [
        "//deploy/charts/cert-manager/templates:_helpers.tpl",
    ],
    values_yaml = ":values.yaml",
)

genrule(
    name = "README",
    srcs = [
        "README.template.md",
        "//:version",
    ],
    outs = ["README.md"],
    cmd = "cat $(location README.template.md) | sed -e \"s:{{RELEASE_VERSION}}:$$(cat $(location //:version)):g\" > $@",
    stamp = 1,
    visibility = ["//visibility:public"],
)

# We don't want alpha and beta releases to show on ArtifactHUB as releases.
# To prevent pre-releases from appearing, we use the ArtifactHUB-specific:
#
#   annotation:
#     artifacthub.io/prerelease: "true"
#
# See https://artifacthub.io/docs/topics/annotations/helm/
genrule(
    name = "chart-yaml",
    srcs = [
        "Chart.template.yaml",
        "//:version",
    ],
    outs = ["Chart.yaml"],
    cmd = """
    IS_PRERELEASE=$$(grep -q '^v[0-9]\\+.[0-9]\\+.[0-9]\\+$$' $(location //:version) && echo "false" || echo "true")
    sed -e "s:{{IS_PRERELEASE}}:$${IS_PRERELEASE}:g" < $(location Chart.template.yaml) > $@
    """,
    stamp = 1,
    visibility = ["//visibility:public"],
)

filegroup(
    name = "chart-srcs",
    srcs = ["//deploy/charts/cert-manager/templates:chart-srcs"] + glob(
        [
            ".helmignore",
            "Chart.yaml",
            "README.md",
            "values.yaml",
        ],
    ),
)

filegroup(
    name = "package-srcs",
    srcs = glob(["**"]),
    tags = ["automanaged"],
    visibility = ["//visibility:private"],
)

filegroup(
    name = "all-srcs",
    srcs = [
        ":package-srcs",
        "//deploy/charts/cert-manager/templates:all-srcs",
    ],
    tags = ["automanaged"],
)
