-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.sh
executable file
·130 lines (109 loc) · 3.97 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
function find_source_files() {
find . \
\( -not -path "*/build/*" \
-not -path "./.clang-tidy-builds/*" \
-not -path "./.deps/*" \
-not -path "./.git/*" \
-not -path "./.jenkins/*/dory/*" \
-not -path "./third-party/*/internal/*" \
\( -name "*.cpp" -o \
-name "*.hpp" -o \
-name "*.c" -o \
-name "*.h" \
\) \
\) -exec "$@"
}
function find_cmake_files() {
find . \
\( -not -path "*/build/*" \
-not -path "./.clang-tidy-builds/*" \
-not -path "./.deps/*" \
-not -path "./.git/*" \
-not -path "./.jenkins/*/dory/*" \
\( -name "CMakeLists.txt" \) \
\) -exec "$@"
}
function find_python_files() {
find . \
\( -not -path "*/build/*" \
-not -path "./.clang-tidy-builds/*" \
-not -path "./.deps/*" \
-not -path "./.git/*" \
-not -path "./.jenkins/*/dory/*" \
\( -name "*.py" \) \
\) -exec "$@"
}
function compute_md5sum() {
local output=`$1 cat {} + | md5sum`
echo $output | awk '{print $1}'
}
# If `-v` is provided as argument, the formatter scans all files
# end returns the exit code `1` if any change was found. If `-v` is
# not provided, then the exit code will be `0`, unless one of the
# formatters fails to format some files.
verbose=false
if [ $# -eq 1 ]; then
if [[ "$1" == "-v" ]]; then
verbose=true
shift
fi
fi
if [ $# -eq 0 ]
then
cd "$DIR" || exit 1
changed=false
echo "No arguments supplied, running on the entire source"
# Execute `clang-format` once for all files (denoted by `+``)
# Replace with `\;` to execute once for every file.
source_md5_before=$(compute_md5sum find_source_files)
find_source_files clang-format-10 -i -style=file {} +
find_source_files awk -i inplace '/^$/ {nlstack=nlstack "\n";next;} {printf "%s",nlstack; nlstack=""; print;}' {} +
source_md5_after=$(compute_md5sum find_source_files)
[[ "$source_md5_before" != "$source_md5_after" ]] && changed=true
cmake_md5_before=$(compute_md5sum find_cmake_files)
find_cmake_files cmake-format -i {} +
find_cmake_files awk -i inplace '/^$/ {nlstack=nlstack "\n";next;} {printf "%s",nlstack; nlstack=""; print;}' {} +
cmake_md5_after=$(compute_md5sum find_cmake_files)
[[ "$cmake_md5_before" != "$cmake_md5_after" ]] && changed=true
python_md5_before=$(compute_md5sum find_python_files)
find_python_files black -q -t py38 {} +
find_python_files awk -i inplace '/^$/ {nlstack=nlstack "\n";next;} {printf "%s",nlstack; nlstack=""; print;}' {} +
python_md5_after=$(compute_md5sum find_python_files)
[[ "$python_md5_before" != "$python_md5_after" ]] && changed=true
if $changed; then
echo "Formatting made some changes"
if $verbose; then
git diff
exit 1
fi
fi
else
echo "Formatting files: ${*:1}"
for path_name in ${*:1}; do
filename=$(basename -- "$path_name")
extension=$([[ "$filename" = *.* ]] && echo ".${filename##*.}" || echo '')
skipped=false
case "$extension" in
.c|.cpp|.h|.hpp)
clang-format-10 -i -assume-filename="$DIR"/.clang-format -style=file "$path_name" ;;
.py)
black -q -t py38 "$path_name" ;;
.txt)
if [[ $filename == "CMakeLists.txt" ]]; then
cmake-format -i "$path_name"
else
skipped=true
fi
;;
*)
echo "Skipping $path_name"
skipped=true
;;
esac
if ! $skipped ; then
awk -i inplace '/^$/ {nlstack=nlstack "\n";next;} {printf "%s",nlstack; nlstack=""; print;}' "$path_name"
fi
done
fi