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
|
|
|
|
|
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
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
cfile_includes=(
|
|
|
|
'*.c'
|
|
|
|
'*.cc'
|
|
|
|
'*.h'
|
|
|
|
'*.m'
|
2022-03-30 21:52:23 +00:00
|
|
|
'*.mm'
|
2020-09-20 20:41:45 +00:00
|
|
|
)
|
2021-08-15 09:41:48 +00:00
|
|
|
cfile_excludes=(
|
2021-08-28 12:46:25 +00:00
|
|
|
'build/*'
|
2021-08-15 09:41:48 +00:00
|
|
|
'libtransmission/ConvertUTF.*'
|
|
|
|
'libtransmission/jsonsl.*'
|
|
|
|
'libtransmission/wildmat.*'
|
|
|
|
'macosx/Sparkle.framework/*'
|
|
|
|
'macosx/VDKQueue/*'
|
|
|
|
'third-party/*'
|
|
|
|
'web/*'
|
2021-10-09 02:46:10 +00:00
|
|
|
'.git/*'
|
2020-09-20 20:41:45 +00:00
|
|
|
)
|
2021-08-15 09:41:48 +00:00
|
|
|
|
|
|
|
get_find_path_args() {
|
|
|
|
local args=$(printf " -o -path ./%s" "$@")
|
|
|
|
echo "${args:4}"
|
|
|
|
}
|
|
|
|
|
|
|
|
find_cfiles() {
|
|
|
|
find . \( $(get_find_path_args "${cfile_includes[@]}") \) ! \( $(get_find_path_args "${cfile_excludes[@]}") \) "$@"
|
|
|
|
}
|
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')"
|
|
|
|
if ! find_cfiles -exec clang-format $clang_format_args '{}' '+'; then
|
|
|
|
[ -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
|
2021-08-15 09:41:48 +00:00
|
|
|
yarn_args='--silent --no-progress --non-interactive'
|
|
|
|
yarn_lint_args="$([ -n "$fix" ] && echo 'lint:fix' || echo 'lint')"
|
|
|
|
if ! yarn $yarn_args install; then
|
|
|
|
[ -n "$fix" ] || echo 'JS code could not be checked -- "yarn install" failed'
|
2020-10-24 01:04:25 +00:00
|
|
|
exitcode=1
|
2021-08-15 09:41:48 +00:00
|
|
|
elif ! yarn $yarn_args $yarn_lint_args; then
|
|
|
|
[ -n "$fix" ] || echo 'JS code needs formatting'
|
2020-09-20 20:41:45 +00:00
|
|
|
exitcode=1
|
|
|
|
fi
|
|
|
|
|
|
|
|
exit $exitcode
|