From 6743c0cbcfb42a377d40359ea409d9cc70f70fdf Mon Sep 17 00:00:00 2001 From: Derek Cormier Date: Wed, 27 Apr 2022 17:32:53 -0700 Subject: [PATCH] feat: make it easier to resolve jq/yq toolchains from a repository rule --- e2e/run_jq_symlinked_bin.sh | 14 ++++++++++++++ e2e/run_yq_symlinked_bin.sh | 13 +++++++++++++ lib/private/jq_toolchain.bzl | 30 ++++++++++-------------------- lib/private/repo_utils.bzl | 4 ++-- lib/private/yq_toolchain.bzl | 30 ++++++++++-------------------- 5 files changed, 49 insertions(+), 42 deletions(-) create mode 100755 e2e/run_jq_symlinked_bin.sh create mode 100755 e2e/run_yq_symlinked_bin.sh diff --git a/e2e/run_jq_symlinked_bin.sh b/e2e/run_jq_symlinked_bin.sh new file mode 100755 index 000000000..bac62532f --- /dev/null +++ b/e2e/run_jq_symlinked_bin.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +set -e + +case "$(uname -s)" in + CYGWIN*|MINGW32*|MSYS*|MINGW*) + bazel run @jq//:jq.exe -- --null-input .a=5 + ;; + + *) + bazel run @jq//:jq -- --null-input .a=5 + ;; +esac + diff --git a/e2e/run_yq_symlinked_bin.sh b/e2e/run_yq_symlinked_bin.sh new file mode 100755 index 000000000..9b32c60e4 --- /dev/null +++ b/e2e/run_yq_symlinked_bin.sh @@ -0,0 +1,13 @@ +#!/bin/bash + +set -e + +case "$(uname -s)" in + CYGWIN*|MINGW32*|MSYS*|MINGW*) + bazel run @yq//:yq.exe -- --null-input .a=5 + ;; + + *) + bazel run @yq//:yq -- --null-input .a=5 + ;; +esac \ No newline at end of file diff --git a/lib/private/jq_toolchain.bzl b/lib/private/jq_toolchain.bzl index c1212cac6..c2db986a9 100644 --- a/lib/private/jq_toolchain.bzl +++ b/lib/private/jq_toolchain.bzl @@ -190,35 +190,25 @@ jq_platform_repo = repository_rule( ) def _jq_host_alias_repo(repository_ctx): + ext = ".exe" if repo_utils.is_windows(repository_ctx) else "" + # Base BUILD file for this repository repository_ctx.file("BUILD.bazel", """# Generated by @aspect_bazel_lib//lib/private:jq_toolchain.bzl package(default_visibility = ["//visibility:public"]) -alias(name = "jq", actual = "@{name}_{platform}//:jq") -exports_files(["index.bzl"]) +exports_files(["jq{ext}"]) """.format( - name = repository_ctx.name, - platform = repo_utils.platform(repository_ctx), + ext = ext, )) - # index.bzl file for this repository - repository_ctx.file("index.bzl", content = """# Generated by @aspect_bazel_lib//lib/private:jq_toolchain.bzl -host_platform="{host_platform}" - -def bin(): - "Returns the label of the jq binary for the host platform" - return Label("@{name}_{host_platform}//:jq{maybe_windows}") -""".format( - name = repository_ctx.name, - host_platform = repo_utils.platform(repository_ctx), - maybe_windows = ".exe" if repo_utils.is_windows(repository_ctx) else "", - )) + repository_ctx.symlink("../{name}_{platform}/jq{ext}".format( + name = repository_ctx.attr.name, + platform = repo_utils.platform(repository_ctx), + ext = ext, + ), "jq{ext}".format(ext = ext)) jq_host_alias_repo = repository_rule( _jq_host_alias_repo, doc = """Creates a repository with a shorter name meant for the host platform, which contains - - - A BUILD.bazel file declaring aliases to the host platform's binaries - - index.bzl containing a `host_platform` constant and a `bin()` function that returns the label - to the jq binary for the host platform + a BUILD.bazel file that exports symlinks to the host platform's binaries """, ) diff --git a/lib/private/repo_utils.bzl b/lib/private/repo_utils.bzl index 7c2c38af0..58c10b1f9 100644 --- a/lib/private/repo_utils.bzl +++ b/lib/private/repo_utils.bzl @@ -77,8 +77,8 @@ def _platform(rctx): # Once we drop support for anything older than Bazel 5.1.1 than we can simplify # this function. if os == "windows": - proc_arch = (_get_env_var(rctx, "PROCESSOR_ARCHITECTURE", "", False) or - _get_env_var(rctx, "PROCESSOR_ARCHITEW6432", "", False)) + proc_arch = (_get_env_var(rctx, "PROCESSOR_ARCHITECTURE", "") or + _get_env_var(rctx, "PROCESSOR_ARCHITEW6432", "")) if proc_arch == "ARM64": arch = "arm64" else: diff --git a/lib/private/yq_toolchain.bzl b/lib/private/yq_toolchain.bzl index 56d6b6b0c..702acb306 100644 --- a/lib/private/yq_toolchain.bzl +++ b/lib/private/yq_toolchain.bzl @@ -224,35 +224,25 @@ yq_platform_repo = repository_rule( ) def _yq_host_alias_repo(repository_ctx): + ext = ".exe" if repo_utils.is_windows(repository_ctx) else "" + # Base BUILD file for this repository repository_ctx.file("BUILD.bazel", """# Generated by @aspect_bazel_lib//lib/private:yq_toolchain.bzl package(default_visibility = ["//visibility:public"]) -alias(name = "yq", actual = "@{name}_{platform}//:yq") -exports_files(["index.bzl"]) +exports_files(["yq{ext}"]) """.format( - name = repository_ctx.name, - platform = repo_utils.platform(repository_ctx), + ext = ext, )) - # index.bzl file for this repository - repository_ctx.file("index.bzl", content = """# Generated by @aspect_bazel_lib//lib/private:yq_toolchain.bzl -host_platform="{host_platform}" - -def bin(): - "Returns the label of the yq binary for the host platform" - return Label("@{name}_{host_platform}//:yq{maybe_windows}") -""".format( - name = repository_ctx.name, - host_platform = repo_utils.platform(repository_ctx), - maybe_windows = ".exe" if repo_utils.is_windows(repository_ctx) else "", - )) + repository_ctx.symlink("../{name}_{platform}/yq{ext}".format( + name = repository_ctx.attr.name, + platform = repo_utils.platform(repository_ctx), + ext = ext, + ), "yq{ext}".format(ext = ext)) yq_host_alias_repo = repository_rule( _yq_host_alias_repo, doc = """Creates a repository with a shorter name meant for the host platform, which contains - - - A BUILD.bazel file declaring aliases to the host platform's binaries - - index.bzl containing a `host_platform` constant and a `bin()` function that returns the label - to the yq binary for the host platform + a BUILD.bazel file that exports symlinks to the host platform's binaries """, )