-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdart_metrics.sh
121 lines (103 loc) · 3.17 KB
/
dart_metrics.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
#!/bin/bash
# Initialize arrays for storing class relationships
classes=()
parents=()
# Initialize counters for regular Dart files
total_lines=0
total_files=0
total_classes=0
max_file_lines=0
max_file_name=""
# Initialize counters for generated Dart files
gen_total_lines=0
gen_total_files=0
gen_total_classes=0
gen_max_file_lines=0
gen_max_file_name=""
# Function to find classes and their parents
find_inheritance() {
grep -oE "class [a-zA-Z0-9_]+ *(extends|implements) *[a-zA-Z0-9_]+" "$1" | awk '{print $2 " " $NF}'
}
# Function to process Dart files and update counters
process_file() {
local file="$1"
local is_generated="$2"
# Count lines in the current file
file_lines=$(wc -l < "$file")
if [ "$is_generated" = true ]; then
gen_total_files=$((gen_total_files + 1))
gen_total_lines=$((gen_total_lines + file_lines))
if (( file_lines > gen_max_file_lines )); then
gen_max_file_lines=$file_lines
gen_max_file_name="$file"
fi
else
total_files=$((total_files + 1))
total_lines=$((total_lines + file_lines))
if (( file_lines > max_file_lines )); then
max_file_lines=$file_lines
max_file_name="$file"
fi
fi
# Find classes and their inheritance relationships
while read -r class parent; do
classes+=("$class")
parents+=("$parent")
if [ "$is_generated" = true ]; then
gen_total_classes=$((gen_total_classes + 1))
else
total_classes=$((total_classes + 1))
fi
done < <(find_inheritance "$file")
}
# Find and process all Dart files
for file in $(find "$1" -name '*.dart'); do
if [[ "$file" =~ \.g\.dart$|\.freezed\.dart$|\.gr\.dart$|\.gen\.dart$ ]]; then
process_file "$file" true
else
process_file "$file" false
fi
done
# Generate and print the class diagram
if [ ${#classes[@]} -gt 0 ]; then
echo -e "\nClass Diagram (Inheritance Relationships):\n"
for i in "${!parents[@]}"; do
parent="${parents[$i]}"
if [[ ! $class_diagram =~ "$parent" ]]; then
echo "$parent"
for j in "${!parents[@]}"; do
if [[ "${parents[$j]}" == "$parent" ]]; then
echo -e " └── ${classes[$j]}"
fi
done
class_diagram="$class_diagram$parent\n"
fi
done
else
echo -e "\nNo inheritance relationships found in the project."
fi
# Calculate average file line counts
if (( total_files > 0 )); then
average_file_lines=$((total_lines / total_files))
else
average_file_lines=0
fi
if (( gen_total_files > 0 )); then
gen_average_file_lines=$((gen_total_lines / gen_total_files))
else
gen_average_file_lines=0
fi
# Print the summary
echo -e "\nSummary:"
echo "Regular Dart Files:"
echo " Total Files Checked: $total_files"
echo " Total Lines of Code: $total_lines"
echo " Total Classes Found: $total_classes"
echo " Average Lines per File: $average_file_lines"
echo " Max Lines in a Single File: $max_file_lines ($max_file_name)"
echo "Generated Dart Files:"
echo " Total Files Checked: $gen_total_files"
echo " Total Lines of Code: $gen_total_lines"
echo " Total Classes Found: $gen_total_classes"
echo " Average Lines per File: $gen_average_file_lines"
echo " Max Lines in a Single File: $gen_max_file_lines ($gen_max_file_name)"