Skip to content

Commit a98fb6c

Browse files
Enhancements to diff script (shader-slang#5460)
* Output diff for prettier format * Allow selective formatting * report progress when formatting cpp files * document --------- Co-authored-by: Yong He <yonghe@outlook.com>
1 parent 90bde47 commit a98fb6c

File tree

1 file changed

+75
-19
lines changed

1 file changed

+75
-19
lines changed

extras/formatting.sh

+75-19
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,33 @@ source_dir="$(dirname "$script_dir")"
77

88
check_only=0
99
no_version_check=0
10+
run_cpp=0
11+
run_yaml=0
12+
run_sh=0
13+
run_cmake=0
14+
run_all=1
1015

1116
while [[ "$#" -gt 0 ]]; do
1217
case $1 in
1318
-h | --help) help=1 ;;
1419
--check-only) check_only=1 ;;
1520
--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+
;;
1637
--source)
1738
source_dir="$2"
1839
shift
@@ -26,12 +47,16 @@ if [ "$help" ]; then
2647
cat <<EOF
2748
$me: Format or check formatting of files in this repo
2849
29-
Usage: $me [--check-only] [--no-version-check] [--source <path>]
50+
Usage: $me [--check-only] [--no-version-check] [--source <path>] [--cpp] [--yaml] [--sh] [--cmake]
3051
3152
Options:
3253
--check-only Check formatting without modifying files
3354
--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
3560
EOF
3661
exit 0
3762
fi
@@ -45,7 +70,7 @@ require_bin() {
4570
local version
4671

4772
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
4974
missing_bin=1
5075
return
5176
fi
@@ -54,14 +79,14 @@ require_bin() {
5479
version=$("$name" --version | grep -oP "\d+\.\d+\.?\d*" | head -n1)
5580

5681
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
5883
missing_bin=1
5984
return
6085
fi
6186

6287
if [ -n "$max_version" ]; then
6388
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
6590
missing_bin=1
6691
return
6792
fi
@@ -84,7 +109,7 @@ fi
84109
exit_code=0
85110

86111
cmake_formatting() {
87-
echo "Formatting CMake files..."
112+
echo "Formatting CMake files..." >&2
88113

89114
readarray -t files < <(git ls-files '*.cmake' 'CMakeLists.txt' '**/CMakeLists.txt')
90115

@@ -102,47 +127,78 @@ cmake_formatting() {
102127
fi
103128
}
104129

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+
105148
cpp_formatting() {
106-
echo "Formatting cpp files..."
149+
echo "Formatting cpp files..." >&2
107150

108151
readarray -t files < <(git ls-files '*.cpp' '*.hpp' '*.c' '*.h' ':!external/**')
109152

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+
110156
if [ "$check_only" -eq 1 ]; then
111157
local tmpdir
112158
tmpdir=$(mktemp -d)
113159
trap 'rm -rf "$tmpdir"' EXIT
114160

115-
printf '%s\n' "${files[@]}" | xargs -P "$(nproc)" -I{} bash -c "
161+
printf '%s\n' "${files[@]}" | xargs --verbose -P "$(nproc)" -I{} bash -c "
116162
mkdir -p \"\$(dirname \"$tmpdir/{}\")\"
117-
diff -u --color=always --label \"{}\" --label \"{}\" \"{}\" <(clang-format \"{}\") > \"$tmpdir/{}\"
163+
diff -u --color=always --label \"{}\" --label \"{}\" \"{}\" <(clang-format \"{}\") > \"$tmpdir/{}\"
118164
:
119-
"
165+
" |& track_progress ${#files[@]}
120166

121167
for file in "${files[@]}"; do
168+
# Fail if any of the diffs have contents
122169
if [ -s "$tmpdir/$file" ]; then
123170
cat "$tmpdir/$file"
124171
exit_code=1
125172
fi
126173
done
127174
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[@]}
129177
fi
130178
}
131179

132180
yaml_json_formatting() {
133-
echo "Formatting yaml and json files..."
181+
echo "Formatting yaml and json files..." >&2
134182

135183
readarray -t files < <(git ls-files "*.yaml" "*.yml" "*.json" ':!external/**')
136184

137185
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
139195
else
140-
prettier --write "${files[@]}" | grep -v '(unchanged)' || :
196+
prettier --write "${files[@]}" | grep -v '(unchanged)' >&2 || :
141197
fi
142198
}
143199

144200
sh_formatting() {
145-
echo "Formatting sh files..."
201+
echo "Formatting sh files..." >&2
146202

147203
readarray -t files < <(git ls-files "*.sh")
148204

@@ -158,9 +214,9 @@ sh_formatting() {
158214
fi
159215
}
160216

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
165221

166222
exit $exit_code

0 commit comments

Comments
 (0)