-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinter.sh
executable file
·244 lines (210 loc) · 6.16 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/usr/bin/env bash
# MKDEV - Boilerplates for isolated development environments
# Copyright (C) 2024 Vinícius Moraes <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Linter to ensure compliance with guidelines, see "./boilerplates/README.md".
# shellcheck disable=SC2016
shopt -s extglob
failure_count=0
assert_files() {
local dir="$1"
local files=("README.md" "Containerfile" "Makefile")
for file in "${files[@]}"; do
if [[ ! -e "$dir/$file" ]]; then
printf "%s: Missing file: %s.\n" "$dir" "$file"
((failure_count++))
fi
done
}
makefiles_assert_sections() {
local file="$1"
local -A sections
sections=(
["Host"]="# Host targets/commands"
["Container"]="# Container targets/commands"
[".PHONY"]=".PHONY: dev start stop clean serestore"
[".PHONY"]=".PHONY: lint test build run deploy debug distclean"
)
for sec in "${!sections[@]}"; do
local expected="${sections[$sec]}"
if ! grep -qe "^${expected}$" "$file" 2>/dev/null; then
printf "%s: missing or incorrect section: %s. Expected: %s.\n" \
"$file" "$sec" "$expected"
((failure_count++))
fi
done
}
makefiles_assert_infos() {
local file="$1"
local -A infos
infos=(
["dev"]='$(info Building development container image...)'
["start"]='$(info Starting development container...)'
["stop"]='$(info Stopping development container...)'
["clean"]='$(info Removing development container and image...)'
["serestore"]='$(info Restoring project SELinux context and permissions...)'
["lint"]='$(info Running linters...)'
["test"]='$(info Running tests...)'
["build"]='$(info Building...)'
["run"]='$(info Running...)'
["deploy"]='$(info Deploying...)'
["debug"]='$(info Debugging tasks...)'
["distclean"]='$(info Cleaning artifacts...)'
)
for inf in "${!infos[@]}"; do
local expected="${infos[$inf]}"
if ! grep -qe "${expected}$" "$file" 2>/dev/null; then
printf "%s: missing or incorrect info: %s. Expected: %s.\n" \
"$file" "$inf" "$expected"
((failure_count++))
fi
done
}
makefiles_assert_targets() {
local file="$1"
local -A targets
targets=(
["dev"]="dev:"
["start"]="start:"
["stop"]="stop:"
["clean"]="clean: distclean"
["serestore"]="serestore:"
["lint"]="lint:"
["test"]="test: lint"
["build"]="build: test"
["run"]="run: build"
["deploy"]="deploy: build"
["debug"]="debug: test"
["distclean"]="distclean:"
)
for tgt in "${!targets[@]}"; do
local expected="${targets[$tgt]}"
if ! grep -qe "^${expected}$" "$file" 2>/dev/null; then
printf "%s: missing or incorrect target: %s. Expected: %s.\n" \
"$file" "$tgt" "$expected"
((failure_count++))
fi
done
}
makefiles_assert_variables() {
local file="$1"
local type="$2"
local -A variables
if [[ "$type" == "project" ]]; then
variables=(
["PROJECT_NAME"]="changeme"
["CONTAINER_ENGINE"]="changeme"
["__USER"]='$(or $(USER),$(shell whoami))'
)
elif [[ "$type" == "omni" ]]; then
variables=(
["OMNI_NAME"]="changeme"
["CONTAINER_ENGINE"]="changeme"
["__USER"]='$(or $(USER),$(shell whoami))'
["__AFFIX"]='omni-$(OMNI_NAME)'
)
else
printf "Unknown type: %s\n" "$type" >&2
exit 1
fi
for var in "${!variables[@]}"; do
local expected="${variables[$var]}"
if ! grep -qe "^${var} = ${expected}$" "$file" 2>/dev/null; then
printf "%s: missing or incorrect variable: %s. Expected: %s.\n" \
"$file" "$var" "$expected"
((failure_count++))
fi
done
}
containerfiles_assert_instructions() {
local file="$1"
local -A instructions
instructions=(
["ARG"]="USERNAME=mkdev"
["LABEL"]="mkdev.name="
["WORKDIR"]='/home/$USERNAME/workspace'
["USER"]='$USERNAME'
["CMD"]='["/bin/bash", "-l"]'
)
for ins in "${!instructions[@]}"; do
local expected="${instructions[$ins]}"
if ! grep -qe "^${ins} ${expected}" "$file" 2>/dev/null; then
printf "%s: missing or incorrect instruction: %s. Expected: %s.\n" \
"$file" "$ins" "$expected"
((failure_count++))
fi
done
}
ci_output() {
if [ -n "$CI" ]; then
local status_linter="$1"
if [[ "$status_linter" == "failure" ]]; then
local word="issue"
(( failure_count > 1 )) && word+="s"
printf "::error title=%s::checks failed: %d %s found.\n" \
"$0" \
"$failure_count" \
"$word"
fi
printf "checks=%s\n" "$status_linter" >> "$GITHUB_OUTPUT"
fi
}
check_failures() {
if (( failure_count > 0 )); then
local word="issue"
(( failure_count == 1 )) && word+="s"
printf "%s: checks failed: %d %s found.\n" \
"$0" \
"$failure_count" \
"$word"
ci_output "failure"
exit 1
fi
}
# Critical path
for dir in ./boilerplates/*/*; do
assert_files "$dir"
done
for mk in ./*.mk; do
makefiles_assert_sections "$mk"
makefiles_assert_infos "$mk"
makefiles_assert_targets "$mk"
if [[ "$mk" == "./Dev.mk" ]]; then
makefiles_assert_variables "$mk" "project"
elif [[ "$mk" == "./Omni.mk" ]]; then
makefiles_assert_variables "$mk" "omni"
fi
done
check_failures
# Makefiles
for mk in ./boilerplates/*/*/Makefile; do
makefiles_assert_sections "$mk"
makefiles_assert_infos "$mk"
makefiles_assert_targets "$mk"
done
for mk in ./boilerplates/!(*omni)/*/Makefile; do
makefiles_assert_variables "$mk" "project"
done
for mk in ./boilerplates/omni/*/Makefile; do
makefiles_assert_variables "$mk" "omni"
done
# Containerfiles
for cf in ./boilerplates/*/*/Containerfile; do
containerfiles_assert_instructions "$cf"
done
check_failures
printf "%s: all checks passed successfully.\n" "$0"
ci_output "success"
exit 0