|
4 | 4 | # |
5 | 5 | # Usage: |
6 | 6 | # ./runformat # format using the configured Uncrustify |
| 7 | +# ./runformat --install # download, build, and use Uncrustify locally |
7 | 8 | # |
8 | 9 | # Requirements: |
9 | 10 | # - All developers must use the *exact* same Uncrustify version to avoid format churn. |
10 | 11 | # - Either: |
11 | 12 | # * 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. |
13 | 15 | # |
14 | 16 | # Notes: |
| 17 | +# - The local install lives under: ./.runformat-uncrustify/uncrustify-<version>-install |
15 | 18 | # - The config file is expected at: ./.uncrustify.cfg |
16 | 19 | # |
| 20 | + |
| 21 | +set -euo pipefail |
| 22 | + |
17 | 23 | SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
18 | 24 | cd "$SCRIPT_DIR" |
19 | 25 |
|
20 | 26 | 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}" |
22 | 35 | UNCRUSTIFY_CONFIG="${SCRIPT_DIR}/.uncrustify.cfg" |
23 | 36 |
|
24 | 37 | err() { echo -e >&2 "ERROR: $@\n"; } |
25 | 38 | die() { err $@; exit 1; } |
26 | 39 |
|
| 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 | + |
27 | 91 | # Check Uncrustify availability |
28 | 92 | if ! command -v "$UNCRUSTIFY" >/dev/null 2>&1; then |
29 | 93 | 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" |
31 | 95 | fi |
32 | 96 |
|
33 | 97 | # Version check |
34 | 98 | DETECTED_VERSION="$("$UNCRUSTIFY" --version 2>&1 | grep -oE '[0-9]+(\.[0-9]+)*' | head -n1 || true)" |
35 | 99 | echo "Detected Uncrustify: ${DETECTED_VERSION:-unknown}" |
36 | 100 | 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." |
38 | 102 | fi |
39 | 103 |
|
40 | 104 | # Config check |
|
0 commit comments