forked from ilai-deutel/kibi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount_loc.sh
executable file
·47 lines (35 loc) · 1.28 KB
/
count_loc.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
#!/usr/bin/env bash
# Count the number of lines of code in kibi source code, display a result
# table. Exit code 0 will be returned if the total LOC count is below 1024.
# The lines of codes are counted using `tokei`, after removing the following
# from the code:
# * Clippy directives
# * Anything after #[cfg(test)]
set -euo pipefail
declare -i file_loc total_loc left_col_width
declare -A file_locs
paths=("$(dirname "${BASH_SOURCE[0]:-$0}")/src"/*.rs)
left_col_width=6
total_loc=0
for path in "${paths[@]}"; do
if (( ${#path} > left_col_width )); then left_col_width=${#path}; fi;
tempfile=$(mktemp --suffix .rs)
# Ignore Clippy directives
code=$(grep -v -P '^\s*#!?\[(?:allow|warn|deny)\(clippy::' "${path}")
# Ignore everything after #[cfg(test)]
echo "${code%'#[cfg(test)]'*}" > "${tempfile}"
file_loc=$(tokei "${tempfile}" -t=Rust -o json | jq .Rust.code)
rm "${tempfile}"
file_locs[${path}]=${file_loc}
total_loc+=${file_loc}
done
for path in "${paths[@]}"; do
printf "%-${left_col_width}s %${#total_loc}s\n" "${path}" "${file_locs[${path}]}"
done
printf "%b%-${left_col_width}s %i %b" '\x1b[1m' 'Total' "${total_loc}" '\x1b[0m'
if [[ ${total_loc} -gt 1024 ]]; then
echo -e ' \x1b[31m(> 1024)\x1b[0m'
exit 1
else
echo -e ' \x1b[32m(≤ 1024)\x1b[0m'
fi