cargo-e 0.3.2

e is for Example. A command-line tool for running and exploring source, examples, and binaries from Rust projects. It will run the first example, if no options are given.
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
use crate::e_prompts::yesno;
use anyhow::{bail, Context, Result};
use std::error::Error;
use std::path::{Path, PathBuf};
use std::process::{Command, Stdio};
use which::which;

// https://github.com/ahaoboy/is-admin
#[cfg(windows)]
pub fn is_admin() -> bool {
    let shell = "[bool]([System.Security.Principal.WindowsPrincipal][System.Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)";
    let output = std::process::Command::new("powershell")
        .args(["-c", shell])
        .output()
        .expect("Failed to execute PowerShell command");
    String::from_utf8(output.stdout).unwrap_or_default().trim() == "True"
}

// https://github.com/ahaoboy/is-admin
#[cfg(unix)]
pub fn is_admin() -> bool {
    use libc::{geteuid, getuid};
    unsafe { getuid() == 0 || geteuid() == 0 }
}

/// Check if the program is running as an administrator.
/// Returns an error if the program is not running with administrative privileges.
pub fn ensure_admin_privileges() -> Result<()> {
    if !is_admin() {
        return Err(anyhow::anyhow!(
            "This program must be run as an administrator. Please restart it with administrative privileges."
        ));
    }
    Ok(())
}
/// Ensure `npm` is on PATH.  
/// Ensures Node.js is installed first.  
/// Returns the full path to the `npm` executable, or an error.
pub fn ensure_npm() -> Result<PathBuf> {
    // Ensure Node.js is installed
    ensure_node()?;
    which("npm").context("`npm` not found in PATH. Please install Node.js and npm.")
}

/// Ensure the `napi` CLI is on PATH (provided by `@napi-rs/cli`).  
/// If missing, prompts the user and installs it globally via `npm install -g @napi-rs/cli`.  
/// Returns the full path to the `napi` executable.
pub fn ensure_napi_cli() -> Result<PathBuf, Box<dyn Error>> {
    // 1) Already installed?
    if let Ok(path) = which("napi") {
        return Ok(path);
    }

    // 2) Prompt the user to install it via npm
    println!("`napi` CLI not found. Install it globally now?");
    match yesno(
        "Do you want to install `@napi-rs/cli` globally via npm?",
        Some(true),
    ) {
        Ok(Some(true)) => {
            let npm = ensure_npm()?;
            println!("Installing `@napi-rs/cli` via `npm install -g @napi-rs/cli`…");
            let mut child = Command::new(npm)
                .args(&["install", "-g", "@napi-rs/cli"])
                .stdin(Stdio::null())
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .spawn()
                .map_err(|e| format!("Failed to spawn install command: {}", e))?;

            child
                .wait()
                .map_err(|e| format!("Error while waiting for installation: {}", e))?;
        }
        Ok(Some(false)) => return Err("User skipped installing `@napi-rs/cli`".into()),
        Ok(None) => return Err("Installation of `@napi-rs/cli` cancelled (timeout)".into()),
        Err(e) => return Err(format!("Error during prompt: {}", e).into()),
    }

    // 3) Retry locating `napi`
    which("napi").map_err(|_| "`napi` still not found after installation".into())
}

/// Ensure `cross-env` is on PATH.  
/// If it’s missing, prompts the user and installs it globally via `npm install -g cross-env`.  
/// Returns the full path to the `cross-env` executable.
pub fn ensure_cross_env() -> Result<PathBuf, Box<dyn Error>> {
    // 1) Already installed?
    if let Ok(path) = which("cross-env") {
        return Ok(path);
    }

    // 2) Prompt the user to install it via npm
    println!("`cross-env` is not installed. Install it globally now?");
    match yesno(
        "Do you want to install `cross-env` globally via npm?",
        Some(true),
    ) {
        Ok(Some(true)) => {
            // Make sure npm is available
            let npm = ensure_npm()?;
            println!("Installing `cross-env` via `npm install -g cross-env`…");
            let mut child = Command::new(npm)
                .args(&["install", "-g", "cross-env"])
                .stdin(Stdio::null())
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .spawn()
                .map_err(|e| format!("Failed to spawn install command: {}", e))?;

            // Wait for the installation to finish
            child
                .wait()
                .map_err(|e| format!("Error while waiting for installation: {}", e))?;
        }
        Ok(Some(false)) => return Err("User skipped installing `cross-env`".into()),
        Ok(None) => return Err("Installation of `cross-env` cancelled (timeout)".into()),
        Err(e) => return Err(format!("Error during prompt: {}", e).into()),
    }

    // 3) Retry locating `cross-env`
    which("cross-env").map_err(|_| "`cross-env` still not found after installation".into())
}
/// Ensure `pnpm` is on PATH.  
/// Ensures Node.js is installed first.  
/// If it’s missing, will use `npm` (via `ensure_npm`) to install `pnpm` globally.  
/// Returns the full path to the `pnpm` executable.
pub fn ensure_pnpm() -> Result<PathBuf> {
    // Ensure Node.js is installed
    ensure_node()?;

    // Check if `pnpm` is already installed
    if let Ok(path) = which("pnpm") {
        return Ok(path);
    }

    // Otherwise, prompt the user to install it via npm
    println!("`pnpm` is not installed. Install it now?");
    match yesno(
        "Do you want to install `pnpm` globally via npm?",
        Some(true),
    ) {
        Ok(Some(true)) => {
            // Make sure we have npm
            let npm_path = ensure_npm()?;
            println!("Installing `pnpm` via `npm install -g pnpm`…");

            let mut child = Command::new(npm_path)
                .args(&["install", "-g", "pnpm"])
                .stdin(Stdio::null())
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .spawn()
                .context("failed to spawn `npm install -g pnpm`")?;

            child
                .wait()
                .context("error while waiting for `npm install -g pnpm` to finish")?;
        }
        Ok(Some(false)) => bail!("user skipped installing `pnpm`"),
        Ok(None) => bail!("installation of `pnpm` cancelled (timeout)"),
        Err(e) => bail!("error during prompt: {}", e),
    }

    // Retry locating `pnpm`
    which("pnpm").context("`pnpm` still not found in PATH after installation")
}

/// Ensure the `dx` CLI (the Dioxus helper) is on PATH.
/// If missing, prompts the user to install the Dioxus CLI via `cargo install dioxus-cli`.
/// Returns the full path to the `dx` executable.
pub fn ensure_dx() -> Result<PathBuf> {
    // 1) Check if `dx` is already on PATH
    if let Ok(path) = which("dx") {
        return Ok(path);
    }

    // 2) Prompt the user to install it
    println!("`dx` CLI not found. Install the Dioxus CLI now?");
    match yesno(
        "Do you want to install the Dioxus CLI via `cargo install dioxus-cli`?",
        Some(true),
    ) {
        Ok(Some(true)) => {
            println!("Installing `dioxus-cli` via `cargo install dioxus-cli`…");
            let mut child = Command::new("cargo")
                .args(&["install", "dioxus-cli"])
                .stdin(Stdio::null())
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .spawn()
                .context("failed to spawn `cargo install dioxus-cli`")?;

            child
                .wait()
                .context("error while waiting for `cargo install dioxus-cli` to finish")?;
        }
        Ok(Some(false)) => bail!("user skipped installing the Dioxus CLI"),
        Ok(None) => bail!("installation of the Dioxus CLI cancelled (timeout)"),
        Err(e) => bail!("error during prompt: {}", e),
    }

    // 3) Retry locating `dx`
    which("dx").context("`dx` still not found in PATH after installation")
}

/// Ensure `trunk` is on PATH.  
/// Returns the full path to the `trunk` executable, or an error.
pub fn ensure_trunk() -> Result<PathBuf> {
    // 1) First try to locate `trunk`
    if let Ok(path) = which("trunk") {
        return Ok(path);
    }

    // 2) Prompt the user to install it
    println!("`trunk` is not installed. Install it now?");
    match yesno("Do you want to install `trunk`?", Some(true)) {
        Ok(Some(true)) => {
            println!("Installing `trunk` via `cargo install trunk`…");
            let mut child = Command::new("cargo")
                .args(&["install", "trunk"])
                .stdin(Stdio::null())
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .spawn()
                .context("failed to spawn `cargo install trunk`")?;

            child
                .wait()
                .context("failed while waiting for `cargo install trunk` to finish")?;
        }
        Ok(Some(false)) => {
            anyhow::bail!("user skipped installing `trunk`");
        }
        Ok(None) => {
            anyhow::bail!("installation of `trunk` cancelled (timeout)");
        }
        Err(e) => {
            anyhow::bail!("error during prompt: {}", e);
        }
    }

    // 3) Re‐try locating `trunk`
    which("trunk").context("`trunk` still not found in PATH after installation")
}

/// Ensure `rust-script` is on PATH.  
/// Returns the full path to the `rust-script` executable, or an error.
pub fn ensure_rust_script() -> Result<PathBuf> {
    // 1) First try to locate `trunk`
    if let Ok(path) = which("rust-script") {
        return Ok(path);
    }

    // 2) Prompt the user to install it
    println!("`rust-script` is not installed. Install it now?");
    match yesno("Do you want to install `rust-script`?", Some(true)) {
        Ok(Some(true)) => {
            println!("Installing `rust-script` via `cargo install rust-script`…");
            let mut child = Command::new("cargo")
                .args(&["install", "rust-script"])
                .stdin(Stdio::null())
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .spawn()
                .context("failed to spawn `cargo install rust-script`")?;

            child
                .wait()
                .context("failed while waiting for `cargo install rust-script` to finish")?;
        }
        Ok(Some(false)) => {
            anyhow::bail!("user skipped installing `rust-script`");
        }
        Ok(None) => {
            anyhow::bail!("installation of `rust-script` cancelled (timeout)");
        }
        Err(e) => {
            anyhow::bail!("error during prompt: {}", e);
        }
    }
    which("rust-script").context("`rust-script` still not found in PATH after installation")
}
// Helper function to check for package.json and run npm install if needed
pub fn check_npm_and_install(
    workspace_parent: &Path,
    be_silent: bool,
) -> Result<(), Box<dyn Error>> {
    if workspace_parent.join("pnpm-workspace.yaml").exists() {
        // If this is a pnpm workspace, skip npm checks
        println!("Skipping npm checks for pnpm workspace.");
        return Ok(());
    }
    // Check if package.json exists at the workspace parent level
    if !be_silent {
        println!(
            "Checking for package.json in: {}",
            workspace_parent.display()
        );
    }
    if workspace_parent.join("package.json").exists() {
        if !be_silent {
            println!("package.json found in: {}", workspace_parent.display());
        }
        // Get the path to npm using `which`.
        match which("npm") {
            Ok(npm_path) => {
                if !be_silent {
                    println!("Found npm at: {}", npm_path.display());
                }

                // Run `npm ls --depth=1` in the specified directory
                let output = Command::new(npm_path.clone())
                    .arg("ls")
                    .arg("--depth=1")
                    .current_dir(workspace_parent)
                    .output()
                    .map_err(|e| eprintln!("Failed to execute npm ls: {}", e))
                    .ok();

                if let Some(output) = output {
                    if !be_silent {
                        println!("npm ls output: {}", String::from_utf8_lossy(&output.stdout));
                    }
                    if !output.status.success() {
                        // Print the npm error output for debugging.
                        eprintln!(
                            "npm ls failed for directory: {}",
                            workspace_parent.display()
                        );
                        eprintln!("{}", String::from_utf8_lossy(&output.stderr));

                        // Run `npm install` to fix the missing dependencies
                        if !be_silent {
                            println!(
                                "Running npm install in directory: {}",
                                workspace_parent.display()
                            );
                        }
                        let install_output = Command::new(npm_path)
                            .arg("install")
                            .current_dir(workspace_parent)
                            .output()
                            .map_err(|e| eprintln!("Failed to execute npm install: {}", e))
                            .ok();

                        if !be_silent {
                            if let Some(install_output) = install_output {
                                println!(
                                    "npm install output: {}",
                                    String::from_utf8_lossy(&install_output.stdout)
                                );
                                if install_output.status.success() {
                                    println!(
                                        "npm install completed successfully in: {}",
                                        workspace_parent.display()
                                    );
                                } else {
                                    eprintln!(
                                        "npm install failed in directory: {}",
                                        workspace_parent.display()
                                    );
                                    eprintln!(
                                        "{}",
                                        String::from_utf8_lossy(&install_output.stderr)
                                    );
                                }
                            }
                        }
                    }
                }
            }
            Err(_) => {
                eprintln!("npm is not installed or not in the system PATH.");
                return Err("npm not found".into());
            }
        }
    }
    Ok(())
}

/// Check for a pnpm workspace and, if found, run `pnpm install`.  
/// Returns the full path to the `pnpm` executable.
pub fn check_pnpm_and_install(workspace_parent: &Path, be_silent: bool) -> Result<PathBuf> {
    // if this is a pnpm workspace, install deps
    let workspace_yaml = workspace_parent.join("pnpm-workspace.yaml");
    if workspace_yaml.exists() {
        // ensure pnpm is available (and install it if necessary)
        let pnpm = ensure_pnpm()?;
        if !be_silent {
            println!(
                "Found pnpm-workspace.yaml in: {}",
                workspace_parent.display()
            );
            println!("Running `pnpm install`…");
        }

        let status = Command::new(&pnpm)
            .arg("install")
            .current_dir(workspace_parent)
            .stdin(Stdio::null())
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .status()
            .context("failed to execute `pnpm install`")?;

        if !status.success() {
            bail!("`pnpm install` failed with exit code {}", status);
        }
        //         if cfg!( target_os = "windows" ) {
        //             #[cfg(windows)]
        // use std::os::windows::process::CommandExt;
        //             // WinAPI flag for “create a new console window”
        // #[cfg(windows)]
        // const CREATE_NEW_CONSOLE: u32 = 0x0000_0010;
        //             println!("Running `pnpm run build:debug windows");
        //                 // Build the command
        //     let mut cmd = Command::new("cmd");
        //     cmd.args(&["/C", "pnpm run build:debug"])
        //        .current_dir(workspace_parent);
        //     //    .stdin(Stdio::null())
        //     //    .stdout(Stdio::inherit())
        //     //    .stderr(Stdio::inherit());

        //     // On Windows, ask for a new console window
        //     #[cfg(windows)]
        //     {
        //         cmd.creation_flags(CREATE_NEW_CONSOLE);
        //     }
        //                 let status =
        //     cmd.status()?;
        // if !status.success() {
        //     anyhow::bail!("`pnpm run build:debug` failed with {}", status);
        // }
        //         } else {
        // ensure_napi_cli().ok();
        // ensure_cross_env().ok();
        Command::new(&pnpm)
            .args(&["run", "build:debug"])
            .current_dir(workspace_parent)
            .env("CARGO", "cargo")
            .status()?;
        // };

        if !be_silent {
            println!("pnpm install succeeded");
        }
        return Ok(pnpm);
    } else {
        if !be_silent {
            println!(
                "No pnpm-workspace.yaml found in {}, skipping `pnpm install`.",
                workspace_parent.display()
            );
        }
    }
    Ok(PathBuf::new())
}

/// Ensure `node` is on PATH.  
/// If missing, attempts to install Node.js using `nvm` (automated for Windows, manual prompt otherwise).  
/// Returns the full path to the `node` executable.
pub fn ensure_node() -> Result<PathBuf> {
    // Check if `node` is already installed
    if let Ok(path) = which("node") {
        return Ok(path);
    }

    #[cfg(target_os = "windows")]
    {
        // On Windows, use Chocolatey to install NVM and set Node.js to LTS
        println!("`node` is not installed.");
        match yesno(
            "Do you want to install Node.js using NVM (via Chocolatey)?",
            Some(true),
        ) {
            Ok(Some(true)) => {
                println!("Installing NVM via Chocolatey...");
                let choco = ensure_choco()?;
                let mut child = Command::new(choco)
                    .args(&["install", "nvm"]) //, "-y"])
                    .stdin(Stdio::null())
                    .stdout(Stdio::inherit())
                    .stderr(Stdio::inherit())
                    .spawn()
                    .context("Failed to spawn `choco install nvm`")?;

                child
                    .wait()
                    .context("Error while waiting for `choco install nvm` to finish")?;

                // Use NVM to install and use the latest LTS version of Node.js
                let nvm = which("nvm").context("`nvm` not found in PATH after installation.")?;
                let mut child = Command::new(&nvm)
                    .args(&["install", "lts"])
                    .stdin(Stdio::null())
                    .stdout(Stdio::inherit())
                    .stderr(Stdio::inherit())
                    .spawn()
                    .context("Failed to spawn `nvm install lts`")?;

                child
                    .wait()
                    .context("Error while waiting for `nvm install lts` to finish")?;

                let mut child = Command::new(&nvm)
                    .args(&["use", "lts"])
                    .stdin(Stdio::null())
                    .stdout(Stdio::inherit())
                    .stderr(Stdio::inherit())
                    .spawn()
                    .context("Failed to spawn `nvm use lts`")?;

                child
                    .wait()
                    .context("Error while waiting for `nvm use lts` to finish")?;
            }
            Ok(Some(false)) => {
                anyhow::bail!("User declined to install Node.js.");
            }
            Ok(None) => {
                anyhow::bail!("Installation of Node.js cancelled (timeout).");
            }
            Err(e) => {
                anyhow::bail!("Error during prompt: {}", e);
            }
        }
    }

    #[cfg(not(target_os = "windows"))]
    {
        // On non-Windows systems, prompt the user to install Node.js manually
        println!("`node` is not installed. Please install Node.js manually.");
        anyhow::bail!("Node.js installation is not automated for this platform.");
    }

    // Retry locating `node`
    which("node").context("`node` still not found after installation")
}

/// Ensure the GitHub CLI (`gh`) is on PATH.  
/// If missing, installs it using Chocolatey on Windows.  
/// Returns the full path to the `gh` executable.
pub fn ensure_github_gh() -> Result<PathBuf> {
    // Check if `gh` is already installed
    if let Ok(path) = which("gh") {
        return Ok(path);
    }
    // Check if `gh.exe` exists in the default installation path
    let default_path = Path::new("C:\\Program Files\\GitHub CLI\\gh.exe");
    if default_path.exists() {
        return Ok(default_path.to_path_buf());
    }
    #[cfg(target_os = "windows")]
    {
        // Ensure Chocolatey is installed
        let choco = ensure_choco()?;

        // Install GitHub CLI using Chocolatey
        println!("Installing GitHub CLI (`gh`) via Chocolatey...");
        if let Err(e) = ensure_admin_privileges() {
            eprintln!("Error: {}", e);
            return Err(e);
        }
        let mut child = Command::new(choco)
            .args(&["install", "gh", "y"])
            .stdin(Stdio::null())
            .stdout(Stdio::inherit())
            .stderr(Stdio::inherit())
            .spawn()
            .context("Failed to spawn `choco install gh`")?;

        child
            .wait()
            .context("Error while waiting for `choco install gh` to finish")?;
        if default_path.exists() {
            return Ok(default_path.to_path_buf());
        }
    }

    #[cfg(not(target_os = "windows"))]
    {
        anyhow::bail!("GitHub CLI installation is only automated on Windows.");
    }

    // Retry locating `gh`
    which("gh").context("`gh` still not found after installation")
}

/// Ensure `choco` (Chocolatey) is on PATH.  
/// If missing, prompts the user to install Chocolatey manually.  
/// Returns the full path to the `choco` executable.
pub fn ensure_choco() -> Result<PathBuf> {
    // Check if `choco` is already installed
    if let Ok(path) = which("choco") {
        return Ok(path);
    }

    #[cfg(target_os = "windows")]
    {
        // On Windows, prompt the user to install Chocolatey manually
        println!("`choco` (Chocolatey) is not installed.");
        println!("It is required to proceed. Do you want to install it manually?");
        match yesno(
            "Do you want to install Chocolatey manually by following the instructions?",
            Some(true),
        ) {
            Ok(Some(true)) => {
                println!("Please run the following command in PowerShell to install Chocolatey:");
                println!("Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))");
                anyhow::bail!(
                    "Chocolatey installation is not automated. Please install it manually."
                );
            }
            Ok(Some(false)) => {
                anyhow::bail!("User declined to install Chocolatey.");
            }
            Ok(None) => {
                anyhow::bail!("Installation of Chocolatey cancelled (timeout).");
            }
            Err(e) => {
                anyhow::bail!("Error during prompt: {}", e);
            }
        }
    }

    #[cfg(not(target_os = "windows"))]
    {
        anyhow::bail!("Chocolatey is only supported on Windows.");
    }
}

/// Ensure the `cargo-leptos` CLI is on PATH.  
/// If missing, prompts the user to install it via `cargo install cargo-leptos`.  
/// Returns the full path to the `cargo-leptos` executable.
pub fn ensure_leptos() -> Result<PathBuf> {
    // 1) Check if `cargo-leptos` is already on PATH
    if let Ok(path) = which("cargo-leptos") {
        return Ok(path);
    }

    // 2) Prompt the user to install it
    println!("`cargo-leptos` CLI not found. Install it now?");
    match yesno(
        "Do you want to install the `cargo-leptos` CLI via `cargo install cargo-leptos`?",
        Some(true),
    ) {
        Ok(Some(true)) => {
            // Check if `perl` is available
            if which("perl").is_err() {
                println!("`perl` is not installed or not found in PATH.");
                println!("OpenSSL requires `perl` for installation unless OpenSSL is already configured in your environment.");
                println!("It is recommended to have a working `perl` distribution installed for openssl.");
                ensure_perl();
            }

            println!("Installing `cargo-leptos` via `cargo install cargo-leptos`…");
            let mut child = Command::new("cargo")
                .args(&["install", "cargo-leptos"])
                .stdin(Stdio::null())
                .stdout(Stdio::inherit())
                .stderr(Stdio::inherit())
                .spawn()
                .context("Failed to spawn `cargo install cargo-leptos`")?;

            child
                .wait()
                .context("Error while waiting for `cargo install cargo-leptos` to finish")?;
        }
        Ok(Some(false)) => bail!("User skipped installing `cargo-leptos`."),
        Ok(None) => bail!("Installation of `cargo-leptos` cancelled (timeout)."),
        Err(e) => bail!("Error during prompt: {}", e),
    }

    // 3) Retry locating `cargo-leptos`
    which("cargo-leptos").context("`cargo-leptos` still not found after installation")
}

#[cfg(target_os = "windows")]
pub fn ensure_perl() {
    use std::process::Command;
    use which::which;

    // Check if choco is installed
    if which("choco").is_err() {
        eprintln!("Chocolatey (choco) is not installed.");
        println!("Please install Chocolatey from https://chocolatey.org/install to proceed with Perl installation.");
        return;
    }

    println!("Perl is missing. You can install Strawberry Perl using Chocolatey (choco).");
    println!("Suggestion: choco install strawberryperl");

    match crate::e_prompts::yesno(
        "Do you want to install Strawberry Perl using choco?",
        Some(true), // Default to yes
    ) {
        Ok(Some(true)) => {
            println!("Installing Strawberry Perl...");
            match Command::new("choco")
                .args(["install", "strawberryperl", "-y"])
                .spawn()
            {
                Ok(mut child) => {
                    child.wait().ok(); // Wait for installation to complete
                    println!("Strawberry Perl installation completed.");
                }
                Err(e) => {
                    eprintln!("Error installing Strawberry Perl via choco: {}", e);
                }
            }
        }
        Ok(Some(false)) => {
            println!("Strawberry Perl installation skipped.");
        }
        Ok(None) => {
            println!("Installation cancelled (timeout or invalid input).");
        }
        Err(e) => {
            eprintln!("Error during prompt: {}", e);
        }
    }
}

#[cfg(not(target_os = "windows"))]
pub fn ensure_perl() {
    println!("auto_sense_perl is only supported on Windows with Chocolatey.");
}