Skip to content

Commit

Permalink
Introduce changelog configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
KonishchevDmitry committed Jul 31, 2024
1 parent 0da1634 commit 1f5f5b8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ pub struct Tool {
#[serde(default, deserialize_with = "deserialize_optional_glob")]
pub binary_matcher: Option<GlobMatcher>,

pub changelog: Option<String>,

#[serde(default, deserialize_with = "deserialize_optional_path")]
pub path: Option<PathBuf>,
}
Expand Down
31 changes: 26 additions & 5 deletions src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ use easy_logging::GlobalContext;
use globset::GlobMatcher;
use itertools::Itertools;
use log::{debug, info, error};
use semver::Version;
use url::Url;

use crate::config::{Config, Tool};
use crate::core::{EmptyResult, GenericResult};
use crate::download;
use crate::github;
use crate::version;
use crate::version::{self, ReleaseVersion};

#[derive(Clone, Copy, PartialEq)]
pub enum Mode {
Expand Down Expand Up @@ -67,7 +68,7 @@ fn install_tool(name: &str, tool: &Tool, mode: Mode, path: &Path) -> EmptyResult

let release = github::get_release(&tool.project).map_err(|e| format!(
"Failed to get latest release info for {project}: {e}"))?;
let release_version = &release.tag;
let release_version = ReleaseVersion::new(&release.tag);

debug!("The latest release is {release_version}:");
for asset in &release.assets {
Expand Down Expand Up @@ -103,7 +104,10 @@ fn install_tool(name: &str, tool: &Tool, mode: Mode, path: &Path) -> EmptyResult
info!("Installing {name}...");
} else {
match version::get_binary_version(&install_path) {
Some(current_version) => info!("Reinstalling {name}: {current_version} -> {release_version}..."),
Some(current_version) => info!(
"Reinstalling {name}: {current_version} -> {release_version}{changelog}...",
changelog=format_changelog(tool.changelog.as_deref(), Some(&current_version), &release_version)),

None => info!("Reinstalling {name}..."),
}
},
Expand All @@ -117,8 +121,13 @@ fn install_tool(name: &str, tool: &Tool, mode: Mode, path: &Path) -> EmptyResult
}

match current_state.as_ref().and_then(|_| version::get_binary_version(&install_path)) {
Some(current_version) => info!("Upgrading {name}: {current_version} -> {release_version}..."),
None => info!("Upgrading {name} to {release_version}..."),
Some(current_version) => info!(
"Upgrading {name}: {current_version} -> {release_version}{changelog}...",
changelog=format_changelog(tool.changelog.as_deref(), Some(&current_version), &release_version)),

None => info!(
"Upgrading {name} to {release_version}{changelog}...",
changelog=format_changelog(tool.changelog.as_deref(), None, &release_version)),
}
},
}
Expand Down Expand Up @@ -262,4 +271,16 @@ fn check_tool(path: &Path) -> GenericResult<Option<ToolState>> {

fn format_list<T: Display, I: Iterator<Item = T>>(mut iter: I) -> String {
"\n* ".to_owned() + &iter.join("\n* ")
}

fn format_changelog(changelog: Option<&str>, from: Option<&Version>, to: &ReleaseVersion) -> String {
let Some(changelog) = changelog else {
return String::new();
};

if matches!((from, to), (Some(from), ReleaseVersion::Version(to)) if from == to) {
return String::new();
}

format!(" (see {changelog})")
}
29 changes: 29 additions & 0 deletions src/version.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
use std::fmt::{self, Display, Formatter};
use std::path::Path;
use std::process::Command;

use log::debug;
use semver::Version;

pub enum ReleaseVersion {
Version(Version),
Tag(String)
}

impl ReleaseVersion {
pub fn new(tag: &str) -> ReleaseVersion {
let mut version = tag;
if version.starts_with('v') {
version = &version[1..];
}

match Version::parse(version) {
Ok(version) => ReleaseVersion::Version(version),
Err(_) => ReleaseVersion::Tag(tag.to_owned()),
}
}
}

impl Display for ReleaseVersion {
fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
match self {
ReleaseVersion::Version(version) => version.fmt(formatter),
ReleaseVersion::Tag(tag) => tag.fmt(formatter),
}
}
}

pub fn get_binary_version(path: &Path) -> Option<Version> {
let mut command = Command::new(path);
command.arg("--version");
Expand Down

0 comments on commit 1f5f5b8

Please sign in to comment.