-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.sh
executable file
·113 lines (98 loc) · 2.58 KB
/
format.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#!/bin/bash
# Check if we can run python3 and pip
python3 -m pip --version > /dev/null
if [[ $? -ne 0 ]]
then
echo "ERROR: cannot run 'python3 -m pip'"
exit 1
fi
# Check if the virtual environment with the clang formatter exists
if [ ! -d clang_formatting_env ]
then
echo "Formatting environment not found, installing it..."
python3 -m venv clang_formatting_env
./clang_formatting_env/bin/python3 -m pip install --upgrade pip # Need to access wheel
./clang_formatting_env/bin/python3 -m pip install clang-format==17.0.6
fi
# Activate enviroment to enable clang-format command
source ./clang_formatting_env/bin/activate
# Formatting command
clang=${CLANG_FORMAT_CMD:="clang-format"}
cmd="$clang -style=file $(git ls-files '*.c' '*.h' '*.cpp')"
# Test if `clang-format` works
command -v $clang > /dev/null
if [[ $? -ne 0 ]]
then
echo "ERROR: cannot find $clang"
exit 1
fi
# Print the help
function show_help {
echo -e "This script formats HBTPlus according to Microsoft style"
echo -e " -h, --help \t Show this help"
echo -e " -t, --test \t Test if HBTPlus is well formatted"
echo -e " -c, --clean \t Remove formatting venv"
}
# Remove the virtual enviroment
function remove_clang_venv {
echo "Removing python venv used to format HBTPlus"
rm -rf clang_formatting_env
}
# Parse arguments (based on https://stackoverflow.com/questions/192249)
TEST=0
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
# print the help and exit
-h|--help)
show_help
exit
;;
# check if the code is well formatted
-t|--test)
TEST=1
shift
;;
# Remove the formatting venv
-c|--clean)
TEST=1
remove_clang_venv
exit
;;
# unknown option
*)
echo "Argument '$1' not implemented"
show_help
exit
;;
esac
done
# Run the required commands
if [[ $TEST -eq 1 ]]
then
# Note trapping the exit status from both commands in the pipe. Also note
# do not use -q in grep as that closes the pipe on first match and we get
# a SIGPIPE error.
echo "Testing if HBTPlus is correctly formatted"
$cmd -output-replacements-xml | grep "<replacement " > /dev/null
status=("${PIPESTATUS[@]}")
# Trap if first command failed. Note 141 is SIGPIPE, that happens when no
# output
if [[ ${status[0]} -ne 0 ]]
then
echo "ERROR: $clang command failed"
exit 1
fi
# Check formatting
if [[ ${status[1]} -eq 0 ]]
then
echo "ERROR: needs formatting"
exit 1
else
echo "...is correctly formatted"
fi
else
echo "Formatting HBTPlus"
$cmd -i
fi