Skip to content

Commit

Permalink
feat: make it easier to resolve jq/yq toolchains from a repository rule
Browse files Browse the repository at this point in the history
  • Loading branch information
kormide committed Apr 28, 2022
1 parent 41ce344 commit 6743c0c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 42 deletions.
14 changes: 14 additions & 0 deletions e2e/run_jq_symlinked_bin.sh
Original file line number Diff line number Diff line change
@@ -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 changes: 13 additions & 0 deletions e2e/run_yq_symlinked_bin.sh
Original file line number Diff line number Diff line change
@@ -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
30 changes: 10 additions & 20 deletions lib/private/jq_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
""",
)
4 changes: 2 additions & 2 deletions lib/private/repo_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
30 changes: 10 additions & 20 deletions lib/private/yq_toolchain.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
""",
)

0 comments on commit 6743c0c

Please sign in to comment.