forked from sld-columbia/esp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
format_modified.sh
213 lines (193 loc) · 5.64 KB
/
format_modified.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/bin/bash
root_dir=$(git rev-parse --show-toplevel)
is_github_actions=false
# Output styles
NC='\033[0m'
BOLD='\033[1m'
GREEN='\033[32m'
RED='\033[31m'
# Display usage instructions
usage() {
echo "ESP format checker ✨🛠️"
echo "Report violations or format files in-place."
echo ""
echo "Usage: $0 [OPTIONS]"
echo " -h Display this help message"
echo ""
echo " -f <file> Fix formatting for file <file>"
echo " -c <file> Check formatting for file <file>"
echo " -a Apply to all"
echo " -g Run as Github Actions workflow or pre-push hook"
echo ""
echo "Examples:"
echo " $0 -fa # Fix all modified files in-place"
echo " $0 -f myfile.py # Fix myfile.py in-place"
echo " $0 -ca # Report violations for all modified files"
echo " $0 -c myfile.py # Report violations for myfile.py"
echo " $0 -g -ca # Report violations as part of a workflow or hook"
exit 1
}
format_file() {
local file_to_format="$1"
local action="$2"
local type="${file_to_format##*.}"
# Parse file extension type and decide which formatting tool to use
# Add the corresponding flags based on whether we are formatting or checking
case "$action" in
Formatt)
case "$type" in
c | h | cpp | hpp)
clang_format_edit="-i" ;;
py)
autopep8_edit="-i" ;;
sv | v)
verible_edit="--inplace" ;;
vhd)
vsg_edit="--fix" ;;
esac
;;
Check)
case "$type" in
c | h | cpp | hpp)
clang_format_edit="--dry-run" ;;
py)
autopep8_edit="--list-fixes" ;;
sv | v)
verible_edit="--verify";;
vhd)
;;
esac
;;
*)
echo "Unknown action: $action" >&2
usage
;;
esac
# Apply formatting tool based on file extension type
local output
case "$type" in
c | h | cpp | hpp)
# Format with clang-format-10
output=$(clang-format-10 $clang_format_edit "$file_to_format" 2>&1);;
py)
# Format with autopep8
output=$(python3 -m autopep8 $autopep8_edit -a -a "$file_to_format" 2>&1);;
sv | v)
# Format with verible
output=$(verible-verilog-format $verible_edit --port_declarations_alignment=preserve -assignment_statement_alignment=align --indentation_spaces=4 "$file_to_format" 2>&1) ;;
vhd)
# Format with vhdl-style-guide
output=$(vsg -f "$file_to_format" $vsg_edit -c ~/esp/vhdl-style-guide.yaml 2>&1) ;;
esac
# If no errors were encountered while formatting, return SUCCESS
# Else return FAILED
if [ $? -eq 0 ] && ! echo "$output" | grep -qi "warning"; then
echo -e "${GREEN}SUCCESS${NC}"
return 0
else
echo -e "${RED}FAILED${NC}"
echo "$output" | sed 's/^/ /'
echo ""
return 1
fi
}
# Based on the command-line flags, determine whether to format in-place or check
# Call the appropriate action
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-f)
if [[ -z $2 || $2 == -* ]]; then
echo "Option -f requires an argument." >&2
usage
fi
action="Formatt"
file_to_format="$2"
shift
;;
-fa)
action="Formatt"
all_files=true
;;
-c)
if [[ -z $2 || $2 == -* ]]; then
echo "Option -c requires an argument." >&2
usage
fi
action="Check"
file_to_format="$2"
shift
;;
-ca)
action="Check"
all_files=true
;;
-g)
is_github_actions=true
;;
-h|--help)
usage
;;
*)
echo "Unknown option: $1" >&2
usage
;;
esac
shift
done
# User must specify a file to format if the -f flag is called
if [ -n "$file_to_format" ]; then
if [ ! -f "$file_to_format" ]; then
echo "$0: Error: File '$file_to_format' not found." >&2
exit 1
fi
echo -n "$action""ing $file_to_format..."
if format_file "$file_to_format" "$action"; then
echo -e "✨ $action""ing done!"
exit 0
else
echo -e "❌ $action""ing failed!"
exit 1
fi
exit 0
fi
# Check all modified files of the file types: C/C++, Python, Verilog/SystemVerilog, VHDL
if [ "$all_files" = true ]; then
modified_files=$(git status --porcelain | grep -E '^ M|^??' | awk '$2 ~ /\.(c|h|cpp|hpp|py|v|sv|vhd)$/ {print $2}')
if [ "$is_github_actions" = true ]; then
modified_files=$(git diff --name-only HEAD^..HEAD | grep -E '\.(c|h|cpp|hpp|py|v|sv|vhd)$')
fi
if [ -z "$modified_files" ]; then
echo -e "🔍 No modified files found."
exit 0
fi
# List the number of modified files found
modified_count=$(echo "$modified_files" | wc -l)
echo -e "🔍 Found $modified_count modified files:"
echo ""
for file in $modified_files; do
echo " - $file"
done
echo ""
modified_files=$(echo "$modified_files" | sed "s|^|$root_dir/|")
error_files=""
success_files=""
# Format/check each file in place with the appropriate tool
for file in $modified_files; do
echo -n "$action""ing $(basename "$file")..."
if ! format_file "$file" "$action"; then
error_files="$error_files $file"
fi
done
# Final pass/fail directive
echo ""
if [ -n "$error_files" ]; then
echo -e "❌ $action""ing failed!"
exit 1
else
echo -e "✨ $action""ing done!"
exit 0
fi
else
usage
fi