forked from PurdueElectricRacing/firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformat.sh
More file actions
executable file
·76 lines (64 loc) · 1.63 KB
/
format.sh
File metadata and controls
executable file
·76 lines (64 loc) · 1.63 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
#!/usr/bin/env bash
#
# format.sh
# ./format.sh format # format all files in place
# ./format.sh check # validate formatting
SOURCE_DIRS=(
common/amk
common/bootloader
common/common_defs
common/faults
common/log
common/phal
common/phal_F4_F7
common/phal_G4
common/phal_L4
common/psched
common/queue
common/syscalls
source
)
FILE_GLOB='\( -iname "*.c" -o -iname "*.h" \)'
format() {
local target_dirs=()
# If an argument was passed to `format`, use it as the only directory
if [[ -n "$2" ]]; then
if [[ -d "$2" ]]; then
target_dirs=("$2")
else
echo "❌ Error: '$2' is not a directory."
exit 1
fi
else
target_dirs=("${SOURCE_DIRS[@]}")
fi
echo "Formatting files in: ${target_dirs[*]}"
find "${target_dirs[@]}" -type f \( -iname '*.c' -o -iname '*.h' \) -exec clang-format -i --style=file {} +
echo "✅ Formatting complete."
}
check() {
local files=()
local failed=0
# Collect .c and .h files
while IFS= read -r file; do
files+=("$file")
done < <(find "${SOURCE_DIRS[@]}" -type f \( -name '*.c' -o -name '*.h' \))
echo "Checking formatting on ${#files[@]} files..."
for f in "${files[@]}"; do
if ! clang-format --dry-run --Werror --style=file "$f" >/dev/null; then
echo "❌ Formatting error in: $f"
failed=1
fi
done
if [[ $failed -ne 0 ]]; then
echo -e "\n❌ Some files are not properly formatted."
echo " Run: ./format.sh format"
exit 1
fi
echo "✅ All files are properly formatted."
}
case "$1" in
format) format "$@" ;;
check) check "$@" ;;
*) echo "Usage: $0 {format|check}" && exit 2 ;;
esac