-
Notifications
You must be signed in to change notification settings - Fork 0
/
clang-format.sh
executable file
·58 lines (50 loc) · 1.21 KB
/
clang-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
#!/bin/bash
# safe https://sipb.mit.edu/doc/safe-shell/
set -euf -o pipefail
function usage()
{
echo "Usage: $0 <directory> | --help"
}
# Try different clang-format commands...
if type "clang-format" > /dev/null 2>&1; then
CLANG_FORMAT="clang-format"
elif type "clang-format-3.8" > /dev/null 2>&1; then
CLANG_FORMAT="clang-format-3.8"
elif type "clang-format-3.7" > /dev/null 2>&1; then
CLANG_FORMAT="clang-format-3.7"
else
CLANG_FORMAT=""
fi
if [ -z "$CLANG_FORMAT" ]; then
echo "No clang-format command can be found"
exit 1
fi
echo "Using: $("$CLANG_FORMAT" --version)"
# no arguments
if [ $# -eq 0 ]; then
usage
exit 1
fi
dir="$1"
# display help
if [ "$dir" = "--help" ]; then
usage
exit 0
fi
# path does not exist
if ! [ -d "$dir" ]; then
if [ -f "$dir" ]; then
echo "Can not format a file, use: $CLANG_FORMAT -style=file -i $dir"
else
echo "Directory $1 does not exist"
fi
usage
exit 1
fi
# format
read -r -p "Are you sure? [y/N] " response
response=${response,,} # tolower
if [[ $response =~ ^(yes|y)$ ]]; then
echo "Formating..."
find "$dir" -regex ".*\.\(hpp\|cpp\|c\|h\)" -print0 | xargs -0 "$CLANG_FORMAT" -style=file -i
fi