forked from UBC-Thunderbots/Software
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlint_and_format.sh
More file actions
executable file
·152 lines (123 loc) · 4.91 KB
/
Copy pathlint_and_format.sh
File metadata and controls
executable file
·152 lines (123 loc) · 4.91 KB
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/bin/bash
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# This script is used for running linting and formatting checks in CI
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~#
# The version of the clang executable to use
export CLANG_VERSION=10.0
# The directory this script is in
CURR_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# The path to clang-format
export CLANG_BIN=/opt/tbotspython/bin/clang-format
# The root bazel directory
BAZEL_ROOT_DIR="$CURR_DIR/../src"
# Extensions to check formatting for clang-format
CLANG_FORMAT_EXTENSIONS=(h cpp c cc hpp tpp proto)
# Function to run clang-format
function run_clang_format () {
printf "Running clang-format over all files...\n\n"
cd $BAZEL_ROOT_DIR
# Generate extension string
# Formatted as -iname *.EXTENSION -o
EXTENSION_STRING=""
for value in "${CLANG_FORMAT_EXTENSIONS[@]}"
do
EXTENSION_STRING="$EXTENSION_STRING -name *.$value -o"
done
# Find all the files that we want to format, and pass them to
# clang-format as arguments
# We remove the last -o flag from the extension string
find $CURR_DIR/../src/ ${EXTENSION_STRING::-2} \
| xargs -I{} -n1000 $CLANG_BIN -i -style=file:$CURR_DIR/../.clang-format
if [[ "$?" != 0 ]]; then
printf "\n***Failed to run clang-format over all files!***\n\n"
exit 1
fi
}
# Function to run bazel formatting
function run_bazel_formatting () {
printf "Running bazel buildifier to format all bazel BUILD files...\n\n"
cd $BAZEL_ROOT_DIR
bazel run //starlark/buildifier:buildifier.fix
if [[ "$?" != 0 ]]; then
printf "\n***Failed to format bazel BUILD files!***\n\n"
exit 1
else
# extra new line to make it look nicer
printf "\n"
fi
}
# Function to run ruff python linting and formatting
function run_ruff() {
printf "Running ruff to lint and format Python files...\n\n"
/opt/tbotspython/bin/python3 -m ruff check $BAZEL_ROOT_DIR --fix-only --extend-select D
/opt/tbotspython/bin/python3 -m ruff format $BAZEL_ROOT_DIR
if [[ "$?" != 0 ]]; then
printf "\n***Failed to lint/format Python files!***\n\n"
exit 1
fi
}
function run_code_spell(){
mkdir -p $CURR_DIR/dictionary
http_code=$(curl -sw '%{http_code}' https://raw.githubusercontent.com/codespell-project/codespell/v1.14.0/codespell_lib/data/dictionary.txt --output $CURR_DIR/dictionary/dictionary.txt)
if [[ "$http_code" != 200 ]]; then
printf "\n***Failed to download codespell dictionary!***\n\n"
if [ ! -f $CURR_DIR/dictionary/edited_dictionary.txt ]; then
printf "\n***Failed to load codespell dictonary from cache!***\n\n"
exit 1
fi
printf "Loading Codespell dictionary from cache!\n\n"
else
sed "/atleast/d" $CURR_DIR/dictionary/dictionary.txt > $CURR_DIR/dictionary/edited_dictionary.txt #removing spell fixes that include the word 'atleast' from codespell dictionary
rm $CURR_DIR/dictionary/dictionary.txt #remove the original dictionary.txt
fi
printf "Fixing spelling...\n\n"
cd $CURR_DIR/../src/software && codespell -w --skip="1,2,0" -D $CURR_DIR/dictionary/edited_dictionary.txt -I $CURR_DIR/codespell_ignored_words.txt # Skip binaries
cd $CURR_DIR/../src/shared && codespell -w -D $CURR_DIR/dictionary/edited_dictionary.txt
cd $CURR_DIR/../docs && codespell -w --skip="*.png,*.svg" -D $CURR_DIR/dictionary/edited_dictionary.txt # Skip images
if [[ "$?" != 0 ]]; then
printf "\n***Failed to fix spelling!***\n\n"
exit 1
fi
}
function run_md_toc() {
printf "Adding table of contents to Markdown files...\n\n"
for file in $CURR_DIR/../docs/*.md
do
/opt/tbotspython/bin/python3 -m md_toc --in-place --no-list-coherence --skip-lines 1 github $file
done
}
function run_git_diff_check(){
printf "Checking for merge conflict markers...\n\n"
cd $CURR_DIR && git -c "core.whitespace=-trailing-space" --no-pager diff --check
if [[ "$?" != 0 ]]; then
printf "***Please fix merge conflict markers!***\n\n"
exit 1
fi
}
function run_eof_new_line(){
printf "Adding missing new lines to end of files...\n\n"
# adds missing new lines to the end of non-binary files
cd $CURR_DIR/../ && git grep -zIl '' | while IFS= read -rd '' f; do tail -c1 < "$f" | read -r _ || echo >> "$f"; done
if [[ "$?" != 0 ]]; then
printf "***Failed to add missing new lines!***\n\n"
exit 1
fi
}
function run_ansible_lint(){
printf "Running ansible-lint...\n\n"
/opt/tbotspython/bin/ansible-lint $CURR_DIR/../src/software/embedded/ansible/**/*.yml --fix
if [[ "$?" != 0 ]]; then
printf "\n***Failed to lint and format Ansible files!***\n\n"
exit 1
fi
}
# Run formatting
run_code_spell
run_clang_format
run_bazel_formatting
run_ruff
run_md_toc
run_eof_new_line
run_git_diff_check
run_ansible_lint
exit 0