chore: Enforce minimum clang-format version

main
Sean McBride 4 years ago
parent bf35467bf8
commit d51fe49c14

@ -1,58 +1,60 @@
#!/bin/bash #!/bin/bash
utility="clang-format" validate() {
utility_version="$("$utility" --version 2>/dev/null)" || { utility="clang-format"
echo "$utility not found in path!" utility_version="$("$utility" --version 2>/dev/null)" || {
exit 1 echo "$utility not found in path!"
exit 1
}
regex="version ([0-9]+).([0-9]+).([0-9]+)"
declare -i major=0
declare -i minor=0
declare -i patch=0
declare -i required_major=9
declare -i required_minor=0
declare -i required_patch=0
if [[ "$utility_version" =~ $regex ]]; then
major="${BASH_REMATCH[1]}"
minor="${BASH_REMATCH[2]}"
patch="${BASH_REMATCH[3]}"
fi
if ((major < required_major)) || ((minor < required_minor)) || ((patch < required_patch)); then
echo "$utility $required_major.$required_minor.$required_patch required, but is $major.$minor.$patch"
exit 1
fi
} }
regex="version ([0-9]+).([0-9]+).([0-9]+)"
declare -i major=0
declare -i minor=0
declare -i patch=0
declare -i required_major=9
declare -i required_minor=0
declare -i required_patch=0
if [[ "$utility_version" =~ $regex ]]; then
major="${BASH_REMATCH[1]}"
minor="${BASH_REMATCH[2]}"
patch="${BASH_REMATCH[3]}"
fi
if ((major < required_major)) || ((minor < required_minor)) || ((patch < required_patch)); then
echo "$utility $required_major.$required_minor.$required_patch required, but is $major.$minor.$patch"
exit 1
fi
help() { help() {
echo "Usage: $0 [OPTION]..." echo "Usage: $0 [OPTION]..."
echo "Formats source code in the repository, applying formatting in place by default" echo "Formats source code in the repository, applying formatting in place by default"
echo "" echo ""
echo "-h, --help Print Help" echo "-h, --help Print Help"
echo "-d, --dry-run Dry run formatters, returning 0 when code passes, 1 otherwise" echo "-d, --dry-run Dry run formatters, returning 0 when code passes, 1 otherwise"
echo "" echo ""
echo "Exit status:" echo "Exit status:"
echo "0 if code required no formatting" echo "0 if code required no formatting"
echo "non-zero exit status otherwise" echo "non-zero exit status otherwise"
} }
dry_run() { dry_run() {
find runtime \ find runtime \
\( -path "runtime/thirdparty" -o -path "runtime/tests/gocr" -o -path "runtime/tests/TinyEKF" -o -path "runtime/tests/CMSIS_5_NN" -o -path "runtime/tests/sod" -o -path "runtime/tests/**/thirdparty" \) -prune -false -o \ \( -path "runtime/thirdparty" -o -path "runtime/tests/gocr" -o -path "runtime/tests/TinyEKF" -o -path "runtime/tests/CMSIS_5_NN" -o -path "runtime/tests/sod" -o -path "runtime/tests/**/thirdparty" \) -prune -false -o \
-type f \( -iname \*.h -o -iname \*.c -o -iname \*.s \) -print | -type f \( -iname \*.h -o -iname \*.c -o -iname \*.s \) -print |
xargs clang-format -Werror -n -ferror-limit=0 xargs clang-format -Werror -n -ferror-limit=0
} }
format() { format() {
find runtime \ find runtime \
\( -path "runtime/thirdparty" -o -path "runtime/tests/gocr" -o -path "runtime/tests/TinyEKF" -o -path "runtime/tests/CMSIS_5_NN" -o -path "runtime/tests/sod" -o -path "runtime/tests/**/thirdparty" \) -prune -false -o \ \( -path "runtime/thirdparty" -o -path "runtime/tests/gocr" -o -path "runtime/tests/TinyEKF" -o -path "runtime/tests/CMSIS_5_NN" -o -path "runtime/tests/sod" -o -path "runtime/tests/**/thirdparty" \) -prune -false -o \
-type f \( -iname \*.h -o -iname \*.c -o -iname \*.s \) -print | -type f \( -iname \*.h -o -iname \*.c -o -iname \*.s \) -print |
xargs clang-format -i xargs clang-format -i
} }
case $1 in case $1 in
"-h" | "--help") help ;; "-h" | "--help") help ;;
"-d" | "--dry-run") dry_run ;; "-d" | "--dry-run") validate && dry_run ;;
"") format ;; "") validate && format ;;
esac esac

Loading…
Cancel
Save