Skip to content

Commit efb4202

Browse files
authored
Add check/shellcheck (#1144)
This adds `check/shellcheck` for checking shell scripts. It's adapted from Cirq's check/shellcheck.
1 parent f1537d2 commit efb4202

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

check/shellcheck

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2025 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# https://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Summary: execute shellcheck for all shell scripts in this repository
18+
#
19+
# Usage:
20+
# check/shellcheck [--dry-run] [shellcheck-arguments]
21+
#
22+
# Use the '--dry-run' option to print out the shellcheck command line without
23+
# running it. This displays all shell script files found in the repository.
24+
25+
set -uo pipefail
26+
27+
# Change working directory to the repository root.
28+
thisdir=$(dirname "${BASH_SOURCE[0]:?}")
29+
repo_dir=$(git -C "${thisdir}" rev-parse --show-toplevel)
30+
cd "${repo_dir}" || exit $?
31+
32+
# Process command line arguments
33+
opt_dry_run=0
34+
declare -a shellcheck_options
35+
declare -a our_shell_scripts
36+
37+
for arg in "$@"; do
38+
if [[ "${arg}" == "--dry-run" ]]; then
39+
opt_dry_run=1
40+
else
41+
shellcheck_options+=( "${arg}" )
42+
fi
43+
done
44+
45+
# Find all shell scripts in this repository.
46+
IFS=$'\n' read -r -d '' -a our_shell_scripts <<< "$(
47+
git ls-files -z -- \
48+
':(exclude)*.'{hdf5,ipynb,json,md,py,rst,toml,ts,txt,yaml} | \
49+
xargs -0 file | grep -i 'shell script' | cut -d: -f1
50+
)"
51+
52+
# Verify our_shell_scripts array - require it must contain files below.
53+
declare -a required_shell_scripts
54+
required_shell_scripts=(
55+
# items below must be sorted
56+
check/format-incremental
57+
check/mypy
58+
check/pylint
59+
check/pytest
60+
)
61+
62+
scripts_not_found=$(comm -13 \
63+
<(printf "%s\n" "${our_shell_scripts[@]}") \
64+
<(printf "%s\n" "${required_shell_scripts[@]}") )
65+
66+
if [[ -n "${scripts_not_found}" ]]; then
67+
echo "Identification of shell scripts failed - files not found:" >&2
68+
printf "\n%s\n\n" "${scripts_not_found}" >&2
69+
echo "Please fix $0." >&2
70+
exit 2
71+
fi
72+
73+
# Ready to run here.
74+
if (( opt_dry_run )); then
75+
printf '%s ' '>>' 'shellcheck' "${shellcheck_options[@]}"
76+
printf '\\\n %s ' "${our_shell_scripts[@]}"
77+
printf '\\\n;\n'
78+
else
79+
shellcheck "${shellcheck_options[@]}" "${our_shell_scripts[@]}"
80+
fi

0 commit comments

Comments
 (0)