-
Notifications
You must be signed in to change notification settings - Fork 16
/
format_code.sh
executable file
·87 lines (66 loc) · 2.13 KB
/
format_code.sh
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/sh
# This script searches all source code files and formats them according to the .clang-format style.
maj_min=5
maj_max=3
base=clang-format-9
format=""
# Redirect output to stderr.
exec 1>&2
# check if clang-format is installed
type "$base" >/dev/null 2>&1 && format="$base"
# if not, check all possible versions
# (i.e. clang-format-<$maj_min-$maj_max>-<0-9>)
if [ -z "$format" ]
then
for j in `seq $maj_max -1 $maj_min`
do
for i in `seq 0 9`
do
type "$base-$j.$i" >/dev/null 2>&1 && format="$base-$j.$i" && break
done
[ -z "$format" ] || break
done
fi
asdf="$(which $format)"
echo "$asdf"
# no versions of clang-format are installed
if [ -z "$format" ]
then
echo "$base is not installed. Cannot format code..."
exit 1
fi
echo "$format was found, going to format your code..." >&2
find $(dirname "$0")/ \
-not \( -path "*/build/*" -prune \) \
-not \( -path "*/_build/*" -prune \) \
-not \( -path "*/cmake/*" -prune \) \
-not \( -path "*/config/*" -prune \) \
-not \( -path "*/scripts/*" -prune \) \
-not \( -path "*/.vscode/*" -prune \) \
-not \( -path "*/third_party/*" -prune \) \
-not \( -path "*/doc/*" -prune \) \
\( -name *.h -o -name *.hpp -o -name *.c -o -name *.cc -o -name *.cpp -o -name *.proto \) \
| xargs $format -i
echo "done formatting with clang"
cmakeFormat="cmake-format"
asdf="$(which $cmakeFormat)"
echo "$asdf"
if [ -z "$cmakeFormat" ]
then
echo "$cmakeFormat is not installed. Cannot format cmake files..."
echo "run: sudo pip3 install cmake-format"
exit 1
fi
echo "$cmakeFormat was found, going to format your cmake scripts..." >&2
find $(dirname "$0")/ \
-not \( -path "*/build/*" -prune \) \
-not \( -path "*/_build/*" -prune \) \
-not \( -path "*/scripts/*" -prune \) \
-not \( -path "*/.vscode/*" -prune \) \
-not \( -path "*/third_party/*" -prune \) \
-not \( -path "*/cmake-build-debug/*" -prune \) \
-not \( -path '*/cmake/conan.cmake' -prune \) \
-not \( -path '*/cmake/debug.cmake' -prune \) \
\( -name *.cmake -o -name CMakeLists.txt ! -iname "*Find*" \) \
| xargs $cmakeFormat -c cmake-format.yaml -i
echo "done formatting with cmake-format"