|
4 | 4 |
|
5 | 5 | # Lint all files in the project.
|
6 | 6 | # Expected to be run from the root of the project.
|
7 |
| -# Expected tools to be installed on the system. |
8 |
| -# Usage: ./scripts/lint-all.sh |
| 7 | +# Expected tools to be installed on the system. A pre-check is done to ensure all tools are installed. |
9 | 8 |
|
10 | 9 | set -e
|
11 |
| -set -x |
12 | 10 |
|
| 11 | +# Base directories to lint. |
13 | 12 | BASE_DIRS=(
|
14 | 13 | include
|
15 | 14 | examples
|
16 | 15 | scripts
|
17 | 16 | src
|
18 | 17 | )
|
| 18 | + |
| 19 | +# Print usage information. |
| 20 | +function usage() { |
| 21 | + echo "Usage: $0" |
| 22 | + echo " -h, --help: Display help." |
| 23 | + echo " -a, --all: Lint all files." |
| 24 | + echo " -c, --cpp: Lint C++ files." |
| 25 | + echo " -s, --shell: Lint shell scripts." |
| 26 | + echo " -m, --markdown: Lint Markdown files." |
| 27 | + echo " -y, --yaml: Lint YAML files." |
| 28 | + echo " -C, --cmake: Lint CMake files." |
| 29 | +} |
| 30 | + |
| 31 | +# Parse command line arguments. |
| 32 | +function parse_args() { |
| 33 | + while [[ $# -gt 0 ]]; do |
| 34 | + case $1 in |
| 35 | + -h|--help) |
| 36 | + usage |
| 37 | + exit 0 |
| 38 | + ;; |
| 39 | + -a|--all) |
| 40 | + lint_all_files |
| 41 | + exit 0 |
| 42 | + ;; |
| 43 | + -c|--cpp) |
| 44 | + lint_cpp_files |
| 45 | + exit 0 |
| 46 | + ;; |
| 47 | + -s|--shell) |
| 48 | + lint_shell_files |
| 49 | + exit 0 |
| 50 | + ;; |
| 51 | + -m|--markdown) |
| 52 | + lint_markdown_files |
| 53 | + exit 0 |
| 54 | + ;; |
| 55 | + -y|--yaml) |
| 56 | + lint_yaml_files |
| 57 | + exit 0 |
| 58 | + ;; |
| 59 | + -C|--cmake) |
| 60 | + lint_cmake_files |
| 61 | + exit 0 |
| 62 | + ;; |
| 63 | + *) |
| 64 | + echo "Unknown option: $1" |
| 65 | + usage |
| 66 | + exit 1 |
| 67 | + ;; |
| 68 | + esac |
| 69 | + done |
| 70 | +} |
| 71 | + |
| 72 | +# Check if all linters are installed. |
| 73 | +function check_linters_installed() { |
| 74 | + echo "Checking linters..." |
| 75 | + linters=( |
| 76 | + clang-format |
| 77 | + shellcheck |
| 78 | + cmake-format |
| 79 | + markdownlint |
| 80 | + yamlfmt |
| 81 | + ) |
| 82 | + |
| 83 | + for linter in "${linters[@]}"; do |
| 84 | + echo " -> ${linter}..." |
| 85 | + if ! command -v "${linter}" &> /dev/null; then |
| 86 | + echo "${linter} could not be found." |
| 87 | + exit 1 |
| 88 | + fi |
| 89 | + done |
| 90 | + echo "Checking linters finished: all linters installed." |
| 91 | + echo "" |
| 92 | +} |
| 93 | + |
19 | 94 | # Lint all C++ files in the project.
|
20 |
| -CPP_DIRS=( |
21 |
| - "${BASE_DIRS[@]}" |
22 |
| -) |
23 |
| -find -E "${CPP_DIRS[@]}" -regex '.*\.(h|hpp|hxx|cpp|c|cxx)' | xargs clang-format -i -style=file || echo "clang-format failed." |
24 |
| -echo "All C++ files were linted." |
| 95 | +function lint_cpp_files() { |
| 96 | + echo "Linting C++ files..." |
25 | 97 |
|
26 |
| -# Lint all scripts in the project. |
27 |
| -SHELL_DIRS=( |
28 |
| - "${BASE_DIRS[@]}" |
29 |
| -) |
30 |
| -find -E "${SHELL_DIRS[@]}" -regex '.*\.(sh)' | xargs shellcheck || echo "shellcheck failed." |
31 |
| -echo "All scripts files were linted." |
| 98 | + CPP_DIRS=( |
| 99 | + "${BASE_DIRS[@]}" |
| 100 | + ) |
| 101 | + find -E "${CPP_DIRS[@]}" -regex '.*\.(h|hpp|hxx|cpp|c|cxx)' | xargs clang-format -i -style=file || echo "clang-format failed." |
| 102 | + |
| 103 | + echo "Done." |
| 104 | + echo "" |
| 105 | +} |
| 106 | + |
| 107 | +# Lint all shell files in the project. |
| 108 | +function lint_shell_files() { |
| 109 | + echo "Linting shell files..." |
| 110 | + |
| 111 | + SHELL_DIRS=( |
| 112 | + "${BASE_DIRS[@]}" |
| 113 | + ) |
| 114 | + find -E "${SHELL_DIRS[@]}" -regex '.*\.(sh)' | xargs shellcheck || echo "shellcheck failed." |
| 115 | + |
| 116 | + echo "Done." |
| 117 | + echo "" |
| 118 | +} |
32 | 119 |
|
33 | 120 | # Lint all CMake files in the project.
|
34 |
| -CMAKE_DIRS=( |
35 |
| - CMakeLists.txt |
36 |
| - "${BASE_DIRS[@]}" |
37 |
| - cmake |
38 |
| -) |
39 |
| -find -E "${CMAKE_DIRS[@]}" -regex '.*CMakeLists\.txt' | xargs cmake-format -i || echo "cmake-format failed." |
40 |
| -echo "All CMake files were linted." |
| 121 | +function lint_cmake_files() { |
| 122 | + echo "Linting CMake files..." |
| 123 | + |
| 124 | + CMAKE_DIRS=( |
| 125 | + CMakeLists.txt |
| 126 | + "${BASE_DIRS[@]}" |
| 127 | + cmake |
| 128 | + ) |
| 129 | + find -E "${CMAKE_DIRS[@]}" -regex '.*CMakeLists\.txt' | xargs cmake-format -i || echo "cmake-format failed." |
| 130 | + |
| 131 | + echo "Done." |
| 132 | + echo "" |
| 133 | +} |
41 | 134 |
|
42 | 135 | # Lint all Markdown files in the project.
|
43 |
| -MD_DIRS=( |
44 |
| - README.md |
45 |
| - "${BASE_DIRS[@]}" |
46 |
| - papers/P2988/README.md |
47 |
| -) |
48 |
| -find -E "${MD_DIRS[@]}" -regex '.*\.(md)' | xargs markdownlint -f || echo "markdownlint failed." |
49 |
| -echo "All Markdown files were linted." |
| 136 | +function lint_markdown_files() { |
| 137 | + echo "Linting Markdown files..." |
| 138 | + |
| 139 | + MD_DIRS=( |
| 140 | + README.md |
| 141 | + "${BASE_DIRS[@]}" |
| 142 | + papers/P2988/README.md |
| 143 | + ) |
| 144 | + find -E "${MD_DIRS[@]}" -regex '.*\.(md)' | xargs markdownlint -f || echo "markdownlint failed." |
| 145 | + |
| 146 | + echo "Done." |
| 147 | + echo "" |
| 148 | +} |
50 | 149 |
|
51 | 150 | # Lint all YAML files in the project.
|
52 |
| -YAML_DIRS=( |
53 |
| - .github/ |
54 |
| - "${BASE_DIRS[@]}" |
55 |
| -) |
56 |
| -find -E "${YAML_DIRS[@]}" -regex '.*\.(yml)' | xargs yamlfmt || echo "yamlfmt failed." |
57 |
| -echo "All YAML files were linted." |
| 151 | +function lint_yaml_files() { |
| 152 | + echo "Linting YAML files..." |
| 153 | + |
| 154 | + YAML_DIRS=( |
| 155 | + .github/ |
| 156 | + "${BASE_DIRS[@]}" |
| 157 | + ) |
| 158 | + find -E "${YAML_DIRS[@]}" -regex '.*\.(yml)' | xargs yamlfmt || echo "yamlfmt failed." |
| 159 | + |
| 160 | + echo "Done." |
| 161 | + echo "" |
| 162 | +} |
| 163 | + |
| 164 | +# Lint all files in the project. |
| 165 | +function lint_all_files() { |
| 166 | + check_linters_installed || exit 1 |
| 167 | + |
| 168 | + lint_cpp_files || exit 1 |
| 169 | + lint_shell_files || exit 1 |
| 170 | + lint_cmake_files || exit 1 |
| 171 | + lint_markdown_files || exit 1 |
| 172 | + lint_yaml_files || exit 1 |
| 173 | + |
| 174 | + echo "All linters finished." |
| 175 | +} |
58 | 176 |
|
59 |
| -echo "All linters finished." |
| 177 | +parse_args "$@" |
0 commit comments