-
Notifications
You must be signed in to change notification settings - Fork 0
/
lint-helper.sh
executable file
·103 lines (89 loc) · 1.88 KB
/
lint-helper.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env sh
USAGE="Usage: $0 [-j] [-c [format|lint]] [-a [format|lint|all]]"
STARTDIR=$(dirname "$0")
FORMAT_PATHS="$(echo "$STARTDIR"/lib/*.cpp) $(echo "$STARTDIR"/include/*.hpp) $(echo "$STARTDIR"/src/*.cpp) $(echo "$STARTDIR"/test/*.cpp)"
CFLAGS="-I$STARTDIR/include -I$STARTDIR/test/catch2 -std=c++20"
RET=0
main() {
if [ "$1" = "format" ]; then
assert_exists "clang-format"
# shellcheck disable=SC2086
EVAL_STR="clang-format $FORMAT_ARGS"
parallel_or_eval "$EVAL_STR" "$EVAL_STR $FORMAT_PATHS" && say_ok
RET=$((RET | $?))
elif [ "$1" = "lint" ]; then
assert_exists "clang-tidy"
parallel_or_eval \
"clang-tidy $TIDY_ARGS {} -- $CFLAGS" \
"clang-tidy $TIDY_ARGS $FORMAT_PATHS -- $CFLAGS" \
&& say_ok
RET=$((RET | $?))
elif [ "$1" = "all" ]; then
main "format"
echo
main "lint"
fi
}
parallel_or_eval() {
if [ -n "$PARALLEL" ]; then
# NB: make sure to NOT use the moreutils version!
assert_exists "parallel"
# shellcheck disable=SC2086
parallel "$1" ::: $FORMAT_PATHS
else
eval "$2"
fi
}
check_opts() {
[ $# -eq 0 ] && fail_msg "$USAGE"
while getopts ":c:a:jh" opt; do
case "${opt}" in
c)
case "$OPTARG" in
format|lint)
FORMAT_ARGS="--verbose --dry-run --Werror"
TIDY_ARGS=""
ACTION="$OPTARG"
;;
*)
fail_msg "$USAGE"
;;
esac
;;
a)
case "$OPTARG" in
format|lint|all)
FORMAT_ARGS="--verbose -i"
TIDY_ARGS="--fix"
ACTION="$OPTARG"
;;
*)
fail_msg "$USAGE"
;;
esac
;;
h)
printf "%s\n" "$USAGE" && exit ;;
j)
PARALLEL=1
;;
*)
fail_msg "$USAGE" ;;
esac
main "$ACTION"
done
shift $((OPTIND-1)) # unnecessary?
exit $RET
}
assert_exists() {
command -v "$1" >/dev/null \
|| fail_msg "No $1??"
}
fail_msg() {
printf "%s\n" "$1" >&2
exit 1
}
say_ok() {
printf "\n\033[0;30;42mOKAY!\033[0m\n"
}
check_opts "$@"