-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalyze-project-optimized.sh
More file actions
executable file
·366 lines (310 loc) · 13.3 KB
/
analyze-project-optimized.sh
File metadata and controls
executable file
·366 lines (310 loc) · 13.3 KB
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/bin/bash
# Optimized Project Analysis Script
# Provides comprehensive statistics about project files and structure
# Optimized for performance while maintaining accuracy
set -e
PROJECT_ROOT="$(pwd)"
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M-%S")
OUTPUT_FILE="project-analysis-${TIMESTAMP}.txt"
echo "🔍 ModularHomeInventory Project Analysis (Optimized)"
echo "===================================================="
echo "Generated: $(date)"
echo "Project Root: $PROJECT_ROOT"
echo ""
# Function to format file sizes
format_size() {
local size=$1
if [ $size -gt 1073741824 ]; then
printf "%.1fGB" $(echo "scale=1; $size / 1073741824" | bc -l)
elif [ $size -gt 1048576 ]; then
printf "%.1fMB" $(echo "scale=1; $size / 1048576" | bc -l)
elif [ $size -gt 1024 ]; then
printf "%.1fKB" $(echo "scale=1; $size / 1024" | bc -l)
else
echo "${size}B"
fi
}
# Function to count lines efficiently
count_lines_safe() {
local file="$1"
if [ -f "$file" ] && [ -r "$file" ]; then
wc -l < "$file" 2>/dev/null | tr -d ' ' || echo "0"
else
echo "0"
fi
}
# Create temporary files for efficiency
TEMP_DIR=$(mktemp -d)
trap "rm -rf $TEMP_DIR" EXIT
echo "📊 Collecting file information (this may take a moment)..."
# Efficiently collect all file information at once
find . -type f -exec stat -f "%z %N" {} \; 2>/dev/null > "$TEMP_DIR/all_files_with_sizes.txt" &
find . -type d > "$TEMP_DIR/all_dirs.txt" &
find . -name "*.swift" > "$TEMP_DIR/swift_files.txt" &
wait
{
echo "🔍 ModularHomeInventory Project Analysis (Optimized)"
echo "===================================================="
echo "Generated: $(date)"
echo "Project Root: $PROJECT_ROOT"
echo ""
# Overall Statistics
echo "📊 OVERALL PROJECT STATISTICS"
echo "=============================="
total_files=$(wc -l < "$TEMP_DIR/all_files_with_sizes.txt" | tr -d ' ')
total_dirs=$(wc -l < "$TEMP_DIR/all_dirs.txt" | tr -d ' ')
total_size=$(awk '{sum+=$1} END {print sum+0}' "$TEMP_DIR/all_files_with_sizes.txt")
echo "Total Files: $total_files"
echo "Total Directories: $total_dirs"
echo "Total Size: $(format_size $total_size)"
echo ""
# File Extension Analysis - Optimized
echo "📁 FILE EXTENSION ANALYSIS"
echo "=========================="
echo "Extension | Count | Total Size | Avg Size"
echo "----------|-------|------------|----------"
# Extract extensions and calculate stats
awk '{
file = $2;
size = $1;
if (match(file, /\.([^.\/]+)$/, arr)) {
ext = arr[1];
count[ext]++;
total_size[ext] += size;
}
}
END {
for (ext in count) {
avg = total_size[ext] / count[ext];
printf "%s %d %d %.0f\n", ext, count[ext], total_size[ext], avg;
}
}' "$TEMP_DIR/all_files_with_sizes.txt" | sort -k2,2nr | head -25 | \
while read ext count size avg; do
printf "%-9s | %-5s | %-10s | %s\n" "$ext" "$count" "$(format_size $size)" "$(format_size $avg)"
done
echo ""
# Swift File Analysis - Optimized
echo "🐦 SWIFT FILE ANALYSIS"
echo "======================"
swift_count=$(wc -l < "$TEMP_DIR/swift_files.txt" | tr -d ' ')
if [ $swift_count -gt 0 ]; then
# Calculate Swift file sizes
grep "\.swift$" "$TEMP_DIR/all_files_with_sizes.txt" | awk '{sum+=$1} END {print sum+0}' > "$TEMP_DIR/swift_total_size.txt"
swift_size=$(cat "$TEMP_DIR/swift_total_size.txt")
echo "Total Swift Files: $swift_count"
echo "Total Swift Size: $(format_size $swift_size)"
# Count lines in Swift files efficiently using parallel processing
echo "Calculating Swift lines (parallel processing)..."
cat "$TEMP_DIR/swift_files.txt" | xargs -n 50 -P 8 wc -l 2>/dev/null | awk '{sum+=$1} END {print sum+0}' > "$TEMP_DIR/swift_lines.txt" &
# Continue with other analysis while lines are being counted
echo "Average Swift File Size: $(format_size $((swift_size / swift_count)))"
echo ""
# Longest Swift Files by Line Count - More Efficient
echo "📏 LONGEST SWIFT FILES (Top 20)"
echo "==============================="
echo "Lines | File"
echo "------|----"
# Use parallel processing for line counting
cat "$TEMP_DIR/swift_files.txt" | head -200 | xargs -n 10 -P 8 -I {} sh -c '
for file in "$@"; do
if [ -f "$file" ] && [ -r "$file" ]; then
lines=$(wc -l < "$file" 2>/dev/null | tr -d " " || echo "0")
echo "$lines|$file"
fi
done
' _ {} | sort -t'|' -k1,1nr | head -20 | \
while IFS='|' read -r lines file; do
printf "%-5s | %s\n" "$lines" "$file"
done
# Wait for total lines calculation to finish
wait
swift_total_lines=$(cat "$TEMP_DIR/swift_lines.txt")
echo ""
echo "Total Swift Lines: $swift_total_lines"
echo ""
else
echo "No Swift files found."
echo ""
fi
# Largest Files by Size - Optimized
echo "📦 LARGEST FILES (Top 20)"
echo "========================="
echo "Size | File"
echo "-----|----"
sort -k1,1nr "$TEMP_DIR/all_files_with_sizes.txt" | head -20 | \
while read size file; do
printf "%-10s | %s\n" "$(format_size $size)" "$file"
done
echo ""
# Largest Directories - More Efficient
echo "📂 LARGEST DIRECTORIES (Top 20)"
echo "==============================="
echo "Size | Directory"
echo "-----|----------"
# Calculate directory sizes more efficiently
awk '{
file = $2;
size = $1;
# Extract directory path
if (match(file, /^(.*)\/[^\/]+$/, arr)) {
dir = arr[1];
if (dir == "") dir = ".";
dir_size[dir] += size;
}
}
END {
for (dir in dir_size) {
printf "%d|%s\n", dir_size[dir], dir;
}
}' "$TEMP_DIR/all_files_with_sizes.txt" | sort -t'|' -k1,1nr | head -20 | \
while IFS='|' read -r size dir; do
printf "%-10s | %s\n" "$(format_size $size)" "$dir"
done
echo ""
# Module Analysis (Swift Package Modules) - Optimized
echo "🏗️ MODULE ANALYSIS"
echo "=================="
echo "Module | Swift Files | Lines | Size"
echo "-------|-------------|-------|-----"
find . -name "Package.swift" -type f | while read package; do
module_dir=$(dirname "$package")
module_name=$(basename "$module_dir")
# Count Swift files in this module
swift_count=$(find "$module_dir" -name "*.swift" -type f | wc -l | tr -d ' ')
if [ $swift_count -gt 0 ]; then
# Calculate lines and size for this module
swift_lines=$(find "$module_dir" -name "*.swift" -type f -exec wc -l {} \; 2>/dev/null | awk '{sum+=$1} END {print sum+0}')
swift_size=$(find "$module_dir" -name "*.swift" -type f -exec stat -f%z {} \; 2>/dev/null | awk '{sum+=$1} END {print sum+0}')
printf "%s|%s|%s|%s\n" "$module_name" "$swift_count" "$swift_lines" "$swift_size"
fi
done | sort -t'|' -k3,3nr | while IFS='|' read -r name count lines size; do
printf "%-25s | %-11s | %-5s | %s\n" "$name" "$count" "$lines" "$(format_size $size)"
done
echo ""
# Code Complexity Indicators - Optimized
echo "🧮 CODE COMPLEXITY INDICATORS"
echo "============================="
if [ $swift_count -gt 0 ]; then
# Use parallel grep for better performance
echo "Analyzing code constructs..."
cat "$TEMP_DIR/swift_files.txt" | xargs -P 8 grep -h "^class \|^struct \|^protocol \|^extension \|^enum \|func " 2>/dev/null | \
awk '
/^class / { classes++ }
/^struct / { structs++ }
/^protocol / { protocols++ }
/^extension / { extensions++ }
/^enum / { enums++ }
/func / { functions++ }
END {
print "Classes: " (classes+0)
print "Structs: " (structs+0)
print "Protocols: " (protocols+0)
print "Extensions: " (extensions+0)
print "Enums: " (enums+0)
print "Functions: " (functions+0)
}'
else
echo "No Swift files to analyze for complexity."
fi
echo ""
# Test File Analysis - Optimized
echo "🧪 TEST FILE ANALYSIS"
echo "====================="
test_files=$(grep -E "(Test|Tests)\.swift$" "$TEMP_DIR/swift_files.txt" | wc -l | tr -d ' ')
if [ $test_files -gt 0 ]; then
test_lines=$(grep -E "(Test|Tests)\.swift$" "$TEMP_DIR/swift_files.txt" | xargs wc -l 2>/dev/null | tail -1 | awk '{print $1}')
test_size=$(grep -E "(Test|Tests)\.swift$" "$TEMP_DIR/swift_files.txt" | xargs -I {} stat -f%z {} 2>/dev/null | awk '{sum+=$1} END {print sum+0}')
echo "Test Files: $test_files"
echo "Test Lines: $test_lines"
echo "Test Size: $(format_size $test_size)"
if [ $swift_count -gt 0 ]; then
test_coverage_ratio=$(echo "scale=1; $test_files * 100 / $swift_count" | bc -l 2>/dev/null || echo "0")
echo "Test Coverage Ratio: ${test_coverage_ratio}% (test files / total swift files)"
fi
else
echo "No test files found."
fi
echo ""
# Configuration Files - Optimized
echo "⚙️ CONFIGURATION FILES"
echo "======================"
config_patterns=("Package.swift" "Makefile" "project.yml" "Podfile" "Cartfile" "Gemfile" "Fastfile" ".swiftlint.yml" ".swiftformat" "Info.plist")
for pattern in "${config_patterns[@]}"; do
count=$(grep -c "$pattern$" "$TEMP_DIR/all_files_with_sizes.txt" 2>/dev/null || echo "0")
if [ $count -gt 0 ]; then
echo "$pattern: $count files"
grep "$pattern$" "$TEMP_DIR/all_files_with_sizes.txt" | head -5 | awk '{print " - " $2}'
fi
done
echo ""
# File Age Analysis (if git is available) - Optimized
if command -v git >/dev/null 2>&1 && [ -d .git ]; then
echo "📅 FILE AGE ANALYSIS (Git History)"
echo "=================================="
echo "Recently Modified Files (Last 7 days):"
git log --since="7 days ago" --name-only --pretty=format: | grep -v "^$" | sort | uniq -c | sort -nr | head -10 | \
while read count file; do
if [ -n "$file" ] && [ "$file" != " " ]; then
echo " $count changes: $file"
fi
done
echo ""
echo "Most Active Files (All Time - Top 10):"
git log --name-only --pretty=format: | grep -v "^$" | sort | uniq -c | sort -nr | head -10 | \
while read count file; do
if [ -n "$file" ] && [ "$file" != " " ]; then
echo " $count changes: $file"
fi
done
echo ""
fi
# Archive Analysis
echo "🗄️ ARCHIVE ANALYSIS"
echo "==================="
zip_count=$(grep "\.zip$" "$TEMP_DIR/all_files_with_sizes.txt" | wc -l | tr -d ' ')
if [ $zip_count -gt 0 ]; then
echo "Archive Files Found:"
grep "\.zip$" "$TEMP_DIR/all_files_with_sizes.txt" | while read size file; do
echo " $(basename "$file") - $(format_size $size)"
done
else
echo "No archive files found."
fi
echo ""
# Build System Analysis
echo "🔧 BUILD SYSTEM ANALYSIS"
echo "========================"
packages=$(grep "Package.swift$" "$TEMP_DIR/all_files_with_sizes.txt" | wc -l | tr -d ' ')
makefiles=$(grep "Makefile$" "$TEMP_DIR/all_files_with_sizes.txt" | wc -l | tr -d ' ')
xcodeproj=$(grep "\.xcodeproj/" "$TEMP_DIR/all_files_with_sizes.txt" | wc -l | tr -d ' ')
echo "Swift Packages: $packages"
echo "Makefiles: $makefiles"
echo "Xcode Project Files: $xcodeproj"
echo ""
# Summary
echo "📋 SUMMARY"
echo "=========="
project_type="medium-sized"
if [ $total_files -gt 5000 ]; then project_type="very large"
elif [ $total_files -gt 2000 ]; then project_type="large"
elif [ $total_files -gt 500 ]; then project_type="medium-sized"
else project_type="small"; fi
echo "This is a $project_type Swift project with:"
echo "• $swift_count Swift source files"
if [ -f "$TEMP_DIR/swift_lines.txt" ]; then
echo "• $(cat "$TEMP_DIR/swift_lines.txt") total lines of Swift code"
fi
echo "• $test_files test files"
echo "• $packages Swift Package modules"
echo "• Total project size: $(format_size $total_size)"
echo "• Most common file type: $(awk '{file=$2; if(match(file,/\.([^.\/]+)$/,arr)) count[arr[1]]++} END {max=0; for(ext in count) if(count[ext]>max) {max=count[ext]; maxext=ext}} END {print maxext}' "$TEMP_DIR/all_files_with_sizes.txt")"
echo ""
echo "Generated: $(date)"
echo "Analysis completed in optimized mode."
} | tee "$OUTPUT_FILE"
echo ""
echo "✅ Analysis complete! Results saved to: $OUTPUT_FILE"
echo "📊 Quick Summary:"
echo " • Total files: $(wc -l < "$TEMP_DIR/all_files_with_sizes.txt" | tr -d ' ')"
echo " • Swift files: $(wc -l < "$TEMP_DIR/swift_files.txt" | tr -d ' ')"
echo " • Project size: $(format_size $(awk '{sum+=$1} END {print sum+0}' "$TEMP_DIR/all_files_with_sizes.txt"))"