pesde/src/cli/commands/self_install.rs
daimond113 95896091cd
refactor: switch out dependencies
Switches the `colored` crate to the `console`
crate. Additionally, to optimize the compiled
program's size switches the `inquire` crate's
backend from `crossterm` to `console`. Console was
picked out because we depend on `indicatif` which
only supports `console`.

Also switches from `winreg` to `windows-registry`,
which `reqwest` depends on to optimize size even
further. Currently has to duplicate dependencies,
as `reqwest` depends on an older version but will
become optimized once `reqwest` updates to the
latest version of the crate.

Signed-off-by: daimond113 <contact@daimond113.com>
2025-01-19 22:29:27 +01:00

77 lines
1.9 KiB
Rust

use crate::cli::{
style::{ADDED_STYLE, CLI_STYLE},
version::replace_pesde_bin_exe,
HOME_DIR,
};
use anyhow::Context;
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;
use windows_registry::CURRENT_USER;
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 = crate::cli::bin_dir().await?;
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(())
}
}