<filename>kustomize/repositories.bzl
"""
repositories for kustomize rule
"""

load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe")
load("//kustomize/private:toolchains_repo.bzl", "PLATFORMS", "toolchains_repo")
load("//kustomize/private:versions.bzl", "TOOL_VERSIONS")

# WARNING: any changes in this function may be BREAKING CHANGES for users
# because we'll fetch a dependency which may be different from one that
# they were previously fetching later in their WORKSPACE setup, and now
# ours took precedence. Such breakages are challenging for users, so any
# changes in this function should be marked as BREAKING in the commit message
# and released only in semver majors.
def rules_kustomize_dependencies():
    maybe(
        http_archive,
        name = "bazel_skylib",
        sha256 = "c6966ec828da198c5d9adbaa94c05e3a1c7f21bd012a0b29ba8ddbccb2c93b0d",
        urls = [
            "https://github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
            "https://mirror.bazel.build/github.com/bazelbuild/bazel-skylib/releases/download/1.1.1/bazel-skylib-1.1.1.tar.gz",
        ],
    )

    maybe(
        http_archive,
        name = "aspect_bazel_lib",
        sha256 = "82e80cd47a5c2127ca0d717b7270fa833996c24dce389999c9d67e6c3eeb92e3",
        strip_prefix = "bazel-lib-0.11.10",
        url = "https://github.com/aspect-build/bazel-lib/archive/refs/tags/v0.11.10.tar.gz",
    )

_DOC = "TODO"
_ATTRS = {
    "kustomize_version": attr.string(mandatory = True, values = TOOL_VERSIONS.keys()),
    "platform": attr.string(mandatory = True, values = PLATFORMS.keys()),
}

def _kustomize_repo_impl(repository_ctx):
    url = "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv{0}/kustomize_v{1}_{2}.tar.gz".format(
        repository_ctx.attr.kustomize_version,
        repository_ctx.attr.kustomize_version,
        repository_ctx.attr.platform,
    )
    repository_ctx.download_and_extract(
        url = url,
        sha256 = TOOL_VERSIONS[repository_ctx.attr.kustomize_version][repository_ctx.attr.platform],
    )
    build_content = """#Generated by kustomize/repositories.bzl
load("@ubiquitous_tech_rules_kustomize//kustomize:toolchain.bzl", "kustomize_toolchain")
kustomize_toolchain(name = "kustomize_toolchain", target_tool = select({
        "@bazel_tools//src/conditions:host_windows": "kustomize.exe",
        "//conditions:default": "kustomize",
    }),
)
"""

    # Base BUILD file for this repository
    repository_ctx.file("BUILD.bazel", build_content)

kustomize_repositories = repository_rule(
    _kustomize_repo_impl,
    doc = _DOC,
    attrs = _ATTRS,
)

def kustomize_register_toolchains(name, **kwargs):
    """macro for kustomize toolchains

    Convenience macro for users which does typical setup.
    - create a repository for each built-in platform like "kustomize_linux_amd64" -
      this repository is lazily fetched when kustomize is needed for that platform.
    - TODO: create a convenience repository for the host platform like "kustomize_host"
    - create a repository exposing toolchains for each platform like "kustomize_platforms"
    - register a toolchain pointing at each platform
    Users can avoid this macro and do these steps themselves, if they want more control.
    Args:
        name: base name for all created repos, like "kustomize1_14"
        **kwargs: passed to each kustomize_repositories call
    """

    for platform in PLATFORMS.keys():
        kustomize_repositories(
            name = name + "_" + platform,
            platform = platform,
            **kwargs
        )
        native.register_toolchains("@%s_toolchains//:%s_toolchain" % (name, platform))

    toolchains_repo(
        name = name + "_toolchains",
        user_repository_name = name,
    )
