mirror of
https://github.com/kevinveenbirkenbach/computer-playbook.git
synced 2026-11-09 12:17:13 +00:00
Adds scripts/lint/autoformat.sh that checks tool availability before running ruff, shfmt, and shellcheck autofixes. Updates Makefile with the new autoformat target, documents it in makefile.md, and adds a commit.md rule requiring autoformat to be run before every commit. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
66 lines
2.0 KiB
Bash
Executable File
66 lines
2.0 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
# shellcheck shell=bash
|
|
# Auto-format all source files using available tools.
|
|
# Missing tools are reported and skipped rather than aborting the run.
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
|
|
REPO_ROOT="$(cd -- "${SCRIPT_DIR}/../.." && pwd)"
|
|
|
|
cd "${REPO_ROOT}"
|
|
|
|
skipped=()
|
|
ran=()
|
|
|
|
# ── ruff (Python formatter + auto-fixer) ─────────────────────────────────────
|
|
if command -v ruff &>/dev/null; then
|
|
ruff format .
|
|
ruff check . --fix
|
|
ran+=("ruff")
|
|
else
|
|
skipped+=("ruff")
|
|
fi
|
|
|
|
# ── shfmt (shell script formatter) ───────────────────────────────────────────
|
|
if command -v shfmt &>/dev/null; then
|
|
shfmt -w scripts
|
|
ran+=("shfmt")
|
|
else
|
|
skipped+=("shfmt")
|
|
fi
|
|
|
|
# ── shellcheck (shell script linter with autofix via diff) ───────────────────
|
|
if command -v shellcheck &>/dev/null; then
|
|
mapfile -t shellcheck_files < <(find . -type f -name '*.sh' 2>/dev/null | sort)
|
|
if [[ "${#shellcheck_files[@]}" -gt 0 ]]; then
|
|
diff_file="$(mktemp)"
|
|
cleanup() { rm -f "${diff_file}"; }
|
|
trap cleanup EXIT
|
|
set +e
|
|
shellcheck -f diff -x "${shellcheck_files[@]}" >"${diff_file}" 2>/dev/null
|
|
set -e
|
|
if [[ -s "${diff_file}" ]]; then
|
|
printf 'Applying ShellCheck autofixes.\n'
|
|
git apply --whitespace=nowarn "${diff_file}"
|
|
fi
|
|
fi
|
|
ran+=("shellcheck")
|
|
else
|
|
skipped+=("shellcheck")
|
|
fi
|
|
|
|
# ── summary ──────────────────────────────────────────────────────────────────
|
|
if [[ "${#ran[@]}" -gt 0 ]]; then
|
|
printf 'autoformat: ran %s\n' "$(
|
|
IFS=', '
|
|
echo "${ran[*]}"
|
|
)"
|
|
fi
|
|
if [[ "${#skipped[@]}" -gt 0 ]]; then
|
|
printf 'autoformat: skipped (not installed) %s\n' "$(
|
|
IFS=', '
|
|
echo "${skipped[*]}"
|
|
)"
|
|
fi
|