Skip to content

Commit

Permalink
feat: add rules_shell if requested (#286)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeagle authored Dec 30, 2024
1 parent 4906299 commit 5ec9875
Show file tree
Hide file tree
Showing 9 changed files with 89 additions and 8 deletions.
11 changes: 11 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ jobs:
- v0.3.0
preset:
- kitchen-sink
- shell
- java
- js
- go
Expand Down Expand Up @@ -107,6 +108,16 @@ jobs:
if: "${{ matrix.preset == 'js' }}"
run: ./tools/pnpm list

- name: Shell smoke test
working-directory: "${{ steps.scaffold.outputs.dir }}"
if: "${{ matrix.preset == 'shell' }}"
run: |
>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
working-directory: "${{ steps.scaffold.outputs.dir }}"
if: "${{ matrix.preset == 'rust' }}"
Expand Down
18 changes: 16 additions & 2 deletions scaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ questions:
- Java
- C & C++
- Rust
- Shell (Bash)
- name: copier
prompt:
confirm: Setup code generation?
Expand All @@ -59,12 +60,14 @@ features:
globs:
- "*/tools/format/*"
- "*/tools/lint/*"
- "*/.shellcheckrc"
- "*/.pre-commit-config.yaml"
- value: "{{ .Scaffold.stamp }}"
globs:
- "*/tools/workspace_status.sh"
- "**/weekly_tag.yaml"
- value: "{{ .Computed.shell }}"
globs:
- "*/.shellcheckrc"
- value: "{{ .Computed.javascript }}"
globs:
- "*/REPO.bazel"
Expand Down Expand Up @@ -117,14 +120,25 @@ computed:
java: "{{ has \"Java\" .Scaffold.langs }}"
cpp: "{{ has \"C & C++\" .Scaffold.langs }}"
rust: "{{ has \"Rust\" .Scaffold.langs }}"
shell: "{{ has \"Shell (Bash)\" .Scaffold.langs }}"

presets:
kitchen-sink:
lint: true
langs: ['C & C++', 'JavaScript & TypeScript', 'Python', 'Go', 'Java', 'Rust']
langs:
- 'C & C++'
- 'JavaScript & TypeScript'
- 'Python'
- 'Go'
- 'Java'
- 'Rust'
- 'Shell (Bash)'
copier: true
stamp: true
oci: true
shell:
langs: ['Shell (Bash)']
lint: true
go:
langs: ['Go']
lint: true
Expand Down
5 changes: 5 additions & 0 deletions {{ .ProjectSnake }}/.aspect/cli/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ configure:
python: true
{{- end }}
plugins:
{{- if .Computed.shell }}
- .aspect/cli/shell.star
{{- end }}
{{- if .Computed.javascript }}
- .aspect/cli/package-json-scripts.star
{{- end }}
Expand All @@ -27,7 +30,9 @@ configure:
{{- if .Scaffold.lint }}
lint:
aspects:
{{- if .Computed.shell }}
- //tools/lint:linters.bzl%shellcheck
{{- end }}
{{- if .Computed.javascript }}
- //tools/lint:linters.bzl%eslint
{{- end }}
Expand Down
45 changes: 45 additions & 0 deletions {{ .ProjectSnake }}/.aspect/cli/shell.star
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
"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

library_srcs = []
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],
}
)
else:
library_srcs.push(file)

if len(library_srcs):
ctx.targets.add(
kind = "sh_library",
name = "shell_lib",
attrs = {
"srcs": library_srcs,
},
)

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
7 changes: 3 additions & 4 deletions {{ .ProjectSnake }}/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ alias(
name = "format",
actual = "//tools/format",
)

{{- end }}
{{- if .Computed.shell }}
exports_files(
[
".shellcheckrc",
],
[".shellcheckrc"],
visibility = ["//:__subpackages__"],
)
{{- end }}
Expand Down
3 changes: 3 additions & 0 deletions {{ .ProjectSnake }}/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ bazel_dep(name = "gazelle", version = "0.40.0")
{{- if .Computed.cpp }}
bazel_dep(name = "toolchains_llvm", version = "1.2.0")
{{- end }}
{{- if .Computed.shell }}
bazel_dep(name = "rules_shell", version = "0.3.0")
{{- end }}
{{- if .Scaffold.oci }}
bazel_dep(name = "rules_oci", version = "2.2.0")
{{- end }}
Expand Down
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") "$@"
2 changes: 2 additions & 0 deletions {{ .ProjectSnake }}/tools/lint/linters.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,11 @@ ruff = lint_ruff_aspect(
ruff_test = lint_test(aspect = ruff)

{{ end -}}
{{ if .Computed.shell }}
shellcheck = lint_shellcheck_aspect(
binary = "@multitool//tools/shellcheck",
config = "@@//:.shellcheckrc",
)

shellcheck_test = lint_test(aspect = shellcheck)
{{ end -}}

0 comments on commit 5ec9875

Please sign in to comment.