@@ -7,12 +7,33 @@ source_dir="$(dirname "$script_dir")"
7
7
8
8
check_only=0
9
9
no_version_check=0
10
+ run_cpp=0
11
+ run_yaml=0
12
+ run_sh=0
13
+ run_cmake=0
14
+ run_all=1
10
15
11
16
while [[ " $# " -gt 0 ]]; do
12
17
case $1 in
13
18
-h | --help) help=1 ;;
14
19
--check-only) check_only=1 ;;
15
20
--no-version-check) no_version_check=1 ;;
21
+ --cpp)
22
+ run_cpp=1
23
+ run_all=0
24
+ ;;
25
+ --yaml)
26
+ run_yaml=1
27
+ run_all=0
28
+ ;;
29
+ --sh)
30
+ run_sh=1
31
+ run_all=0
32
+ ;;
33
+ --cmake)
34
+ run_cmake=1
35
+ run_all=0
36
+ ;;
16
37
--source)
17
38
source_dir=" $2 "
18
39
shift
@@ -26,12 +47,16 @@ if [ "$help" ]; then
26
47
cat << EOF
27
48
$me : Format or check formatting of files in this repo
28
49
29
- Usage: $me [--check-only] [--no-version-check] [--source <path>]
50
+ Usage: $me [--check-only] [--no-version-check] [--source <path>] [--cpp] [--yaml] [--sh] [--cmake]
30
51
31
52
Options:
32
53
--check-only Check formatting without modifying files
33
54
--no-version-check Skip version compatibility checks
34
- --source Path to source directory to format (defaults to parent of script directory)
55
+ --source Path to source directory to format (defaults to parent of script directory)
56
+ --cpp Format only C++ files
57
+ --yaml Format only YAML/JSON files
58
+ --sh Format only shell script files
59
+ --cmake Format only CMake files
35
60
EOF
36
61
exit 0
37
62
fi
@@ -45,7 +70,7 @@ require_bin() {
45
70
local version
46
71
47
72
if ! command -v " $name " & > /dev/null; then
48
- echo " This script needs $name , but it isn't in \$ PATH"
73
+ echo " This script needs $name , but it isn't in \$ PATH" >&2
49
74
missing_bin=1
50
75
return
51
76
fi
@@ -54,14 +79,14 @@ require_bin() {
54
79
version=$( " $name " --version | grep -oP " \d+\.\d+\.?\d*" | head -n1)
55
80
56
81
if ! printf ' %s\n%s\n' " $min_version " " $version " | sort -V -C; then
57
- echo " $name version $version is too old. Version $min_version or newer is required."
82
+ echo " $name version $version is too old. Version $min_version or newer is required." >&2
58
83
missing_bin=1
59
84
return
60
85
fi
61
86
62
87
if [ -n " $max_version " ]; then
63
88
if ! printf ' %s\n%s\n' " $version " " $max_version " | sort -V -C; then
64
- echo " $name version $version is too new. Version less than $max_version is required."
89
+ echo " $name version $version is too new. Version less than $max_version is required." >&2
65
90
missing_bin=1
66
91
return
67
92
fi
84
109
exit_code=0
85
110
86
111
cmake_formatting () {
87
- echo " Formatting CMake files..."
112
+ echo " Formatting CMake files..." >&2
88
113
89
114
readarray -t files < <( git ls-files ' *.cmake' ' CMakeLists.txt' ' **/CMakeLists.txt' )
90
115
@@ -102,47 +127,78 @@ cmake_formatting() {
102
127
fi
103
128
}
104
129
130
+ track_progress () {
131
+ # Don't output the progress bar if stderr isn't a terminal, just eat all the input
132
+ [ -t 2 ] || {
133
+ cat > /dev/null
134
+ return
135
+ }
136
+
137
+ local total=$1
138
+ local current=0
139
+
140
+ while IFS= read -r _; do
141
+ (( current++ )) || :
142
+ percent=$(( current * 100 / total))
143
+ printf ' \rProgress: [%-50s] %d%%' " $( printf ' #%.0s' $( seq 1 $(( percent / 2 )) ) ) " " $percent " >&2
144
+ done
145
+ echo >&2
146
+ }
147
+
105
148
cpp_formatting () {
106
- echo " Formatting cpp files..."
149
+ echo " Formatting cpp files..." >&2
107
150
108
151
readarray -t files < <( git ls-files ' *.cpp' ' *.hpp' ' *.c' ' *.h' ' :!external/**' )
109
152
153
+ # The progress reporting is a bit sneaky, we use `--verbose` with xargs which
154
+ # prints a line to stderr for each command, and we simply count these...
155
+
110
156
if [ " $check_only " -eq 1 ]; then
111
157
local tmpdir
112
158
tmpdir=$( mktemp -d)
113
159
trap ' rm -rf "$tmpdir"' EXIT
114
160
115
- printf ' %s\n' " ${files[@]} " | xargs -P " $( nproc) " -I{} bash -c "
161
+ printf ' %s\n' " ${files[@]} " | xargs --verbose - P " $( nproc) " -I{} bash -c "
116
162
mkdir -p \"\$ (dirname \" $tmpdir /{}\" )\"
117
- diff -u --color=always --label \" {}\" --label \" {}\" \" {}\" <(clang-format \" {}\" ) > \" $tmpdir /{}\"
163
+ diff -u --color=always --label \" {}\" --label \" {}\" \" {}\" <(clang-format \" {}\" ) > \" $tmpdir /{}\"
118
164
:
119
- "
165
+ " | & track_progress ${ # files[@]}
120
166
121
167
for file in " ${files[@]} " ; do
168
+ # Fail if any of the diffs have contents
122
169
if [ -s " $tmpdir /$file " ]; then
123
170
cat " $tmpdir /$file "
124
171
exit_code=1
125
172
fi
126
173
done
127
174
else
128
- printf ' %s\n' " ${files[@]} " | xargs -n1 -P " $( nproc) " clang-format -i
175
+ printf ' %s\n' " ${files[@]} " | xargs --verbose -n1 -P " $( nproc) " clang-format -i | &
176
+ track_progress ${# files[@]}
129
177
fi
130
178
}
131
179
132
180
yaml_json_formatting () {
133
- echo " Formatting yaml and json files..."
181
+ echo " Formatting yaml and json files..." >&2
134
182
135
183
readarray -t files < <( git ls-files " *.yaml" " *.yml" " *.json" ' :!external/**' )
136
184
137
185
if [ " $check_only " -eq 1 ]; then
138
- prettier --check " ${files[@]} " || exit_code=1
186
+ for file in " ${files[@]} " ; do
187
+ if ! output=$( prettier " $file " 2> /dev/null) ; then
188
+ continue
189
+ fi
190
+ if ! diff -q " $file " <( echo " $output " ) > /dev/null 2>&1 ; then
191
+ diff --color -u --label " $file " --label " $file " " $file " <( echo " $output " )
192
+ exit_code=1
193
+ fi
194
+ done
139
195
else
140
- prettier --write " ${files[@]} " | grep -v ' (unchanged)' || :
196
+ prettier --write " ${files[@]} " | grep -v ' (unchanged)' >&2 || :
141
197
fi
142
198
}
143
199
144
200
sh_formatting () {
145
- echo " Formatting sh files..."
201
+ echo " Formatting sh files..." >&2
146
202
147
203
readarray -t files < <( git ls-files " *.sh" )
148
204
@@ -158,9 +214,9 @@ sh_formatting() {
158
214
fi
159
215
}
160
216
161
- cmake_formatting
162
- cpp_formatting
163
- yaml_json_formatting
164
- sh_formatting
217
+ (( run_all || run_sh )) && sh_formatting
218
+ (( run_all || run_cmake )) && cmake_formatting
219
+ (( run_all || run_yaml )) && yaml_json_formatting
220
+ (( run_all || run_cpp )) && cpp_formatting
165
221
166
222
exit $exit_code
0 commit comments