summaryrefslogtreecommitdiff
path: root/scripts/format.sh
blob: 3b77776b3d03ac7709142206f64442662b8a144e (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
#!/usr/bin/env bash
#
# format.sh — Formats all source files in the project using clang-format.
#
# Usage:
#   ./scripts/format.sh          # Format all C/C++ source files
#   ./scripts/format.sh --check  # Check formatting without modifying files
#

set -euo pipefail

# Project root (one level up from scripts/)
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT_DIR"

# Find all C/C++ files (excluding build directories and third-party code)
FILES=$(find src include -type f \( -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" \) \
  ! -path "*/build/*" ! -path "*/third_party/*")

# Default clang-format style (fallback if no .clang-format exists)
STYLE="file"
if [ ! -f ".clang-format" ]; then
  STYLE="llvm"
fi

# Command arguments
CMD_ARGS=("-i" "--style=${STYLE}")
if [[ "${1:-}" == "--check" ]]; then
  CMD_ARGS=("--dry-run" "--Werror" "--style=${STYLE}")
  echo "Checking code format..."
else
  echo "Formatting source files..."
fi

# Run clang-format on each file
for file in $FILES; do
  clang-format "${CMD_ARGS[@]}" "$file"
done

if [[ "${1:-}" == "--check" ]]; then
  echo "Format check passed!"
else
  echo "Formatting complete."
fi