|
| 1 | +#!/usr/bin/env bash |
| 2 | + |
| 3 | +############################################################################### |
| 4 | +# Execute shellcheck for all shell scripts in this repository |
| 5 | +# |
| 6 | +# Usage: |
| 7 | +# check/shellcheck [--dry-run] [shellcheck-arguments] |
| 8 | +# |
| 9 | +# Use the '--dry-run' option to print out the shellcheck command line without |
| 10 | +# running it. This displays all shell script files found in the repository. |
| 11 | +# |
| 12 | +################################################################################ |
| 13 | + |
| 14 | +# Change working directory to the repository root. |
| 15 | +thisdir="$(dirname "${BASH_SOURCE[0]}")" || exit $? |
| 16 | +topdir="$(git -C "${thisdir}" rev-parse --show-toplevel)" || exit $? |
| 17 | +cd "${topdir}" || exit $? |
| 18 | + |
| 19 | +# Process command line arguments |
| 20 | +opt_dry_run=0 |
| 21 | +typeset -a shellcheck_options |
| 22 | +typeset -a our_shell_scripts |
| 23 | + |
| 24 | +for arg in "$@"; do |
| 25 | + if [[ "${arg}" == "--dry-run" ]]; then |
| 26 | + opt_dry_run=1 |
| 27 | + else |
| 28 | + shellcheck_options+=( "${arg}" ) |
| 29 | + fi |
| 30 | +done |
| 31 | + |
| 32 | +# Find all shell scripts in this repository. |
| 33 | +IFS=$'\n' read -r -d '' -a our_shell_scripts < \ |
| 34 | + <(git ls-files -z -- \ |
| 35 | + ':(exclude)*.'{py,json,json_inward,repr,repr_inward,ipynb,txt,md} \ |
| 36 | + ':(exclude)*.'{yaml,ts,tst,rst,pyi,cfg} | \ |
| 37 | + xargs -0 file | grep -i 'shell script' | cut -d: -f1) |
| 38 | + |
| 39 | +# Verify our_shell_scripts array - require it must contain files below. |
| 40 | +typeset -a required_shell_scripts |
| 41 | +required_shell_scripts=( |
| 42 | + # items below must be sorted |
| 43 | + check/all |
| 44 | + check/doctest |
| 45 | + check/format-incremental |
| 46 | + check/mypy |
| 47 | + check/nbformat |
| 48 | + check/pylint |
| 49 | + check/pytest |
| 50 | + dev_tools/pypath |
| 51 | +) |
| 52 | + |
| 53 | +scripts_not_found="$(comm -13 \ |
| 54 | + <(printf "%s\n" "${our_shell_scripts[@]}") \ |
| 55 | + <(printf "%s\n" "${required_shell_scripts[@]}") )" |
| 56 | + |
| 57 | +if [[ -n "${scripts_not_found}" ]]; then |
| 58 | + echo "Identification of shell scripts failed - files not found:" >&2 |
| 59 | + printf "\n%s\n\n" "${scripts_not_found}" >&2 |
| 60 | + echo "Please fix $0." >&2 |
| 61 | + exit 2 |
| 62 | +fi |
| 63 | + |
| 64 | +# Ready to run here. |
| 65 | +if (( opt_dry_run )); then |
| 66 | + printf '%s ' '>>' 'shellcheck' "${shellcheck_options[@]}" |
| 67 | + printf '\\\n %s ' "${our_shell_scripts[@]}" |
| 68 | + printf '\\\n;\n' |
| 69 | +else |
| 70 | + shellcheck "${shellcheck_options[@]}" "${our_shell_scripts[@]}" |
| 71 | +fi |
0 commit comments