feat: make it easier to resolve jq/yq toolchains from a repository rule

This commit is contained in:
Derek Cormier 2022-04-27 17:32:53 -07:00 committed by Derek Cormier
parent 41ce34470f
commit 6743c0cbcf
5 changed files with 51 additions and 44 deletions

14
e2e/run_jq_symlinked_bin.sh Executable file
View File

@ -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

13
e2e/run_yq_symlinked_bin.sh Executable file
View File

@ -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

View File

@ -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,
ext = ext,
))
repository_ctx.symlink("../{name}_{platform}/jq{ext}".format(
name = repository_ctx.attr.name,
platform = repo_utils.platform(repository_ctx),
))
# 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 "",
))
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
""",
)

View File

@ -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:

View File

@ -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,
ext = ext,
))
repository_ctx.symlink("../{name}_{platform}/yq{ext}".format(
name = repository_ctx.attr.name,
platform = repo_utils.platform(repository_ctx),
))
# 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 "",
))
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
""",
)