Skip to content

Commit

Permalink
feat: generate sh_binary
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle committed Dec 25, 2024
1 parent b1c7b2f commit c3dee70
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ jobs:
working-directory: "${{ steps.scaffold.outputs.dir }}"
if: "${{ matrix.preset == 'shell' }}"
run: |
>test.sh echo -e '#!/usr/bin/env bash\necho "Hello from Bash"'
chmod u+x test.sh
>>BUILD.bazel echo -e 'load("@rules_shell//shell:sh_binary.bzl", "sh_binary")\nsh_binary(name = "test", srcs = ["test.sh"])'
output="$(bazel run :test)"
>hello.sh echo -e '#!/usr/bin/env bash\necho "Hello from Bash"'
chmod u+x hello.sh
bazel configure || true
output="$(bazel run :hello)"
[[ "${output}" == "Hello from Bash" ]] || { echo >&2 "Wanted output 'Hello from Bash' but got '${output}'" ; exit 1 ; }
- name: Rust smoke test
Expand Down
24 changes: 20 additions & 4 deletions {{ .ProjectSnake }}/.aspect/cli/shell.star
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
"Create sh_library targets for .bash and .sh files"
# TODO(alex): also create sh_test and sh_binary targets
"Create sh_* targets for .bash and .sh files"

def declare_targets(ctx):
if not ctx.sources:
return

is_test = lambda f: f.path.endswith("_test.sh")
is_executable = lambda f: len(f.query_results["shebang"]) > 0
ctx.targets.add(
kind = "sh_library",
name = "scripts",
name = "shell",
attrs = {
"srcs": [s.path for s in ctx.sources],
"srcs": [s.path for s in ctx.sources if not is_test(s) and not is_executable(s)],
},
)

for file in ctx.sources:
if is_executable(file) or is_test(file):
ctx.targets.add(
kind = "sh_test" if is_test(file) else "sh_binary",
name = path.base(file.path).rstrip(path.ext(file.path)),
attrs = {
"srcs": [file.path],
}
)

aspect.register_configure_extension(
id = "rules_shell",
prepare = lambda cfg: aspect.PrepareResult(
sources = [
aspect.SourceExtensions(".bash", ".sh"),
],
queries = {
"shebang": aspect.RegexQuery(
expression = "#!/.*sh",
),
},
),
declare = declare_targets,
)
2 changes: 1 addition & 1 deletion {{ .ProjectSnake }}/.bazeliskrc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
BAZELISK_BASE_URL=https://static.aspect.build/aspect
USE_BAZEL_VERSION=aspect/2024.34.43
USE_BAZEL_VERSION=aspect/2024.51.11
4 changes: 3 additions & 1 deletion {{ .ProjectSnake }}/tools/_run_under_cwd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ case "$(basename "$0")" in
esac

# NB: we don't use 'bazel run' because it may leave behind zombie processes under ibazel
bazel 2>/dev/null build --build_runfile_links "$target" && BAZEL_BINDIR=. exec $(bazel 2>/dev/null info execution_root)/$(bazel 2>/dev/null cquery --output=files "$target") "$@"
# shellcheck disable=SC2046
bazel 2>/dev/null build --build_runfile_links "$target" && \
BAZEL_BINDIR=. exec $(bazel 2>/dev/null info execution_root)/$(bazel 2>/dev/null cquery --output=files "$target") "$@"

0 comments on commit c3dee70

Please sign in to comment.