Skip to content

Commit

Permalink
feat: add an alias repo to run the CLI
Browse files Browse the repository at this point in the history
I can now 'bazel run @aws' to run the CLI in this repo.

Since users call our repository macro (or extension) with a name, we should create a repo of that name.
  • Loading branch information
alexeagle committed Feb 8, 2024
1 parent 8805bb3 commit adadd29
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/release_prep.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ git archive --format=tar --prefix=${PREFIX}/ ${TAG} | gzip > $ARCHIVE
SHA=$(shasum -a 256 $ARCHIVE | awk '{print $1}')

cat << EOF
## Using Bzlmod with Bazel 6
## Using Bzlmod with Bazel 6 or greater
1. Enable with \`common --enable_bzlmod\` in \`.bazelrc\`.
1. (Bazel 6 only) Enable with \`common --enable_bzlmod\` in \`.bazelrc\`.
2. Add to your \`MODULE.bazel\` file:
\`\`\`starlark
Expand Down
2 changes: 1 addition & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ bazel_dep(name = "container_structure_test", version = "1.16.0", dev_dependency

aws = use_extension("//aws:extensions.bzl", "aws")
aws.toolchain(aws_cli_version = "2.13.0")
use_repo(aws, "aws_darwin", "aws_linux-aarch64", "aws_linux-x86_64", "aws_toolchains")
use_repo(aws, "aws", "aws_darwin", "aws_linux-aarch64", "aws_linux-x86_64", "aws_toolchains")

oci = use_extension(
"@rules_oci//oci:extensions.bzl",
Expand Down
29 changes: 28 additions & 1 deletion aws/repositories.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ def _aws_repo_impl(rctx):
# Generated by aws/repositories.bzl
load("@aspect_rules_aws//aws:toolchain.bzl", "aws_toolchain")
aws_toolchain(name = "aws_toolchain", target_tool_path = "{}")
""".format(target_tool_path)
alias(name = "aws", actual = "//installed:{}/aws", visibility = ["//visibility:public"])
""".format(target_tool_path, version_dir)

# Base BUILD file for this repository
rctx.file("BUILD.bazel", build_content)
Expand Down Expand Up @@ -147,6 +148,31 @@ aws_repositories = repository_rule(
attrs = _ATTRS,
)

def _aws_alias_impl(rctx):
rctx.file("BUILD.bazel", """\
load("@bazel_skylib//rules:native_binary.bzl", "native_binary")
native_binary(
name = "aws",
src = select(
{{
"@bazel_tools//src/conditions:linux_x86_64": "@@{0}_linux-x86_64//:aws",
"@bazel_tools//src/conditions:linux_aarch64": "@@{0}_linux-aarch64//:aws",
"@bazel_tools//src/conditions:darwin_x86_64": "@@{0}_darwin//:aws",
"@bazel_tools//src/conditions:darwin_arm64": "@@{0}_darwin//:aws",
}},
),
out = "aws",
visibility = ["//visibility:public"],
)
""".format(rctx.name))

aws_alias = repository_rule(
_aws_alias_impl,
doc = "Create a repository that provides the AWS CLI aliases for host platforms",
attrs = {},
)

# Wrapper macro around everything above, this is the primary API
def aws_register_toolchains(name, register = True, **kwargs):
"""Convenience macro for users which does typical setup.
Expand All @@ -163,6 +189,7 @@ def aws_register_toolchains(name, register = True, **kwargs):
Should be True for WORKSPACE users, but false when used under bzlmod extension
**kwargs: passed to each node_repositories call
"""
aws_alias(name = name)
for platform in PLATFORMS.keys():
aws_repositories(
name = name + "_" + platform,
Expand Down

0 comments on commit adadd29

Please sign in to comment.