pesde/src/cli/commands/self_install.rs
2025-03-08 22:37:42 +01:00

78 lines
1.9 KiB
Rust

use crate::cli::{
style::{ADDED_STYLE, CLI_STYLE},
version::replace_pesde_bin_exe,
HOME_DIR,
};
use anyhow::Context as _;
use clap::Args;
use console::style;
use std::env::current_exe;
#[derive(Debug, Args)]
pub struct SelfInstallCommand {
/// Skip adding the bin directory to the PATH
#[cfg(windows)]
#[arg(short, long)]
skip_add_to_path: bool,
}
impl SelfInstallCommand {
pub async fn run(self) -> anyhow::Result<()> {
#[cfg(windows)]
{
if !self.skip_add_to_path {
use crate::cli::style::WARN_STYLE;
use anyhow::Context as _;
use windows_registry::CURRENT_USER;
let bin_dir = crate::cli::bin_dir().await?;
let env = CURRENT_USER
.create("Environment")
.context("failed to open Environment key")?;
let path = env.get_string("Path").context("failed to get Path value")?;
let bin_dir = bin_dir.to_string_lossy();
let exists = path.split(';').any(|part| *part == bin_dir);
if !exists {
let new_path = format!("{path};{bin_dir}");
env.set_string("Path", &new_path)
.context("failed to set Path value")?;
println!(
"\nin order to allow proper functionality {} was added to PATH.\n\n{}",
style(format!("`~/{HOME_DIR}/bin`")).green(),
WARN_STYLE.apply_to("please restart your shell for this to take effect")
);
}
}
println!(
"installed {} {}!",
CLI_STYLE.apply_to(env!("CARGO_BIN_NAME")),
ADDED_STYLE.apply_to(env!("CARGO_PKG_VERSION")),
);
};
#[cfg(unix)]
{
println!(
r"installed {} {}! add the following line to your shell profile in order to get the binary and binary exports as executables usable from anywhere:
{}
and then restart your shell.
",
CLI_STYLE.apply_to(env!("CARGO_BIN_NAME")),
ADDED_STYLE.apply_to(env!("CARGO_PKG_VERSION")),
style(format!(r#"export PATH="$PATH:$HOME/{HOME_DIR}/bin""#)).green(),
);
};
replace_pesde_bin_exe(&current_exe().context("failed to get current exe path")?).await?;
Ok(())
}
}