-
-
Notifications
You must be signed in to change notification settings - Fork 368
Expand file tree
/
Copy pathclang-format.sh
More file actions
executable file
·87 lines (74 loc) · 2.6 KB
/
Copy pathclang-format.sh
File metadata and controls
executable file
·87 lines (74 loc) · 2.6 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
#!/bin/bash
set -eo pipefail
echo "💡 If you get 'spawn Unknown system error -86', try running this script manually:"
echo " ./scripts/clang-format.sh $1"
echo "💡 It is also recommended to use clang-format v22 or newer."
# Prioritize finding clang-format executable from brew since the run-s may introduce X86/ARM64 mismatch.
CLANG_FORMAT_PATH=""
if [ -f "/opt/homebrew/bin/clang-format" ]; then
CLANG_FORMAT_PATH="/opt/homebrew/bin/clang-format"
else
CLANG_FORMAT_PATH=$(which clang-format 2>/dev/null || true)
fi
if [ -z "$CLANG_FORMAT_PATH" ]; then
echo "❌ clang-format is not installed or not found in PATH"
echo ""
echo "To install clang-format:"
echo " * macOS: brew install clang-format"
echo " * Ubuntu: follow steps used on file .github/workflows/buildandtest.yml clang-format setup."
echo " * Arch: pacman -S clang llvm llvm-libs"
exit 1
fi
CLANG_VERSION="$("$CLANG_FORMAT_PATH" --version 2>/dev/null)"
CLANG_MAJOR_VERSION="$(printf '%s' "$CLANG_VERSION" | grep -oE '[0-9]+\.[0-9]+(\.[0-9]+)?' | head -n1 | cut -d. -f1)"
echo "clang-format version: $CLANG_VERSION, MAJOR: $CLANG_MAJOR_VERSION"
if ! printf '%s' "$CLANG_MAJOR_VERSION" | grep -qE '^[0-9]+$'; then
echo "❌ Could not parse clang-format version from: $CLANG_VERSION"
exit 1
fi
REQUIRED_MAJOR=22
if [ "$CLANG_MAJOR_VERSION" -lt "$REQUIRED_MAJOR" ]; then
echo "❌ clang-format major version $CLANG_MAJOR_VERSION is lower than required $REQUIRED_MAJOR"
exit 1
fi
# Check if an argument is provided
if [ $# -eq 0 ]; then
echo "Usage: $0 <fix|lint>"
exit 1
fi
# Set the mode based on the first argument
mode=$1
# Base command
cmd="find . -type f \( \
-name \"*.h\" -or \
-name \"*.hpp\" -or \
-name \"*.c\" -or \
-name \"*.cpp\" -or \
-name \"*.m\" -or \
-name \"*.mm\" \) -and \
! \( \
-path \"**.build/*\" -or \
-path \"**Build/*\" -or \
-path \"**ios/build/**\" -or \
-path \"**android/build/**\" -or \
-path \"**.cxx/**\" -or \
-path \"**build/generated/**\" -or \
-path \"**/DerivedData/**\" -or \
-path \"**/Carthage/Checkouts/*\" -or \
-path \"**/libs/**\" -or \
-path \"**/.yalc/**\" -or \
-path \"**/node_modules/**\" -or \
-path \"**/gems/**\" -or \
-path \"**/Pods/**\" \) \
| xargs \"$CLANG_FORMAT_PATH\" --Werror --verbose -i -style=file"
# Add --replace flag if mode is 'fix'
if [ "$mode" = "fix" ]; then
echo "clang-format fixing files..."
elif [ "$mode" = "lint" ]; then
echo "clang-format lint files..."
cmd+=" --dry-run"
else
echo "Invalid mode. Use 'fix' or 'lint'."
exit 1
fi
eval $cmd