fix: resolve pesde_version tags properly
Some checks are pending
Test & Lint / lint (push) Waiting to run

This commit is contained in:
daimond113 2024-12-18 16:03:50 +01:00
parent 60fb68fcf3
commit d9d27cf45b
No known key found for this signature in database
GPG key ID: 3A8ECE51328B513C
4 changed files with 58 additions and 19 deletions

View file

@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
### Fixed ### Fixed
- Change dependency types for removed peer dependencies by @daimond113 - Change dependency types for removed peer dependencies by @daimond113
- Resolve version to correct tag for `pesde_version` field by @daimond113
### Changed ### Changed
- Switch from `log` to `tracing` for logging by @daimond113 - Switch from `log` to `tracing` for logging by @daimond113

View file

@ -1,8 +1,8 @@
use crate::cli::{ use crate::cli::{
config::read_config, config::read_config,
version::{ version::{
current_version, get_latest_remote_version, get_or_download_version, no_build_metadata, current_version, get_or_download_version, get_remote_version, no_build_metadata,
update_bin_exe, update_bin_exe, TagInfo, VersionType,
}, },
}; };
use anyhow::Context; use anyhow::Context;
@ -25,7 +25,7 @@ impl SelfUpgradeCommand {
.context("no cached version found")? .context("no cached version found")?
.1 .1
} else { } else {
get_latest_remote_version(&reqwest).await? get_remote_version(&reqwest, VersionType::Latest).await?
}; };
let latest_version_no_metadata = no_build_metadata(&latest_version); let latest_version_no_metadata = no_build_metadata(&latest_version);
@ -46,12 +46,12 @@ impl SelfUpgradeCommand {
return Ok(()); return Ok(());
} }
let path = get_or_download_version(&reqwest, &latest_version, true) let path = get_or_download_version(&reqwest, &TagInfo::Complete(latest_version), true)
.await? .await?
.unwrap(); .unwrap();
update_bin_exe(&path).await?; update_bin_exe(&path).await?;
println!("upgraded to version {display_latest_version}!",); println!("upgraded to version {display_latest_version}!");
Ok(()) Ok(())
} }

View file

@ -47,11 +47,20 @@ fn get_repo() -> (String, String) {
(owner, repo) (owner, repo)
} }
#[derive(Debug)]
pub enum VersionType {
Latest,
Specific(Version),
}
#[instrument(skip(reqwest), level = "trace")] #[instrument(skip(reqwest), level = "trace")]
pub async fn get_latest_remote_version(reqwest: &reqwest::Client) -> anyhow::Result<Version> { pub async fn get_remote_version(
reqwest: &reqwest::Client,
ty: VersionType,
) -> anyhow::Result<Version> {
let (owner, repo) = get_repo(); let (owner, repo) = get_repo();
let releases = reqwest let mut releases = reqwest
.get(format!( .get(format!(
"https://api.github.com/repos/{owner}/{repo}/releases", "https://api.github.com/repos/{owner}/{repo}/releases",
)) ))
@ -62,13 +71,17 @@ pub async fn get_latest_remote_version(reqwest: &reqwest::Client) -> anyhow::Res
.context("failed to get GitHub API response")? .context("failed to get GitHub API response")?
.json::<Vec<Release>>() .json::<Vec<Release>>()
.await .await
.context("failed to parse GitHub API response")?; .context("failed to parse GitHub API response")?
releases
.into_iter() .into_iter()
.filter_map(|release| Version::parse(release.tag_name.trim_start_matches('v')).ok()) .filter_map(|release| Version::parse(release.tag_name.trim_start_matches('v')).ok());
.max()
.context("failed to find latest version") match ty {
VersionType::Latest => releases.max(),
VersionType::Specific(version) => {
releases.find(|v| no_build_metadata(v) == no_build_metadata(&version))
}
}
.context("failed to find latest version")
} }
pub fn no_build_metadata(version: &Version) -> Version { pub fn no_build_metadata(version: &Version) -> Version {
@ -91,7 +104,7 @@ pub async fn check_for_updates(reqwest: &reqwest::Client) -> anyhow::Result<()>
version version
} else { } else {
tracing::debug!("checking for updates"); tracing::debug!("checking for updates");
let version = get_latest_remote_version(reqwest).await?; let version = get_remote_version(reqwest, VersionType::Latest).await?;
write_config(&CliConfig { write_config(&CliConfig {
last_checked_updates: Some((chrono::Utc::now(), version.clone())), last_checked_updates: Some((chrono::Utc::now(), version.clone())),
@ -229,10 +242,16 @@ pub async fn download_github_release<W: AsyncWrite + Unpin>(
.map(|_| ()) .map(|_| ())
} }
#[derive(Debug)]
pub enum TagInfo {
Complete(Version),
Incomplete(Version),
}
#[instrument(skip(reqwest), level = "trace")] #[instrument(skip(reqwest), level = "trace")]
pub async fn get_or_download_version( pub async fn get_or_download_version(
reqwest: &reqwest::Client, reqwest: &reqwest::Client,
version: &Version, tag: &TagInfo,
always_give_path: bool, always_give_path: bool,
) -> anyhow::Result<Option<PathBuf>> { ) -> anyhow::Result<Option<PathBuf>> {
let path = home_dir()?.join("versions"); let path = home_dir()?.join("versions");
@ -240,7 +259,17 @@ pub async fn get_or_download_version(
.await .await
.context("failed to create versions directory")?; .context("failed to create versions directory")?;
let path = path.join(format!("{version}{}", std::env::consts::EXE_SUFFIX)); let version = match tag {
TagInfo::Complete(version) => version,
// don't fetch the version since it could be cached
TagInfo::Incomplete(version) => version,
};
let path = path.join(format!(
"{}{}",
no_build_metadata(version),
std::env::consts::EXE_SUFFIX
));
let is_requested_version = !always_give_path && *version == current_version(); let is_requested_version = !always_give_path && *version == current_version();
@ -260,10 +289,19 @@ pub async fn get_or_download_version(
.await .await
.context("failed to copy current executable to version directory")?; .context("failed to copy current executable to version directory")?;
} else { } else {
let version = match tag {
TagInfo::Complete(version) => version.clone(),
TagInfo::Incomplete(version) => {
get_remote_version(reqwest, VersionType::Specific(version.clone()))
.await
.context("failed to get remote version")?
}
};
tracing::debug!("downloading version"); tracing::debug!("downloading version");
download_github_release( download_github_release(
reqwest, reqwest,
version, &version,
fs::File::create(&path) fs::File::create(&path)
.await .await
.context("failed to create version file")?, .context("failed to create version file")?,

View file

@ -1,5 +1,5 @@
#[cfg(feature = "version-management")] #[cfg(feature = "version-management")]
use crate::cli::version::{check_for_updates, get_or_download_version}; use crate::cli::version::{check_for_updates, get_or_download_version, TagInfo};
use crate::cli::{auth::get_tokens, display_err, home_dir, HOME_DIR}; use crate::cli::{auth::get_tokens, display_err, home_dir, HOME_DIR};
use anyhow::Context; use anyhow::Context;
use clap::{builder::styling::AnsiColor, Parser}; use clap::{builder::styling::AnsiColor, Parser};
@ -281,7 +281,7 @@ async fn run() -> anyhow::Result<()> {
.and_then(|manifest| manifest.pesde_version); .and_then(|manifest| manifest.pesde_version);
let exe_path = if let Some(version) = target_version { let exe_path = if let Some(version) = target_version {
get_or_download_version(&reqwest, &version, false).await? get_or_download_version(&reqwest, &TagInfo::Incomplete(version), false).await?
} else { } else {
None None
}; };