mirror of
https://github.com/alterware/alterware-launcher.git
synced 2025-12-04 15:27:48 +00:00
add --prerelease
This commit is contained in:
@@ -1,6 +1,21 @@
|
||||
use semver::Version;
|
||||
|
||||
pub async fn latest_tag(owner: &str, repo: &str) -> Result<String, Box<dyn std::error::Error>> {
|
||||
pub async fn latest_tag(
|
||||
owner: &str,
|
||||
repo: &str,
|
||||
prerelease: Option<bool>,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
if prerelease.unwrap_or(false) {
|
||||
latest_tag_prerelease(owner, repo).await
|
||||
} else {
|
||||
latest_tag_full(owner, repo).await
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn latest_tag_full(
|
||||
owner: &str,
|
||||
repo: &str,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
let github_body = crate::http_async::get_body_string(
|
||||
format!(
|
||||
"https://api.github.com/repos/{}/{}/releases/latest",
|
||||
@@ -8,34 +23,60 @@ pub async fn latest_tag(owner: &str, repo: &str) -> Result<String, Box<dyn std::
|
||||
)
|
||||
.as_str(),
|
||||
)
|
||||
.await?;
|
||||
.await
|
||||
.map_err(|e| format!("Failed to fetch GitHub API: {}", e))?;
|
||||
|
||||
let github_json: serde_json::Value = serde_json::from_str(&github_body)?;
|
||||
let github_json: serde_json::Value = serde_json::from_str(&github_body)
|
||||
.map_err(|e| format!("Failed to parse GitHub API response: {}", e))?;
|
||||
|
||||
if let Some(tag_name) = github_json.get("tag_name") {
|
||||
if let Some(tag_name_str) = tag_name.as_str() {
|
||||
return Ok(tag_name_str.to_string().replace('"', ""));
|
||||
}
|
||||
}
|
||||
let tag_name = github_json
|
||||
.get("tag_name")
|
||||
.ok_or("Missing tag_name field in GitHub response")?
|
||||
.as_str()
|
||||
.ok_or("tag_name is not a string")?;
|
||||
|
||||
Ok("0.0.0".to_string())
|
||||
Ok(tag_name.replace('"', ""))
|
||||
}
|
||||
|
||||
pub async fn latest_version(owner: &str, repo: &str) -> Version {
|
||||
match latest_tag(owner, repo).await {
|
||||
Ok(tag) => {
|
||||
let cleaned_tag = tag.replace('v', "");
|
||||
Version::parse(&cleaned_tag).unwrap_or_else(|_| Version::new(0, 0, 0))
|
||||
}
|
||||
Err(_) => {
|
||||
crate::println_error!(
|
||||
"Failed to get latest version for {owner}/{repo}, assuming we are up to date."
|
||||
);
|
||||
Version::new(0, 0, 0)
|
||||
}
|
||||
pub async fn latest_tag_prerelease(
|
||||
owner: &str,
|
||||
repo: &str,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
let github_body = crate::http_async::get_body_string(
|
||||
format!("https://api.github.com/repos/{}/{}/releases", owner, repo).as_str(),
|
||||
)
|
||||
.await
|
||||
.map_err(|e| format!("Failed to fetch GitHub API: {}", e))?;
|
||||
|
||||
let github_json: serde_json::Value = serde_json::from_str(&github_body)
|
||||
.map_err(|e| format!("Failed to parse GitHub API response: {}", e))?;
|
||||
|
||||
let latest_release = github_json.get(0).ok_or("No releases found")?;
|
||||
|
||||
let tag_name = latest_release
|
||||
.get("tag_name")
|
||||
.ok_or("Release missing tag_name")?
|
||||
.as_str()
|
||||
.ok_or("tag_name is not a string")?;
|
||||
|
||||
Ok(tag_name.replace('"', ""))
|
||||
}
|
||||
|
||||
pub async fn latest_version(
|
||||
owner: &str,
|
||||
repo: &str,
|
||||
prerelease: Option<bool>,
|
||||
) -> Result<Version, Box<dyn std::error::Error>> {
|
||||
let tag = latest_tag(owner, repo, prerelease).await?;
|
||||
let cleaned_tag = tag.replace('v', "");
|
||||
Version::parse(&cleaned_tag)
|
||||
.map_err(|e| format!("Failed to parse version '{}': {}", cleaned_tag, e).into())
|
||||
}
|
||||
|
||||
pub fn download_url(owner: &str, repo: &str, tag: Option<&str>) -> String {
|
||||
if let Some(tag) = tag {
|
||||
format!("https://github.com/{owner}/{repo}/releases/download/{tag}")
|
||||
} else {
|
||||
format!("https://github.com/{owner}/{repo}/releases/latest")
|
||||
}
|
||||
}
|
||||
|
||||
pub fn latest_release_url(owner: &str, repo: &str) -> String {
|
||||
format!("https://github.com/{owner}/{repo}/releases/latest")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user