syncable-cli 0.37.1

A Rust-based CLI that analyzes code repositories and generates Infrastructure as Code configurations
Documentation
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
use crate::analyzer::{AnalysisConfig, DetectedLanguage};
use crate::common::file_utils;
use crate::error::Result;
use serde_json::Value as JsonValue;
use std::collections::HashMap;
use std::path::PathBuf;

/// Language detection results with detailed information
#[derive(Debug, Clone)]
pub struct LanguageInfo {
    pub name: String,
    pub version: Option<String>,
    pub edition: Option<String>,
    pub package_manager: Option<String>,
    pub main_dependencies: Vec<String>,
    pub dev_dependencies: Vec<String>,
    pub confidence: f32,
    pub source_files: Vec<PathBuf>,
    pub manifest_files: Vec<PathBuf>,
}

/// Detects programming languages with advanced manifest parsing
pub fn detect_languages(
    files: &[PathBuf],
    config: &AnalysisConfig,
) -> Result<Vec<DetectedLanguage>> {
    let mut language_info = HashMap::new();

    // First pass: collect files by extension and find manifests
    let mut source_files_by_lang = HashMap::new();
    let mut manifest_files = Vec::new();

    for file in files {
        if let Some(extension) = file.extension().and_then(|e| e.to_str()) {
            match extension {
                // Rust files
                "rs" => source_files_by_lang
                    .entry("rust")
                    .or_insert_with(Vec::new)
                    .push(file.clone()),

                // JavaScript/TypeScript files
                "js" | "jsx" | "ts" | "tsx" | "mjs" | "cjs" => source_files_by_lang
                    .entry("javascript")
                    .or_insert_with(Vec::new)
                    .push(file.clone()),

                // Python files
                "py" | "pyx" | "pyi" => source_files_by_lang
                    .entry("python")
                    .or_insert_with(Vec::new)
                    .push(file.clone()),

                // Go files
                "go" => source_files_by_lang
                    .entry("go")
                    .or_insert_with(Vec::new)
                    .push(file.clone()),

                // Java/Kotlin files
                "java" | "kt" | "kts" => source_files_by_lang
                    .entry("jvm")
                    .or_insert_with(Vec::new)
                    .push(file.clone()),

                _ => {}
            }
        }

        // Check for manifest files
        if let Some(filename) = file.file_name().and_then(|n| n.to_str())
            && is_manifest_file(filename)
        {
            manifest_files.push(file.clone());
        }
    }

    // Second pass: analyze each detected language with manifest parsing
    if (source_files_by_lang.contains_key("rust") || has_manifest(&manifest_files, &["Cargo.toml"]))
        && let Ok(info) =
            analyze_rust_project(&manifest_files, source_files_by_lang.get("rust"), config)
    {
        language_info.insert("rust", info);
    }

    if (source_files_by_lang.contains_key("javascript")
        || has_manifest(&manifest_files, &["package.json"]))
        && let Ok(info) = analyze_javascript_project(
            &manifest_files,
            source_files_by_lang.get("javascript"),
            config,
        )
    {
        language_info.insert("javascript", info);
    }

    if (source_files_by_lang.contains_key("python")
        || has_manifest(
            &manifest_files,
            &["requirements.txt", "Pipfile", "pyproject.toml", "setup.py"],
        ))
        && let Ok(info) =
            analyze_python_project(&manifest_files, source_files_by_lang.get("python"), config)
    {
        language_info.insert("python", info);
    }

    if (source_files_by_lang.contains_key("go") || has_manifest(&manifest_files, &["go.mod"]))
        && let Ok(info) =
            analyze_go_project(&manifest_files, source_files_by_lang.get("go"), config)
    {
        language_info.insert("go", info);
    }

    if (source_files_by_lang.contains_key("jvm")
        || has_manifest(
            &manifest_files,
            &["pom.xml", "build.gradle", "build.gradle.kts"],
        ))
        && let Ok(info) =
            analyze_jvm_project(&manifest_files, source_files_by_lang.get("jvm"), config)
    {
        language_info.insert("jvm", info);
    }

    // Convert to DetectedLanguage format
    let mut detected_languages = Vec::new();
    for (_, info) in language_info {
        detected_languages.push(DetectedLanguage {
            name: info.name,
            version: info.version,
            confidence: info.confidence,
            files: info.source_files,
            main_dependencies: info.main_dependencies,
            dev_dependencies: info.dev_dependencies,
            package_manager: info.package_manager,
        });
    }

    // Sort by confidence (highest first)
    detected_languages.sort_by(|a, b| {
        b.confidence
            .partial_cmp(&a.confidence)
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    Ok(detected_languages)
}

/// Analyze Rust project from Cargo.toml
fn analyze_rust_project(
    manifest_files: &[PathBuf],
    source_files: Option<&Vec<PathBuf>>,
    config: &AnalysisConfig,
) -> Result<LanguageInfo> {
    let mut info = LanguageInfo {
        name: "Rust".to_string(),
        version: None,
        edition: None,
        package_manager: Some("cargo".to_string()),
        main_dependencies: Vec::new(),
        dev_dependencies: Vec::new(),
        confidence: 0.5,
        source_files: source_files.map_or(Vec::new(), |f| f.clone()),
        manifest_files: Vec::new(),
    };

    // Find and parse Cargo.toml
    for manifest in manifest_files {
        if manifest.file_name().and_then(|n| n.to_str()) == Some("Cargo.toml") {
            info.manifest_files.push(manifest.clone());

            if let Ok(content) = file_utils::read_file_safe(manifest, config.max_file_size)
                && let Ok(cargo_toml) = toml::from_str::<toml::Value>(&content)
            {
                // Extract edition
                if let Some(package) = cargo_toml.get("package")
                    && let Some(edition) = package.get("edition").and_then(|e| e.as_str())
                {
                    info.edition = Some(edition.to_string());
                }

                // Estimate Rust version from edition
                info.version = match info.edition.as_deref() {
                    Some("2021") => Some("1.56+".to_string()),
                    Some("2018") => Some("1.31+".to_string()),
                    Some("2015") => Some("1.0+".to_string()),
                    _ => Some("unknown".to_string()),
                };

                // Extract dependencies
                if let Some(deps_table) = cargo_toml.get("dependencies").and_then(|d| d.as_table())
                {
                    for (name, _) in deps_table {
                        info.main_dependencies.push(name.clone());
                    }
                }

                // Extract dev dependencies if enabled
                if config.include_dev_dependencies
                    && let Some(dev_deps_table) = cargo_toml
                        .get("dev-dependencies")
                        .and_then(|d| d.as_table())
                {
                    for (name, _) in dev_deps_table {
                        info.dev_dependencies.push(name.clone());
                    }
                }

                info.confidence = 0.95; // High confidence with manifest
            }
            break;
        }
    }

    // Boost confidence if we have source files
    if !info.source_files.is_empty() {
        info.confidence = (info.confidence + 0.9) / 2.0;
    }

    Ok(info)
}

/// Analyze JavaScript/TypeScript project from package.json
fn analyze_javascript_project(
    manifest_files: &[PathBuf],
    source_files: Option<&Vec<PathBuf>>,
    config: &AnalysisConfig,
) -> Result<LanguageInfo> {
    let mut info = LanguageInfo {
        name: "JavaScript/TypeScript".to_string(),
        version: None,
        edition: None,
        package_manager: None,
        main_dependencies: Vec::new(),
        dev_dependencies: Vec::new(),
        confidence: 0.5,
        source_files: source_files.map_or(Vec::new(), |f| f.clone()),
        manifest_files: Vec::new(),
    };

    // Detect package manager from lock files
    for manifest in manifest_files {
        if let Some(filename) = manifest.file_name().and_then(|n| n.to_str()) {
            match filename {
                "package-lock.json" => info.package_manager = Some("npm".to_string()),
                "yarn.lock" => info.package_manager = Some("yarn".to_string()),
                "pnpm-lock.yaml" => info.package_manager = Some("pnpm".to_string()),
                _ => {}
            }
        }
    }

    // Default to npm if no package manager detected
    if info.package_manager.is_none() {
        info.package_manager = Some("npm".to_string());
    }

    // Find and parse package.json
    for manifest in manifest_files {
        if manifest.file_name().and_then(|n| n.to_str()) == Some("package.json") {
            info.manifest_files.push(manifest.clone());

            if let Ok(content) = file_utils::read_file_safe(manifest, config.max_file_size)
                && let Ok(package_json) = serde_json::from_str::<JsonValue>(&content)
            {
                // Extract Node.js version from engines
                if let Some(node_version) = package_json
                    .get("engines")
                    .and_then(|e| e.get("node"))
                    .and_then(|v| v.as_str())
                {
                    info.version = Some(node_version.to_string());
                }

                // Extract dependencies (always include all buckets for framework detection)
                if let Some(deps) = package_json.get("dependencies").and_then(|d| d.as_object()) {
                    for (name, _) in deps {
                        info.main_dependencies.push(name.clone());
                    }
                }

                // Frameworks like Vite/Remix/Next are often in devDependencies; always include
                if let Some(dev_deps) = package_json
                    .get("devDependencies")
                    .and_then(|d| d.as_object())
                {
                    for (name, _) in dev_deps {
                        info.main_dependencies.push(name.clone());
                        info.dev_dependencies.push(name.clone());
                    }
                }

                // peerDependencies frequently carry framework identity (e.g., react-router)
                if let Some(peer_deps) = package_json
                    .get("peerDependencies")
                    .and_then(|d| d.as_object())
                {
                    for (name, _) in peer_deps {
                        info.main_dependencies.push(name.clone());
                    }
                }

                // optional/bundled deps can also hold framework markers (rare but cheap to add)
                if let Some(opt_deps) = package_json
                    .get("optionalDependencies")
                    .and_then(|d| d.as_object())
                {
                    for (name, _) in opt_deps {
                        info.main_dependencies.push(name.clone());
                    }
                }
                if let Some(bundle_deps) = package_json
                    .get("bundledDependencies")
                    .and_then(|d| d.as_array())
                {
                    for dep in bundle_deps.iter().filter_map(|d| d.as_str()) {
                        info.main_dependencies.push(dep.to_string());
                    }
                }

                info.confidence = 0.95; // High confidence with manifest
            }
            break;
        }
    }

    // Adjust name based on file types
    if let Some(files) = source_files {
        let has_typescript = files.iter().any(|f| {
            f.extension()
                .and_then(|e| e.to_str())
                .is_some_and(|ext| ext == "ts" || ext == "tsx")
        });

        if has_typescript {
            info.name = "TypeScript".to_string();
        } else {
            info.name = "JavaScript".to_string();
        }
    }

    // Boost confidence if we have source files
    if !info.source_files.is_empty() {
        info.confidence = (info.confidence + 0.9) / 2.0;
    }

    Ok(info)
}

/// Analyze Python project from various manifest files
fn analyze_python_project(
    manifest_files: &[PathBuf],
    source_files: Option<&Vec<PathBuf>>,
    config: &AnalysisConfig,
) -> Result<LanguageInfo> {
    let mut info = LanguageInfo {
        name: "Python".to_string(),
        version: None,
        edition: None,
        package_manager: None,
        main_dependencies: Vec::new(),
        dev_dependencies: Vec::new(),
        confidence: 0.5,
        source_files: source_files.map_or(Vec::new(), |f| f.clone()),
        manifest_files: Vec::new(),
    };

    // Detect package manager and parse manifest files
    for manifest in manifest_files {
        if let Some(filename) = manifest.file_name().and_then(|n| n.to_str()) {
            info.manifest_files.push(manifest.clone());

            match filename {
                "requirements.txt" => {
                    info.package_manager = Some("pip".to_string());
                    if let Ok(content) = file_utils::read_file_safe(manifest, config.max_file_size)
                    {
                        parse_requirements_txt(&content, &mut info);
                        info.confidence = 0.85;
                    }
                }
                "Pipfile" => {
                    info.package_manager = Some("pipenv".to_string());
                    if let Ok(content) = file_utils::read_file_safe(manifest, config.max_file_size)
                    {
                        parse_pipfile(&content, &mut info, config);
                        info.confidence = 0.90;
                    }
                }
                "pyproject.toml" => {
                    info.package_manager = Some("poetry/pip".to_string());
                    if let Ok(content) = file_utils::read_file_safe(manifest, config.max_file_size)
                    {
                        parse_pyproject_toml(&content, &mut info, config);
                        info.confidence = 0.95;
                    }
                }
                "setup.py" => {
                    info.package_manager = Some("setuptools".to_string());
                    if let Ok(content) = file_utils::read_file_safe(manifest, config.max_file_size)
                    {
                        parse_setup_py(&content, &mut info);
                        info.confidence = 0.80;
                    }
                }
                _ => {}
            }
        }
    }

    // Default to pip if no package manager detected
    if info.package_manager.is_none() && !info.source_files.is_empty() {
        info.package_manager = Some("pip".to_string());
        info.confidence = 0.75;
    }

    // Boost confidence if we have source files
    if !info.source_files.is_empty() {
        info.confidence = (info.confidence + 0.8) / 2.0;
    }

    Ok(info)
}

/// Parse requirements.txt file
fn parse_requirements_txt(content: &str, info: &mut LanguageInfo) {
    for line in content.lines() {
        let line = line.trim();
        if line.is_empty() || line.starts_with('#') {
            continue;
        }

        // Extract package name (before ==, >=, etc.)
        if let Some(package_name) = line.split(&['=', '>', '<', '!', '~', ';'][..]).next() {
            let clean_name = package_name.trim();
            if !clean_name.is_empty() && !clean_name.starts_with('-') {
                info.main_dependencies.push(clean_name.to_string());
            }
        }
    }
}

/// Parse Pipfile (TOML format)
fn parse_pipfile(content: &str, info: &mut LanguageInfo, config: &AnalysisConfig) {
    if let Ok(pipfile) = toml::from_str::<toml::Value>(content) {
        // Extract Python version requirement
        if let Some(requires) = pipfile.get("requires") {
            if let Some(python_version) = requires.get("python_version").and_then(|v| v.as_str()) {
                info.version = Some(format!("~={}", python_version));
            } else if let Some(python_full) =
                requires.get("python_full_version").and_then(|v| v.as_str())
            {
                info.version = Some(format!("=={}", python_full));
            }
        }

        // Extract packages
        if let Some(packages_table) = pipfile.get("packages").and_then(|p| p.as_table()) {
            for (name, _) in packages_table {
                info.main_dependencies.push(name.clone());
            }
        }

        // Extract dev packages if enabled
        if config.include_dev_dependencies
            && let Some(dev_packages_table) = pipfile.get("dev-packages").and_then(|d| d.as_table())
        {
            for (name, _) in dev_packages_table {
                info.dev_dependencies.push(name.clone());
            }
        }
    }
}

/// Parse pyproject.toml file
fn parse_pyproject_toml(content: &str, info: &mut LanguageInfo, config: &AnalysisConfig) {
    if let Ok(pyproject) = toml::from_str::<toml::Value>(content) {
        // Extract Python version from project metadata
        if let Some(project) = pyproject.get("project") {
            if let Some(requires_python) = project.get("requires-python").and_then(|v| v.as_str()) {
                info.version = Some(requires_python.to_string());
            }

            // Extract dependencies
            if let Some(deps_array) = project.get("dependencies").and_then(|d| d.as_array()) {
                for dep in deps_array {
                    if let Some(dep_str) = dep.as_str()
                        && let Some(package_name) =
                            dep_str.split(&['=', '>', '<', '!', '~', ';'][..]).next()
                    {
                        let clean_name = package_name.trim();
                        if !clean_name.is_empty() {
                            info.main_dependencies.push(clean_name.to_string());
                        }
                    }
                }
            }

            // Extract optional dependencies (dev dependencies)
            if config.include_dev_dependencies
                && let Some(optional_table) = project
                    .get("optional-dependencies")
                    .and_then(|o| o.as_table())
            {
                for (_, deps) in optional_table {
                    if let Some(deps_array) = deps.as_array() {
                        for dep in deps_array {
                            if let Some(dep_str) = dep.as_str()
                                && let Some(package_name) =
                                    dep_str.split(&['=', '>', '<', '!', '~', ';'][..]).next()
                            {
                                let clean_name = package_name.trim();
                                if !clean_name.is_empty() {
                                    info.dev_dependencies.push(clean_name.to_string());
                                }
                            }
                        }
                    }
                }
            }
        }

        // Check for Poetry configuration
        if let Some(poetry) = pyproject.get("tool").and_then(|t| t.get("poetry")) {
            info.package_manager = Some("poetry".to_string());

            // Extract Poetry dependencies
            if let Some(deps_table) = poetry.get("dependencies").and_then(|d| d.as_table()) {
                for (name, _) in deps_table {
                    if name != "python" {
                        info.main_dependencies.push(name.clone());
                    }
                }
            }

            if config.include_dev_dependencies
                && let Some(dev_deps_table) = poetry
                    .get("group")
                    .and_then(|g| g.get("dev"))
                    .and_then(|d| d.get("dependencies"))
                    .and_then(|d| d.as_table())
            {
                for (name, _) in dev_deps_table {
                    info.dev_dependencies.push(name.clone());
                }
            }
        }
    }
}

/// Parse setup.py file (basic extraction)
fn parse_setup_py(content: &str, info: &mut LanguageInfo) {
    // Basic regex-based parsing for common patterns
    for line in content.lines() {
        let line = line.trim();

        // Look for python_requires
        if line.contains("python_requires") {
            if let Some(start) = line.find('"')
                && let Some(end) = line[start + 1..].find('"')
            {
                let version = &line[start + 1..start + 1 + end];
                info.version = Some(version.to_string());
            } else if let Some(start) = line.find('\'')
                && let Some(end) = line[start + 1..].find('\'')
            {
                let version = &line[start + 1..start + 1 + end];
                info.version = Some(version.to_string());
            }
        }

        // Look for install_requires (basic pattern)
        if line.contains("install_requires") && line.contains("[") {
            // This is a simplified parser - could be enhanced
            info.main_dependencies
                .push("setuptools-detected".to_string());
        }
    }
}

/// Analyze Go project from go.mod
fn analyze_go_project(
    manifest_files: &[PathBuf],
    source_files: Option<&Vec<PathBuf>>,
    config: &AnalysisConfig,
) -> Result<LanguageInfo> {
    let mut info = LanguageInfo {
        name: "Go".to_string(),
        version: None,
        edition: None,
        package_manager: Some("go mod".to_string()),
        main_dependencies: Vec::new(),
        dev_dependencies: Vec::new(),
        confidence: 0.5,
        source_files: source_files.map_or(Vec::new(), |f| f.clone()),
        manifest_files: Vec::new(),
    };

    // Find and parse go.mod
    for manifest in manifest_files {
        if let Some(filename) = manifest.file_name().and_then(|n| n.to_str()) {
            match filename {
                "go.mod" => {
                    info.manifest_files.push(manifest.clone());
                    if let Ok(content) = file_utils::read_file_safe(manifest, config.max_file_size)
                    {
                        parse_go_mod(&content, &mut info);
                        info.confidence = 0.95;
                    }
                }
                "go.sum" => {
                    info.manifest_files.push(manifest.clone());
                    // go.sum contains checksums, indicates a real Go project
                    info.confidence = (info.confidence + 0.9) / 2.0;
                }
                _ => {}
            }
        }
    }

    // Boost confidence if we have source files
    if !info.source_files.is_empty() {
        info.confidence = (info.confidence + 0.85) / 2.0;
    }

    Ok(info)
}

/// Parse go.mod file
fn parse_go_mod(content: &str, info: &mut LanguageInfo) {
    for line in content.lines() {
        let line = line.trim();

        // Parse go version directive
        if let Some(version) = line.strip_prefix("go ") {
            info.version = Some(version.trim().to_string());
        }

        // Parse require block
        if let Some(require_line) = line.strip_prefix("require ") {
            // Single line require
            let require_line = require_line.trim();
            if let Some(module_name) = require_line.split_whitespace().next() {
                info.main_dependencies.push(module_name.to_string());
            }
        }
    }

    // Parse multi-line require blocks
    let mut in_require_block = false;
    for line in content.lines() {
        let line = line.trim();

        if line == "require (" {
            in_require_block = true;
            continue;
        }

        if in_require_block {
            if line == ")" {
                in_require_block = false;
                continue;
            }

            // Parse dependency line
            if !line.is_empty()
                && !line.starts_with("//")
                && let Some(module_name) = line.split_whitespace().next()
            {
                info.main_dependencies.push(module_name.to_string());
            }
        }
    }
}

/// Analyze JVM project (Java/Kotlin) from build files
fn analyze_jvm_project(
    manifest_files: &[PathBuf],
    source_files: Option<&Vec<PathBuf>>,
    config: &AnalysisConfig,
) -> Result<LanguageInfo> {
    let mut info = LanguageInfo {
        name: "Java/Kotlin".to_string(),
        version: None,
        edition: None,
        package_manager: None,
        main_dependencies: Vec::new(),
        dev_dependencies: Vec::new(),
        confidence: 0.5,
        source_files: source_files.map_or(Vec::new(), |f| f.clone()),
        manifest_files: Vec::new(),
    };

    // Detect build tool and parse manifest files
    for manifest in manifest_files {
        if let Some(filename) = manifest.file_name().and_then(|n| n.to_str()) {
            info.manifest_files.push(manifest.clone());

            match filename {
                "pom.xml" => {
                    info.package_manager = Some("maven".to_string());
                    if let Ok(content) = file_utils::read_file_safe(manifest, config.max_file_size)
                    {
                        parse_maven_pom(&content, &mut info, config);
                        info.confidence = 0.90;
                    }
                }
                "build.gradle" => {
                    info.package_manager = Some("gradle".to_string());
                    if let Ok(content) = file_utils::read_file_safe(manifest, config.max_file_size)
                    {
                        parse_gradle_build(&content, &mut info, config);
                        info.confidence = 0.85;
                    }
                }
                "build.gradle.kts" => {
                    info.package_manager = Some("gradle".to_string());
                    if let Ok(content) = file_utils::read_file_safe(manifest, config.max_file_size)
                    {
                        parse_gradle_kts_build(&content, &mut info, config);
                        info.confidence = 0.85;
                    }
                }
                _ => {}
            }
        }
    }

    // Adjust name based on file types
    if let Some(files) = source_files {
        let has_kotlin = files.iter().any(|f| {
            f.extension()
                .and_then(|e| e.to_str())
                .is_some_and(|ext| ext == "kt" || ext == "kts")
        });

        if has_kotlin {
            info.name = "Kotlin".to_string();
        } else {
            info.name = "Java".to_string();
        }
    }

    // Boost confidence if we have source files
    if !info.source_files.is_empty() {
        info.confidence = (info.confidence + 0.8) / 2.0;
    }

    Ok(info)
}

/// Parse Maven pom.xml file (basic XML parsing)
fn parse_maven_pom(content: &str, info: &mut LanguageInfo, config: &AnalysisConfig) {
    // Simple regex-based XML parsing for common Maven patterns

    // Extract Java version from maven.compiler.source or java.version
    for line in content.lines() {
        let line = line.trim();

        // Look for Java version in properties
        if line.contains("<maven.compiler.source>")
            && let Some(version) = extract_xml_content(line, "maven.compiler.source")
        {
            info.version = Some(version);
        } else if line.contains("<java.version>")
            && let Some(version) = extract_xml_content(line, "java.version")
        {
            info.version = Some(version);
        } else if line.contains("<maven.compiler.target>")
            && info.version.is_none()
            && let Some(version) = extract_xml_content(line, "maven.compiler.target")
        {
            info.version = Some(version);
        }

        // Extract dependencies
        if line.contains("<groupId>")
            && line.contains("<artifactId>")
            && let Some(group_id) = extract_xml_content(line, "groupId")
            && let Some(artifact_id) = extract_xml_content(line, "artifactId")
        {
            // This is a simplified approach - real XML parsing would be better
            let dependency = format!("{}:{}", group_id, artifact_id);
            info.main_dependencies.push(dependency);
        } else if line.contains("<artifactId>")
            && !line.contains("<groupId>")
            && let Some(artifact_id) = extract_xml_content(line, "artifactId")
        {
            info.main_dependencies.push(artifact_id);
        }
    }

    // Look for dependencies in a more structured way
    let mut in_dependencies = false;
    let mut in_test_dependencies = false;

    for line in content.lines() {
        let line = line.trim();

        if line.contains("<dependencies>") {
            in_dependencies = true;
            continue;
        }

        if line.contains("</dependencies>") {
            in_dependencies = false;
            in_test_dependencies = false;
            continue;
        }

        if in_dependencies && line.contains("<scope>test</scope>") {
            in_test_dependencies = true;
        }

        if in_dependencies
            && line.contains("<artifactId>")
            && let Some(artifact_id) = extract_xml_content(line, "artifactId")
        {
            if in_test_dependencies && config.include_dev_dependencies {
                info.dev_dependencies.push(artifact_id);
            } else if !in_test_dependencies {
                info.main_dependencies.push(artifact_id);
            }
        }
    }
}

/// Parse Gradle build.gradle file (Groovy syntax)
fn parse_gradle_build(content: &str, info: &mut LanguageInfo, config: &AnalysisConfig) {
    for line in content.lines() {
        let line = line.trim();

        // Look for Java version
        if (line.contains("sourceCompatibility") || line.contains("targetCompatibility"))
            && let Some(version) = extract_gradle_version(line)
        {
            info.version = Some(version);
        } else if line.contains("JavaVersion.VERSION_")
            && let Some(pos) = line.find("VERSION_")
        {
            let version_part = &line[pos + 8..];
            if let Some(end) = version_part.find(|c: char| !c.is_numeric() && c != '_') {
                let version = &version_part[..end].replace('_', ".");
                info.version = Some(version.to_string());
            }
        }

        // Look for dependencies
        if (line.starts_with("implementation ") || line.starts_with("compile "))
            && let Some(dep) = extract_gradle_dependency(line)
        {
            info.main_dependencies.push(dep);
        } else if (line.starts_with("testImplementation ") || line.starts_with("testCompile "))
            && config.include_dev_dependencies
            && let Some(dep) = extract_gradle_dependency(line)
        {
            info.dev_dependencies.push(dep);
        }
    }
}

/// Parse Gradle build.gradle.kts file (Kotlin syntax)
fn parse_gradle_kts_build(content: &str, info: &mut LanguageInfo, config: &AnalysisConfig) {
    // Kotlin DSL is similar to Groovy but with some syntax differences
    parse_gradle_build(content, info, config); // Reuse the same logic for now
}

/// Extract content from XML tags
fn extract_xml_content(line: &str, tag: &str) -> Option<String> {
    let open_tag = format!("<{}>", tag);
    let close_tag = format!("</{}>", tag);

    if let Some(start) = line.find(&open_tag)
        && let Some(end) = line.find(&close_tag)
    {
        let content_start = start + open_tag.len();
        if content_start < end {
            return Some(line[content_start..end].trim().to_string());
        }
    }
    None
}

/// Extract version from Gradle configuration line
fn extract_gradle_version(line: &str) -> Option<String> {
    // Look for patterns like sourceCompatibility = '11' or sourceCompatibility = "11"
    let equals_pos = line.find('=')?;
    let value_part = line[equals_pos + 1..].trim();
    let start_quote = value_part.find(['\'', '"'])?;
    let quote_char = value_part.chars().nth(start_quote)?;
    let end_quote = value_part[start_quote + 1..].find(quote_char)?;
    let version = &value_part[start_quote + 1..start_quote + 1 + end_quote];
    Some(version.to_string())
}

/// Extract dependency from Gradle dependency line
fn extract_gradle_dependency(line: &str) -> Option<String> {
    // Look for patterns like implementation 'group:artifact:version' or implementation("group:artifact:version")
    let start_quote = line.find(['\'', '"'])?;
    let quote_char = line.chars().nth(start_quote)?;
    let end_quote = line[start_quote + 1..].find(quote_char)?;
    let dependency = &line[start_quote + 1..start_quote + 1 + end_quote];
    // Extract just the artifact name for simplicity
    if let Some(last_colon) = dependency.rfind(':')
        && let Some(first_colon) = dependency[..last_colon].rfind(':')
    {
        return Some(dependency[first_colon + 1..last_colon].to_string());
    }
    Some(dependency.to_string())
}

/// Check if a filename is a known manifest file
fn is_manifest_file(filename: &str) -> bool {
    matches!(
        filename,
        "Cargo.toml"
            | "Cargo.lock"
            | "package.json"
            | "package-lock.json"
            | "yarn.lock"
            | "pnpm-lock.yaml"
            | "requirements.txt"
            | "Pipfile"
            | "Pipfile.lock"
            | "pyproject.toml"
            | "setup.py"
            | "go.mod"
            | "go.sum"
            | "pom.xml"
            | "build.gradle"
            | "build.gradle.kts"
    )
}

/// Check if any of the specified manifest files exist
fn has_manifest(manifest_files: &[PathBuf], target_files: &[&str]) -> bool {
    manifest_files.iter().any(|path| {
        path.file_name()
            .and_then(|name| name.to_str())
            .is_some_and(|name| target_files.contains(&name))
    })
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::TempDir;

    #[test]
    fn test_rust_project_detection() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create Cargo.toml
        let cargo_toml = r#"
[package]
name = "test-project"
version = "0.1.0"
edition = "2021"

[dependencies]
serde = "1.0"
tokio = "1.0"

[dev-dependencies]
assert_cmd = "2.0"
"#;
        fs::write(root.join("Cargo.toml"), cargo_toml).unwrap();
        fs::create_dir_all(root.join("src")).unwrap();
        fs::write(root.join("src/main.rs"), "fn main() {}").unwrap();

        let config = AnalysisConfig::default();
        let files = vec![root.join("Cargo.toml"), root.join("src/main.rs")];

        let languages = detect_languages(&files, &config).unwrap();
        assert_eq!(languages.len(), 1);
        assert_eq!(languages[0].name, "Rust");
        assert_eq!(languages[0].version, Some("1.56+".to_string()));
        assert!(languages[0].confidence > 0.9);
    }

    #[test]
    fn test_javascript_project_detection() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create package.json
        let package_json = r#"
{
  "name": "test-project",
  "version": "1.0.0",
  "engines": {
    "node": ">=16.0.0"
  },
  "dependencies": {
    "express": "^4.18.0",
    "lodash": "^4.17.21"
  },
  "devDependencies": {
    "jest": "^29.0.0"
  }
}
"#;
        fs::write(root.join("package.json"), package_json).unwrap();
        fs::write(root.join("index.js"), "console.log('hello');").unwrap();

        let config = AnalysisConfig::default();
        let files = vec![root.join("package.json"), root.join("index.js")];

        let languages = detect_languages(&files, &config).unwrap();
        assert_eq!(languages.len(), 1);
        assert_eq!(languages[0].name, "JavaScript");
        assert_eq!(languages[0].version, Some(">=16.0.0".to_string()));
        assert!(languages[0].confidence > 0.9);
    }

    #[test]
    fn test_python_project_detection() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create pyproject.toml
        let pyproject_toml = r#"
[project]
name = "test-project"
version = "0.1.0"
requires-python = ">=3.8"
dependencies = [
    "flask>=2.0.0",
    "requests>=2.25.0",
    "pandas>=1.3.0"
]

[project.optional-dependencies]
dev = [
    "pytest>=6.0.0",
    "black>=21.0.0"
]
"#;
        fs::write(root.join("pyproject.toml"), pyproject_toml).unwrap();
        fs::write(root.join("app.py"), "print('Hello, World!')").unwrap();

        let config = AnalysisConfig::default();
        let files = vec![root.join("pyproject.toml"), root.join("app.py")];

        let languages = detect_languages(&files, &config).unwrap();
        assert_eq!(languages.len(), 1);
        assert_eq!(languages[0].name, "Python");
        assert_eq!(languages[0].version, Some(">=3.8".to_string()));
        assert!(languages[0].confidence > 0.8);
    }

    #[test]
    fn test_go_project_detection() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create go.mod
        let go_mod = r#"
module example.com/myproject

go 1.21

require (
    github.com/gin-gonic/gin v1.9.1
    github.com/stretchr/testify v1.8.4
    golang.org/x/time v0.3.0
)
"#;
        fs::write(root.join("go.mod"), go_mod).unwrap();
        fs::write(root.join("main.go"), "package main\n\nfunc main() {}").unwrap();

        let config = AnalysisConfig::default();
        let files = vec![root.join("go.mod"), root.join("main.go")];

        let languages = detect_languages(&files, &config).unwrap();
        assert_eq!(languages.len(), 1);
        assert_eq!(languages[0].name, "Go");
        assert_eq!(languages[0].version, Some("1.21".to_string()));
        assert!(languages[0].confidence > 0.8);
    }

    #[test]
    fn test_java_maven_project_detection() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create pom.xml
        let pom_xml = r#"
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0">
    <modelVersion>4.0.0</modelVersion>
    
    <groupId>com.example</groupId>
    <artifactId>test-project</artifactId>
    <version>1.0.0</version>
    
    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>
    
    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>5.3.21</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.2</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>
"#;
        fs::create_dir_all(root.join("src/main/java")).unwrap();
        fs::write(root.join("pom.xml"), pom_xml).unwrap();
        fs::write(root.join("src/main/java/App.java"), "public class App {}").unwrap();

        let config = AnalysisConfig::default();
        let files = vec![root.join("pom.xml"), root.join("src/main/java/App.java")];

        let languages = detect_languages(&files, &config).unwrap();
        assert_eq!(languages.len(), 1);
        assert_eq!(languages[0].name, "Java");
        assert_eq!(languages[0].version, Some("17".to_string()));
        assert!(languages[0].confidence > 0.8);
    }

    #[test]
    fn test_kotlin_gradle_project_detection() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create build.gradle.kts
        let build_gradle_kts = r#"
plugins {
    kotlin("jvm") version "1.9.0"
    application
}

java {
    sourceCompatibility = JavaVersion.VERSION_17
    targetCompatibility = JavaVersion.VERSION_17
}

dependencies {
    implementation("org.jetbrains.kotlin:kotlin-stdlib")
    implementation("io.ktor:ktor-server-core:2.3.2")
    testImplementation("org.jetbrains.kotlin:kotlin-test")
}
"#;
        fs::create_dir_all(root.join("src/main/kotlin")).unwrap();
        fs::write(root.join("build.gradle.kts"), build_gradle_kts).unwrap();
        fs::write(root.join("src/main/kotlin/Main.kt"), "fun main() {}").unwrap();

        let config = AnalysisConfig::default();
        let files = vec![
            root.join("build.gradle.kts"),
            root.join("src/main/kotlin/Main.kt"),
        ];

        let languages = detect_languages(&files, &config).unwrap();
        assert_eq!(languages.len(), 1);
        assert_eq!(languages[0].name, "Kotlin");
        assert!(languages[0].confidence > 0.8);
    }

    #[test]
    fn test_python_requirements_txt_detection() {
        let temp_dir = TempDir::new().unwrap();
        let root = temp_dir.path();

        // Create requirements.txt
        let requirements_txt = r#"
Flask==2.3.2
requests>=2.28.0
pandas==1.5.3
pytest==7.4.0
black>=23.0.0
"#;
        fs::write(root.join("requirements.txt"), requirements_txt).unwrap();
        fs::write(root.join("app.py"), "import flask").unwrap();

        let config = AnalysisConfig::default();
        let files = vec![root.join("requirements.txt"), root.join("app.py")];

        let languages = detect_languages(&files, &config).unwrap();
        assert_eq!(languages.len(), 1);
        assert_eq!(languages[0].name, "Python");
        assert!(languages[0].confidence > 0.8);
    }
}