Skip to content

Commit 4865408

Browse files
committed
chore(runformat): Add local Uncrustify installation support
This change introduces the ability to install a specific version of Uncrustify locally via the `./runformat --install` command. This ensures all developers use the exact same version to avoid format churn. - Adds a new `.runformat-uncrustify` directory to `.gitignore`. - Updates `runformat` script with install logic and usage instructions. - Verifies Uncrustify version and guides user if mismatch is detected.
1 parent 455f010 commit 4865408

2 files changed

Lines changed: 69 additions & 4 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
*.app
2929
simplecpp
3030
testrunner
31+
/.runformat-uncrustify
3132

3233
# CLion
3334
/.idea

runformat

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,101 @@
44
#
55
# Usage:
66
# ./runformat # format using the configured Uncrustify
7+
# ./runformat --install # download, build, and use Uncrustify locally
78
#
89
# Requirements:
910
# - All developers must use the *exact* same Uncrustify version to avoid format churn.
1011
# - Either:
1112
# * Have `uncrustify` in PATH, or
12-
# * Set env var UNCRUSTIFY=/absolute/path/to/uncrustify
13+
# * Set env var UNCRUSTIFY=/absolute/path/to/uncrustify, or
14+
# * Run `./runformat --install` to fetch & build the pinned version locally.
1315
#
1416
# Notes:
17+
# - The local install lives under: ./.runformat-uncrustify/uncrustify-<version>-install
1518
# - The config file is expected at: ./.uncrustify.cfg
1619
#
20+
21+
set -euo pipefail
22+
1723
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
1824
cd "$SCRIPT_DIR"
1925

2026
UNCRUSTIFY_VERSION="0.80.1"
21-
UNCRUSTIFY="${UNCRUSTIFY-uncrustify}"
27+
UNCRUSTIFY_HASH="6bf662e05c4140dd4df5e45d6690cad96b4ef23c293b85813f5c725bbf1894d0"
28+
29+
UNCRUSTIFY_LOCAL_DIR="${SCRIPT_DIR}/.runformat-uncrustify"
30+
UNCRUSTIFY_LOCAL_INSTALL_DIR="${UNCRUSTIFY_LOCAL_DIR}/uncrustify-${UNCRUSTIFY_VERSION}-install"
31+
UNCRUSTIFY_LOCAL_INSTALL_EXECUTABLE="${UNCRUSTIFY_LOCAL_INSTALL_DIR}/bin/uncrustify"
32+
33+
# Allow override via env; default to local pinned build path.
34+
UNCRUSTIFY="${UNCRUSTIFY:-$UNCRUSTIFY_LOCAL_INSTALL_EXECUTABLE}"
2235
UNCRUSTIFY_CONFIG="${SCRIPT_DIR}/.uncrustify.cfg"
2336

2437
err() { echo -e >&2 "ERROR: $@\n"; }
2538
die() { err $@; exit 1; }
2639

40+
install_uncrustify() {
41+
local root="uncrustify-${UNCRUSTIFY_VERSION}"
42+
local file="${root}.tar.gz"
43+
local url="https://github.com/uncrustify/uncrustify/releases/download/${root}/${file}"
44+
45+
mkdir -p "${UNCRUSTIFY_LOCAL_DIR}"
46+
47+
echo "Downloading ${file}..."
48+
curl -fsSL -o "${UNCRUSTIFY_LOCAL_DIR}/${file}" "${url}"
49+
50+
( cd "${UNCRUSTIFY_LOCAL_DIR}" && {
51+
echo "${UNCRUSTIFY_HASH} ${file}" > "${file}.sha256"
52+
sha256sum -c "${file}.sha256"
53+
rm -f "${file}.sha256"
54+
55+
command -v cmake >/dev/null 2>&1 || die "cmake executable not found."
56+
57+
echo "Extracting archive..."
58+
rm -rf "${root}" "${root}-build" "${root}-install"
59+
mkdir -p "${root}"
60+
tar -xzf "${file}" --strip-components=1 -C "${root}"
61+
62+
echo "Configuring..."
63+
cmake \
64+
-DCMAKE_BUILD_TYPE:STRING=Release \
65+
-DCMAKE_INSTALL_PREFIX:PATH="${UNCRUSTIFY_LOCAL_DIR}/${root}-install" \
66+
-S "${root}" -B "${root}-build"
67+
68+
echo "Building & installing..."
69+
cmake --build "${root}-build" --config Release --target install --parallel
70+
}
71+
)
72+
echo "Installed Uncrustify to: ${UNCRUSTIFY_LOCAL_INSTALL_DIR}"
73+
}
74+
75+
print_usage_and_exit() {
76+
sed -n '2,19p' "$0" | sed 's/^# \{0,1\}//'
77+
exit 0
78+
}
79+
80+
# Argument handling
81+
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
82+
print_usage_and_exit
83+
fi
84+
85+
if [[ "${1:-}" == "--install" ]]; then
86+
install_uncrustify
87+
# Ensure we use the freshly installed binary for this run
88+
UNCRUSTIFY="$UNCRUSTIFY_LOCAL_INSTALL_EXECUTABLE"
89+
fi
90+
2791
# Check Uncrustify availability
2892
if ! command -v "$UNCRUSTIFY" >/dev/null 2>&1; then
2993
err "Uncrustify executable not found: $UNCRUSTIFY"
30-
die "Add it to PATH or set UNCRUSTIFY=/path/to/uncrustify"
94+
die "Add it to PATH, set UNCRUSTIFY=/path/to/uncrustify, or run: $0 --install"
3195
fi
3296

3397
# Version check
3498
DETECTED_VERSION="$("$UNCRUSTIFY" --version 2>&1 | grep -oE '[0-9]+(\.[0-9]+)*' | head -n1 || true)"
3599
echo "Detected Uncrustify: ${DETECTED_VERSION:-unknown}"
36100
if [[ "$DETECTED_VERSION" != "${UNCRUSTIFY_VERSION}" ]]; then
37-
die "Expected Uncrustify ${UNCRUSTIFY_VERSION}."
101+
die "Expected Uncrustify ${UNCRUSTIFY_VERSION}. Re-run with --install or adjust UNCRUSTIFY."
38102
fi
39103

40104
# Config check

0 commit comments

Comments
 (0)