summaryrefslogtreecommitdiff
path: root/scripts/formatthecode.sh
blob: 6263d179d3b1a58b2fd9663a19a34dccd6ee8312 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#!/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.
Use `--` before filenames that begin with `-`.

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
}

sanitize_path_for_astyle() {
    local path="$1"

    if [[ "${path}" == -* ]]; then
        printf './%s\n' "${path}"
        return
    fi

    printf '%s\n' "${path}"
}

main() {
    local -a file_args=()
    local -a astyle_args=()

    while [[ $# -gt 0 ]]; do
        case "$1" in
            -h|--help)
                usage
                return 0
                ;;
            --check-deps)
                check_deps
                return 0
                ;;
            --)
                shift
                file_args=("$@")
                break
                ;;
            -*)
                echo "error: unknown option: $1" >&2
                echo "Use -- before filenames that begin with '-'." >&2
                return 1
                ;;
            *)
                file_args+=("$1")
                ;;
        esac

        shift
    done

    check_deps

    if [[ ${#file_args[@]} -gt 0 ]]; then
        local target
        for target in "${file_args[@]}"; do
            if [[ ! -f "${target}" ]]; then
                echo "error: file not found: ${target}" >&2
                return 1
            fi
        done

        for target in "${file_args[@]}"; do
            astyle_args+=("$(sanitize_path_for_astyle "${target}")")
        done

        astyle --style=java --mode=java -n "${astyle_args[@]}"
        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 "$@"