From 97b6a69688ef6fad52a9d41c6453e3311c807b25 Mon Sep 17 00:00:00 2001 From: daimond113 <72147841+daimond113@users.noreply.github.com> Date: Sun, 28 Jul 2024 18:34:24 +0200 Subject: [PATCH] fix: update binary when upgrading --- src/cli/commands/install.rs | 4 ++-- src/cli/commands/self_install.rs | 17 ++++++----------- src/cli/commands/self_upgrade.rs | 6 +++++- src/cli/mod.rs | 8 +++++++- src/cli/version.rs | 12 ++++++++++++ 5 files changed, 32 insertions(+), 15 deletions(-) diff --git a/src/cli/commands/install.rs b/src/cli/commands/install.rs index 7d0e1bd..8ab35b7 100644 --- a/src/cli/commands/install.rs +++ b/src/cli/commands/install.rs @@ -1,4 +1,4 @@ -use crate::cli::{files::make_executable, home_dir, IsUpToDate}; +use crate::cli::{bin_dir, files::make_executable, home_dir, IsUpToDate}; use anyhow::Context; use clap::Args; use indicatif::MultiProgress; @@ -167,7 +167,7 @@ impl InstallCommand { .apply_patches(&downloaded_graph) .context("failed to apply patches")?; - let bin_folder = home_dir()?.join("bin"); + let bin_folder = bin_dir()?; for versions in downloaded_graph.values() { for node in versions.values() { diff --git a/src/cli/commands/self_install.rs b/src/cli/commands/self_install.rs index 05f7af8..fead2cc 100644 --- a/src/cli/commands/self_install.rs +++ b/src/cli/commands/self_install.rs @@ -1,4 +1,7 @@ -use crate::cli::{files::make_executable, home_dir, scripts::update_scripts_folder, HOME_DIR}; +use crate::cli::{ + bin_dir, files::make_executable, home_dir, scripts::update_scripts_folder, + version::update_bin_exe, HOME_DIR, +}; use anyhow::Context; use clap::Args; use colored::Colorize; @@ -16,8 +19,7 @@ impl SelfInstallCommand { pub fn run(self, project: Project) -> anyhow::Result<()> { update_scripts_folder(&project)?; - let bin_dir = home_dir()?.join("bin"); - create_dir_all(&bin_dir).context("failed to create bin folder")?; + let bin_dir = bin_dir()?; #[cfg(windows)] if !self.skip_add_to_path { @@ -74,14 +76,7 @@ and then restart your shell. ); } - let copy_to = bin_dir - .join(env!("CARGO_BIN_NAME")) - .with_extension(std::env::consts::EXE_EXTENSION); - - std::fs::copy(std::env::current_exe()?, ©_to) - .context("failed to copy executable to bin folder")?; - - make_executable(©_to)?; + update_bin_exe()?; Ok(()) } diff --git a/src/cli/commands/self_upgrade.rs b/src/cli/commands/self_upgrade.rs index 62c99a5..155bc44 100644 --- a/src/cli/commands/self_upgrade.rs +++ b/src/cli/commands/self_upgrade.rs @@ -1,4 +1,7 @@ -use crate::cli::{config::read_config, version::get_or_download_version}; +use crate::cli::{ + config::read_config, + version::{get_or_download_version, update_bin_exe}, +}; use clap::Args; #[derive(Debug, Args)] @@ -13,6 +16,7 @@ impl SelfUpgradeCommand { let config = read_config()?; get_or_download_version(&reqwest, &config.last_checked_updates.unwrap().1)?; + update_bin_exe()?; Ok(()) } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index d831a6b..8d6e6cb 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -10,7 +10,7 @@ use anyhow::Context; use pesde::{ lockfile::DownloadedGraph, names::PackageNames, source::version_id::VersionId, Project, }; -use std::{collections::HashSet, str::FromStr}; +use std::{collections::HashSet, fs::create_dir_all, str::FromStr}; pub const HOME_DIR: &str = concat!(".", env!("CARGO_PKG_NAME")); @@ -20,6 +20,12 @@ pub fn home_dir() -> anyhow::Result { .join(HOME_DIR)) } +pub fn bin_dir() -> anyhow::Result { + let bin_dir = home_dir()?.join("bin"); + create_dir_all(&bin_dir).context("failed to create bin folder")?; + Ok(bin_dir) +} + pub trait IsUpToDate { fn is_up_to_date(&self, strict: bool) -> anyhow::Result; } diff --git a/src/cli/version.rs b/src/cli/version.rs index b5b67ae..214247f 100644 --- a/src/cli/version.rs +++ b/src/cli/version.rs @@ -7,6 +7,7 @@ use semver::Version; use serde::Deserialize; use crate::cli::{ + bin_dir, config::{read_config, write_config, CliConfig}, files::make_executable, home_dir, @@ -206,3 +207,14 @@ pub fn max_installed_version() -> anyhow::Result { Ok(max_version) } + +pub fn update_bin_exe() -> anyhow::Result<()> { + let copy_to = bin_dir()? + .join(env!("CARGO_BIN_NAME")) + .with_extension(std::env::consts::EXE_EXTENSION); + + std::fs::copy(std::env::current_exe()?, ©_to) + .context("failed to copy executable to bin folder")?; + + make_executable(©_to) +}