waxpkg 0.19.2

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
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
//! winget-pkgs installs.
//! Uses the public GitHub API / raw.githubusercontent.com — no winget.exe.

use crate::bottle::BottleDownloader;
use crate::error::{Result, WaxError};
use crate::package_spec::Ecosystem;
use crate::scoop;
use crate::version;
use crate::windows_state::{self, WindowsNativeUninstall, WindowsPackageManifest};
use indicatif::{ProgressBar, ProgressStyle};
use serde::Deserialize;
use std::path::{Component, Path, PathBuf};
use std::process::Command;
use tempfile::TempDir;
use tracing::debug;

const WINGET_PKGS_REPO_CONTENTS: &str =
    "https://api.github.com/repos/microsoft/winget-pkgs/contents";
const WINGET_PKGS_RAW: &str = "https://raw.githubusercontent.com/microsoft/winget-pkgs/master";

#[derive(Debug, Deserialize)]
struct GhContentEntry {
    name: String,
    #[serde(rename = "type")]
    entry_type: String,
    path: String,
}

fn package_id_to_content_path(id: &str) -> Result<String> {
    let parts: Vec<&str> = id.split('.').filter(|s| !s.is_empty()).collect();
    if parts.len() < 2 {
        return Err(WaxError::InvalidInput(
            "winget PackageIdentifier needs at least two dot-separated segments (e.g. JesseDuffield.lazygit)"
                .into(),
        ));
    }
    let first = parts[0]
        .chars()
        .next()
        .ok_or_else(|| WaxError::InvalidInput("empty winget id".into()))?
        .to_ascii_lowercase();
    Ok(format!("manifests/{}/{}", first, parts.join("/")))
}

fn github_client() -> Result<reqwest::Client> {
    reqwest::Client::builder()
        .timeout(std::time::Duration::from_secs(120))
        .user_agent(concat!(
            "wax/",
            env!("CARGO_PKG_VERSION"),
            " (winget-resolve)"
        ))
        .build()
        .map_err(|e| WaxError::InstallError(e.to_string()))
}

async fn gh_get_json(url: &str) -> Result<Vec<GhContentEntry>> {
    let client = github_client()?;
    let resp = client.get(url).send().await?;
    if !resp.status().is_success() {
        return Err(WaxError::InstallError(format!(
            "GitHub API {} -> HTTP {}",
            url,
            resp.status()
        )));
    }
    let v: serde_json::Value = resp.json().await?;
    if v.is_array() {
        Ok(serde_json::from_value(v).map_err(WaxError::JsonError)?)
    } else {
        Err(WaxError::InstallError(
            "Unexpected GitHub API response (expected directory listing)".into(),
        ))
    }
}

/// True if `microsoft/winget-pkgs` has a manifest directory for this PackageIdentifier.
#[cfg(target_os = "windows")]
pub async fn winget_package_exists(package_id: &str) -> bool {
    let Ok(path) = package_id_to_content_path(package_id) else {
        return false;
    };
    let url = format!("{WINGET_PKGS_REPO_CONTENTS}/{path}?ref=master");
    gh_get_json(&url)
        .await
        .map(|v| !v.is_empty())
        .unwrap_or(false)
}

fn winget_arch_token() -> &'static str {
    match std::env::consts::ARCH {
        "x86_64" => "x64",
        "aarch64" => "arm64",
        "x86" => "x86",
        _ => "x64",
    }
}

fn join_under_root(root: &Path, rel: &Path) -> Result<PathBuf> {
    for component in rel.components() {
        match component {
            Component::Normal(_) | Component::CurDir => {}
            Component::ParentDir | Component::RootDir | Component::Prefix(_) => {
                return Err(WaxError::InstallError(format!(
                    "Unsafe path in winget manifest: {}",
                    rel.display()
                )));
            }
        }
    }
    Ok(root.join(rel))
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct WingetInstallerDoc {
    installer_type: Option<String>,
    nested_installer_type: Option<String>,
    nested_installer_files: Option<Vec<WingetNestedFile>>,
    installer_switches: Option<WingetInstallerSwitches>,
    apps_and_features_entries: Option<Vec<WingetAppsAndFeaturesEntry>>,
    installers: Vec<WingetInstallerEntry>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct WingetNestedFile {
    relative_file_path: String,
    portable_command_alias: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct WingetInstallerEntry {
    architecture: String,
    installer_url: String,
    installer_sha256: String,
    installer_type: Option<String>,
    installer_switches: Option<WingetInstallerSwitches>,
    product_code: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct WingetInstallerSwitches {
    silent: Option<String>,
    silent_with_progress: Option<String>,
    custom: Option<String>,
    install_location: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "PascalCase")]
struct WingetAppsAndFeaturesEntry {
    product_code: Option<String>,
    package_family_name: Option<String>,
    silent_uninstall_string: Option<String>,
}

fn pick_installer(doc: &WingetInstallerDoc) -> Result<&WingetInstallerEntry> {
    let want = winget_arch_token();
    doc.installers
        .iter()
        .find(|i| i.architecture.eq_ignore_ascii_case(want))
        .or_else(|| doc.installers.first())
        .ok_or_else(|| WaxError::InstallError("winget manifest has no installers".into()))
}

fn installer_type_for<'a>(doc: &'a WingetInstallerDoc, inst: &'a WingetInstallerEntry) -> &'a str {
    inst.installer_type
        .as_deref()
        .or(doc.installer_type.as_deref())
        .unwrap_or("")
}

fn installer_switches_for<'a>(
    doc: &'a WingetInstallerDoc,
    inst: &'a WingetInstallerEntry,
) -> Option<&'a WingetInstallerSwitches> {
    inst.installer_switches
        .as_ref()
        .or(doc.installer_switches.as_ref())
}

fn split_switches(raw: &str) -> Vec<String> {
    raw.split_whitespace().map(|s| s.to_string()).collect()
}

fn msi_install_args(path: &Path) -> Vec<String> {
    vec![
        "/i".into(),
        path.to_string_lossy().to_string(),
        "/qn".into(),
        "/norestart".into(),
    ]
}

fn msi_uninstall(product_code: &str) -> WindowsNativeUninstall {
    WindowsNativeUninstall {
        command: "msiexec.exe".into(),
        args: vec![
            "/x".into(),
            product_code.to_string(),
            "/qn".into(),
            "/norestart".into(),
        ],
    }
}

fn ps_quote(s: &str) -> String {
    format!("'{}'", s.replace('\'', "''"))
}

fn msix_install_args(path: &Path) -> Vec<String> {
    vec![
        "-NoProfile".into(),
        "-ExecutionPolicy".into(),
        "Bypass".into(),
        "-Command".into(),
        format!(
            "Add-AppxPackage -Path {}",
            ps_quote(&path.to_string_lossy())
        ),
    ]
}

fn msix_uninstall(package_family_name: &str) -> WindowsNativeUninstall {
    WindowsNativeUninstall {
        command: "powershell.exe".into(),
        args: vec![
            "-NoProfile".into(),
            "-ExecutionPolicy".into(),
            "Bypass".into(),
            "-Command".into(),
            format!(
                "Get-AppxPackage -PackageFamilyName {} | Remove-AppxPackage",
                ps_quote(package_family_name)
            ),
        ],
    }
}

fn exe_install_args(switches: &WingetInstallerSwitches) -> Result<Vec<String>> {
    let raw = switches
        .silent
        .as_deref()
        .or(switches.silent_with_progress.as_deref())
        .ok_or_else(|| {
            WaxError::InstallError("winget exe installer is missing silent install switches".into())
        })?;
    let mut args = split_switches(raw);
    if let Some(custom) = &switches.custom {
        args.extend(split_switches(custom));
    }
    if let Some(location) = &switches.install_location {
        args.extend(split_switches(location));
    }
    Ok(args)
}

fn exe_uninstall(doc: &WingetInstallerDoc) -> Result<WindowsNativeUninstall> {
    let raw = doc
        .apps_and_features_entries
        .as_deref()
        .unwrap_or(&[])
        .iter()
        .find_map(|entry| entry.silent_uninstall_string.as_deref())
        .ok_or_else(|| {
            WaxError::InstallError(
                "winget exe installer is missing silent uninstall metadata".into(),
            )
        })?;
    let mut parts = split_switches(raw);
    let command = parts
        .drain(..1)
        .next()
        .ok_or_else(|| WaxError::InstallError("empty silent uninstall metadata".into()))?;
    Ok(WindowsNativeUninstall {
        command,
        args: parts,
    })
}

fn product_code_for(doc: &WingetInstallerDoc, inst: &WingetInstallerEntry) -> Option<String> {
    inst.product_code.clone().or_else(|| {
        doc.apps_and_features_entries
            .as_deref()
            .unwrap_or(&[])
            .iter()
            .find_map(|entry| entry.product_code.clone())
    })
}

fn package_family_name_for(doc: &WingetInstallerDoc) -> Option<String> {
    doc.apps_and_features_entries
        .as_deref()
        .unwrap_or(&[])
        .iter()
        .find_map(|entry| entry.package_family_name.clone())
}

fn native_plan(
    doc: &WingetInstallerDoc,
    inst: &WingetInstallerEntry,
    installer_path: &Path,
) -> Result<(String, Vec<String>, WindowsNativeUninstall)> {
    let inst_type = installer_type_for(doc, inst).to_ascii_lowercase();
    match inst_type.as_str() {
        "msi" | "wix" => {
            let product_code = product_code_for(doc, inst).ok_or_else(|| {
                WaxError::InstallError(
                    "winget msi installer is missing ProductCode for managed uninstall".into(),
                )
            })?;
            Ok((
                "msiexec.exe".into(),
                msi_install_args(installer_path),
                msi_uninstall(&product_code),
            ))
        }
        "msix" | "appx" => {
            let family = package_family_name_for(doc).ok_or_else(|| {
                WaxError::InstallError(
                    "winget msix installer is missing PackageFamilyName for managed uninstall"
                        .into(),
                )
            })?;
            Ok((
                "powershell.exe".into(),
                msix_install_args(installer_path),
                msix_uninstall(&family),
            ))
        }
        "exe" | "inno" | "nullsoft" | "burn" => {
            let switches = installer_switches_for(doc, inst).ok_or_else(|| {
                WaxError::InstallError("winget exe installer is missing InstallerSwitches".into())
            })?;
            Ok((
                installer_path.to_string_lossy().to_string(),
                exe_install_args(switches)?,
                exe_uninstall(doc)?,
            ))
        }
        _ => Err(WaxError::InstallError(format!(
            "wax does not support native winget InstallerType={inst_type}"
        ))),
    }
}

fn run_native_command(command: &str, args: &[String]) -> Result<()> {
    if !cfg!(target_os = "windows") {
        return Err(WaxError::PlatformNotSupported(
            "native Windows installer execution is only supported on Windows".into(),
        ));
    }
    let status = Command::new(command).args(args).status()?;
    if status.success() {
        Ok(())
    } else {
        Err(WaxError::InstallError(format!(
            "native installer command failed with {status}: {command}"
        )))
    }
}

pub async fn install_winget_package(package_id: &str) -> Result<()> {
    if !cfg!(target_os = "windows") {
        return Err(WaxError::PlatformNotSupported(
            "winget install is only supported on Windows".into(),
        ));
    }

    let rel = package_id_to_content_path(package_id)?;
    let list_url = format!("{WINGET_PKGS_REPO_CONTENTS}/{rel}?ref=master");
    let entries = gh_get_json(&list_url).await?;

    let mut versions: Vec<String> = entries
        .iter()
        .filter(|e| e.entry_type == "dir")
        .map(|e| e.name.clone())
        .collect();
    if versions.is_empty() {
        return Err(WaxError::FormulaNotFound(format!(
            "no version folders under winget-pkgs/{rel}"
        )));
    }
    version::sort_versions(&mut versions);
    let latest = versions
        .last()
        .ok_or_else(|| WaxError::InstallError("no winget versions".into()))?
        .clone();

    let ver_url = format!("{WINGET_PKGS_REPO_CONTENTS}/{rel}/{latest}?ref=master");
    let files = gh_get_json(&ver_url).await?;
    let installer_yaml = files
        .iter()
        .find(|e| e.name.ends_with(".installer.yaml") && e.entry_type == "file")
        .ok_or_else(|| {
            WaxError::InstallError(
                "No .installer.yaml in latest winget version (wax only supports installer manifests)"
                    .into(),
            )
        })?;

    let yaml_path = &installer_yaml.path;
    let raw_url = format!("{WINGET_PKGS_RAW}/{yaml_path}");
    debug!("Fetching winget installer yaml {}", raw_url);
    let yaml_text = github_client()?
        .get(&raw_url)
        .send()
        .await?
        .error_for_status()?
        .text()
        .await?;

    let doc: WingetInstallerDoc =
        serde_yaml::from_str(&yaml_text).map_err(|e| WaxError::ParseError(e.to_string()))?;

    let inst = pick_installer(&doc)?;
    let inst_type = installer_type_for(&doc, inst);
    let nested = doc.nested_installer_type.as_deref().unwrap_or("");
    let sha_expected = inst.installer_sha256.trim().to_ascii_lowercase();

    let tmp = TempDir::new()?;
    let archive_name = Path::new(&inst.installer_url)
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("winget-installer.bin");
    let archive_path = tmp.path().join(archive_name);

    let dl = BottleDownloader::new();
    let size = dl.probe_size(&inst.installer_url).await;
    let conns =
        BottleDownloader::num_connections(size, BottleDownloader::MAX_CONNECTIONS_PER_DOWNLOAD);
    let pb = ProgressBar::new(0);
    pb.set_style(
        ProgressStyle::default_bar()
            .template("{spinner:.cyan} {msg} [{bar:30.cyan/blue}] {bytes}/{total_bytes}")
            .unwrap()
            .progress_chars("=>-"),
    );
    pb.set_message(format!("{} {}", package_id, latest));

    dl.download(&inst.installer_url, &archive_path, Some(&pb), conns, None)
        .await?;
    pb.finish_and_clear();

    BottleDownloader::verify_checksum(&archive_path, &sha_expected)?;

    if !inst_type.eq_ignore_ascii_case("zip") || !nested.eq_ignore_ascii_case("portable") {
        return install_native_winget_package(package_id, &latest, &doc, inst, &archive_path).await;
    }

    let extract_root = tmp.path().join("extract");
    std::fs::create_dir_all(&extract_root)?;
    scoop::extract_zip_file(&archive_path, &extract_root)?;

    let bin_dir = windows_state::wax_bin_dir()?;
    std::fs::create_dir_all(&bin_dir)?;

    let nested_files = doc.nested_installer_files.as_ref().ok_or_else(|| {
        WaxError::InstallError("winget manifest missing NestedInstallerFiles".into())
    })?;

    let mut copy_actions = Vec::new();
    for nf in nested_files {
        let rel = PathBuf::from(nf.relative_file_path.replace('\\', "/"));
        let src = join_under_root(&extract_root, &rel)?;
        if !src.exists() {
            return Err(WaxError::InstallError(format!(
                "nested portable file missing: {}",
                src.display()
            )));
        }
        let dest_name = nf
            .portable_command_alias
            .as_ref()
            .map(|s| format!("{s}.exe"))
            .unwrap_or_else(|| {
                src.file_name()
                    .map(|n| n.to_string_lossy().into_owned())
                    .unwrap_or_else(|| "app.exe".into())
            });
        let dest = bin_dir.join(dest_name);
        copy_actions.push((src, dest));
    }
    let bin_links: Vec<PathBuf> = copy_actions.iter().map(|(_, dest)| dest.clone()).collect();
    windows_state::validate_bin_links_available(Ecosystem::Winget, package_id, &bin_links)?;

    for (src, dest) in copy_actions {
        if dest.exists() {
            let _ = std::fs::remove_file(&dest);
        }
        std::fs::copy(&src, &dest)?;
    }

    let staging = windows_state::wax_windows_root()?
        .join("winget-apps")
        .join(package_id.replace('.', "_"))
        .join(&latest);
    if staging.exists() {
        let _ = std::fs::remove_dir_all(&staging);
    }
    std::fs::create_dir_all(staging.parent().unwrap())?;
    crate::ui::copy_dir_all(&extract_root, &staging)?;

    let mut files = windows_state::collect_files(&staging)?;
    files.extend(bin_links.iter().cloned());
    WindowsPackageManifest::new(
        Ecosystem::Winget,
        package_id,
        latest.clone(),
        inst.installer_url.clone(),
        staging.clone(),
        bin_links,
        files,
    )
    .save()?;

    println!(
        "Installed {} {} (winget portable zip) — binaries under:\n  {}",
        package_id,
        latest,
        bin_dir.display()
    );

    Ok(())
}

async fn install_native_winget_package(
    package_id: &str,
    latest: &str,
    doc: &WingetInstallerDoc,
    inst: &WingetInstallerEntry,
    installer_path: &Path,
) -> Result<()> {
    let staging = windows_state::wax_windows_root()?
        .join("winget-installers")
        .join(package_id.replace('.', "_"))
        .join(latest);
    if staging.exists() {
        let _ = std::fs::remove_dir_all(&staging);
    }
    let staged_installer = staging.join(
        installer_path
            .file_name()
            .unwrap_or_else(|| std::ffi::OsStr::new("installer.bin")),
    );
    let (command, args, uninstall) = native_plan(doc, inst, &staged_installer)?;
    std::fs::create_dir_all(&staging)?;
    std::fs::copy(installer_path, &staged_installer)?;

    if let Err(err) = run_native_command(&command, &args) {
        let _ = std::fs::remove_dir_all(&staging);
        return Err(err);
    }

    let files = windows_state::collect_files(&staging)?;
    let manifest = WindowsPackageManifest::new(
        Ecosystem::Winget,
        package_id,
        latest.to_string(),
        inst.installer_url.clone(),
        staging.clone(),
        Vec::new(),
        files,
    )
    .with_native_uninstall(uninstall);
    if let Err(err) = manifest.save() {
        if let Some(native) = &manifest.native_uninstall {
            let _ = run_native_command(&native.command, &native.args);
        }
        let _ = std::fs::remove_dir_all(&staging);
        return Err(err);
    }

    println!(
        "Installed {} {} (winget native installer)",
        package_id, latest
    );

    Ok(())
}

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

    fn entry(installer_type: &str) -> WingetInstallerEntry {
        WingetInstallerEntry {
            architecture: "x64".into(),
            installer_url: "https://example.invalid/app.msi".into(),
            installer_sha256: "abc".into(),
            installer_type: Some(installer_type.into()),
            installer_switches: None,
            product_code: None,
        }
    }

    #[test]
    fn native_msi_requires_product_code_and_builds_msiexec() {
        let mut inst = entry("msi");
        inst.product_code = Some("{PRODUCT}".into());
        let doc = WingetInstallerDoc {
            installer_type: None,
            nested_installer_type: None,
            nested_installer_files: None,
            installer_switches: None,
            apps_and_features_entries: None,
            installers: vec![],
        };
        let (cmd, args, uninstall) = native_plan(&doc, &inst, Path::new("C:/tmp/app.msi")).unwrap();
        assert_eq!(cmd, "msiexec.exe");
        assert_eq!(args[0], "/i");
        assert_eq!(uninstall.command, "msiexec.exe");
        assert_eq!(uninstall.args[0], "/x");
        assert_eq!(uninstall.args[1], "{PRODUCT}");
    }

    #[test]
    fn native_exe_rejects_missing_uninstall_metadata() {
        let mut inst = entry("exe");
        inst.installer_switches = Some(WingetInstallerSwitches {
            silent: Some("/S".into()),
            silent_with_progress: None,
            custom: None,
            install_location: None,
        });
        let doc = WingetInstallerDoc {
            installer_type: None,
            nested_installer_type: None,
            nested_installer_files: None,
            installer_switches: None,
            apps_and_features_entries: None,
            installers: vec![],
        };
        assert!(native_plan(&doc, &inst, Path::new("C:/tmp/app.exe")).is_err());
    }

    #[test]
    fn native_msix_builds_powershell_commands() {
        let inst = entry("msix");
        let doc = WingetInstallerDoc {
            installer_type: None,
            nested_installer_type: None,
            nested_installer_files: None,
            installer_switches: None,
            apps_and_features_entries: Some(vec![WingetAppsAndFeaturesEntry {
                product_code: None,
                package_family_name: Some("Example.App_123".into()),
                silent_uninstall_string: None,
            }]),
            installers: vec![],
        };
        let (cmd, args, uninstall) =
            native_plan(&doc, &inst, Path::new("C:/tmp/app.msix")).unwrap();
        assert_eq!(cmd, "powershell.exe");
        assert!(args.iter().any(|arg| arg.contains("Add-AppxPackage")));
        assert_eq!(uninstall.command, "powershell.exe");
        assert!(uninstall
            .args
            .iter()
            .any(|arg| arg.contains("Remove-AppxPackage")));
    }
}