zv 0.10.0

Ziglang Version Manager and Project Starter
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
use crate::app::toolchain::ToolchainManager;
use crate::cli::CleanTarget;
use crate::{App, ResolvedZigVersion, ZigVersion};
use yansi::Paint;

pub async fn clean(
    app: &mut App,
    targets: Vec<CleanTarget>,
    except: Vec<ZigVersion>,
    outdated: bool,
) -> crate::Result<()> {
    // Handle --outdated flag
    if outdated {
        let should_clean_outdated = if targets.is_empty() {
            true
        } else {
            targets.iter().any(|t| matches!(t, CleanTarget::Versions(versions) if versions.iter().any(|v| matches!(v, ZigVersion::Master(_)))))
        };

        if should_clean_outdated {
            return clean_outdated_master(app).await;
        } else {
            return Ok(());
        }
    }

    // Handle --except flag
    if !except.is_empty() {
        return clean_except_versions(app, except).await;
    }

    // Strict Target Parsing
    let mut should_clean_all = false;
    let mut should_clean_downloads = false;

    let has_all = targets.iter().any(|t| matches!(t, CleanTarget::All));
    let has_versions = targets
        .iter()
        .any(|t| matches!(t, CleanTarget::Versions(_)));

    // Validate mutual exclusivity
    if has_all && has_versions {
        eprintln!(
            "{} Usage: zv clean [all] OR zv clean <version>...",
            Paint::red("")
        );
        return Ok(());
    }

    let mut specific_versions = Vec::new();

    if targets.is_empty() {
        // No targets -> prompt for all
        if !confirm_clean_all()? {
            return Ok(());
        }
        should_clean_all = true;
        should_clean_downloads = true;
    } else if has_all {
        should_clean_all = true;
        should_clean_downloads = true;
    } else {
        // Collect versions
        for target in targets {
            match target {
                CleanTarget::Versions(versions) => specific_versions.extend(versions),
                CleanTarget::Downloads => should_clean_downloads = true,
                _ => {}
            }
        }
    }

    if should_clean_all {
        clean_all_versions(app).await?;
    } else if !specific_versions.is_empty() {
        clean_specific_versions(app, specific_versions).await?;
    }

    if should_clean_downloads {
        clean_downloads(app).await?;
    }

    // Summary
    if should_clean_all && should_clean_downloads {
        println!("{}", Paint::green("Full cleanup completed!").bold());
    }

    Ok(())
}

fn confirm_clean_all() -> crate::Result<bool> {
    if !crate::tools::supports_interactive_prompts() {
        return Ok(true); // Assume yes in non-interactive mode
    }

    use dialoguer::theme::ColorfulTheme;

    println!();
    println!(
        "{}",
        Paint::yellow("WARNING: This will remove ALL installed Zig versions and cached downloads.")
            .bold()
    );

    dialoguer::Confirm::with_theme(&ColorfulTheme::default())
        .with_prompt("Are you sure you want to continue?")
        .default(true)
        .interact()
        .map_err(|e| crate::ZvError::from(color_eyre::eyre::eyre!(e)).into())
}

/// Clean specific versions from the list
async fn clean_specific_versions(app: &mut App, versions: Vec<ZigVersion>) -> crate::Result<()> {
    // Get local master version early for resolution
    let local_master_version: Option<String> = app.toolchain_manager.get_local_master_version();

    // Resolve Master(None) to Master(Some(v)) if local_master_version is available
    let versions: Vec<ZigVersion> = versions
        .into_iter()
        .map(|v| match v {
            ZigVersion::Master(None) => {
                if let Some(ref master_ver_str) = local_master_version {
                    // Parse the local master version string to a Version
                    if let Ok(master_ver) = semver::Version::parse(master_ver_str) {
                        ZigVersion::Master(Some(master_ver))
                    } else {
                        v // Keep as Master(None) if parsing fails
                    }
                } else {
                    v // Keep as Master(None) if no local master
                }
            }
            _ => v,
        })
        .collect();

    // Deduplicate semver variants
    let versions = crate::tools::deduplicate_semver_variants(versions);

    // Format the version list for display
    let version_list: Vec<String> = versions.iter().map(|v| v.to_string()).collect();

    let versions_display = if version_list.len() == 1 {
        version_list[0].clone()
    } else {
        version_list.join(", ")
    };

    println!(
        "{}",
        Paint::cyan(&format!("Removing version(s): {}", versions_display)).bold()
    );

    let installations = ToolchainManager::scan_installations(app.versions_path())?;
    let active_install = app.toolchain_manager.get_active_install().cloned();

    let mut removed_count = 0;
    let mut not_found_count = 0;
    let mut failed_count = 0;
    let mut active_version_removed = false;
    let mut master_version_removed = false;

    for version in versions {
        let installation = match version {
            ZigVersion::Master(Some(ref v)) => {
                // Target specific master version - just match on the semver
                installations.iter().find(|i| &i.version == v)
            }
            ZigVersion::Master(None) => {
                // Target generic master - prefer local_master_version if it exists
                if let Some(ref local_master) = local_master_version {
                    installations
                        .iter()
                        .find(|i| i.is_master && i.version.to_string() == *local_master)
                } else {
                    // fallback to any master? or maybe latest master?
                    // current logic was: find ANY master (first one found)
                    installations.iter().find(|i| i.is_master)
                }
            }
            _ => installations.iter().find(|install| match &version {
                ZigVersion::Semver(target_v) => !install.is_master && &install.version == target_v,
                ZigVersion::Stable(Some(target_v)) | ZigVersion::Latest(Some(target_v)) => {
                    !install.is_master && &install.version == target_v
                }
                _ => false,
            }),
        };

        match installation {
            Some(install) => {
                let is_active = active_install.as_ref().is_some_and(|active| {
                    active.version == install.version && active.is_master == install.is_master
                });

                if is_active {
                    active_version_removed = true;
                    println!(
                        "{} Warning: Removing currently active version: {}",
                        Paint::yellow(""),
                        if install.is_master {
                            format!("master/{}", install.version)
                        } else {
                            install.version.to_string()
                        }
                    );
                }

                if install.is_master {
                    // Only clear local_master_verison if we are removing the one that is tracked
                    if let Some(ref local_master) = local_master_version {
                        if install.version.to_string() == *local_master {
                            master_version_removed = true;
                        }
                    } else {
                        // if we don't know which one is local master, assume we might be removing it?
                        // or maybe we shouldn't clear it if we don't know.
                        // But if local_master_version is None, then there is nothing to clear.
                        // So master_version_removed=true is fine, clear_local_master_version handles it.
                        master_version_removed = true;
                    }
                }

                match app.toolchain_manager.delete_install(install).await {
                    Ok(()) => {
                        removed_count += 1;
                        println!(
                            "{} Removed: {}",
                            Paint::green(""),
                            if install.is_master {
                                format!("master/{}", install.version)
                            } else {
                                install.version.to_string()
                            }
                        );
                    }
                    Err(e) => {
                        failed_count += 1;
                        eprintln!(
                            "{} Failed to remove {}: {}",
                            Paint::yellow(""),
                            if install.is_master {
                                format!("master/{}", install.version)
                            } else {
                                install.version.to_string()
                            },
                            e
                        );
                    }
                }
            }
            None => {
                not_found_count += 1;
                println!("{} Version {} not found", Paint::yellow(""), version);
            }
        }
    }

    if master_version_removed {
        let _ = app.toolchain_manager.clear_local_master_version();
    }

    if active_version_removed {
        handle_active_version_removal(app).await?;
    }

    let mut summary_parts = Vec::new();
    if removed_count > 0 {
        summary_parts.push(format!("{} removed", removed_count));
    }
    if not_found_count > 0 {
        summary_parts.push(format!("{} not found", not_found_count));
    }
    if failed_count > 0 {
        summary_parts.push(format!("{} failed", failed_count));
    }

    let summary = if summary_parts.is_empty() {
        "No versions processed".to_string()
    } else {
        summary_parts.join(", ")
    };

    println!("{} Cleanup completed: {}", Paint::green(""), summary);

    Ok(())
}

async fn clean_except_versions(
    app: &mut App,
    except_versions: Vec<ZigVersion>,
) -> crate::Result<()> {
    let except_versions = crate::tools::deduplicate_semver_variants(except_versions);

    let except_list: Vec<String> = except_versions
        .iter()
        .map(|v| match v {
            ZigVersion::Semver(ver) => ver.to_string(),
            ZigVersion::Master(Some(ver)) => format!("master/{}", ver),
            ZigVersion::Master(None) => "master".to_string(),
            _ => format!("{:?}", v),
        })
        .collect();

    let except_display = if except_list.len() == 1 {
        except_list[0].clone()
    } else {
        except_list.join(", ")
    };

    println!(
        "{}",
        Paint::cyan(&format!("Removing all versions except: {}", except_display)).bold()
    );

    let installations = ToolchainManager::scan_installations(app.versions_path())?;
    let active_install = app.toolchain_manager.get_active_install().cloned();
    let mut removed_count = 0;
    let mut kept_count = 0;
    let mut failed_count = 0;
    let mut active_version_removed = false;
    let mut found_except_versions = std::collections::HashSet::new();

    for install in &installations {
        let should_keep = except_versions.iter().any(|except_ver| {
            let matches = match except_ver {
                ZigVersion::Semver(v) => !install.is_master && v == &install.version,
                ZigVersion::Master(Some(v)) => install.is_master && v == &install.version,
                ZigVersion::Master(None) => install.is_master,
                _ => false,
            };

            if matches {
                found_except_versions.insert(except_ver.clone());
            }

            matches
        });

        if should_keep {
            kept_count += 1;
            let display_name = if install.is_master {
                format!("master/{}", install.version)
            } else {
                install.version.to_string()
            };
            println!("{} Kept: {}", Paint::green(""), display_name);
        } else {
            let is_active = active_install
                .as_ref()
                .is_some_and(|active| active == install);

            if is_active {
                active_version_removed = true;
                let display_name = if install.is_master {
                    format!("master/{}", install.version)
                } else {
                    install.version.to_string()
                };
                println!(
                    "{} Warning: Removing currently active version: {}",
                    Paint::yellow(""),
                    display_name
                );
            }

            match app.toolchain_manager.delete_install(install).await {
                Ok(()) => {
                    removed_count += 1;
                    let display_name = if install.is_master {
                        format!("master/{}", install.version)
                    } else {
                        install.version.to_string()
                    };
                    println!("{} Removed: {}", Paint::red(""), display_name);
                }
                Err(e) => {
                    failed_count += 1;
                    let display_name = if install.is_master {
                        format!("master/{}", install.version)
                    } else {
                        install.version.to_string()
                    };
                    eprintln!(
                        "{} Failed to remove {}: {}",
                        Paint::red(""),
                        display_name,
                        e
                    );
                }
            }
        }
    }

    for except_ver in &except_versions {
        if !found_except_versions.contains(except_ver) {
            let display_name = match except_ver {
                ZigVersion::Semver(v) => v.to_string(),
                ZigVersion::Master(Some(v)) => format!("master/{}", v),
                ZigVersion::Master(None) => "master".to_string(),
                _ => format!("{:?}", except_ver),
            };
            println!(
                "{} Version {} not found (specified in --except)",
                Paint::yellow(""),
                display_name
            );
        }
    }

    if removed_count == 0 && failed_count == 0 {
        println!(
            "{} No cleanup needed - all installed versions were in the --except list",
            Paint::green("")
        );
    } else {
        let mut summary_parts = Vec::new();
        if removed_count > 0 {
            summary_parts.push(format!("{} removed", removed_count));
        }
        if kept_count > 0 {
            summary_parts.push(format!("{} kept", kept_count));
        }
        if failed_count > 0 {
            summary_parts.push(format!("{} failed", failed_count));
        }

        let summary = summary_parts.join(", ");
        let icon = if failed_count > 0 {
            Paint::yellow("")
        } else {
            Paint::green("")
        };

        println!("{} Cleanup completed: {}", icon, summary);
    }

    if active_version_removed {
        handle_active_version_removal(app).await?;
    }

    Ok(())
}

async fn clean_outdated_master(app: &mut App) -> crate::Result<()> {
    println!(
        "{}",
        Paint::cyan("Removing outdated master versions...").bold()
    );

    let installations = ToolchainManager::scan_installations(app.versions_path())?;
    let active_install = app.toolchain_manager.get_active_install().cloned();
    let mut master_installs: Vec<_> = installations
        .into_iter()
        .filter(|install| install.is_master)
        .collect();

    if master_installs.is_empty() {
        println!("{} No master versions found", Paint::yellow(""));
        return Ok(());
    }

    master_installs.sort_by(|a, b| a.version.cmp(&b.version));
    let latest_master = master_installs.last().unwrap().clone();

    let mut removed_count = 0;
    let mut active_version_removed = false;

    for install in &master_installs {
        if install.version != latest_master.version {
            let is_active = active_install
                .as_ref()
                .is_some_and(|active| active == install);

            if is_active {
                active_version_removed = true;
                println!(
                    "{} Warning: Removing currently active version: master/{}",
                    Paint::yellow(""),
                    install.version
                );
            }

            match app.toolchain_manager.delete_install(install).await {
                Ok(()) => {
                    removed_count += 1;
                    println!(
                        "{} Removed outdated: master/{}",
                        Paint::red(""),
                        install.version
                    );
                }
                Err(e) => {
                    eprintln!(
                        "{} Failed to remove master/{}: {}",
                        Paint::red(""),
                        install.version,
                        e
                    );
                }
            }
        }
    }

    if removed_count == 0 {
        println!(
            "{} No outdated master versions to remove",
            Paint::green("")
        );
    } else {
        println!(
            "{} Removed {} outdated master version(s), kept latest: master/{}",
            Paint::green(""),
            removed_count,
            latest_master.version
        );
    }

    if active_version_removed {
        handle_active_version_removal(app).await?;
    }

    Ok(())
}

pub async fn clean_all_versions(app: &mut App) -> crate::Result<()> {
    println!("{}", Paint::cyan("Removing all versions...").bold());

    match app.toolchain_manager.delete_all_versions().await {
        Ok(()) => {
            println!(
                "{} Successfully cleaned versions directory",
                Paint::green("")
            );
        }
        Err(e) => {
            eprintln!(
                "{} Failed to remove versions directory: {}",
                Paint::red(""),
                e
            );
            return Err(e);
        }
    }

    Ok(())
}

pub async fn clean_downloads(app: &mut App) -> crate::Result<()> {
    println!("{}", Paint::cyan("Cleaning downloads directory...").bold());

    match app.toolchain_manager.clean_downloads_cache().await {
        Ok(()) => {
            println!(
                "{} Successfully cleaned downloads directory",
                Paint::green("")
            );
        }
        Err(e) => {
            eprintln!(
                "{} Failed to remove downloads directory: {}",
                Paint::red(""),
                e
            );
            return Err(e);
        }
    }

    Ok(())
}

async fn handle_active_version_removal(app: &mut App) -> crate::Result<()> {
    println!();

    let installations = ToolchainManager::scan_installations(app.versions_path())?;

    if installations.is_empty() {
        println!(
            "{} No Zig versions remain installed. Run 'zv use <version>' to install and activate a version.",
            Paint::cyan("")
        );
        let _ = app.toolchain_manager.clear_active_version();
        return Ok(());
    }

    let new_active = installations
        .iter()
        .filter(|install| !install.is_master)
        .max_by(|a, b| a.version.cmp(&b.version))
        .map(|install| (install, false))
        .or_else(|| {
            installations
                .iter()
                .filter(|install| install.is_master)
                .max_by(|a, b| a.version.cmp(&b.version))
                .map(|install| (install, true))
        });

    match new_active {
        Some((install, is_master)) => {
            if is_master {
                println!(
                    "{} Automatically setting new active version: master <{}>",
                    Paint::cyan(""),
                    Paint::yellow(&install.version)
                );
            } else {
                println!(
                    "{} Automatically setting new active version: <{}>",
                    Paint::cyan(""),
                    Paint::yellow(&install.version)
                );
            };

            let resolved_version = if is_master {
                ResolvedZigVersion::Master(install.version.clone())
            } else {
                ResolvedZigVersion::Semver(install.version.clone())
            };

            match app
                .set_active_version(&resolved_version, Some(install.path.clone()))
                .await
            {
                Ok(()) => {
                    println!(
                        "{} Successfully set active version to: {}",
                        Paint::green(""),
                        Paint::yellow(&install.version),
                    );
                }
                Err(e) => {
                    eprintln!(
                        "{} Failed to set active version to {}: {e}",
                        Paint::red(""),
                        Paint::yellow(&install.version),
                    );
                    println!(
                        "{} Run 'zv use {}' to manually set the active version.",
                        Paint::cyan(""),
                        Paint::yellow(&install.version),
                    );
                }
            }
        }
        None => {
            println!(
                "{} No Zig versions remain installed. Run 'zv use <version>' to install and activate a version.",
                Paint::cyan("")
            );
            let _ = app.toolchain_manager.clear_active_version();
        }
    }

    Ok(())
}