diff options
| author | Paul Buetow <paul@buetow.org> | 2026-03-27 06:48:06 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-03-27 06:48:06 +0200 |
| commit | 0944cf33791a3ffa0ef34fe3eeb94093b68314bc (patch) | |
| tree | 056a04beda43ce08c6cd737956d0e7b52df1e37b /scripts/formatthecode.sh | |
| parent | 508c2c50325e8436e6700445bf8e811e4228c4b7 (diff) | |
Restore formatter workflow script
Diffstat (limited to 'scripts/formatthecode.sh')
| -rwxr-xr-x | scripts/formatthecode.sh | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/scripts/formatthecode.sh b/scripts/formatthecode.sh new file mode 100755 index 0000000..a146f33 --- /dev/null +++ b/scripts/formatthecode.sh @@ -0,0 +1,78 @@ +#!/usr/bin/env bash + +set -euo pipefail + +SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd) +REPO_ROOT=$(cd "${SCRIPT_DIR}/.." && pwd) + +usage() { + cat <<'EOF' +Usage: ./scripts/formatthecode.sh [JAVA_FILE ...] + +Formats Java sources with Artistic Style using the project's historical style. + +Without arguments, formats all Java files under: + - src/main/java + - src/test/java + +With arguments, formats only the provided files. + +Options: + -h, --help Show this help text + --check-deps Verify required formatter dependencies are installed +EOF +} + +check_deps() { + if ! command -v astyle >/dev/null 2>&1; then + echo "error: required formatter 'astyle' was not found in PATH" >&2 + echo "Install Artistic Style to use ./scripts/formatthecode.sh" >&2 + return 1 + fi +} + +collect_default_targets() { + find "${REPO_ROOT}/src/main/java" "${REPO_ROOT}/src/test/java" \ + -type f -name '*.java' -print0 +} + +main() { + if [[ $# -gt 0 ]]; then + case "$1" in + -h|--help) + usage + return 0 + ;; + --check-deps) + check_deps + return 0 + ;; + esac + fi + + check_deps + + if [[ $# -gt 0 ]]; then + local target + for target in "$@"; do + if [[ ! -f "${target}" ]]; then + echo "error: file not found: ${target}" >&2 + return 1 + fi + done + + astyle --style=java --mode=java -n "$@" + return 0 + fi + + mapfile -d '' targets < <(collect_default_targets) + + if [[ ${#targets[@]} -eq 0 ]]; then + echo "No Java files found to format." >&2 + return 0 + fi + + astyle --style=java --mode=java -n "${targets[@]}" +} + +main "$@" |
