Skip to content

Commit

Permalink
chore: remove obsolete update notification logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Kha committed Jun 17, 2024
1 parent e9f33d6 commit 891c4ef
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 109 deletions.
71 changes: 14 additions & 57 deletions src/elan-cli/common.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Just a dumping ground for cli stuff
use elan::{self, Cfg, Notification, Toolchain, UpdateStatus};
use elan::{Cfg, Notification, Toolchain};
use elan_dist::dist::ToolchainDesc;
use elan_utils::notify::NotificationLevel;
use elan_utils::utils;
Expand All @@ -11,7 +11,6 @@ use std::path::Path;
use std::process::{Command, Stdio};
use std::sync::Arc;
use std::time::Duration;
use std::{cmp, iter};
use term2;
use wait_timeout::ChildExt;

Expand Down Expand Up @@ -133,67 +132,25 @@ pub fn set_globals(verbose: bool) -> Result<Cfg> {

pub fn show_channel_update(
cfg: &Cfg,
name: &ToolchainDesc,
updated: elan::Result<UpdateStatus>,
desc: &ToolchainDesc,
) -> Result<()> {
show_channel_updates(cfg, vec![(name.clone(), updated)])
}
let ref toolchain = cfg.get_toolchain(&desc, false).expect("");
let version = lean_version(toolchain);
let name = desc.to_string();

fn show_channel_updates(
cfg: &Cfg,
toolchains: Vec<(ToolchainDesc, elan::Result<UpdateStatus>)>,
) -> Result<()> {
let data = toolchains.into_iter().map(|(desc, result)| {
let ref toolchain = cfg.get_toolchain(&desc, false).expect("");
let version = lean_version(toolchain);
let name = desc.to_string();

let banner;
let color;
match result {
Ok(UpdateStatus::Installed) => {
banner = "installed";
color = Some(term2::color::BRIGHT_GREEN);
}
Ok(UpdateStatus::Updated) => {
banner = "updated";
color = Some(term2::color::BRIGHT_GREEN);
}
Ok(UpdateStatus::Unchanged) => {
banner = "unchanged";
color = None;
}
Err(_) => {
banner = "update failed";
color = Some(term2::color::BRIGHT_RED);
}
}

let width = name.len() + 1 + banner.len();

(name, banner, width, color, version)
});
let banner = "installed";
let color = Some(term2::color::BRIGHT_GREEN);

let mut t = term2::stdout();

let data: Vec<_> = data.collect();
let max_width = data
.iter()
.fold(0, |a, &(_, _, width, _, _)| cmp::max(a, width));

for (name, banner, width, color, version) in data {
let padding = max_width - width;
let padding: String = iter::repeat(' ').take(padding).collect();
let _ = write!(t, " {}", padding);
let _ = t.attr(term2::Attr::Bold);
if let Some(color) = color {
let _ = t.fg(color);
}
let _ = write!(t, "{} ", name);
let _ = write!(t, "{}", banner);
let _ = t.reset();
let _ = writeln!(t, " - {}", version);
let _ = t.attr(term2::Attr::Bold);
if let Some(color) = color {
let _ = t.fg(color);
}
let _ = write!(t, "{} ", name);
let _ = write!(t, "{}", banner);
let _ = t.reset();
let _ = writeln!(t, " - {}", version);
let _ = writeln!(t, "");

Ok(())
Expand Down
24 changes: 3 additions & 21 deletions src/elan-cli/elan_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,15 +245,10 @@ fn install(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let desc = lookup_toolchain_desc(cfg, name)?;
let toolchain = cfg.get_toolchain(&desc, false)?;

let status = if !toolchain.exists() || !toolchain.is_custom() {
Some(toolchain.install_from_dist()?)
} else {
None
};

if let Some(status) = status {
if !toolchain.exists() || !toolchain.is_custom() {
toolchain.install_from_dist()?;
println!("");
common::show_channel_update(cfg, &toolchain.desc, Ok(status))?;
common::show_channel_update(cfg, &toolchain.desc)?;
}
}

Expand Down Expand Up @@ -427,20 +422,7 @@ fn override_add(cfg: &Cfg, m: &ArgMatches) -> Result<()> {
let ref toolchain = m.value_of("toolchain").expect("");
let desc = lookup_toolchain_desc(cfg, toolchain)?;
let toolchain = cfg.get_toolchain(&desc, false)?;

let status = if !toolchain.exists() || !toolchain.is_custom() {
Some(toolchain.install_from_dist_if_not_installed()?)
} else {
None
};

toolchain.make_override(&utils::current_dir()?)?;

if let Some(status) = status {
println!("");
common::show_channel_update(cfg, &toolchain.desc, Ok(status))?;
}

Ok(())
}

Expand Down
8 changes: 4 additions & 4 deletions src/elan/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub enum InstallMethod<'a> {
}

impl<'a> InstallMethod<'a> {
pub fn run(self, path: &Path, notify_handler: &dyn Fn(Notification)) -> Result<bool> {
pub fn run(self, path: &Path, notify_handler: &dyn Fn(Notification)) -> Result<()> {
if path.exists() {
// Don't uninstall first for Dist method
match self {
Expand All @@ -58,11 +58,11 @@ impl<'a> InstallMethod<'a> {
match self {
InstallMethod::Copy(src) => {
utils::copy_dir(src, path, &|n| notify_handler(n.into()))?;
Ok(true)
Ok(())
}
InstallMethod::Link(src) => {
utils::symlink_dir(src, &path, &|n| notify_handler(n.into()))?;
Ok(true)
Ok(())
}
InstallMethod::Dist(toolchain, dl_cfg) => {
if let Some(version) = check_self_update()? {
Expand All @@ -76,7 +76,7 @@ impl<'a> InstallMethod<'a> {
prefix,
)?;

Ok(true)
Ok(())
}
}
}
Expand Down
36 changes: 9 additions & 27 deletions src/elan/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ pub struct ComponentStatus {
pub available: bool,
}

pub enum UpdateStatus {
Installed,
Updated,
Unchanged,
}

pub fn lookup_toolchain_desc(cfg: &Cfg, name: &str) -> Result<ToolchainDesc> {
let pattern = r"^(?:([a-zA-Z0-9-]+[/][a-zA-Z0-9-]+)[:])?([a-zA-Z0-9-.]+)$";

Expand Down Expand Up @@ -126,38 +120,26 @@ impl<'a> Toolchain<'a> {
}
Ok(result?)
}
fn install(&self, install_method: InstallMethod) -> Result<UpdateStatus> {
fn install(&self, install_method: InstallMethod) -> Result<()> {
let exists = self.exists();
if exists {
return Err(format!("'{}' is already installed", self.desc).into())
} else {
(self.cfg.notify_handler)(Notification::InstallingToolchain(&self.desc));
}
(self.cfg.notify_handler)(Notification::ToolchainDirectory(&self.path, &self.desc));
let updated = install_method.run(&self.path, &|n| (self.cfg.notify_handler)(n.into()))?;

if !updated {
(self.cfg.notify_handler)(Notification::UpdateHashMatches);
} else {
(self.cfg.notify_handler)(Notification::InstalledToolchain(&self.desc));
}
install_method.run(&self.path, &|n| (self.cfg.notify_handler)(n.into()))?;

let status = match (updated, exists) {
(true, false) => UpdateStatus::Installed,
(true, true) => UpdateStatus::Updated,
(false, true) => UpdateStatus::Unchanged,
(false, false) => UpdateStatus::Unchanged,
};
(self.cfg.notify_handler)(Notification::InstalledToolchain(&self.desc));

Ok(status)
Ok(())
}
fn install_if_not_installed(&self, install_method: InstallMethod) -> Result<UpdateStatus> {
fn install_if_not_installed(&self, install_method: InstallMethod) -> Result<()> {
(self.cfg.notify_handler)(Notification::LookingForToolchain(&self.desc));
if !self.exists() {
Ok(self.install(install_method)?)
self.install(install_method)
} else {
(self.cfg.notify_handler)(Notification::UsingExistingToolchain(&self.desc));
Ok(UpdateStatus::Unchanged)
Ok(())
}
}

Expand All @@ -168,14 +150,14 @@ impl<'a> Toolchain<'a> {
}
}

pub fn install_from_dist(&self) -> Result<UpdateStatus> {
pub fn install_from_dist(&self) -> Result<()> {
self.install(InstallMethod::Dist(
&self.desc,
self.download_cfg(),
))
}

pub fn install_from_dist_if_not_installed(&self) -> Result<UpdateStatus> {
pub fn install_from_dist_if_not_installed(&self) -> Result<()> {
self.install_if_not_installed(InstallMethod::Dist(
&self.desc,
self.download_cfg(),
Expand Down

0 comments on commit 891c4ef

Please sign in to comment.