Debian Codegraph @

build-aux/checks.sh

raw
1 #!/bin/bash
2 # Source: https://gitlab.gnome.org/GNOME/fractal/-/blob/ee6791518094c9c72a0a9630e334f73264f2aff9/scripts/checks.sh
3 # Slightly customized to make it easier to reuse for other projects.
4
5 resources="data/de.haeckerfelix.Fragments.gresource.xml.in"
6 ui_files="data/gtk"
7
8 export LC_ALL=C
9
10 # Usage info
11 show_help() {
12 cat << EOF
13 Run conformity checks on the current Rust project.
14
15 If a dependency is not found, helps the user to install it.
16
17 USAGE: ${0##*/} [OPTIONS]
18
19 OPTIONS:
20 -f, --force-install Install missing dependencies without asking
21 -v, --verbose Use verbose output
22 -h, --help Display this help and exit
23
24 ERROR CODES:
25 1 Check failed
26 2 Missing dependency
27 EOF
28 }
29
30 # Style helpers
31 act="\e[1;32m"
32 err="\e[1;31m"
33 pos="\e[32m"
34 neg="\e[31m"
35 res="\e[0m"
36
37 # Common styled strings
38 Installing="${act}Installing${res}"
39 Checking=" ${act}Checking${res}"
40 Failed=" ${err}Failed${res}"
41 error="${err}error:${res}"
42 invalid="${neg}Invalid input${res}"
43 ok="${pos}ok${res}"
44 fail="${neg}fail${res}"
45
46 # Initialize variables
47 force_install=0
48 verbose=0
49
50 # Helper functions
51 # Sort to_sort in natural order.
52 sort() {
53 local size=${#to_sort[@]}
54 local swapped=0;
55
56 for (( i = 0; i < $size-1; i++ ))
57 do
58 swapped=0
59 for ((j = 0; j < $size-1-$i; j++ ))
60 do
61 if [[ "${to_sort[$j]}" > "${to_sort[$j+1]}" ]]
62 then
63 temp="${to_sort[$j]}";
64 to_sort[$j]="${to_sort[$j+1]}";
65 to_sort[$j+1]="$temp";
66 swapped=1;
67 fi
68 done
69
70 if [[ $swapped -eq 0 ]]; then
71 break;
72 fi
73 done
74 }
75
76 # Remove common entries in to_diff1 and to_diff2.
77 diff() {
78 for i in ${!to_diff1[@]}; do
79 for j in ${!to_diff2[@]}; do
80 if [[ "${to_diff1[$i]}" == "${to_diff2[$j]}" ]]; then
81 unset to_diff1[$i]
82 unset to_diff2[$j]
83 break
84 fi
85 done
86 done
87 }
88
89 # Check if rustup is available.
90 # Argument:
91 # '-i' to install if missing.
92 check_rustup() {
93 if ! which rustup &> /dev/null; then
94 if [[ "$1" == '-i' ]]; then
95 echo -e "$Installing rustup…"
96 curl https://sh.rustup.rs -sSf | sh -s -- -y --default-toolchain nightly
97 export PATH=$PATH:$HOME/.cargo/bin
98 if ! which rustup &> /dev/null; then
99 echo -e "$Failed to install rustup"
100 exit 2
101 fi
102 else
103 exit 2
104 fi
105 fi
106 }
107
108 # Install cargo via rustup.
109 install_cargo() {
110 check_rustup -i
111 if ! which cargo >/dev/null 2>&1; then
112 echo -e "$Failed to install cargo"
113 exit 2
114 fi
115 }
116
117 # Check if cargo is available. If not, ask to install it.
118 check_cargo() {
119 if ! which cargo >/dev/null 2>&1; then
120 echo "Unable to find cargo for pre-commit checks"
121
122 if [[ $force_install -eq 1 ]]; then
123 install_cargo
124 elif [ ! -t 1 ]; then
125 exit 2
126 elif check_rustup; then
127 echo -e "$error rustup is installed but the cargo command isn't available"
128 exit 2
129 else
130 echo ""
131 echo "y: Install cargo via rustup"
132 echo "N: Don't install cargo and abort checks"
133 echo ""
134 while true; do
135 echo -n "Install cargo? [y/N]: "; read yn < /dev/tty
136 case $yn in
137 [Yy]* )
138 install_cargo
139 break
140 ;;
141 [Nn]* | "" )
142 exit 2
143 ;;
144 * )
145 echo $invalid
146 ;;
147 esac
148 done
149 fi
150 fi
151
152 if [[ $verbose -eq 1 ]]; then
153 echo ""
154 rustc -Vv && cargo +nightly -Vv
155 fi
156 }
157
158 # Install rustfmt with rustup.
159 install_rustfmt() {
160 check_rustup -i
161
162 echo -e "$Installing rustfmt…"
163 rustup component add --toolchain nightly rustfmt
164 if ! cargo +nightly fmt --version >/dev/null 2>&1; then
165 echo -e "$Failed to install rustfmt"
166 exit 2
167 fi
168 }
169
170 # Run rustfmt to enforce code style.
171 run_rustfmt() {
172 if ! cargo +nightly fmt --version >/dev/null 2>&1; then
173 if [[ $force_install -eq 1 ]]; then
174 install_rustfmt
175 elif [ ! -t 1 ]; then
176 echo "Unable to check code style, because rustfmt could not be run"
177 exit 2
178 else
179 echo "Rustfmt is needed to check code style, but it isn’t available"
180 echo ""
181 echo "y: Install rustfmt via rustup"
182 echo "N: Don't install rustfmt and abort checks"
183 echo ""
184 while true; do
185 echo -n "Install rustfmt? [y/N]: "; read yn < /dev/tty
186 case $yn in
187 [Yy]* )
188 install_rustfmt
189 break
190 ;;
191 [Nn]* | "" )
192 exit 2
193 ;;
194 * )
195 echo $invalid
196 ;;
197 esac
198 done
199 fi
200 fi
201
202 echo -e "$Checking code style…"
203
204 if [[ $verbose -eq 1 ]]; then
205 echo ""
206 cargo +nightly fmt --version
207 echo ""
208 fi
209
210 if ! cargo +nightly fmt --all -- --check; then
211 echo -e " Checking code style result: $fail"
212 echo "Please fix the above issues, either manually or by running: cargo fmt --all"
213 exit 1
214 else
215 echo -e " Checking code style result: $ok"
216 fi
217 }
218
219 # Install cargo-fmt with cargo.
220 install_cargo_deny() {
221 echo -e "$Installing cargo-deny…"
222 cargo install cargo-deny
223 if ! cargo-deny --version >/dev/null 2>&1; then
224 echo -e "$Failed to install cargo-deny"
225 exit 2
226 fi
227 }
228
229 # Run cargo-fmt to lint dependencies.
230 run_cargo_deny() {
231 if ! cargo-deny --version >/dev/null 2>&1; then
232 if [[ $force_install -eq 1 ]]; then
233 install_cargo_deny
234 elif [ ! -t 1 ]; then
235 echo "Unable to lint dependencies, because cargo-deny could not be run"
236 exit 2
237 else
238 echo "Cargo-deny is needed to lint dependencies, but it isn’t available"
239 echo ""
240 echo "y: Install cargo-deny via cargo"
241 echo "N: Don't install cargo-deny and abort checks"
242 echo ""
243 while true; do
244 echo -n "Install cargo-deny? [y/N]: "; read yn < /dev/tty
245 case $yn in
246 [Yy]* )
247 install_cargo_deny
248 break
249 ;;
250 [Nn]* | "" )
251 exit 2
252 ;;
253 * )
254 echo $invalid
255 ;;
256 esac
257 done
258 fi
259 fi
260
261 echo -e "$Checking dependencies…"
262
263 if [[ $verbose -eq 1 ]]; then
264 echo ""
265 cargo-deny --version
266 echo ""
267 fi
268
269 echo -n " "
270
271 if ! cargo-deny --log-level error check; then
272 echo -e " Checking dependencies: $fail"
273 echo "Please fix the above issues"
274 exit 1
275 fi
276 }
277
278 # Install typos with cargo.
279 install_typos() {
280 echo -e "$Installing typos…"
281 cargo install typos-cli
282 if ! typos --version >/dev/null 2>&1; then
283 echo -e "$Failed to install typos"
284 exit 2
285 fi
286 }
287
288 # Run typos to check for spelling mistakes.
289 run_typos() {
290 if ! typos --version >/dev/null 2>&1; then
291 if [[ $force_install -eq 1 ]]; then
292 install_typos
293 elif [ ! -t 1 ]; then
294 echo "Unable to check spelling mistakes, because typos could not be run"
295 exit 2
296 else
297 echo "Typos is needed to check spelling mistakes, but it isn’t available"
298 echo ""
299 echo "y: Install typos via cargo"
300 echo "N: Don't install typos and abort checks"
301 echo ""
302 while true; do
303 echo -n "Install typos? [y/N]: "; read yn < /dev/tty
304 case $yn in
305 [Yy]* )
306 install_typos
307 break
308 ;;
309 [Nn]* | "" )
310 exit 2
311 ;;
312 * )
313 echo $invalid
314 ;;
315 esac
316 done
317 fi
318 fi
319
320 echo -e "$Checking spelling mistakes…"
321
322 if [[ $verbose -eq 1 ]]; then
323 echo ""
324 typos --version
325 echo ""
326 fi
327
328 if ! typos --color always; then
329 echo -e " Checking spelling mistakes result: $fail"
330 echo "Please fix the above issues, either manually or by running: typos -w"
331 exit 1
332 else
333 echo -e " Checking spelling mistakes result: $ok"
334 fi
335 }
336
337 # Check if files in POTFILES.in are correct.
338 #
339 # This checks, in that order:
340 # - All files exist
341 # - All files with translatable strings are present and only those
342 # - Files are sorted alphabetically
343 #
344 # This assumes the following:
345 # - POTFILES is located at 'po/POTFILES.in'
346 # - UI (Glade) files are located in $ui_files and use 'translatable="yes"'
347 # - Rust files are located in 'src' and use '*gettext' methods or macros
348 check_potfiles() {
349 echo -e "$Checking po/POTFILES.in…"
350
351 local ret=0
352
353 # Check that files in POTFILES exist.
354 while read -r line; do
355 if [[ -n $line && ${line::1} != '#' ]]; then
356 if [[ ! -f $line ]]; then
357 echo -e "$error File '$line' in POTFILES.in does not exist"
358 ret=1
359 fi
360 if [[ ${line:(-3):3} == '.ui' ]]; then
361 ui_potfiles+=($line)
362 elif [[ ${line:(-3):3} == '.rs' ]]; then
363 rs_potfiles+=($line)
364 fi
365 fi
366 done < po/POTFILES.in
367
368 if [[ ret -eq 1 ]]; then
369 echo -e " Checking po/POTFILES.in result: $fail"
370 echo "Please fix the above issues"
371 exit 1
372 fi
373
374 # Get UI files with 'translatable="yes"'.
375 ui_files=(`grep -lIr 'translatable="yes"' $ui_files/*`)
376
377 # Get Rust files with regex 'gettext(_f)?\(', except `src/i18n.rs`.
378 rs_files=(`grep -lIrE 'gettext(_f)?\(' --exclude=i18n.rs src/*`)
379
380 # Get Rust files with regex '.*i18n.*\(', except `src/i18n.rs`.
381 rs_files=(`grep -lIrE '.*i18n.*\(' --exclude=i18n.rs src/*`)
382
383 # Get Rust files with macros, regex 'gettext!\('.
384 rs_macro_files=(`grep -lIrE 'gettext!\(' src/*`)
385
386 # Remove common files
387 to_diff1=("${ui_potfiles[@]}")
388 to_diff2=("${ui_files[@]}")
389 diff
390 ui_potfiles=("${to_diff1[@]}")
391 ui_files=("${to_diff2[@]}")
392
393 to_diff1=("${rs_potfiles[@]}")
394 to_diff2=("${rs_files[@]}")
395 diff
396 rs_potfiles=("${to_diff1[@]}")
397 rs_files=("${to_diff2[@]}")
398
399 potfiles_count=$((${#ui_potfiles[@]} + ${#rs_potfiles[@]}))
400 if [[ $potfiles_count -eq 1 ]]; then
401 echo ""
402 echo -e "$error Found 1 file in POTFILES.in without translatable strings:"
403 ret=1
404 elif [[ $potfiles_count -ne 0 ]]; then
405 echo ""
406 echo -e "$error Found $potfiles_count files in POTFILES.in without translatable strings:"
407 ret=1
408 fi
409 for file in ${ui_potfiles[@]}; do
410 echo $file
411 done
412 for file in ${rs_potfiles[@]}; do
413 echo $file
414 done
415
416 let files_count=$((${#ui_files[@]} + ${#rs_files[@]}))
417 if [[ $files_count -eq 1 ]]; then
418 echo ""
419 echo -e "$error Found 1 file with translatable strings not present in POTFILES.in:"
420 ret=1
421 elif [[ $files_count -ne 0 ]]; then
422 echo ""
423 echo -e "$error Found $files_count files with translatable strings not present in POTFILES.in:"
424 ret=1
425 fi
426 for file in ${ui_files[@]}; do
427 echo $file
428 done
429 for file in ${rs_files[@]}; do
430 echo $file
431 done
432
433 let rs_macro_count=$((${#rs_macro_files[@]}))
434 if [[ $rs_macro_count -eq 1 ]]; then
435 echo ""
436 echo -e "$error Found 1 Rust file that uses a gettext-rs macro, use the corresponding i18n method instead:"
437 ret=1
438 elif [[ $rs_macro_count -ne 0 ]]; then
439 echo ""
440 echo -e "$error Found $rs_macro_count Rust files that use a gettext-rs macro, use the corresponding i18n method instead:"
441 ret=1
442 fi
443 for file in ${rs_macro_files[@]}; do
444 echo $file
445 done
446
447 if [[ ret -eq 1 ]]; then
448 echo ""
449 echo -e " Checking po/POTFILES.in result: $fail"
450 echo "Please fix the above issues"
451 exit 1
452 fi
453
454 # Check sorted alphabetically
455 to_sort=("${potfiles[@]}")
456 sort
457 for i in ${!potfiles[@]}; do
458 if [[ "${potfiles[$i]}" != "${to_sort[$i]}" ]]; then
459 echo -e "$error Found file '${potfiles[$i]}' before '${to_sort[$i]}' in POTFILES.in"
460 ret=1
461 break
462 fi
463 done
464
465 if [[ ret -eq 1 ]]; then
466 echo ""
467 echo -e " Checking po/POTFILES.in result: $fail"
468 echo "Please fix the above issues"
469 exit 1
470 else
471 echo -e " Checking po/POTFILES.in result: $ok"
472 fi
473 }
474
475 # Check if files in gresource xml are sorted alphabetically.
476 check_resources() {
477 echo -e "$Checking $resources…"
478
479 local ret=0
480
481 # Get files.
482 regex="<file .*>(.*)</file>"
483 while read -r line; do
484 if [[ $line =~ $regex ]]; then
485 files+=("${BASH_REMATCH[1]}")
486 fi
487 done < $resources
488
489 # Check sorted alphabetically
490 to_sort=("${files[@]}")
491 sort
492 for i in ${!files[@]}; do
493 if [[ "${files[$i]}" != "${to_sort[$i]}" ]]; then
494 echo -e "$error Found file '${files[$i]#src/}' before '${to_sort[$i]#src/}' in gresource xml"
495 ret=1
496 break
497 fi
498 done
499
500 if [[ ret -eq 1 ]]; then
501 echo ""
502 echo -e " Checking $resources result: $fail"
503 echo "Please fix the above issues"
504 exit 1
505 else
506 echo -e " Checking $resources result: $ok"
507 fi
508 }
509
510 # Check arguments
511 while [[ "$1" ]]; do case $1 in
512 -f | --force-install )
513 force_install=1
514 ;;
515 -v | --verbose )
516 verbose=1
517 ;;
518 -h | --help )
519 show_help
520 exit 0
521 ;;
522 *)
523 show_help >&2
524 exit 1
525 esac; shift; done
526
527 # Run
528 check_cargo
529 echo ""
530 run_rustfmt
531 echo ""
532 run_cargo_deny
533 echo ""
534 run_typos
535 echo ""
536 check_potfiles
537 echo ""
538 check_resources
539 echo ""