Skip to content

Commit 1f344cf

Browse files
committed
chore(runformat): Enhance script with user-defined install directory
This improves the runformat script by adding support for specifying a custom installation directory for Uncrustify using the --install-dir argument. It also refines the argument parsing logic and updates usage documentation to reflect these changes.
1 parent 4865408 commit 1f344cf

1 file changed

Lines changed: 76 additions & 36 deletions

File tree

runformat

Lines changed: 76 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,13 @@
33
# runformat - format this project's C++ sources with Uncrustify.
44
#
55
# Usage:
6-
# ./runformat # format using the configured Uncrustify
7-
# ./runformat --install # download, build, and use Uncrustify locally
6+
# ./runformat # format using the configured Uncrustify
7+
# ./runformat --install # download, build, and use Uncrustify locally
8+
# ./runformat --install --install-dir /abs/path
9+
#
10+
# You may also set:
11+
# UNCRUSTIFY=/abs/path/to/uncrustify # use a specific binary
12+
# UNCRUSTIFY_INSTALL_DIR=/abs/path # where `--install` will install
813
#
914
# Requirements:
1015
# - All developers must use the *exact* same Uncrustify version to avoid format churn.
@@ -26,12 +31,15 @@ cd "$SCRIPT_DIR"
2631
UNCRUSTIFY_VERSION="0.80.1"
2732
UNCRUSTIFY_HASH="6bf662e05c4140dd4df5e45d6690cad96b4ef23c293b85813f5c725bbf1894d0"
2833

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"
34+
UNCRUSTIFY_WORK_DIR="${SCRIPT_DIR}/.runformat-uncrustify"
35+
36+
# Allow external install dir override (arg or env). If not set, default under work dir.
37+
DEFAULT_INSTALL_DIR="${UNCRUSTIFY_WORK_DIR}/uncrustify-${UNCRUSTIFY_VERSION}-install"
38+
UNCRUSTIFY_INSTALL_DIR="${UNCRUSTIFY_INSTALL_DIR:-$DEFAULT_INSTALL_DIR}"
39+
UNCRUSTIFY_BIN="${UNCRUSTIFY_INSTALL_DIR}/bin/uncrustify"
3240

3341
# Allow override via env; default to local pinned build path.
34-
UNCRUSTIFY="${UNCRUSTIFY:-$UNCRUSTIFY_LOCAL_INSTALL_EXECUTABLE}"
42+
UNCRUSTIFY="${UNCRUSTIFY:-$UNCRUSTIFY_BIN}"
3543
UNCRUSTIFY_CONFIG="${SCRIPT_DIR}/.uncrustify.cfg"
3644

3745
err() { echo -e >&2 "ERROR: $@\n"; }
@@ -42,63 +50,95 @@ install_uncrustify() {
4250
local file="${root}.tar.gz"
4351
local url="https://github.com/uncrustify/uncrustify/releases/download/${root}/${file}"
4452

45-
mkdir -p "${UNCRUSTIFY_LOCAL_DIR}"
53+
mkdir -p "${UNCRUSTIFY_WORK_DIR}"
4654

4755
echo "Downloading ${file}..."
48-
curl -fsSL -o "${UNCRUSTIFY_LOCAL_DIR}/${file}" "${url}"
56+
curl -fsSL -o "${UNCRUSTIFY_WORK_DIR}/${file}" "${url}"
4957

50-
( cd "${UNCRUSTIFY_LOCAL_DIR}" && {
51-
echo "${UNCRUSTIFY_HASH} ${file}" > "${file}.sha256"
52-
sha256sum -c "${file}.sha256"
53-
rm -f "${file}.sha256"
58+
(
59+
cd "${UNCRUSTIFY_WORK_DIR}"
5460

55-
command -v cmake >/dev/null 2>&1 || die "cmake executable not found."
61+
echo "${UNCRUSTIFY_HASH} ${file}" > "${file}.sha256"
62+
sha256sum -c "${file}.sha256"
63+
rm -f "${file}.sha256"
5664

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}"
65+
command -v cmake >/dev/null 2>&1 || die "cmake executable not found."
6166

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+
echo "Extracting archive..."
68+
rm -rf "${root}" "${root}-build"
69+
mkdir -p "${root}"
70+
tar -xzf "${file}" --strip-components=1 -C "${root}"
6771

68-
echo "Building & installing..."
69-
cmake --build "${root}-build" --config Release --target install --parallel
70-
}
72+
echo "Configuring (prefix: ${UNCRUSTIFY_INSTALL_DIR})..."
73+
cmake \
74+
-DCMAKE_BUILD_TYPE:STRING=Release \
75+
-DCMAKE_INSTALL_PREFIX:PATH="${UNCRUSTIFY_INSTALL_DIR}" \
76+
-S "${root}" -B "${root}-build"
77+
78+
echo "Building & installing..."
79+
cmake --build "${root}-build" --config Release --target install --parallel
7180
)
72-
echo "Installed Uncrustify to: ${UNCRUSTIFY_LOCAL_INSTALL_DIR}"
81+
82+
echo "Installed Uncrustify to: ${UNCRUSTIFY_INSTALL_DIR}"
7383
}
7484

7585
print_usage_and_exit() {
76-
sed -n '2,19p' "$0" | sed 's/^# \{0,1\}//'
86+
sed -n '2,24p' "$0" | sed 's/^# \{0,1\}//'
7787
exit 0
7888
}
7989

80-
# Argument handling
81-
if [[ "${1:-}" == "-h" || "${1:-}" == "--help" ]]; then
82-
print_usage_and_exit
83-
fi
84-
85-
if [[ "${1:-}" == "--install" ]]; then
90+
# --------------------------
91+
# Argument parsing
92+
# --------------------------
93+
DO_INSTALL=0
94+
# Accept: --install, --install-dir <dir>, -h/--help
95+
while [[ $# -gt 0 ]]; do
96+
case "$1" in
97+
-h|--help)
98+
print_usage_and_exit
99+
;;
100+
--install)
101+
DO_INSTALL=1
102+
shift
103+
;;
104+
--install-dir)
105+
[[ $# -ge 2 ]] || die "$1 requires a path argument"
106+
UNCRUSTIFY_INSTALL_DIR="$(readlink -m "$2" 2>/dev/null || realpath -m "$2")"
107+
UNCRUSTIFY_BIN="${UNCRUSTIFY_INSTALL_DIR}/bin/uncrustify"
108+
# Only update UNCRUSTIFY default if user hasn't explicitly set it
109+
if [[ "${UNCRUSTIFY:-}" != "${UNCRUSTIFY_BIN}" ]]; then
110+
UNCRUSTIFY="${UNCRUSTIFY_BIN}"
111+
fi
112+
shift 2
113+
;;
114+
*)
115+
# ignore unrecognized positional args for now
116+
shift
117+
;;
118+
esac
119+
done
120+
121+
if [[ "$DO_INSTALL" -eq 1 ]]; then
86122
install_uncrustify
87123
# Ensure we use the freshly installed binary for this run
88-
UNCRUSTIFY="$UNCRUSTIFY_LOCAL_INSTALL_EXECUTABLE"
124+
UNCRUSTIFY="$UNCRUSTIFY_BIN"
89125
fi
90126

127+
# --------------------------
128+
# Validate & run
129+
# --------------------------
130+
91131
# Check Uncrustify availability
92132
if ! command -v "$UNCRUSTIFY" >/dev/null 2>&1; then
93133
err "Uncrustify executable not found: $UNCRUSTIFY"
94-
die "Add it to PATH, set UNCRUSTIFY=/path/to/uncrustify, or run: $0 --install"
134+
die "Add it to PATH, set UNCRUSTIFY=/path/to/uncrustify, or run: $0 --install [--install-dir DIR]"
95135
fi
96136

97137
# Version check
98138
DETECTED_VERSION="$("$UNCRUSTIFY" --version 2>&1 | grep -oE '[0-9]+(\.[0-9]+)*' | head -n1 || true)"
99139
echo "Detected Uncrustify: ${DETECTED_VERSION:-unknown}"
100140
if [[ "$DETECTED_VERSION" != "${UNCRUSTIFY_VERSION}" ]]; then
101-
die "Expected Uncrustify ${UNCRUSTIFY_VERSION}. Re-run with --install or adjust UNCRUSTIFY."
141+
die "Expected Uncrustify ${UNCRUSTIFY_VERSION}. Re-run with --install (and optionally --install-dir) or set UNCRUSTIFY."
102142
fi
103143

104144
# Config check

0 commit comments

Comments
 (0)