2019-11-06 19:47:54 +00:00
|
|
|
#!/usr/bin/env bash
|
2019-02-27 23:01:21 +00:00
|
|
|
|
2020-09-20 20:41:45 +00:00
|
|
|
# Usage: ./code_style.sh
|
|
|
|
# Usage: ./code_style.sh --check
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
set -o noglob
|
|
|
|
|
2022-05-13 23:48:33 +00:00
|
|
|
# PATH workaround for SourceTree
|
|
|
|
# for Intel Mac
|
|
|
|
PATH="${PATH}:/usr/local/bin"
|
|
|
|
# for Apple Silicon Mac
|
|
|
|
PATH="${PATH}:/opt/homebrew/bin"
|
|
|
|
|
2020-09-20 20:41:45 +00:00
|
|
|
if [[ "x$1" == *"check"* ]]; then
|
|
|
|
echo "checking code format"
|
|
|
|
else
|
|
|
|
fix=1
|
|
|
|
fi
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
root="$(dirname "$0")"
|
|
|
|
root="$(cd "${root}" && pwd)"
|
|
|
|
cd "${root}" || exit 1
|
2020-09-20 20:41:45 +00:00
|
|
|
|
2022-05-13 23:48:33 +00:00
|
|
|
trim_comments() {
|
|
|
|
# 1. remove comments, ignoring backslash-escaped characters
|
|
|
|
# 2. trim spaces
|
|
|
|
# 3. remove empty lines
|
|
|
|
# note: GNU extensions like +?| aren't supported on macOS
|
|
|
|
sed 's/^\(\(\(\\.\)*[^\\#]*\)*\)#.*/\1/;s/^[[:blank:]]*//;s/[[:blank:]]*$//;/^$/d' "$@"
|
|
|
|
}
|
2021-08-15 09:41:48 +00:00
|
|
|
|
|
|
|
get_find_path_args() {
|
|
|
|
local args=$(printf " -o -path ./%s" "$@")
|
|
|
|
echo "${args:4}"
|
|
|
|
}
|
|
|
|
|
|
|
|
find_cfiles() {
|
2022-05-13 23:48:33 +00:00
|
|
|
# We use the same files as Meson: https://mesonbuild.com/Code-formatting.html
|
|
|
|
find . \( $(get_find_path_args $(trim_comments .clang-format-include)) \)\
|
|
|
|
! \( $(get_find_path_args $(trim_comments .clang-format-ignore)) \) "$@"
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2020-09-20 20:41:45 +00:00
|
|
|
|
2022-12-02 15:57:50 +00:00
|
|
|
# We're targeting clang-format version 15 and other versions give slightly
|
|
|
|
# different results, so prefer `clang-format-15` if it's installed.
|
2022-04-17 03:23:20 +00:00
|
|
|
clang_format_exe_names=(
|
2022-12-02 15:57:50 +00:00
|
|
|
'clang-format-15'
|
2022-04-17 03:23:20 +00:00
|
|
|
'clang-format'
|
|
|
|
)
|
|
|
|
for name in ${clang_format_exe_names[@]}; do
|
|
|
|
clang_format_exe=$(command -v "${name}")
|
|
|
|
if [ "$?" -eq 0 ]; then
|
|
|
|
clang_format_exe="${name}"
|
|
|
|
break
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
if [ -z "${clang_format_exe}" ]; then
|
|
|
|
echo "error: clang-format not found";
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
|
2020-09-20 20:41:45 +00:00
|
|
|
# format C/C++
|
2021-08-15 09:41:48 +00:00
|
|
|
clang_format_args="$([ -n "$fix" ] && echo '-i' || echo '--dry-run --Werror')"
|
2022-04-17 03:23:20 +00:00
|
|
|
if ! find_cfiles -exec "${clang_format_exe}" $clang_format_args '{}' '+'; then
|
2021-08-15 09:41:48 +00:00
|
|
|
[ -n "$fix" ] || echo 'C/C++ code needs formatting'
|
|
|
|
exitcode=1
|
2020-09-20 20:41:45 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# enforce east const
|
2021-09-12 17:41:49 +00:00
|
|
|
matches="$(find_cfiles -exec perl -ne 'print "west const:",$ARGV,":",$_ if /((?:^|[(<,;]|\bstatic\s+)\s*)\b(const)\b(?!\s+\w+\s*\[)/' '{}' '+')"
|
2020-09-20 20:41:45 +00:00
|
|
|
if [ -n "$matches" ]; then
|
|
|
|
echo "$matches"
|
|
|
|
exitcode=1
|
|
|
|
fi
|
|
|
|
if [ -n "$fix" ]; then
|
2021-09-12 17:41:49 +00:00
|
|
|
find_cfiles -exec perl -pi -e 's/((?:^|[(<,;]|\bstatic\s+)\s*)\b(const)\b(?!\s+\w+\s*\[)/\1>\2</g' '{}' '+'
|
2020-09-20 20:41:45 +00:00
|
|
|
fi
|
|
|
|
|
|
|
|
# format JS
|
|
|
|
cd "${root}/web" || exit 1
|
2023-01-22 20:47:59 +00:00
|
|
|
npm_lint_args="$([ -n "$fix" ] && echo 'lint:fix' || echo 'lint')"
|
2023-01-24 07:02:48 +00:00
|
|
|
if ! npm ci --no-audit --no-fund --no-progress &>/dev/null; then
|
|
|
|
[ -n "$fix" ] || echo 'JS code could not be checked -- "npm ci" failed'
|
2020-10-24 01:04:25 +00:00
|
|
|
exitcode=1
|
2023-01-23 20:50:21 +00:00
|
|
|
elif ! npm run --silent $npm_lint_args; then
|
2021-08-15 09:41:48 +00:00
|
|
|
[ -n "$fix" ] || echo 'JS code needs formatting'
|
2020-09-20 20:41:45 +00:00
|
|
|
exitcode=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit $exitcode
|