ym 0.3.58

Yummy - A modern Java build tool
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
use anyhow::Result;
use console::style;
use std::process::Command;

/// Diagnose environment issues
pub fn execute(fix: bool) -> Result<()> {
    println!();
    println!("  {}", style("ym doctor").bold());
    println!();

    let mut ok = true;

    // Check Java
    ok &= check_command("java", &["-version"], "Java Runtime");

    // Check javac
    ok &= check_command("javac", &["-version"], "Java Compiler (javac)");

    // Check JAVA_HOME
    check_java_home();

    // Check JDK version matches target
    check_jdk_version_match(fix);

    // Check jar
    ok &= check_command("jar", &["--version"], "JAR tool");

    // Check git
    check_command("git", &["--version"], "Git");

    // Check package.toml
    check_config();

    // Check Maven cache
    check_maven_cache();

    // Check JAR integrity (SHA-256)
    ok &= check_jar_integrity(fix);

    // Check permissions
    check_permissions(fix);

    // Check project structure
    check_project_structure(fix);

    println!();
    if ok {
        println!(
            "  {} All critical checks passed",
            style("").green().bold()
        );
    } else if fix {
        println!(
            "  {} Some issues were auto-fixed, but missing tools require manual installation",
            style("!").yellow().bold()
        );
    } else {
        println!(
            "  {} Some checks failed. Run {} to auto-fix or install missing tools.",
            style("").red().bold(),
            style("ym doctor --fix").cyan()
        );
    }
    println!();

    Ok(())
}

fn check_project_structure(fix: bool) {
    let cwd = std::env::current_dir().unwrap_or_default();
    let config_path = crate::config::find_config(&cwd);

    if config_path.is_none() {
        return;
    }

    let project = config_path.as_ref().unwrap().parent().unwrap_or(&cwd);

    // Check src directory
    let src = project.join("src");
    if !src.exists() {
        if fix {
            let _ = std::fs::create_dir_all(&src);
            println!(
                "  {} Created missing src/ directory",
                style("").green()
            );
        } else {
            println!(
                "  {} src/ directory missing (run --fix to create)",
                style("!").yellow()
            );
        }
    } else {
        println!(
            "  {} src/ directory",
            style("").green()
        );
    }

    // Check .gitignore
    let gitignore = project.join(".gitignore");
    if !gitignore.exists() {
        if fix {
            let _ = std::fs::write(&gitignore, "out/\n.ym/\n.ym-sources.txt\n*.class\n");
            println!(
                "  {} Created .gitignore",
                style("").green()
            );
        } else {
            println!(
                "  {} .gitignore missing (run --fix to create)",
                style("!").yellow()
            );
        }
    } else {
        // Check if .ym/ is in gitignore
        let content = std::fs::read_to_string(&gitignore).unwrap_or_default();
        if !content.contains(".ym/") {
            if fix {
                let _ = std::fs::write(&gitignore, format!("{}\n.ym/\nout/\n", content));
                println!(
                    "  {} Added .ym/ and out/ to .gitignore",
                    style("").green()
                );
            } else {
                println!(
                    "  {} .gitignore exists but missing .ym/ entry (run --fix)",
                    style("!").yellow()
                );
            }
        } else {
            println!(
                "  {} .gitignore",
                style("").green()
            );
        }
    }
}

fn check_command(cmd: &str, args: &[&str], label: &str) -> bool {
    match Command::new(cmd).args(args).output() {
        Ok(output) if output.status.success() => {
            let ver = String::from_utf8_lossy(&output.stdout);
            let ver_err = String::from_utf8_lossy(&output.stderr);
            // java -version outputs to stderr
            let version_line = if ver.trim().is_empty() {
                ver_err.lines().next().unwrap_or("").trim().to_string()
            } else {
                ver.lines().next().unwrap_or("").trim().to_string()
            };
            println!(
                "  {} {}  {}",
                style("").green(),
                label,
                style(&version_line).dim()
            );
            true
        }
        _ => {
            println!(
                "  {} {}  {}",
                style("").red(),
                label,
                style("not found").red()
            );
            false
        }
    }
}

fn check_java_home() {
    match std::env::var("JAVA_HOME") {
        Ok(home) if !home.is_empty() => {
            let exists = std::path::Path::new(&home).exists();
            if exists {
                println!(
                    "  {} JAVA_HOME  {}",
                    style("").green(),
                    style(&home).dim()
                );
            } else {
                println!(
                    "  {} JAVA_HOME  {} (path does not exist)",
                    style("!").yellow(),
                    style(&home).dim()
                );
            }
        }
        _ => {
            println!(
                "  {} JAVA_HOME  {}",
                style("!").yellow(),
                style("not set (ym will use java from PATH)").dim()
            );
        }
    }
}

fn check_jdk_version_match(fix: bool) {
    let cwd = std::env::current_dir().unwrap_or_default();
    let config_path = crate::config::find_config(&cwd);
    let target = config_path
        .and_then(|p| crate::config::load_config(&p).ok())
        .and_then(|cfg| cfg.target.clone());

    if let Some(ref target) = target {
        // Get current java version
        let output = Command::new("java").arg("-version").output();
        if let Ok(out) = output {
            let stderr = String::from_utf8_lossy(&out.stderr);
            // Parse version from output like: openjdk version "21.0.1" or java version "1.8.0_xxx"
            let detected = stderr.lines().next().and_then(|line| {
                let start = line.find('"')?;
                let end = line[start + 1..].find('"')?;
                let ver = &line[start + 1..start + 1 + end];
                // Extract major version: "21.0.1" → "21", "1.8.0_xxx" → "8"
                if ver.starts_with("1.") {
                    ver.split('.').nth(1).map(|s| s.to_string())
                } else {
                    ver.split('.').next().map(|s| s.to_string())
                }
            });

            if let Some(ref major) = detected {
                if major == target {
                    println!(
                        "  {} JDK version  {} (matches target {})",
                        style("").green(),
                        style(major).dim(),
                        target
                    );
                } else if fix {
                    println!(
                        "  {} JDK version {} does not match target {}, downloading...",
                        style("").green(),
                        major,
                        target
                    );
                    match crate::jvm::ensure_jdk(target, None, true) {
                        Ok(path) => {
                            println!(
                                "  {} Downloaded JDK {} to {}",
                                style("").green(),
                                target,
                                style(path.display()).dim()
                            );
                        }
                        Err(e) => {
                            println!(
                                "  {} Failed to download JDK {}: {}",
                                style("").red(),
                                target,
                                e
                            );
                        }
                    }
                } else {
                    println!(
                        "  {} JDK version {} does not match target {} (run --fix to download)",
                        style("!").yellow(),
                        major,
                        target
                    );
                }
            }
        } else if fix {
            // No java found at all, try to download
            println!(
                "  {} No Java found, downloading JDK {}...",
                style("").green(),
                target
            );
            match crate::jvm::ensure_jdk(target, None, true) {
                Ok(path) => {
                    println!(
                        "  {} Downloaded JDK {} to {}",
                        style("").green(),
                        target,
                        style(path.display()).dim()
                    );
                }
                Err(e) => {
                    println!(
                        "  {} Failed to download JDK {}: {}",
                        style("").red(),
                        target,
                        e
                    );
                }
            }
        }
    }
}

fn check_config() {
    let cwd = std::env::current_dir().unwrap_or_default();
    if let Some(path) = crate::config::find_config(&cwd) {
        match crate::config::load_config(&path) {
            Ok(cfg) => {
                let project = crate::config::project_dir(&path);
                println!(
                    "  {} package.toml  {} ({})",
                    style("").green(),
                    style(&cfg.name).dim(),
                    style(path.display()).dim()
                );
                // Schema validation
                validate_config_schema(&cfg, &project);
            }
            Err(e) => {
                println!(
                    "  {} package.toml  {} ({})",
                    style("").red(),
                    style("parse error").red(),
                    style(e).dim()
                );
            }
        }
    } else {
        println!(
            "  {} package.toml  {}",
            style("-").dim(),
            style("not found in current directory tree").dim()
        );
    }
}

fn validate_config_schema(cfg: &crate::config::schema::YmConfig, project: &std::path::Path) {
    // Build workspace graph if this is a workspace root (for module existence checks)
    let ws = if cfg.workspaces.is_some() {
        crate::workspace::graph::WorkspaceGraph::build(project).ok()
    } else {
        None
    };
    let ws_packages: std::collections::HashSet<String> = ws
        .as_ref()
        .map(|w| w.all_packages().into_iter().collect())
        .unwrap_or_default();

    // Load root config for { workspace = true } Maven dep validation
    let root_cfg = crate::config::find_workspace_root(project)
        .and_then(|root| {
            if root != project {
                crate::config::load_config(&root.join(crate::config::CONFIG_FILE)).ok()
            } else {
                None
            }
        });

    // Validate dependency coordinate format
    if let Some(ref deps) = cfg.dependencies {
        for (key, value) in deps {
            if crate::config::schema::is_maven_dep(key) {
                // Maven coordinate: validate format
                if key.contains(':') {
                    let parts: Vec<&str> = key.split(':').collect();
                    if parts.len() != 2 || parts[0].is_empty() || parts[1].is_empty() {
                        println!(
                            "  {} Invalid coordinate format: '{}' (expected groupId:artifactId or @scope/name)",
                            style("!").yellow(),
                            key
                        );
                    }
                }
                // Validate scope value
                let scope = value.scope();
                if !["compile", "runtime", "provided", "test"].contains(&scope) {
                    println!(
                        "  {} Invalid scope '{}' for dependency '{}'",
                        style("!").yellow(),
                        scope,
                        key
                    );
                }
                // Validate { workspace = true } references root version
                if value.is_workspace() {
                    if let Some(ref root) = root_cfg {
                        let found = root.dependencies.as_ref()
                            .and_then(|d| d.get(key))
                            .and_then(|v| v.version())
                            .is_some();
                        if !found {
                            println!(
                                "  {} Dependency '{}' uses {{workspace = true}} but root has no version for it",
                                style("!").yellow(),
                                key
                            );
                        }
                    }
                }
            } else {
                // Module reference: must have workspace = true
                if !value.is_workspace() {
                    println!(
                        "  {} Dependency '{}' has no colon but no workspace = true",
                        style("!").yellow(),
                        key
                    );
                } else if !ws_packages.is_empty() && !ws_packages.contains(key) {
                    // Check if referenced module exists in workspace
                    println!(
                        "  {} Module dependency '{}' not found in workspace",
                        style("!").yellow(),
                        key
                    );
                }
            }
        }
    }

    // Validate target value
    if let Some(ref target) = cfg.target {
        if target.parse::<u32>().is_err() {
            println!(
                "  {} target '{}' is not a valid Java version number",
                style("!").yellow(),
                target
            );
        }
    }
}

fn check_maven_cache() {
    let cache = crate::config::maven_cache_dir();
    if cache.exists() {
        let size = crate::config::dir_size(&cache);
        let jar_count = count_files_with_ext(&cache, "jar");
        println!(
            "  {} Maven cache  {} ({} JARs)",
            style("").green(),
            style(crate::config::format_size(size)).dim(),
            jar_count
        );
    } else {
        println!(
            "  {} Maven cache  {}",
            style("-").dim(),
            style("empty").dim()
        );
    }
}

fn check_jar_integrity(fix: bool) -> bool {
    let cwd = std::env::current_dir().unwrap_or_default();
    let resolved = match crate::config::load_resolved_cache(&cwd) {
        Ok(r) => r,
        Err(_) => return true, // No resolved.json, nothing to check
    };

    if resolved.dependencies.is_empty() {
        return true;
    }

    let cache = crate::config::maven_cache_dir();
    let mut all_ok = true;
    let mut missing = 0;
    let mut corrupt = 0;

    for (key, entry) in &resolved.dependencies {
        let parts: Vec<&str> = key.split(':').collect();
        if parts.len() != 3 {
            continue;
        }
        let jar_path = cache
            .join(parts[0])
            .join(parts[1])
            .join(parts[2])
            .join(format!("{}-{}.jar", parts[1], parts[2]));

        if !jar_path.exists() {
            missing += 1;
            all_ok = false;
            continue;
        }

        if let Some(ref expected_sha) = entry.sha256 {
            if let Ok(data) = std::fs::read(&jar_path) {
                let actual = crate::compiler::incremental::hash_bytes(&data);
                if &actual != expected_sha {
                    corrupt += 1;
                    all_ok = false;
                }
            }
        }
    }

    if all_ok {
        println!(
            "  {} Dependency integrity  {} deps verified",
            style("").green(),
            resolved.dependencies.len()
        );
    } else if fix {
        // Delete corrupt/missing JARs and clear resolved cache so next build re-downloads
        let resolved_path = cwd.join(".ym").join("resolved.json");
        let _ = std::fs::remove_file(&resolved_path);
        println!(
            "  {} Cleared resolved cache ({} missing, {} corrupt). Next build will re-download.",
            style("").green(),
            missing,
            corrupt
        );
    } else {
        println!(
            "  {} Dependency integrity  {} missing, {} corrupt (run --fix to re-download)",
            style("!").yellow(),
            missing,
            corrupt
        );
    }

    all_ok
}

fn count_files_with_ext(dir: &std::path::Path, ext: &str) -> usize {
    walkdir::WalkDir::new(dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().and_then(|x| x.to_str()) == Some(ext))
        .count()
}

#[allow(unused_variables)]
fn check_permissions(fix: bool) {
    // Check ~/.ym/credentials.json permissions (should be 0o600)
    let creds_path = crate::home_dir().join(".ym").join("credentials.json");
    if creds_path.exists() {
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            if let Ok(meta) = std::fs::metadata(&creds_path) {
                let mode = meta.permissions().mode() & 0o777;
                if mode != 0o600 {
                    if fix {
                        let _ = std::fs::set_permissions(
                            &creds_path,
                            std::fs::Permissions::from_mode(0o600),
                        );
                        println!(
                            "  {} Fixed credentials.json permissions (was {:o}, now 600)",
                            style("").green(),
                            mode
                        );
                    } else {
                        println!(
                            "  {} credentials.json permissions {:o} (should be 600, run --fix)",
                            style("!").yellow(),
                            mode
                        );
                    }
                } else {
                    println!(
                        "  {} credentials.json permissions",
                        style("").green()
                    );
                }
            }
        }
        #[cfg(not(unix))]
        {
            println!(
                "  {} credentials.json exists",
                style("").green()
            );
        }
    }

    // Check .ym/ directory permissions
    let cwd = std::env::current_dir().unwrap_or_default();
    let ym_dir = cwd.join(".ym");
    if ym_dir.exists() {
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            if let Ok(meta) = std::fs::metadata(&ym_dir) {
                let mode = meta.permissions().mode() & 0o777;
                if mode != 0o755 {
                    if fix {
                        let _ = std::fs::set_permissions(
                            &ym_dir,
                            std::fs::Permissions::from_mode(0o755),
                        );
                        println!(
                            "  {} Fixed .ym/ directory permissions (was {:o}, now 755)",
                            style("").green(),
                            mode
                        );
                    } else {
                        println!(
                            "  {} .ym/ directory permissions {:o} (should be 755, run --fix)",
                            style("!").yellow(),
                            mode
                        );
                    }
                } else {
                    println!(
                        "  {} .ym/ directory permissions",
                        style("").green()
                    );
                }
            }
        }
    }
}