Skip to content
Open
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
11 changes: 5 additions & 6 deletions src/cli/common.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
//! Just a dumping ground for cli stuff

use std::cell::RefCell;
use std::fmt::Display;
use std::fs;
use std::io::{BufRead, Write};
use std::path::{Path, PathBuf};
use std::sync::{Arc, LazyLock, Mutex};
use std::sync::{Arc, LazyLock, Mutex, RwLock};
use std::{cmp, env};

use anyhow::{Context, Result, anyhow};
Expand Down Expand Up @@ -123,14 +122,14 @@ pub(crate) fn read_line(process: &Process) -> Result<String> {

pub(super) struct Notifier {
tracker: Mutex<DownloadTracker>,
ram_notice_shown: RefCell<bool>,
ram_notice_shown: RwLock<bool>,
}

impl Notifier {
pub(super) fn new(quiet: bool, process: &Process) -> Self {
Self {
tracker: Mutex::new(DownloadTracker::new_with_display_progress(!quiet, process)),
ram_notice_shown: RefCell::new(false),
ram_notice_shown: RwLock::new(false),
}
}

Expand All @@ -140,10 +139,10 @@ impl Notifier {
}

if let Notification::SetDefaultBufferSize(_) = &n {
if *self.ram_notice_shown.borrow() {
if *self.ram_notice_shown.read().unwrap() {
return;
} else {
*self.ram_notice_shown.borrow_mut() = true;
*self.ram_notice_shown.write().unwrap() = true;
}
};
let level = n.level();
Expand Down
63 changes: 60 additions & 3 deletions src/cli/download_tracker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ impl DownloadTracker {
self.retrying_download(url);
true
}
Notification::InstallingComponent(component, _, _) => {
self.installing_component(component);
true
}
Notification::ComponentInstalled(component, _, _) => {
self.component_installed(component);
true
}
_ => false,
}
}
Expand Down Expand Up @@ -120,10 +128,13 @@ impl DownloadTracker {
return;
};
pb.set_style(
ProgressStyle::with_template("{msg:>12.bold} downloaded {total_bytes} in {elapsed}")
.unwrap(),
ProgressStyle::with_template(if pb.position() != 0 {
"{msg:>12.bold} downloaded {total_bytes} in {elapsed}"
} else {
"{msg:>12.bold} component already downloaded"
})
.unwrap(),
Comment on lines +131 to +136
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again there's a lot of duplication here, suggest adding a little abstraction to make it more obvious what happens here.

);
pb.finish();
}

/// Notifies self that the download has failed.
Expand All @@ -146,4 +157,50 @@ impl DownloadTracker {
*retry_time = Some(Instant::now());
pb.set_style(ProgressStyle::with_template("{msg:>12.bold} retrying download").unwrap());
}

/// Notifies self that the component is being installed.
pub(crate) fn installing_component(&mut self, component: &str) {
let key = self
.file_progress_bars
.keys()
.find(|comp| comp.contains(component))
.cloned();
if let Some(key) = key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: invert these for early returns.

&& let Some((pb, _)) = self.file_progress_bars.get(&key)
{
pb.set_style(
ProgressStyle::with_template( if pb.position() != 0 {
"{msg:>12.bold} downloaded {total_bytes} in {elapsed} and installing {spinner:.green}"
} else {
"{msg:>12.bold} component already downloaded and installing {spinner:.green}"
}
)
.unwrap()
.tick_chars(r"|/-\ "),
);
pb.enable_steady_tick(Duration::from_millis(100));
}
}

/// Notifies self that the component has been installed.
pub(crate) fn component_installed(&mut self, component: &str) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we share the duplicate code?

let key = self
.file_progress_bars
.keys()
.find(|comp| comp.contains(component))
.cloned();
if let Some(key) = key
&& let Some((pb, _)) = self.file_progress_bars.get(&key)
{
pb.set_style(
ProgressStyle::with_template(if pb.position() != 0 {
"{msg:>12.bold} downloaded {total_bytes} and installed"
} else {
"{msg:>12.bold} component already downloaded and installed"
})
.unwrap(),
);
pb.finish();
}
}
}
25 changes: 11 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,19 @@ pub(crate) struct Cfg<'a> {
pub toolchains_dir: PathBuf,
pub update_hash_dir: PathBuf,
pub download_dir: PathBuf,
pub tmp_cx: temp::Context,
pub tmp_cx: Arc<temp::Context>,
pub toolchain_override: Option<ResolvableToolchainName>,
pub env_override: Option<LocalToolchainName>,
pub dist_root_url: String,
pub notify_handler: Arc<dyn Fn(Notification<'_>)>,
pub notify_handler: Arc<NotifyHandler>,
pub current_dir: PathBuf,
pub process: &'a Process,
}

impl<'a> Cfg<'a> {
pub(crate) fn from_env(
current_dir: PathBuf,
notify_handler: Arc<dyn Fn(Notification<'_>)>,
notify_handler: Arc<NotifyHandler>,
process: &'a Process,
) -> Result<Self> {
// Set up the rustup home directory
Expand Down Expand Up @@ -292,11 +292,11 @@ impl<'a> Cfg<'a> {
let dist_root_server = dist_root_server(process)?;

let notify_clone = notify_handler.clone();
let tmp_cx = temp::Context::new(
let tmp_cx = Arc::new(temp::Context::new(
rustup_dir.join("tmp"),
dist_root_server.as_str(),
Box::new(move |n| (notify_clone)(n)),
);
Arc::new(move |n| (notify_clone)(n)),
));
let dist_root = dist_root_server + "/dist";

let cfg = Self {
Expand Down Expand Up @@ -328,16 +328,13 @@ impl<'a> Cfg<'a> {
}

/// construct a download configuration
pub(crate) fn download_cfg(
&'a self,
notify_handler: &'a dyn Fn(Notification<'_>),
) -> DownloadCfg<'a> {
pub(crate) fn download_cfg(&self, notify_handler: Arc<NotifyHandler>) -> DownloadCfg {
DownloadCfg {
dist_root: &self.dist_root_url,
tmp_cx: &self.tmp_cx,
download_dir: &self.download_dir,
dist_root: Arc::from(self.dist_root_url.clone()),
tmp_cx: Arc::clone(&self.tmp_cx),
download_dir: Arc::new(self.download_dir.clone()),
notify_handler,
process: self.process,
process: Arc::new(self.process.clone()),
}
}

Expand Down
26 changes: 14 additions & 12 deletions src/dist/component/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl Components {
Ok(None)
}
}
fn write_version(&self, tx: &mut Transaction<'_>) -> Result<()> {
fn write_version(&self, tx: &mut Transaction) -> Result<()> {
tx.modify_file(self.prefix.rel_manifest_file(VERSION_FILE))?;
utils::write_file(
VERSION_FILE,
Expand All @@ -79,7 +79,7 @@ impl Components {
})
.collect())
}
pub(crate) fn add<'a>(&self, name: &str, tx: Transaction<'a>) -> ComponentBuilder<'a> {
pub(crate) fn add(&self, name: &str, tx: Transaction) -> ComponentBuilder {
ComponentBuilder {
components: self.clone(),
name: name.to_owned(),
Expand All @@ -96,14 +96,14 @@ impl Components {
}
}

pub(crate) struct ComponentBuilder<'a> {
pub(crate) struct ComponentBuilder {
components: Components,
name: String,
parts: Vec<ComponentPart>,
tx: Transaction<'a>,
tx: Transaction,
}

impl<'a> ComponentBuilder<'a> {
impl ComponentBuilder {
pub(crate) fn copy_file(&mut self, path: PathBuf, src: &Path) -> Result<()> {
self.parts.push(ComponentPart {
kind: ComponentPartKind::File,
Expand Down Expand Up @@ -132,7 +132,7 @@ impl<'a> ComponentBuilder<'a> {
});
self.tx.move_dir(&self.name, path, src)
}
pub(crate) fn finish(mut self) -> Result<Transaction<'a>> {
pub(crate) fn finish(mut self) -> Result<Transaction> {
// Write component manifest
let path = self.components.rel_component_manifest(&self.name);
let abs_path = self.components.prefix.abs_path(&path);
Expand Down Expand Up @@ -255,18 +255,20 @@ impl Component {
}
Ok(result)
}
pub fn uninstall<'a>(
&self,
mut tx: Transaction<'a>,
process: &Process,
) -> Result<Transaction<'a>> {
pub fn uninstall(&self, mut tx: Transaction, process: &Process) -> Result<Transaction> {
// Update components file
let path = self.components.rel_components_file();
let abs_path = self.components.prefix.abs_path(&path);
let temp = tx.temp().new_file()?;
utils::filter_file("components", &abs_path, &temp, |l| l != self.name)?;
tx.modify_file(path)?;
utils::rename("components", &temp, &abs_path, tx.notify_handler(), process)?;
utils::rename(
"components",
&temp,
&abs_path,
&*tx.notify_handler(),
process,
)?;

// TODO: If this is the last component remove the components file
// and the version file.
Expand Down
Loading
Loading