diff --git a/CHANGELOG.md b/CHANGELOG.md index d81d97e..ce0eb3a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] +### Fixed +- Ignore build metadata when comparing CLI versions by @daimond113 + ## [0.5.0] - 2024-12-14 ### Added - Add support for multiple targets under the same package name in workspace members by @daimond113 diff --git a/src/cli/commands/self_upgrade.rs b/src/cli/commands/self_upgrade.rs index 9117664..0426faf 100644 --- a/src/cli/commands/self_upgrade.rs +++ b/src/cli/commands/self_upgrade.rs @@ -1,13 +1,13 @@ use crate::cli::{ config::read_config, version::{ - current_version, get_latest_remote_version, get_or_download_version, update_bin_exe, + current_version, get_latest_remote_version, get_or_download_version, no_build_metadata, + update_bin_exe, }, }; use anyhow::Context; use clap::Args; use colored::Colorize; -use semver::BuildMetadata; #[derive(Debug, Args)] pub struct SelfUpgradeCommand { @@ -28,17 +28,14 @@ impl SelfUpgradeCommand { get_latest_remote_version(&reqwest).await? }; - if latest_version <= current_version() { + let latest_version_no_metadata = no_build_metadata(&latest_version); + + if latest_version_no_metadata <= current_version() { println!("already up to date"); return Ok(()); } - let display_latest_version = { - let mut ver = latest_version.clone(); - // remove build metadata to make it more readable - ver.build = BuildMetadata::EMPTY; - ver.to_string().yellow().bold() - }; + let display_latest_version = latest_version_no_metadata.to_string().yellow().bold(); if !inquire::prompt_confirmation(format!( "are you sure you want to upgrade {} from {} to {display_latest_version}?", diff --git a/src/cli/version.rs b/src/cli/version.rs index 3c0494b..80e3267 100644 --- a/src/cli/version.rs +++ b/src/cli/version.rs @@ -64,6 +64,12 @@ pub async fn get_latest_remote_version(reqwest: &reqwest::Client) -> anyhow::Res .context("failed to find latest version") } +pub fn no_build_metadata(version: &Version) -> Version { + let mut version = version.clone(); + version.build = semver::BuildMetadata::EMPTY; + version +} + const CHECK_INTERVAL: chrono::Duration = chrono::Duration::hours(6); pub async fn check_for_updates(reqwest: &reqwest::Client) -> anyhow::Result<()> { @@ -86,72 +92,68 @@ pub async fn check_for_updates(reqwest: &reqwest::Client) -> anyhow::Result<()> version }; let current_version = current_version(); + let version_no_metadata = no_build_metadata(&version); - if version > current_version { - let name = env!("CARGO_BIN_NAME"); - let changelog = format!("{}/releases/tag/v{version}", env!("CARGO_PKG_REPOSITORY")); - - let display_version = { - let mut ver = version.clone(); - // remove build metadata to make it more readable - ver.build = semver::BuildMetadata::EMPTY; - ver.to_string() - }; - - let unformatted_messages = [ - "".to_string(), - format!("update available! {current_version} → {display_version}"), - format!("changelog: {changelog}"), - format!("run `{name} self-upgrade` to upgrade"), - "".to_string(), - ]; - - let width = unformatted_messages - .iter() - .map(|s| s.chars().count()) - .max() - .unwrap() - + 4; - - let column = "│".bright_magenta(); - - let message = [ - "".to_string(), - format!( - "update available! {} → {}", - current_version.to_string().red(), - display_version.green() - ), - format!("changelog: {}", changelog.blue()), - format!( - "run `{} {}` to upgrade", - name.blue(), - "self-upgrade".yellow() - ), - "".to_string(), - ] - .into_iter() - .enumerate() - .map(|(i, s)| { - let text_length = unformatted_messages[i].chars().count(); - let padding = (width as f32 - text_length as f32) / 2f32; - let padding_l = " ".repeat(padding.floor() as usize); - let padding_r = " ".repeat(padding.ceil() as usize); - format!("{column}{padding_l}{s}{padding_r}{column}") - }) - .collect::>() - .join("\n"); - - let lines = "─".repeat(width).bright_magenta(); - - let tl = "╭".bright_magenta(); - let tr = "╮".bright_magenta(); - let bl = "╰".bright_magenta(); - let br = "╯".bright_magenta(); - - println!("\n{tl}{lines}{tr}\n{message}\n{bl}{lines}{br}\n"); + if version_no_metadata <= current_version { + return Ok(()); } + let name = env!("CARGO_BIN_NAME"); + let changelog = format!("{}/releases/tag/v{version}", env!("CARGO_PKG_REPOSITORY")); + + let unformatted_messages = [ + "".to_string(), + format!("update available! {current_version} → {version_no_metadata}"), + format!("changelog: {changelog}"), + format!("run `{name} self-upgrade` to upgrade"), + "".to_string(), + ]; + + let width = unformatted_messages + .iter() + .map(|s| s.chars().count()) + .max() + .unwrap() + + 4; + + let column = "│".bright_magenta(); + + let message = [ + "".to_string(), + format!( + "update available! {} → {}", + current_version.to_string().red(), + version_no_metadata.to_string().green() + ), + format!("changelog: {}", changelog.blue()), + format!( + "run `{} {}` to upgrade", + name.blue(), + "self-upgrade".yellow() + ), + "".to_string(), + ] + .into_iter() + .enumerate() + .map(|(i, s)| { + let text_length = unformatted_messages[i].chars().count(); + let padding = (width as f32 - text_length as f32) / 2f32; + let padding_l = " ".repeat(padding.floor() as usize); + let padding_r = " ".repeat(padding.ceil() as usize); + format!("{column}{padding_l}{s}{padding_r}{column}") + }) + .collect::>() + .join("\n"); + + let lines = "─".repeat(width).bright_magenta(); + + let tl = "╭".bright_magenta(); + let tr = "╮".bright_magenta(); + let bl = "╰".bright_magenta(); + let br = "╯".bright_magenta(); + + println!("\n{tl}{lines}{tr}\n{message}\n{bl}{lines}{br}\n"); + Ok(()) }