Skip to content

Check for updates concurrently #4388

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,17 @@ cfg-if = "1.0"
chrono = { version = "0.4", default-features = false, features = ["std"] }
clap = { version = "4", features = ["derive", "wrap_help"] }
clap_complete = "4"
console = "0.16"
curl = { version = "0.4.44", optional = true }
effective-limits = "0.5.5"
enum-map = "2.5.0"
env_proxy = { version = "0.4.1", optional = true }
flate2 = { version = "1.1.1", default-features = false, features = ["zlib-rs"] }
fs_at = "0.2.1"
futures-util = "0.3.31"
git-testament = "0.2"
home = "0.5.4"
indicatif = "0.18"
itertools = "0.14"
libc = "0.2"
opener = "0.8.0"
Expand Down
122 changes: 90 additions & 32 deletions src/cli/rustup_mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ use std::{
path::{Path, PathBuf},
process::ExitStatus,
str::FromStr,
time::Duration,
};

use anyhow::{Context, Error, Result, anyhow};
use clap::{Args, CommandFactory, Parser, Subcommand, ValueEnum, builder::PossibleValue};
use clap_complete::Shell;
use console::style;
use futures_util::stream::StreamExt;
use indicatif::{MultiProgress, ProgressBar, ProgressDrawTarget, ProgressStyle};
use itertools::Itertools;
use tracing::{info, trace, warn};
use tracing_subscriber::{EnvFilter, Registry, reload::Handle};
Expand All @@ -34,7 +38,7 @@ use crate::{
install::{InstallMethod, UpdateStatus},
process::{
Process,
terminalsource::{self, ColorableTerminal},
terminalsource::{self, ColorChoice, ColorableTerminal},
},
toolchain::{
CustomToolchainName, DistributableToolchain, LocalToolchainName,
Expand Down Expand Up @@ -792,45 +796,99 @@ async fn default_(
}

async fn check_updates(cfg: &Cfg<'_>, opts: CheckOpts) -> Result<utils::ExitCode> {
let t = cfg.process.stdout().terminal(cfg.process);
let is_a_tty = t.is_a_tty();
let use_colors = matches!(t.color_choice(), ColorChoice::Auto | ColorChoice::Always);
let mut update_available = false;

let mut t = cfg.process.stdout().terminal(cfg.process);
let channels = cfg.list_channels()?;

for channel in channels {
let (name, distributable) = channel;
let current_version = distributable.show_version()?;
let dist_version = distributable.show_dist_version().await?;
let _ = t.attr(terminalsource::Attr::Bold);
write!(t.lock(), "{name} - ")?;
match (current_version, dist_version) {
(None, None) => {
let _ = t.fg(terminalsource::Color::Red);
writeln!(t.lock(), "Cannot identify installed or update versions")?;
}
(Some(cv), None) => {
let _ = t.fg(terminalsource::Color::Green);
write!(t.lock(), "Up to date")?;
let _ = t.reset();
writeln!(t.lock(), " : {cv}")?;
let num_channels = channels.len();
// Ensure that `.buffered()` is never called with 0 as this will cause a hang.
// See: https://github.com/rust-lang/futures-rs/pull/1194#discussion_r209501774
if num_channels > 0 {
let multi_progress_bars = if is_a_tty {
MultiProgress::with_draw_target(ProgressDrawTarget::term_like(Box::new(t)))
} else {
MultiProgress::with_draw_target(ProgressDrawTarget::hidden())
};
let channels = tokio_stream::iter(channels.into_iter()).map(|(name, distributable)| {
let pb = multi_progress_bars.add(ProgressBar::new(1));
pb.set_style(
ProgressStyle::with_template("{msg:.bold} - Checking... {spinner:.green}")
.unwrap()
.tick_chars("⠁⠂⠄⡀⢀⠠⠐⠈ "),
);
pb.set_message(format!("{name}"));
pb.enable_steady_tick(Duration::from_millis(100));
async move {
let current_version = distributable.show_version()?;
let dist_version = distributable.show_dist_version().await?;
let mut update_a = false;

let mut styled_name = style(format!("{name} - "));
if use_colors {
styled_name = styled_name.bold();
}
let message = match (current_version, dist_version) {
(None, None) => {
let mut m = style("Cannot identify installed or update versions");
if use_colors {
m = m.red().bold();
}
format!("{styled_name}{m}")
}
(Some(cv), None) => {
let mut m = style("Up to date");
if use_colors {
m = m.green().bold();
}
format!("{styled_name}{m} : {cv}")
}
(Some(cv), Some(dv)) => {
let mut m = style("Update available");
if use_colors {
m = m.yellow().bold();
}
update_a = true;
format!("{styled_name}{m} : {cv} -> {dv}")
}
(None, Some(dv)) => {
let mut m = style("Update available");
if use_colors {
m = m.yellow().bold();
}
update_a = true;
format!("{styled_name}{m} : (Unknown version) -> {dv}")
}
};
pb.set_style(ProgressStyle::with_template(message.as_str()).unwrap());
pb.finish();
Ok::<(bool, String), Error>((update_a, message))
}
(Some(cv), Some(dv)) => {
});

// If we are running in a TTY, we can use `buffer_unordered` since
// displaying the output in the correct order is already handled by
// `indicatif`.
let channels = if is_a_tty {
channels
.buffer_unordered(num_channels)
.collect::<Vec<_>>()
.await
} else {
channels.buffered(num_channels).collect::<Vec<_>>().await
};

let t = cfg.process.stdout().terminal(cfg.process);
for result in channels {
let (update_a, message) = result?;
if update_a {
update_available = true;
let _ = t.fg(terminalsource::Color::Yellow);
write!(t.lock(), "Update available")?;
let _ = t.reset();
writeln!(t.lock(), " : {cv} -> {dv}")?;
}
(None, Some(dv)) => {
update_available = true;
let _ = t.fg(terminalsource::Color::Yellow);
write!(t.lock(), "Update available")?;
let _ = t.reset();
writeln!(t.lock(), " : (Unknown version) -> {dv}")?;
if !is_a_tty {
writeln!(t.lock(), "{message}")?;
}
}
}

let self_update_mode = cfg.get_self_update_mode()?;
// Priority: no-self-update feature > self_update_mode > no-self-update args.
// Check for update only if rustup does **not** have the no-self-update feature,
Expand Down
Loading