-
-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathFormat.sh
More file actions
125 lines (106 loc) · 3.45 KB
/
Copy pathFormat.sh
File metadata and controls
125 lines (106 loc) · 3.45 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/bin/sh
# Format all maintained C++ and Rux source code in the repository.
# Portable across Linux, macOS, and FreeBSD.
set -eu
check=false
rux_executable=
usage() {
printf '%s\n' \
'Usage: sh Format.sh [options]' \
'' \
'Options:' \
' --check Check formatting without modifying files' \
' --rux-executable PATH Existing rux executable (default: Bin/rux)' \
' -h, --help Show this help'
}
die() {
printf 'error: %s\n' "$*" >&2
exit 1
}
require_value() {
[ "$#" -ge 2 ] || die "option '$1' requires a value"
}
step() {
printf '\n==> %s\n' "$1"
}
passed() {
if [ -t 1 ] && [ -z "${NO_COLOR:-}" ]; then
printf '\033[32m[PASSED]\033[0m %s\n' "$1"
else
printf '[PASSED] %s\n' "$1"
fi
}
while [ "$#" -gt 0 ]; do
case "$1" in
--check)
check=true
shift
;;
--rux-executable)
require_value "$@"
rux_executable=$2
shift 2
;;
-h | --help)
usage
exit 0
;;
*)
die "unknown option '$1'"
;;
esac
done
script_directory=$(CDPATH= cd -P "$(dirname "$0")" && pwd)
repository_root=$script_directory
if [ -z "$rux_executable" ]; then
rux_path=$repository_root/Bin/rux
else
case "$rux_executable" in
/*) rux_path=$rux_executable ;;
*) rux_path=$repository_root/$rux_executable ;;
esac
fi
[ -f "$rux_path" ] || die "Rux executable not found at '$rux_path'"
clang_format=
for candidate in clang-format-22 clang-format22 \
/opt/homebrew/opt/llvm@22/bin/clang-format /usr/local/opt/llvm@22/bin/clang-format \
/opt/homebrew/opt/llvm/bin/clang-format /usr/local/opt/llvm/bin/clang-format clang-format; do
if clang_format=$(command -v "$candidate" 2>/dev/null); then
break
fi
clang_format=
done
[ -n "$clang_format" ] || die "required tool not found: clang-format 22"
started_at=$(date +%s)
cd "$repository_root"
cpp_file_count=$(find Compiler Tests/Unit -type f \( -name '*.cpp' -o -name '*.h' \) \
! -path '*/ThirdParty/*' -print | wc -l | tr -d '[:space:]')
manifest_count=$(find Packages Tests -type f -name Rux.toml -print | wc -l | tr -d '[:space:]')
if [ "$check" = true ]; then
step "Checking C++ formatting ($cpp_file_count files)"
find Compiler Tests/Unit -type f \( -name '*.cpp' -o -name '*.h' \) \
! -path '*/ThirdParty/*' -exec "$clang_format" --dry-run -Werror {} +
passed "C++ formatting ($cpp_file_count files)"
step "Checking Rux formatting ($manifest_count packages)"
find Packages Tests -type f -name Rux.toml -print | while IFS= read -r manifest; do
"$rux_path" --manifest "$manifest" fmt --check
done
passed "Rux formatting ($manifest_count packages)"
else
step "Formatting C++ sources ($cpp_file_count files)"
find Compiler Tests/Unit -type f \( -name '*.cpp' -o -name '*.h' \) \
! -path '*/ThirdParty/*' -exec "$clang_format" -i {} +
passed "C++ formatting ($cpp_file_count files)"
step "Formatting Rux sources ($manifest_count packages)"
find Packages Tests -type f -name Rux.toml -print | while IFS= read -r manifest; do
"$rux_path" --manifest "$manifest" fmt
done
passed "Rux formatting ($manifest_count packages)"
fi
elapsed=$(( $(date +%s) - started_at ))
if [ "$check" = true ]; then
action=check
else
action=formatting
fi
printf '\nSource %s passed in %02d:%02d.\n' "$action" "$((elapsed / 60))" "$((elapsed % 60))"