Skip to content

Commit 2b280ae

Browse files
committed
dist: move Notifier into DownloadCfg
1 parent b7b9d81 commit 2b280ae

File tree

13 files changed

+141
-151
lines changed

13 files changed

+141
-151
lines changed

src/cli/common.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::fmt::Display;
44
use std::fs;
55
use std::io::{BufRead, Write};
66
use std::path::{Path, PathBuf};
7-
use std::sync::{Arc, LazyLock};
7+
use std::sync::LazyLock;
88
use std::{cmp, env};
99

1010
use anyhow::{Context, Result, anyhow};
@@ -13,7 +13,6 @@ use termcolor::Color;
1313
use tracing::{error, info, warn};
1414
use tracing_subscriber::{EnvFilter, Registry, reload::Handle};
1515

16-
use crate::dist::download::Notifier;
1716
use crate::{
1817
config::Cfg,
1918
dist::{TargetTriple, ToolchainDesc},
@@ -122,8 +121,7 @@ pub(crate) fn read_line(process: &Process) -> Result<String> {
122121

123122
#[tracing::instrument(level = "trace", skip(process))]
124123
pub(crate) fn set_globals(current_dir: PathBuf, quiet: bool, process: &Process) -> Result<Cfg<'_>> {
125-
let notifier = Notifier::new(quiet, process);
126-
Cfg::from_env(current_dir, Arc::new(move |n| notifier.handle(n)), process)
124+
Cfg::from_env(current_dir, quiet, process)
127125
}
128126

129127
pub(crate) fn show_channel_update(

src/cli/rustup_mode.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ use crate::{
3434
config::{ActiveReason, Cfg},
3535
dist::{
3636
AutoInstallMode, PartialToolchainDesc, Profile, TargetTriple,
37+
download::DownloadCfg,
3738
manifest::{Component, ComponentStatus},
3839
},
3940
errors::RustupError,
@@ -909,7 +910,7 @@ async fn check_updates(cfg: &Cfg<'_>, opts: CheckOpts) -> Result<utils::ExitCode
909910
&& self_update_mode == SelfUpdateMode::Enable
910911
&& !opts.no_self_update;
911912

912-
if self_update && check_rustup_update(cfg.process).await? {
913+
if self_update && check_rustup_update(&DownloadCfg::new(cfg)).await? {
913914
update_available = true;
914915
}
915916

@@ -937,10 +938,13 @@ async fn update(
937938
if let Some(p) = opts.profile {
938939
cfg.set_profile_override(p);
939940
}
941+
940942
let cfg = &cfg;
941943
if cfg.get_profile()? == Profile::Complete {
942944
warn!("{}", common::WARN_COMPLETE_PROFILE);
943945
}
946+
947+
let dl_cfg = DownloadCfg::new(cfg);
944948
let names = opts.toolchain;
945949
if !names.is_empty() {
946950
for name in names {
@@ -994,7 +998,7 @@ async fn update(
994998
}
995999
}
9961000
if self_update {
997-
exit_code &= self_update::self_update(cfg.process).await?;
1001+
exit_code &= self_update::self_update(&dl_cfg).await?;
9981002
}
9991003
} else if ensure_active_toolchain {
10001004
let (toolchain, reason) = cfg.ensure_active_toolchain(force_non_host, true).await?;
@@ -1003,7 +1007,7 @@ async fn update(
10031007
} else {
10041008
exit_code &= common::update_all_channels(cfg, opts.force).await?;
10051009
if self_update {
1006-
exit_code &= self_update::self_update(cfg.process).await?;
1010+
exit_code &= self_update::self_update(&dl_cfg).await?;
10071011
}
10081012

10091013
info!("cleaning up downloads & tmp directories");
@@ -1012,7 +1016,7 @@ async fn update(
10121016
}
10131017

10141018
if !cfg!(feature = "no-self-update") && self_update_mode == SelfUpdateMode::CheckOnly {
1015-
check_rustup_update(cfg.process).await?;
1019+
check_rustup_update(&dl_cfg).await?;
10161020
}
10171021

10181022
if cfg!(feature = "no-self-update") {

src/cli/self_update.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ use serde::{Deserialize, Serialize};
5050
use termcolor::Color;
5151
use tracing::{error, info, trace, warn};
5252

53+
use crate::dist::download::DownloadCfg;
5354
use crate::{
5455
DUP_TOOLS, TOOLS,
5556
cli::{
@@ -541,7 +542,7 @@ pub(crate) async fn install(
541542
let mut term = cfg.process.stdout();
542543

543544
#[cfg(windows)]
544-
windows::maybe_install_msvc(&mut term, no_prompt, quiet, &opts, process).await?;
545+
windows::maybe_install_msvc(&mut term, no_prompt, &opts, &*cfg).await?;
545546

546547
if !no_prompt {
547548
let msg = pre_install_msg(opts.no_modify_path, cfg.process)?;
@@ -576,7 +577,7 @@ pub(crate) async fn install(
576577
// window closes.
577578
#[cfg(windows)]
578579
if !no_prompt {
579-
windows::ensure_prompt(process)?;
580+
windows::ensure_prompt(cfg.process)?;
580581
}
581582

582583
return Ok(utils::ExitCode(1));
@@ -620,7 +621,7 @@ pub(crate) async fn install(
620621
// On windows, where installation happens in a console
621622
// that may have opened just for this purpose, require
622623
// the user to press a key to continue.
623-
windows::ensure_prompt(process)?;
624+
windows::ensure_prompt(cfg.process)?;
624625
}
625626

626627
Ok(exit_code)
@@ -1105,7 +1106,7 @@ pub(crate) fn self_update_permitted(explicit: bool) -> Result<SelfUpdatePermissi
11051106
}
11061107

11071108
/// Performs all of a self-update: check policy, download, apply and exit.
1108-
pub(crate) async fn self_update(process: &Process) -> Result<utils::ExitCode> {
1109+
pub(crate) async fn self_update(dl_cfg: &DownloadCfg<'_>) -> Result<utils::ExitCode> {
11091110
match self_update_permitted(false)? {
11101111
SelfUpdatePermission::HardFail => {
11111112
error!("Unable to self-update. STOP");
@@ -1116,13 +1117,13 @@ pub(crate) async fn self_update(process: &Process) -> Result<utils::ExitCode> {
11161117
SelfUpdatePermission::Permit => {}
11171118
}
11181119

1119-
let setup_path = prepare_update(process).await?;
1120+
let setup_path = prepare_update(dl_cfg).await?;
11201121

11211122
if let Some(setup_path) = &setup_path {
11221123
return run_update(setup_path);
11231124
} else {
11241125
// Try again in case we emitted "tool `{}` is already installed" last time.
1125-
install_proxies(process)?;
1126+
install_proxies(dl_cfg.process)?;
11261127
}
11271128

11281129
Ok(utils::ExitCode(0))
@@ -1167,7 +1168,7 @@ pub(crate) async fn update(cfg: &Cfg<'_>) -> Result<utils::ExitCode> {
11671168
Permit => {}
11681169
}
11691170

1170-
match prepare_update(cfg.process).await? {
1171+
match prepare_update(&DownloadCfg::new(cfg)).await? {
11711172
Some(setup_path) => {
11721173
let Some(version) = get_and_parse_new_rustup_version(&setup_path) else {
11731174
error!("failed to get rustup version");
@@ -1220,8 +1221,8 @@ fn parse_new_rustup_version(version: String) -> String {
12201221
String::from(matched_version)
12211222
}
12221223

1223-
pub(crate) async fn prepare_update(process: &Process) -> Result<Option<PathBuf>> {
1224-
let cargo_home = process.cargo_home()?;
1224+
pub(crate) async fn prepare_update(dl_cfg: &DownloadCfg<'_>) -> Result<Option<PathBuf>> {
1225+
let cargo_home = dl_cfg.process.cargo_home()?;
12251226
let rustup_path = cargo_home.join(format!("bin{MAIN_SEPARATOR}rustup{EXE_SUFFIX}"));
12261227
let setup_path = cargo_home.join(format!("bin{MAIN_SEPARATOR}rustup-init{EXE_SUFFIX}"));
12271228

@@ -1243,22 +1244,22 @@ pub(crate) async fn prepare_update(process: &Process) -> Result<Option<PathBuf>>
12431244
// If someone really wants to use another version, they still can enforce
12441245
// that using the environment variable RUSTUP_OVERRIDE_HOST_TRIPLE.
12451246
#[cfg(windows)]
1246-
let triple = dist::TargetTriple::from_host(process).unwrap_or(triple);
1247+
let triple = dist::TargetTriple::from_host(dl_cfg.process).unwrap_or(triple);
12471248

12481249
// Get update root.
1249-
let update_root = update_root(process);
1250+
let update_root = update_root(dl_cfg.process);
12501251

12511252
// Get current version
12521253
let current_version = env!("CARGO_PKG_VERSION");
12531254

12541255
// Get available version
12551256
info!("checking for self-update (current version: {current_version})");
1256-
let available_version = match process.var_opt("RUSTUP_VERSION")? {
1257+
let available_version = match dl_cfg.process.var_opt("RUSTUP_VERSION")? {
12571258
Some(ver) => {
12581259
info!("`RUSTUP_VERSION` has been set to `{ver}`");
12591260
ver
12601261
}
1261-
None => get_available_rustup_version(process).await?,
1262+
None => get_available_rustup_version(dl_cfg).await?,
12621263
};
12631264

12641265
// If up-to-date
@@ -1274,16 +1275,23 @@ pub(crate) async fn prepare_update(process: &Process) -> Result<Option<PathBuf>>
12741275

12751276
// Download new version
12761277
info!("downloading self-update (new version: {available_version})");
1277-
download_file(&download_url, &setup_path, None, &|_| (), process).await?;
1278+
download_file(
1279+
&download_url,
1280+
&setup_path,
1281+
None,
1282+
&dl_cfg.notifier,
1283+
dl_cfg.process,
1284+
)
1285+
.await?;
12781286

12791287
// Mark as executable
12801288
utils::make_executable(&setup_path)?;
12811289

12821290
Ok(Some(setup_path))
12831291
}
12841292

1285-
async fn get_available_rustup_version(process: &Process) -> Result<String> {
1286-
let update_root = update_root(process);
1293+
async fn get_available_rustup_version(dl_cfg: &DownloadCfg<'_>) -> Result<String> {
1294+
let update_root = update_root(dl_cfg.process);
12871295
let tempdir = tempfile::Builder::new()
12881296
.prefix("rustup-update")
12891297
.tempdir()
@@ -1293,7 +1301,14 @@ async fn get_available_rustup_version(process: &Process) -> Result<String> {
12931301
let release_file_url = format!("{update_root}/release-stable.toml");
12941302
let release_file_url = utils::parse_url(&release_file_url)?;
12951303
let release_file = tempdir.path().join("release-stable.toml");
1296-
download_file(&release_file_url, &release_file, None, &|_| (), process).await?;
1304+
download_file(
1305+
&release_file_url,
1306+
&release_file,
1307+
None,
1308+
&dl_cfg.notifier,
1309+
dl_cfg.process,
1310+
)
1311+
.await?;
12971312
let release_toml_str = utils::read_file("rustup release", &release_file)?;
12981313
let release_toml = toml::from_str::<RustupManifest>(&release_toml_str)
12991314
.context("unable to parse rustup release file")?;
@@ -1341,13 +1356,13 @@ impl fmt::Display for SchemaVersion {
13411356
}
13421357

13431358
/// Returns whether an update was available
1344-
pub(crate) async fn check_rustup_update(process: &Process) -> anyhow::Result<bool> {
1345-
let mut t = process.stdout();
1359+
pub(crate) async fn check_rustup_update(dl_cfg: &DownloadCfg<'_>) -> anyhow::Result<bool> {
1360+
let mut t = dl_cfg.process.stdout();
13461361
// Get current rustup version
13471362
let current_version = env!("CARGO_PKG_VERSION");
13481363

13491364
// Get available rustup version
1350-
let available_version = get_available_rustup_version(process).await?;
1365+
let available_version = get_available_rustup_version(dl_cfg).await?;
13511366

13521367
let _ = t.attr(Attr::Bold);
13531368
write!(t.lock(), "rustup - ")?;

src/cli/self_update/windows.rs

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::io::Write;
55
use std::os::windows::ffi::OsStrExt;
66
use std::path::Path;
77
use std::process::Command;
8-
use std::sync::{Arc, Mutex};
98
#[cfg(any(test, feature = "test"))]
109
use std::sync::{LockResult, MutexGuard};
1110

@@ -20,8 +19,10 @@ use windows_sys::Win32::Foundation::{ERROR_FILE_NOT_FOUND, ERROR_INVALID_DATA};
2019
use super::super::errors::*;
2120
use super::common;
2221
use super::{InstallOpts, install_bins, report_error};
23-
use crate::cli::{download_tracker::DownloadTracker, markdown::md};
22+
use crate::cli::markdown::md;
23+
use crate::config::Cfg;
2424
use crate::dist::TargetTriple;
25+
use crate::dist::download::DownloadCfg;
2526
use crate::download::download_file;
2627
use crate::process::{ColorableTerminal, Process};
2728
use crate::utils;
@@ -91,36 +92,35 @@ pub(crate) fn choose_vs_install(process: &Process) -> Result<Option<VsInstallPla
9192
pub(super) async fn maybe_install_msvc(
9293
term: &mut ColorableTerminal,
9394
no_prompt: bool,
94-
quiet: bool,
9595
opts: &InstallOpts<'_>,
96-
process: &Process,
96+
cfg: &Cfg<'_>,
9797
) -> Result<()> {
98-
let Some(plan) = do_msvc_check(opts, process) else {
98+
let Some(plan) = do_msvc_check(opts, cfg.process) else {
9999
return Ok(());
100100
};
101101

102102
if no_prompt {
103103
warn!("installing msvc toolchain without its prerequisites");
104-
} else if !quiet && plan == VsInstallPlan::Automatic {
104+
} else if !cfg.quiet && plan == VsInstallPlan::Automatic {
105105
md(term, MSVC_AUTO_INSTALL_MESSAGE);
106-
match choose_vs_install(process)? {
106+
match choose_vs_install(cfg.process)? {
107107
Some(VsInstallPlan::Automatic) => {
108-
match try_install_msvc(opts, process).await {
108+
match try_install_msvc(opts, cfg).await {
109109
Err(e) => {
110110
// Make sure the console doesn't exit before the user can
111111
// see the error and give the option to continue anyway.
112-
report_error(&e, process);
113-
if !common::question_bool("\nContinue?", false, process)? {
112+
report_error(&e, cfg.process);
113+
if !common::question_bool("\nContinue?", false, cfg.process)? {
114114
info!("aborting installation");
115115
}
116116
}
117-
Ok(ContinueInstall::No) => ensure_prompt(process)?,
117+
Ok(ContinueInstall::No) => ensure_prompt(cfg.process)?,
118118
_ => {}
119119
}
120120
}
121121
Some(VsInstallPlan::Manual) => {
122122
md(term, MSVC_MANUAL_INSTALL_MESSAGE);
123-
if !common::question_bool("\nContinue?", false, process)? {
123+
if !common::question_bool("\nContinue?", false, cfg.process)? {
124124
info!("aborting installation");
125125
}
126126
}
@@ -129,7 +129,7 @@ pub(super) async fn maybe_install_msvc(
129129
} else {
130130
md(term, MSVC_MESSAGE);
131131
md(term, MSVC_MANUAL_INSTALL_MESSAGE);
132-
if !common::question_bool("\nContinue?", false, process)? {
132+
if !common::question_bool("\nContinue?", false, cfg.process)? {
133133
info!("aborting installation");
134134
}
135135
}
@@ -260,7 +260,7 @@ pub(crate) enum ContinueInstall {
260260
/// but the rustup install should not be continued at this time.
261261
pub(crate) async fn try_install_msvc(
262262
opts: &InstallOpts<'_>,
263-
process: &Process,
263+
cfg: &Cfg<'_>,
264264
) -> Result<ContinueInstall> {
265265
// download the installer
266266
let visual_studio_url = utils::parse_url("https://aka.ms/vs/17/release/vs_community.exe")?;
@@ -271,23 +271,14 @@ pub(crate) async fn try_install_msvc(
271271
.context("error creating temp directory")?;
272272

273273
let visual_studio = tempdir.path().join("vs_setup.exe");
274-
let download_tracker = Arc::new(Mutex::new(DownloadTracker::new_with_display_progress(
275-
true, process,
276-
)));
277-
download_tracker
278-
.lock()
279-
.unwrap()
280-
.download_finished(visual_studio_url.as_str());
281-
274+
let dl_cfg = DownloadCfg::new(cfg);
282275
info!("downloading Visual Studio installer");
283276
download_file(
284277
&visual_studio_url,
285278
&visual_studio,
286279
None,
287-
&move |n| {
288-
download_tracker.lock().unwrap().handle_notification(&n);
289-
},
290-
process,
280+
&dl_cfg.notifier,
281+
dl_cfg.process,
291282
)
292283
.await?;
293284

@@ -304,7 +295,7 @@ pub(crate) async fn try_install_msvc(
304295

305296
// It's possible an earlier or later version of the Windows SDK has been
306297
// installed separately from Visual Studio so installing it can be skipped.
307-
if !has_windows_sdk_libs(process) {
298+
if !has_windows_sdk_libs(cfg.process) {
308299
cmd.args([
309300
"--add",
310301
"Microsoft.VisualStudio.Component.Windows11SDK.22000",
@@ -337,8 +328,8 @@ pub(crate) async fn try_install_msvc(
337328
// It's possible that the installer returned a non-zero exit code
338329
// even though the required components were successfully installed.
339330
// In that case we warn about the error but continue on.
340-
let have_msvc = do_msvc_check(opts, process).is_none();
341-
let has_libs = has_windows_sdk_libs(process);
331+
let have_msvc = do_msvc_check(opts, cfg.process).is_none();
332+
let has_libs = has_windows_sdk_libs(cfg.process);
342333
if have_msvc && has_libs {
343334
warn!("Visual Studio is installed but a problem occurred during installation");
344335
warn!("{}", err);

0 commit comments

Comments
 (0)