devflow-pro 0.1.0

Advanced codebase analysis tool for project insights and optimization
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use std::{env, fs, path::{Path, PathBuf}, time::Instant, collections::HashMap};

#[derive(Default)]
struct ProjectInsights {
    metrics: Metrics,
    tech_stack: TechStack,
    quality: CodeQuality,
    patterns: ArchitecturePatterns,
    recommendations: Vec<Recommendation>,
}

#[derive(Default)]
struct Metrics {
    lines_of_code: usize,
    files: usize,
    complexity: f32,
    test_coverage: f32,
}

#[derive(Default)]
struct TechStack {
    languages: HashMap<String, usize>,
    frameworks: Vec<String>,
    databases: Vec<String>,
    cloud_services: Vec<String>,
}

#[derive(Default)]
struct CodeQuality {
    duplications: Vec<(String, f32)>,
    security_issues: Vec<String>,
    performance_hotspots: Vec<String>,
    best_practices: Vec<String>,
}

#[derive(Default)]
struct ArchitecturePatterns {
    design_patterns: Vec<String>,
    anti_patterns: Vec<String>,
    architecture_style: String,
}

struct Recommendation {
    category: String,
    priority: u8,
    description: String,
    impact: String,
    effort: String,
}

fn main() {
    let start = Instant::now();
    println!("🚀 DevFlow Pro - Advanced Codebase Analysis");
    let path = env::args().nth(1).unwrap_or_else(|| ".".to_string());
    let insights = analyze_codebase(Path::new(&path));
    display_insights(&insights);
    generate_detailed_report(&insights);
    suggest_optimizations(&insights);
    println!("\n✨ Analysis completed in {:.2}s", start.elapsed().as_secs_f32());
}

fn analyze_codebase(root: &Path) -> ProjectInsights {
    let mut insights = ProjectInsights::default();
    analyze_structure(root, &mut insights);
    detect_code_patterns(&mut insights);
    evaluate_quality(&mut insights);
    generate_recommendations(&mut insights);
    insights
}

fn analyze_structure(dir: &Path, insights: &mut ProjectInsights) {
    if let Ok(entries) = fs::read_dir(dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.is_dir() {
                if !is_excluded_directory(&path) {
                    analyze_structure(&path, insights);
                }
            } else {
                analyze_file(&path, insights);
            }
        }
    }
}

fn is_excluded_directory(path: &Path) -> bool {
    let excluded = ["node_modules", "target", "dist", "build", ".git", "vendor"];
    path.file_name()
        .and_then(|n| n.to_str())
        .map(|n| excluded.contains(&n))
        .unwrap_or(false)
}

fn analyze_file(path: &PathBuf, insights: &mut ProjectInsights) {
    if let Ok(content) = fs::read_to_string(path) {
        insights.metrics.files += 1;
        insights.metrics.lines_of_code += content.lines().count();
        if path.to_string_lossy().contains("test") {
            insights.metrics.test_coverage += 1.0;
        }
        detect_language(path, &content, insights);
        analyze_complexity(&content, insights);
        detect_frameworks(path, &content, insights);
        find_security_issues(&content, insights);
        detect_architecture_patterns(&content, insights);
        detect_databases(&content, insights);
        detect_cloud_services(&content, insights);
        analyze_code_duplication(&content, insights);
        detect_performance_issues(&content, insights);
    }
}

fn detect_performance_issues(content: &str, insights: &mut ProjectInsights) {
    let patterns = [
        (".*\\*.*", "Inefficient wildcard operation"),
        ("Thread\\.sleep", "Blocking thread sleep"),
        ("for.*for.*for", "Nested triple loop"),
    ];
    
    for (pattern, issue) in patterns {
        if content.contains(pattern) {
            insights.quality.performance_hotspots.push(issue.to_string());
        }
    }
}

fn detect_language(path: &Path, _content: &str, insights: &mut ProjectInsights) {
    let ext = path.extension().and_then(|e| e.to_str()).unwrap_or("");
    let language = match ext {
        "rs" => Some("Rust"),
        "go" => Some("Go"),
        "js" | "jsx" => Some("JavaScript"),
        "ts" | "tsx" => Some("TypeScript"),
        "py" => Some("Python"),
        "java" => Some("Java"),
        "rb" => Some("Ruby"),
        "php" => Some("PHP"),
        "swift" => Some("Swift"),
        "kt" => Some("Kotlin"),
        "cpp" | "hpp" | "cc" | "h" => Some("C++"),
        "c" => Some("C"),
        "cs" => Some("C#"),
        _ => None
    };
    if let Some(lang) = language {
        *insights.tech_stack.languages.entry(lang.to_string()).or_insert(0) += 1;
    }
}

fn analyze_complexity(content: &str, insights: &mut ProjectInsights) {
    let line_count = content.lines().count();
    let nested_count = content.matches("{").count();
    let conditional_count = content.matches("if").count() + 
                          content.matches("match").count() +
                          content.matches("while").count();
    if line_count > 0 {
        insights.metrics.complexity += (nested_count as f32 + conditional_count as f32) / (line_count as f32);
    }
}

fn detect_frameworks(path: &Path, content: &str, insights: &mut ProjectInsights) {
    let frameworks = match path.file_name().and_then(|n| n.to_str()) {
        Some("package.json") => detect_js_frameworks(content),
        Some("Cargo.toml") => detect_rust_frameworks(content),
        Some("go.mod") => detect_go_frameworks(content),
        Some("requirements.txt") => detect_python_frameworks(content),
        Some("composer.json") => detect_php_frameworks(content),
        Some("pom.xml") => detect_java_frameworks(content),
        _ => vec![]
    };
    insights.tech_stack.frameworks.extend(frameworks);
}

fn detect_js_frameworks(content: &str) -> Vec<String> {
    let patterns = [("react", "React"), ("vue", "Vue.js"), ("angular", "Angular")];
    patterns.iter()
        .filter(|(p, _)| content.contains(p))
        .map(|(_, f)| f.to_string())
        .collect()
}

fn detect_rust_frameworks(content: &str) -> Vec<String> {
    let patterns = [("rocket", "Rocket"), ("actix", "Actix"), ("tokio", "Tokio")];
    patterns.iter()
        .filter(|(p, _)| content.contains(p))
        .map(|(_, f)| f.to_string())
        .collect()
}

fn detect_python_frameworks(content: &str) -> Vec<String> {
    let patterns = [("django", "Django"), ("flask", "Flask"), ("fastapi", "FastAPI")];
    patterns.iter()
        .filter(|(p, _)| content.contains(p))
        .map(|(_, f)| f.to_string())
        .collect()
}

fn detect_go_frameworks(content: &str) -> Vec<String> {
    let patterns = [("gin-gonic", "Gin"), ("fiber", "Fiber")];
    patterns.iter()
        .filter(|(p, _)| content.contains(p))
        .map(|(_, f)| f.to_string())
        .collect()
}

fn detect_php_frameworks(content: &str) -> Vec<String> {
    let patterns = [("laravel", "Laravel"), ("symfony", "Symfony")];
    patterns.iter()
        .filter(|(p, _)| content.contains(p))
        .map(|(_, f)| f.to_string())
        .collect()
}

fn detect_java_frameworks(content: &str) -> Vec<String> {
    let patterns = [("springframework", "Spring"), ("hibernate", "Hibernate")];
    patterns.iter()
        .filter(|(p, _)| content.contains(p))
        .map(|(_, f)| f.to_string())
        .collect()
}

fn detect_databases(content: &str, insights: &mut ProjectInsights) {
    let patterns = [("mongodb", "MongoDB"), ("postgresql", "PostgreSQL")];
    for (pattern, db) in patterns {
        if content.to_lowercase().contains(pattern) {
            insights.tech_stack.databases.push(db.to_string());
        }
    }
}

fn detect_cloud_services(content: &str, insights: &mut ProjectInsights) {
    let patterns = [("aws", "AWS"), ("azure", "Azure"), ("gcp", "Google Cloud")];
    for (pattern, service) in patterns {
        if content.to_lowercase().contains(pattern) {
            insights.tech_stack.cloud_services.push(service.to_string());
        }
    }
}

fn analyze_code_duplication(content: &str, insights: &mut ProjectInsights) {
    let lines: Vec<&str> = content.lines().collect();
    for (i, line) in lines.iter().enumerate() {
        if line.len() > 30 {
            let duplicates = lines[i+1..].iter()
                .filter(|&&other| other == *line)
                .count();
            if duplicates > 0 {
                insights.quality.duplications.push((line.to_string(), duplicates as f32));
            }
        }
    }
}

fn find_security_issues(content: &str, insights: &mut ProjectInsights) {
    let checks = [("eval(", "eval() usage"), ("innerHTML", "XSS risk")];
    for (pattern, issue) in checks {
        if content.contains(pattern) {
            insights.quality.security_issues.push(format!("⚠️ {}", issue));
        }
    }
}


fn detect_architecture_patterns(content: &str, insights: &mut ProjectInsights) {
    let patterns = [("interface", "Interface"), ("abstract", "Abstract Factory")];
    for (pattern, design) in patterns {
        if content.to_lowercase().contains(pattern) {
            insights.patterns.design_patterns.push(design.to_string());
        }
    }

    let anti_patterns = [
        ("goto", "Goto Statement"),
        ("global", "Global State"),
        ("singleton", "Singleton Abuse")
    ];
    
    for (pattern, anti) in anti_patterns {
        if content.to_lowercase().contains(pattern) {
            insights.patterns.anti_patterns.push(anti.to_string());
        }
    }
}

fn detect_code_patterns(insights: &mut ProjectInsights) {
    if insights.metrics.complexity > 0.3 {
        insights.patterns.design_patterns.push("Consider Command Pattern".into());
    }
    if insights.metrics.lines_of_code > 5000 {
        insights.patterns.architecture_style = "Microservices Candidate".into();
    }
}

fn evaluate_quality(insights: &mut ProjectInsights) {
    if insights.metrics.complexity > 0.5 {
        insights.quality.best_practices.push("High complexity - consider refactoring".into());
    }
    if (insights.metrics.test_coverage / insights.metrics.files as f32) < 0.5 {
        insights.quality.best_practices.push("Low test coverage detected".into());
    }
}

fn generate_recommendations(insights: &mut ProjectInsights) {
    if insights.metrics.complexity > 0.3 {
        insights.recommendations.push(Recommendation {
            category: "Performance".into(),
            priority: 1,
            description: "High code complexity detected".into(),
            impact: "Affects runtime performance and maintainability".into(),
            effort: "Medium - Requires systematic refactoring".into(),
        });
    }
    
    if !insights.quality.performance_hotspots.is_empty() {
        insights.recommendations.push(Recommendation {
            category: "Performance".into(),
            priority: 2,
            description: "Performance hotspots found".into(),
            impact: "System performance degradation".into(),
            effort: "High - Requires optimization work".into(),
        });
    }

    if !insights.patterns.anti_patterns.is_empty() {
        insights.recommendations.push(Recommendation {
            category: "Architecture".into(),
            priority: 1,
            description: "Anti-patterns detected".into(),
            impact: "Code maintainability at risk".into(),
            effort: "Medium - Refactoring needed".into(),
        });
    }
}


fn display_insights(insights: &ProjectInsights) {
    println!("\n📊 Project Metrics");
    println!("================");
    println!("Lines of Code: {}", insights.metrics.lines_of_code);
    println!("Files: {}", insights.metrics.files);
    println!("Complexity: {:.2}", insights.metrics.complexity);
    println!("Test Coverage: {:.1}%", (insights.metrics.test_coverage / insights.metrics.files as f32) * 100.0);
    
    if !insights.tech_stack.languages.is_empty() {
        println!("\n🔍 Languages:");
        for (lang, count) in &insights.tech_stack.languages {
            println!("{} ({} files)", lang, count);
        }
    }
    
    if !insights.quality.security_issues.is_empty() {
        println!("\n🛡️ Security Issues:");
        for issue in &insights.quality.security_issues {
            println!("{}", issue);
        }
    }

    if !insights.quality.performance_hotspots.is_empty() {
        println!("\n⚡ Performance Hotspots:");
        for hotspot in &insights.quality.performance_hotspots {
            println!("{}", hotspot);
        }
    }

    if !insights.patterns.anti_patterns.is_empty() {
        println!("\n⚠️ Anti-Patterns Detected:");
        for pattern in &insights.patterns.anti_patterns {
            println!("{}", pattern);
        }
    }

    if !insights.recommendations.is_empty() {
        println!("\n💡 Recommendations:");
        for rec in &insights.recommendations {
            println!("\n{} (Priority: {})", rec.category, rec.priority);
            println!("Description: {}", rec.description);
            println!("Impact: {}", rec.impact);
            println!("Effort: {}", rec.effort);
        }
    }
}
fn generate_detailed_report(insights: &ProjectInsights) {
    let report = format!(
        "DevFlow Analysis Report\n====================\n\
        Project Overview:\n\
        Files: {}\n\
        Lines: {}\n\
        Complexity: {:.2}\n\
        Test Coverage: {:.1}%\n\n\
        Performance Hotspots: {}\n\
        Security Issues: {}\n\
        Anti-Patterns: {}\n\
        Recommendations: {}\n\n\
        Priority Actions:\n\
        {}\n",
        insights.metrics.files,
        insights.metrics.lines_of_code,
        insights.metrics.complexity,
        (insights.metrics.test_coverage / insights.metrics.files as f32) * 100.0,
        insights.quality.performance_hotspots.len(),
        insights.quality.security_issues.len(),
        insights.patterns.anti_patterns.len(),
        insights.recommendations.len(),
        insights.recommendations.iter()
            .map(|r| format!("{} (Priority: {}): {}", r.category, r.priority, r.description))
            .collect::<Vec<_>>()
            .join("\n")
    );
    let _ = fs::write("devflow-report.txt", report);
}

fn suggest_optimizations(insights: &ProjectInsights) {
    if insights.metrics.complexity > 0.4 {
        println!("\n⚡ Quick Wins");
        println!("===========");
        println!("• Break down complex functions (score: {:.2})", insights.metrics.complexity);
        println!("• Add performance monitoring");
        
        for hotspot in &insights.quality.performance_hotspots {
            println!("• Fix: {}", hotspot);
        }
        
        for pattern in &insights.patterns.anti_patterns {
            println!("• Refactor: {}", pattern);
        }
    }
}