forked from danmar/simplecpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunformat
More file actions
executable file
·176 lines (147 loc) · 5.46 KB
/
runformat
File metadata and controls
executable file
·176 lines (147 loc) · 5.46 KB
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#!/bin/bash
#
# runformat - format this project's C++ sources with Uncrustify.
#
# Usage:
# ./runformat # format using the configured Uncrustify
# ./runformat --install # download, build, and use Uncrustify locally
# ./runformat --install --install-dir /abs/path
# ./runformat --expected-uncrustify-version # print ONLY the expected Uncrustify version
#
# You may also set:
# UNCRUSTIFY=/abs/path/to/uncrustify # use a specific binary
# UNCRUSTIFY_INSTALL_DIR=/abs/path # where `--install` will install
#
# Requirements:
# - All developers must use the *exact* same Uncrustify version to avoid format churn.
# - Either:
# * Have `uncrustify` in PATH, or
# * Set env var UNCRUSTIFY=/absolute/path/to/uncrustify, or
# * Run `./runformat --install` to fetch & build the pinned version locally.
#
# Notes:
# - The local install lives under: ./.runformat-uncrustify/uncrustify-<version>-install
# - The config file is expected at: ./.uncrustify.cfg
#
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"
UNCRUSTIFY_VERSION="0.80.1"
UNCRUSTIFY_HASH="6bf662e05c4140dd4df5e45d6690cad96b4ef23c293b85813f5c725bbf1894d0"
UNCRUSTIFY_WORK_DIR="${SCRIPT_DIR}/.runformat-uncrustify"
# Allow external install dir override (arg or env). If not set, default under work dir.
DEFAULT_INSTALL_DIR="${UNCRUSTIFY_WORK_DIR}/uncrustify-${UNCRUSTIFY_VERSION}-install"
UNCRUSTIFY_INSTALL_DIR="${UNCRUSTIFY_INSTALL_DIR:-$DEFAULT_INSTALL_DIR}"
UNCRUSTIFY_BIN="${UNCRUSTIFY_INSTALL_DIR}/bin/uncrustify"
# Allow override via env; default to local pinned build path.
UNCRUSTIFY="${UNCRUSTIFY:-$UNCRUSTIFY_BIN}"
UNCRUSTIFY_CONFIG="${SCRIPT_DIR}/.uncrustify.cfg"
err() { echo -e >&2 "ERROR: $@\n"; }
die() { err $@; exit 1; }
install_uncrustify() {
local root="uncrustify-${UNCRUSTIFY_VERSION}"
local file="${root}.tar.gz"
local url="https://github.com/uncrustify/uncrustify/releases/download/${root}/${file}"
mkdir -p "${UNCRUSTIFY_WORK_DIR}"
echo "Downloading ${file}..."
curl -fsSL -o "${UNCRUSTIFY_WORK_DIR}/${file}" "${url}"
(
cd "${UNCRUSTIFY_WORK_DIR}"
echo "${UNCRUSTIFY_HASH} ${file}" > "${file}.sha256"
sha256sum -c "${file}.sha256"
rm -f "${file}.sha256"
command -v cmake >/dev/null 2>&1 || die "cmake executable not found."
echo "Extracting archive..."
rm -rf "${root}" "${root}-build"
mkdir -p "${root}"
tar -xzf "${file}" --strip-components=1 -C "${root}"
echo "Configuring (prefix: ${UNCRUSTIFY_INSTALL_DIR})..."
cmake \
-DCMAKE_BUILD_TYPE:STRING=Release \
-DCMAKE_INSTALL_PREFIX:PATH="${UNCRUSTIFY_INSTALL_DIR}" \
-S "${root}" -B "${root}-build"
echo "Building & installing..."
cmake --build "${root}-build" --config Release --target install --parallel
)
echo "Installed Uncrustify to: ${UNCRUSTIFY_INSTALL_DIR}"
}
print_usage_and_exit() {
sed -n '2,25p' "$0" | sed 's/^# \{0,1\}//'
exit 0
}
# Print ONLY expected Uncrustify version (no extra text).
print_expected_uncrustify_version_and_exit() {
printf '%s\n' "$UNCRUSTIFY_VERSION"
exit 0
}
# --------------------------
# Argument parsing
# --------------------------
DO_INSTALL=0
PRINT_EXPECTED_UNCRUSTIFY_VERSION=0
# Accept: --install, --install-dir <dir>, -h/--help
while [[ $# -gt 0 ]]; do
case "$1" in
-h|--help)
print_usage_and_exit
;;
--install)
DO_INSTALL=1
shift
;;
--install-dir)
[[ $# -ge 2 ]] || die "$1 requires a path argument"
UNCRUSTIFY_INSTALL_DIR="$(readlink -m "$2" 2>/dev/null || realpath -m "$2")"
UNCRUSTIFY_BIN="${UNCRUSTIFY_INSTALL_DIR}/bin/uncrustify"
# Only update UNCRUSTIFY default if user hasn't explicitly set it
if [[ "${UNCRUSTIFY:-}" != "${UNCRUSTIFY_BIN}" ]]; then
UNCRUSTIFY="${UNCRUSTIFY_BIN}"
fi
shift 2
;;
--expected-uncrustify-version)
PRINT_EXPECTED_UNCRUSTIFY_VERSION=1
shift
;;
*)
# ignore unrecognized positional args for now
shift
;;
esac
done
if [[ "$DO_INSTALL" -eq 1 ]]; then
install_uncrustify
# Ensure we use the freshly installed binary for this run
UNCRUSTIFY="$UNCRUSTIFY_BIN"
fi
# If requested, print ONLY the expected Uncrustify version and exit.
if [[ "$PRINT_EXPECTED_UNCRUSTIFY_VERSION" -eq 1 ]]; then
print_expected_uncrustify_version_and_exit
fi
# --------------------------
# Validate & run
# --------------------------
# Check Uncrustify availability
if ! command -v "$UNCRUSTIFY" >/dev/null 2>&1; then
err "Uncrustify executable not found: $UNCRUSTIFY"
die "Add it to PATH, set UNCRUSTIFY=/path/to/uncrustify, or run: $0 --install [--install-dir DIR]"
fi
# Version check
DETECTED_VERSION="$("$UNCRUSTIFY" --version 2>&1 | grep -oE '[0-9]+(\.[0-9]+)*' | head -n1 || true)"
echo "Detected Uncrustify: ${DETECTED_VERSION:-unknown}"
if [[ "$DETECTED_VERSION" != "${UNCRUSTIFY_VERSION}" ]]; then
die "Expected Uncrustify ${UNCRUSTIFY_VERSION}. Re-run with --install (and optionally --install-dir) or set UNCRUSTIFY."
fi
# Config check
[[ -f "$UNCRUSTIFY_CONFIG" ]] || die "Uncrustify config not found at: $UNCRUSTIFY_CONFIG"
# Run formatter
echo "Running formatter..."
$UNCRUSTIFY -c "$UNCRUSTIFY_CONFIG" -l CPP --no-backup --replace *.cpp *.h
# Show diff and fail if changes exist
echo "Checking for formatting changes..."
git diff --exit-code || {
echo
echo "Formatting changes were applied. Please review and commit."
exit 1
}
echo "Formatting is clean."