-
Notifications
You must be signed in to change notification settings - Fork 7
/
linter.sh
41 lines (35 loc) · 1.05 KB
/
linter.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
#!/bin/bash
COLOR_REST='\e[0m';
COLOR_BLUE='\e[1;34m';
DIFF_EXE=""
CLANG_FORMATTER=""
AUTO_FIX="false"
if [ "${1}" == '-i' ]; then
AUTO_FIX="true"
fi
if command -v colordiff > /dev/null; then
DIFF_EXE=$(command -v colordiff)
elif command -v diff > /dev/null; then
DIFF_EXE=$(command -v diff)
fi
if command -v clang-format > /dev/null; then
CLANG_FORMATTER=$(command -v clang-format)
else
echo -e "Missing clang-format, please install it with: \n ${COLOR_BLUE}sudo apt-get install clang-format${COLOR_REST}"
exit 1
fi
file_list=$(find ini test \( -name '*.h' -or -name '*.cpp' \))
return_code=0
for f in ${file_list}; do
checker_cmd="${DIFF_EXE} -u <(cat ${f}) <(${CLANG_FORMATTER} ${f} -style=file)"
if ! diff=$(bash -c "${checker_cmd}"); then
echo -e ">>>>> In file ${COLOR_BLUE}${f}${COLOR_REST} <<<<<"
echo "${diff}" | tail -n +4
return_code=1
if [ "${AUTO_FIX}" == "true" ]; then
bash -c "${CLANG_FORMATTER} ${f} -i -style=file"
echo -e ">>>>> Auto-format ${COLOR_BLUE}${f}${COLOR_REST} done. <<<<<"
fi
fi
done
exit ${return_code}