Skip to content

Commit 8317ab6

Browse files
authored
Merge pull request #15 from SSARCandy/linter
Linter
2 parents fee1074 + a703e32 commit 8317ab6

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

.github/workflows/c-cpp.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ jobs:
1515
steps:
1616
- uses: actions/checkout@v2
1717
- name: apt
18-
run: sudo apt-get install g++ cmake lcov libboost-all-dev
18+
run: sudo apt-get install g++ cmake lcov libboost-all-dev clang-format
1919
- name: submodule
2020
run: git submodule update --init --recursive
21+
- name: lint
22+
run: bash linter.sh
2123
- name: test
2224
run: ./build.sh -t

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ addons:
99
packages: clang-format colordiff lcov libboost-all-dev
1010

1111
script:
12+
- bash linter.sh
1213
- ./build.sh -t
1314
- ./build.sh -c
1415

linter.sh

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
COLOR_REST='\e[0m';
3+
COLOR_BLUE='\e[1;34m';
4+
5+
DIFF_EXE=""
6+
CLANG_FORMATTER=""
7+
AUTO_FIX="false"
8+
9+
if [ "${1}" == '-i' ]; then
10+
AUTO_FIX="true"
11+
fi
12+
13+
if command -v colordiff > /dev/null; then
14+
DIFF_EXE=$(command -v colordiff)
15+
elif command -v diff > /dev/null; then
16+
DIFF_EXE=$(command -v diff)
17+
fi
18+
19+
if command -v clang-format > /dev/null; then
20+
CLANG_FORMATTER=$(command -v clang-format)
21+
else
22+
echo -e "Missing clang-format, please install it with: \n ${COLOR_BLUE}sudo apt-get install clang-format${COLOR_REST}"
23+
exit 1
24+
fi
25+
26+
file_list=$(find ini test \( -name '*.h' -or -name '*.cpp' \))
27+
return_code=0
28+
for f in ${file_list}; do
29+
checker_cmd="${DIFF_EXE} -u <(cat ${f}) <(${CLANG_FORMATTER} ${f} -style=file)"
30+
if ! diff=$(bash -c "${checker_cmd}"); then
31+
echo -e ">>>>> In file ${COLOR_BLUE}${f}${COLOR_REST} <<<<<"
32+
echo "${diff}" | tail -n +4
33+
return_code=1
34+
if [ "${AUTO_FIX}" == "true" ]; then
35+
bash -c "${CLANG_FORMATTER} ${f} -i -style=file"
36+
echo -e ">>>>> Auto-format ${COLOR_BLUE}${f}${COLOR_REST} done. <<<<<"
37+
fi
38+
fi
39+
done
40+
41+
exit ${return_code}

0 commit comments

Comments
 (0)