waxpkg 0.15.9

Fast Homebrew-compatible package manager
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
use crate::cache::Cache;
use crate::cask::CaskState;
use crate::discovery::discover_manually_installed_casks;
use crate::error::{Result, WaxError};
use crate::install::{remove_symlinks, InstallState};
use crate::lockfile::Lockfile;
use crate::signal::{clear_current_op, set_current_op};
use crate::ui::dirs;
use crate::ui::SPINNER_TICK_CHARS;
use console::style;
use indicatif::{ProgressBar, ProgressStyle};
use inquire::Confirm;
use std::path::Path;
use std::time::Instant;

pub async fn uninstall(
    cache: &Cache,
    formulae: &[String],
    dry_run: bool,
    cask: bool,
    yes: bool,
    all: bool,
) -> Result<()> {
    let names: Vec<String> = if all {
        let state = InstallState::new()?;
        state.sync_from_cellar().await.ok();
        let installed = state.load().await?;
        let mut names: Vec<String> = installed.keys().cloned().collect();
        names.sort();
        names
    } else {
        if formulae.is_empty() {
            return Err(WaxError::InvalidInput(
                "Specify package name(s) or use --all to uninstall everything".to_string(),
            ));
        }
        for name in formulae {
            crate::error::validate_package_name(name)?;
        }
        formulae.to_vec()
    };

    let total = names.len();
    let start = Instant::now();

    if total > 1 {
        println!("uninstalling {} packages\n", style(total).bold());
    }

    for (i, name) in names.iter().enumerate() {
        let prefix = if total > 1 {
            format!("[{}/{}] ", i + 1, total)
        } else {
            String::new()
        };
        uninstall_impl(cache, name, dry_run, cask, yes, false, &prefix).await?;
    }
    clear_current_op();

    if total > 1 && !dry_run {
        println!(
            "\n{} {} removed [{}ms]",
            style(total).bold(),
            if total == 1 { "package" } else { "packages" },
            start.elapsed().as_millis()
        );
    }

    Ok(())
}

pub async fn uninstall_quiet(cache: &Cache, formula_name: &str, cask: bool) -> Result<()> {
    uninstall_impl(cache, formula_name, false, cask, true, true, "").await
}

async fn uninstall_impl(
    cache: &Cache,
    formula_name: &str,
    dry_run: bool,
    cask: bool,
    yes: bool,
    quiet: bool,
    prefix: &str,
) -> Result<()> {
    let start = std::time::Instant::now();

    if cask {
        return uninstall_cask(cache, formula_name, dry_run, start, quiet).await;
    }

    let state = InstallState::new()?;
    let installed_packages = state.load().await?;

    let package = if let Some(pkg) = installed_packages.get(formula_name) {
        pkg.clone()
    } else {
        let cask_state = CaskState::new()?;
        let installed_casks = cask_state.load().await?;

        if installed_casks.contains_key(formula_name) {
            return uninstall_cask(cache, formula_name, dry_run, start, quiet).await;
        }

        state.sync_from_cellar().await?;
        let updated_packages = state.load().await?;

        updated_packages
            .get(formula_name)
            .cloned()
            .ok_or_else(|| WaxError::NotInstalled(formula_name.to_string()))?
    };

    let formulae = cache.load_formulae().await?;
    let dependents: Vec<String> = formulae
        .iter()
        .filter(|f| {
            if let Some(deps) = &f.dependencies {
                if deps.contains(&formula_name.to_string()) {
                    return installed_packages.contains_key(&f.name);
                }
            }
            false
        })
        .map(|f| f.name.clone())
        .collect();

    if !dependents.is_empty() && !quiet {
        println!("{} is a dependency of:", style(formula_name).magenta());
        for dep in &dependents {
            println!("  - {}", dep);
        }

        if !dry_run && !yes {
            let confirm = Confirm::new("Continue with uninstall?")
                .with_default(false)
                .prompt();

            match confirm {
                Ok(true) => {}
                Ok(false) => {
                    println!("uninstall cancelled");
                    return Ok(());
                }
                Err(_) => return Ok(()),
            }
        }
    }

    uninstall_package_direct(formula_name, &package, state, dry_run, start, quiet, prefix).await
}

async fn uninstall_package_direct(
    formula_name: &str,
    package: &crate::install::InstalledPackage,
    state: InstallState,
    dry_run: bool,
    start: std::time::Instant,
    quiet: bool,
    prefix: &str,
) -> Result<()> {
    if dry_run {
        if !quiet {
            println!(
                "{}would remove {}@{}",
                prefix,
                style(formula_name).magenta(),
                style(&package.version).dim()
            );
        }
        return Ok(());
    }

    set_current_op(format!("removing {}", formula_name));

    let spinner = if !quiet {
        let pb = ProgressBar::new_spinner();
        pb.set_style(
            ProgressStyle::default_spinner()
                .template("{spinner:.red} {msg}")
                .unwrap()
                .tick_chars(SPINNER_TICK_CHARS),
        );
        pb.enable_steady_tick(std::time::Duration::from_millis(80));
        pb.set_message(format!(
            "{}removing {}@{}...",
            prefix,
            style(formula_name).magenta(),
            style(&package.version).dim()
        ));
        Some(pb)
    } else {
        None
    };

    let install_mode = package.install_mode;
    let cellar = install_mode.cellar_path()?;

    if let Some(ref pb) = spinner {
        pb.set_message(format!(
            "{}removing {} {}",
            prefix,
            style(formula_name).magenta(),
            style("unlinking...").dim()
        ));
    }
    remove_symlinks(
        formula_name,
        &package.version,
        &cellar,
        false, /* dry_run */
        install_mode,
    )
    .await?;

    if let Some(ref pb) = spinner {
        pb.set_message(format!(
            "{}removing {} {}",
            prefix,
            style(formula_name).magenta(),
            style("deleting files...").dim()
        ));
    }
    let formula_dir = cellar.join(formula_name);
    if formula_dir.exists() {
        tokio::fs::remove_dir_all(&formula_dir).await?;
    }

    state.remove(formula_name).await?;

    let lockfile_path = Lockfile::default_path();
    if lockfile_path.exists() {
        if let Ok(mut lockfile) = Lockfile::load(&lockfile_path).await {
            lockfile.remove_package(formula_name).await;
            let _ = lockfile.save(&lockfile_path).await;
        }
    }

    if let Some(pb) = spinner {
        pb.finish_and_clear();
    }

    if !quiet {
        println!(
            "{} {}{}{} {}",
            style("").red().bold(),
            prefix,
            style(formula_name).magenta(),
            style(format!("@{}", package.version)).dim(),
            style(format!("[{}ms]", start.elapsed().as_millis())).dim(),
        );
    }

    Ok(())
}

async fn uninstall_cask(
    cache: &Cache,
    cask_name: &str,
    dry_run: bool,
    start: std::time::Instant,
    quiet: bool,
) -> Result<()> {
    let state = CaskState::new()?;
    let mut installed_casks = state.load().await?;

    // If cask not found, try discovering manually installed apps
    if !installed_casks.contains_key(cask_name) {
        let casks = cache.load_casks().await?;
        if let Ok(discovered) = discover_manually_installed_casks(&casks).await {
            for (name, cask) in discovered {
                installed_casks.entry(name).or_insert(cask);
            }
        }
    }

    // Last resort: check /Applications for a matching .app bundle
    if !installed_casks.contains_key(cask_name) {
        let app_candidates = [
            std::path::PathBuf::from("/Applications").join(format!("{}.app", cask_name)),
            dirs::home_dir()
                .map(|h| h.join("Applications").join(format!("{}.app", cask_name)))
                .unwrap_or_default(),
        ];
        for app_path in app_candidates {
            if app_path.exists() {
                let version = read_app_version_from_plist(&app_path)
                    .await
                    .unwrap_or_else(|| "unknown".to_string());
                installed_casks.insert(
                    cask_name.to_string(),
                    crate::cask::InstalledCask {
                        name: cask_name.to_string(),
                        version,
                        install_date: std::time::SystemTime::now()
                            .duration_since(std::time::UNIX_EPOCH)
                            .unwrap()
                            .as_secs() as i64,
                        artifact_type: Some("app".to_string()),
                        binary_paths: None,
                        app_name: Some(
                            app_path
                                .file_name()
                                .map(|n| n.to_string_lossy().into_owned())
                                .unwrap_or_default(),
                        ),
                    },
                );
                break;
            }
        }
    }

    let cask = installed_casks
        .get(cask_name)
        .ok_or_else(|| WaxError::NotInstalled(cask_name.to_string()))?;

    if dry_run {
        if !quiet {
            println!("- {} (cask)", cask_name);
            let elapsed = start.elapsed();
            println!("\ndry run - no changes made [{}ms]", elapsed.as_millis());
        }
        return Ok(());
    }

    let artifact_type = cask.artifact_type.as_deref().unwrap_or("dmg");

    match artifact_type {
        "tar.gz" | "binary" => {
            if let Some(binary_paths) = &cask.binary_paths {
                for binary_path in binary_paths {
                    let path = std::path::PathBuf::from(binary_path);
                    if path.exists() {
                        tokio::fs::remove_file(&path).await?;
                    }
                }
            }
        }
        "pkg" => {
            if !quiet {
                println!(
                    "PKG uninstallation not fully supported - you may need to manually remove files"
                );
            }
        }
        _ => {
            let raw_name = cask
                .app_name
                .clone()
                .unwrap_or_else(|| format!("{}.app", cask_name));
            let app_with_ext = if raw_name.ends_with(".app") {
                raw_name.clone()
            } else {
                format!("{}.app", raw_name)
            };
            let app_basename = std::path::Path::new(&app_with_ext)
                .components()
                .find(|c| {
                    matches!(c, std::path::Component::Normal(n)
                        if n.to_string_lossy().ends_with(".app"))
                })
                .map(|c| c.as_os_str().to_string_lossy().into_owned())
                .unwrap_or_else(|| {
                    // Try to find the actual app in Caskroom if not stored
                    find_app_in_caskroom(cask_name, &cask.version).unwrap_or(app_with_ext)
                });

            // On macOS: check /Applications, then ~/Applications.
            // On Linux: check ~/Applications only (no system /Applications).
            #[cfg(target_os = "macos")]
            let candidates: Vec<std::path::PathBuf> = vec![
                std::path::PathBuf::from("/Applications").join(&app_basename),
                dirs::home_dir()
                    .map(|h| h.join("Applications").join(&app_basename))
                    .unwrap_or_default(),
            ];
            #[cfg(not(target_os = "macos"))]
            let candidates: Vec<std::path::PathBuf> = vec![dirs::home_dir()
                .map(|h| h.join("Applications").join(&app_basename))
                .unwrap_or_default()];

            let mut removed = false;
            for app_path in &candidates {
                if app_path.exists() {
                    #[cfg(target_os = "macos")]
                    if tokio::fs::remove_dir_all(app_path).await.is_err() {
                        // Fall back to sudo for system-installed apps.
                        crate::sudo::sudo_remove(app_path)?;
                        removed = true;
                        break;
                    }
                    #[cfg(not(target_os = "macos"))]
                    tokio::fs::remove_dir_all(app_path).await?;
                    removed = true;
                    break;
                }
            }

            if !removed && !quiet {
                eprintln!(
                    "warning: could not find {} in Applications — \
                    you may need to remove it manually",
                    app_basename
                );
            }
        }
    }

    state.remove(cask_name).await?;

    let lockfile_path = Lockfile::default_path();
    if lockfile_path.exists() {
        if let Ok(mut lockfile) = Lockfile::load(&lockfile_path).await {
            lockfile.remove_cask(cask_name).await;
            let _ = lockfile.save(&lockfile_path).await;
        }
    }

    if !quiet {
        println!(
            "{} {}{}  {}",
            style("").red().bold(),
            style(cask_name).magenta(),
            style(format!("@{} (cask)", cask.version)).dim(),
            style(format!("[{}ms]", start.elapsed().as_millis())).dim(),
        );
    }

    Ok(())
}

fn find_app_in_caskroom(cask_name: &str, version: &str) -> Option<String> {
    let caskroom = CaskState::caskroom_dir();
    let version_dir = caskroom.join(cask_name).join(version);
    if !version_dir.exists() {
        return None;
    }

    if let Ok(entries) = std::fs::read_dir(&version_dir) {
        for entry in entries.flatten() {
            let path = entry.path();
            if path.extension().and_then(|s| s.to_str()) == Some("app") {
                return path.file_name().map(|n| n.to_string_lossy().into_owned());
            }
        }
    }
    None
}

async fn read_app_version_from_plist(path: &Path) -> Option<String> {
    let plist = path.join("Contents/Info.plist");
    if !plist.exists() {
        return None;
    }

    let output = tokio::process::Command::new("plutil")
        .arg("-extract")
        .arg("CFBundleShortVersionString")
        .arg("raw")
        .arg("-o")
        .arg("-")
        .arg(&plist)
        .output()
        .await
        .ok()?;

    if !output.status.success() {
        return None;
    }

    let value = String::from_utf8_lossy(&output.stdout).trim().to_string();
    if value.is_empty() {
        None
    } else {
        Some(value)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_find_app_in_caskroom_nonexistent() {
        let result = find_app_in_caskroom("nonexistent", "1.0.0");
        assert_eq!(result, None);
    }
}