forked from pantsbuild/pants
/
check_rust_formatting.sh
executable file
·77 lines (66 loc) · 1.73 KB
/
check_rust_formatting.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
#!/usr/bin/env bash
REPO_ROOT="$(git rev-parse --show-toplevel)"
function usage() {
echo "Checks formatting of rust files, optionally fixing mis-formatted files."
echo
echo "Usage: $0 (-h|-f)"
echo " -h print out this help message"
echo " -f instead of erroring on files with bad formatting, fix those files"
if [[ -n "$@" ]]; then
echo
echo "$@"
fi
}
check="true"
while getopts "hf" opt; do
case ${opt} in
h)
usage
exit 0
;;
f)
check="false"
;;
*)
usage "Unrecognized arguments."
exit 1
;;
esac
done
NATIVE_ROOT="${REPO_ROOT}/src/rust/engine"
cmd=(
"${REPO_ROOT}/build-support/bin/native/cargo" fmt --all --
)
if [[ "${check}" == "true" ]]; then
cmd=("${cmd[@]}" "--check")
fi
bad_files=(
$(
cd "${NATIVE_ROOT}" || exit "${PIPESTATUS[0]}"
# Ensure generated code is present since `cargo fmt` needs to do enough parsing to follow use's
# and these will land in generated code.
echo >&2 "Ensuring generated code is present for downstream formatting checks..."
"${REPO_ROOT}/build-support/bin/native/cargo" check -p bazel_protos
"${cmd[@]}" | \
awk '$0 ~ /^Diff in/ {print $3}' | \
sort -u
exit "${PIPESTATUS[0]}"
)
)
exit_code=$?
if [[ ${exit_code} -ne 0 ]]; then
if [[ "${check}" == "true" ]]; then
echo >&2 "The following rust files were incorrectly formatted, run \`$0 -f\` to reformat them:"
for bad_file in ${bad_files[*]}; do
echo >&2 "${bad_file}"
done
else
cat << EOF >&2
An error occurred while checking the formatting of rust files.
Try running \`(cd "${NATIVE_ROOT}" && ${cmd[@]})\` to investigate.
Its error is:
EOF
cd "${NATIVE_ROOT}" && "${cmd[@]}" >/dev/null
fi
exit 1
fi