Skip to content

Commit 4a3fb37

Browse files
authored
format bash files (#12)
Change-Id: I0c52ce9c0fcba9bf02e157125100ae3a9fef5633
1 parent c1bbf3c commit 4a3fb37

10 files changed

+111
-50
lines changed

.github/workflows/check.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- uses: actions/checkout@v4
1919
- shell: bash
2020
run: |
21-
bazel run //tools:buildifier.check
21+
bazel run //tools:format.check
2222
2323
matrix-single-test:
2424
runs-on: ubuntu-latest

WORKSPACE.bazel

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
workspace(name = "rules_clang_tidy")
22

3-
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
3+
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive", "http_file")
44

55
http_archive(
66
name = "bazel_skylib",
@@ -37,3 +37,16 @@ load("//private:local_workspace_directories.bzl", "local_workspace_directories")
3737
local_workspace_directories(
3838
name = "local_workspace_directories",
3939
)
40+
41+
http_file(
42+
name = "shfmt",
43+
executable = True,
44+
integrity = "sha256-2ZsGUGruKskRPa7DBJki5w3Iz/uEZY465RLGpsvhAbY=",
45+
url = "https://github.com/mvdan/sh/releases/download/v3.9.0/shfmt_v3.9.0_linux_amd64",
46+
)
47+
48+
http_archive(
49+
name = "rules_multirun",
50+
sha256 = "0e124567fa85287874eff33a791c3bbdcc5343329a56faa828ef624380d4607c",
51+
url = "https://github.com/keith/rules_multirun/releases/download/0.9.0/rules_multirun.0.9.0.tar.gz",
52+
)

apply-fixes.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ set -euo pipefail
55
apply_bin="$1"
66
output_path="$2"
77

8-
find "$output_path" -type f -name "*.yaml" | \
9-
xargs sed -i -e "s+%workspace%+$BUILD_WORKSPACE_DIRECTORY+g"
8+
find "$output_path" -type f -name "*.yaml" \
9+
| xargs sed -i -e "s+%workspace%+$BUILD_WORKSPACE_DIRECTORY+g"
1010

1111
"$apply_bin" "$output_path"

test/check_extra-options_test.bash

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ check_setup example/misc-unused
1010
unset TEST_TMPDIR
1111

1212
bazel \
13-
--bazelrc="$test_bazelrc" \
14-
build \
15-
--config=clang-tidy \
16-
--@rules_clang_tidy//:extra-options=--enable-check-profile \
17-
--@rules_clang_tidy//:extra-options=--checks='fuchsia-*' \
18-
//... 2>&1 | tee "$log" || true
13+
--bazelrc="$test_bazelrc" \
14+
build \
15+
--config=clang-tidy \
16+
--@rules_clang_tidy//:extra-options=--enable-check-profile \
17+
--@rules_clang_tidy//:extra-options=--checks='fuchsia-*' \
18+
//... 2>&1 | tee "$log" || true
1919

2020
grep "clang-tidy checks profiling" "$log"
2121
grep "error: .*misc-unused-alias-decls" "$log"

test/check_misc-unused_test.bash

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ check_setup example/misc-unused
1010
unset TEST_TMPDIR
1111

1212
bazel \
13-
--bazelrc="$test_bazelrc" \
14-
build \
15-
--config=clang-tidy \
16-
//... | tee "$log" || true
13+
--bazelrc="$test_bazelrc" \
14+
build \
15+
--config=clang-tidy \
16+
//... | tee "$log" || true
1717

1818
grep "error: .*misc-unused-alias-decls" "$log"

test/fix_desired-deps_test.bash

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ cat "$test_bazelrc" >> .bazelrc
1313

1414
# build everything
1515
bazel \
16-
build \
17-
//...
16+
build \
17+
//...
1818

1919
# add a new target
2020
echo 'cc_library(name = "a")' >> BUILD.bazel
2121

2222
# rebuild detects stale deps attr
2323
bazel \
24-
build \
25-
//... 2>&1 | tee log || true
24+
build \
25+
//... 2>&1 | tee log || true
2626

2727
grep "ERROR.* 'deps' does not match 'desired_deps'" log

test/fix_rule_test.bash

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ cat "$test_bazelrc" >> .bazelrc
1313

1414
# build everything
1515
bazel \
16-
build \
17-
//...
16+
build \
17+
//...
1818

1919
# apply fixes target not run
2020
! cmp \
@@ -23,13 +23,13 @@ bazel \
2323

2424
# linting delayed until this binary is explicitly built/run
2525
bazel \
26-
run \
27-
//:apply-fixes | tee "$log"
26+
run \
27+
//:apply-fixes | tee "$log"
2828

2929
grep "warning: .*misc-unused-alias-decls" "$log"
3030

3131
diff \
32-
--color=always \
33-
--report-identical-files \
34-
misc-unused-alias-decls.cpp \
35-
misc-unused-alias-decls.cpp.fixed
32+
--color=always \
33+
--report-identical-files \
34+
misc-unused-alias-decls.cpp \
35+
misc-unused-alias-decls.cpp.fixed

test/fix_script_test.bash

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ unset TEST_TMPDIR
1111

1212
# generate fixes in output base
1313
bazel \
14-
--bazelrc="$test_bazelrc" \
15-
build \
16-
--config=clang-tidy-export-fixes \
17-
//...
14+
--bazelrc="$test_bazelrc" \
15+
build \
16+
--config=clang-tidy-export-fixes \
17+
//...
1818

1919
# apply fixes
2020
bazel \
21-
--bazelrc="$test_bazelrc" \
22-
run @rules_clang_tidy//:apply-fixes -- \
23-
$(bazel --bazelrc="$test_bazelrc" info output_base)
21+
--bazelrc="$test_bazelrc" \
22+
run @rules_clang_tidy//:apply-fixes -- \
23+
$(bazel --bazelrc="$test_bazelrc" info output_base)
2424

2525
diff \
26-
--color=always \
27-
--report-identical-files \
28-
misc-unused-alias-decls.cpp \
29-
misc-unused-alias-decls.cpp.fixed
26+
--color=always \
27+
--report-identical-files \
28+
misc-unused-alias-decls.cpp \
29+
misc-unused-alias-decls.cpp.fixed

test/prelude.template.bash

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ function symlink_externals
1111
| xargs -I {} basename {} \
1212
| grep -v -e "^local_" \
1313
| xargs -I {} ln -s \
14-
"$BAZEL_EXTERNAL_DIRECTORY/{}" \
15-
"$output_base/external/{}"
14+
"$BAZEL_EXTERNAL_DIRECTORY/{}" \
15+
"$output_base/external/{}"
1616
}
1717

1818
function output_base
@@ -22,11 +22,11 @@ function output_base
2222

2323
function common_setup
2424
{
25-
log=$(mktemp)
26-
test_bazelrc=$(mktemp)
27-
output_base="$TEST_TMPDIR"
25+
log=$(mktemp)
26+
test_bazelrc=$(mktemp)
27+
output_base="$TEST_TMPDIR"
2828

29-
cat > $test_bazelrc <<EOF
29+
cat > $test_bazelrc << EOF
3030
startup --noblock_for_lock
3131
startup --max_idle_secs=1
3232
startup --output_base=$output_base
@@ -38,23 +38,23 @@ build --show_timestamps
3838
build --experimental_convenience_symlinks=ignore
3939
EOF
4040

41-
symlink_externals "$output_base"
41+
symlink_externals "$output_base"
4242
}
4343

4444
function check_setup
4545
{
46-
common_setup
46+
common_setup
4747

48-
cd "$BUILD_WORKSPACE_DIRECTORY/$1"
48+
cd "$BUILD_WORKSPACE_DIRECTORY/$1"
4949
}
5050

5151
function fix_setup
5252
{
53-
common_setup
53+
common_setup
5454

55-
cp -r "$BUILD_WORKSPACE_DIRECTORY/$1" temp
55+
cp -r "$BUILD_WORKSPACE_DIRECTORY/$1" temp
5656

57-
echo "common --override_repository=rules_clang_tidy=$BUILD_WORKSPACE_DIRECTORY" >> "$test_bazelrc"
57+
echo "common --override_repository=rules_clang_tidy=$BUILD_WORKSPACE_DIRECTORY" >> "$test_bazelrc"
5858

59-
cd temp
59+
cd temp
6060
}

tools/BUILD.bazel

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
load("@buildifier_prebuilt//:rules.bzl", "buildifier")
2+
load("@local_workspace_directories//:defs.bzl", "BUILD_WORKSPACE_DIRECTORY")
3+
load("@rules_multirun//:defs.bzl", "multirun")
24

35
buildifier(
46
name = "buildifier.check",
@@ -11,3 +13,49 @@ buildifier(
1113
lint_mode = "warn",
1214
mode = "fix",
1315
)
16+
17+
_shfmt_opts = [
18+
"--simplify",
19+
"--indent",
20+
"2",
21+
"--binary-next-line",
22+
"--case-indent",
23+
"--space-redirects",
24+
"--func-next-line",
25+
BUILD_WORKSPACE_DIRECTORY,
26+
]
27+
28+
sh_binary(
29+
name = "shfmt.check",
30+
srcs = ["@shfmt//file"],
31+
args = [
32+
"--diff",
33+
] + _shfmt_opts,
34+
)
35+
36+
sh_binary(
37+
name = "shfmt.fix",
38+
srcs = ["@shfmt//file"],
39+
args = [
40+
"--list",
41+
"--write",
42+
] + _shfmt_opts,
43+
)
44+
45+
multirun(
46+
name = "format.check",
47+
commands = [
48+
":buildifier.check",
49+
":shfmt.check",
50+
],
51+
jobs = 0,
52+
)
53+
54+
multirun(
55+
name = "format.fix",
56+
commands = [
57+
":buildifier.fix",
58+
":shfmt.fix",
59+
],
60+
jobs = 0,
61+
)

0 commit comments

Comments
 (0)