Skip to content

Commit 1a8bae1

Browse files
committed
Attach Process-dependent utils to Process
1 parent 710ef22 commit 1a8bae1

File tree

8 files changed

+43
-40
lines changed

8 files changed

+43
-40
lines changed

src/cli/self_update.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,10 @@ fn update_root(process: &Process) -> String {
504504
/// `CARGO_HOME` suitable for display, possibly with $HOME
505505
/// substituted for the directory prefix
506506
fn canonical_cargo_home(process: &Process) -> Result<Cow<'static, str>> {
507-
let path = utils::cargo_home(process)?;
507+
let path = process.cargo_home()?;
508508

509-
let default_cargo_home = utils::home_dir(process)
509+
let default_cargo_home = process
510+
.home_dir()
510511
.unwrap_or_else(|| PathBuf::from("."))
511512
.join(".cargo");
512513
Ok(if default_cargo_home == path {
@@ -727,7 +728,7 @@ fn check_existence_of_rustc_or_cargo_in_path(no_prompt: bool, process: &Process)
727728
fn do_pre_install_sanity_checks(no_prompt: bool, process: &Process) -> Result<()> {
728729
let rustc_manifest_path = PathBuf::from("/usr/local/lib/rustlib/manifest-rustc");
729730
let uninstaller_path = PathBuf::from("/usr/local/lib/rustlib/uninstall.sh");
730-
let rustup_sh_path = utils::home_dir(process).unwrap().join(".rustup");
731+
let rustup_sh_path = process.home_dir().unwrap().join(".rustup");
731732
let rustup_sh_version_path = rustup_sh_path.join("rustup-version");
732733

733734
let rustc_exists = rustc_manifest_path.exists() && uninstaller_path.exists();
@@ -761,7 +762,7 @@ fn do_pre_install_sanity_checks(no_prompt: bool, process: &Process) -> Result<()
761762
}
762763

763764
fn pre_install_msg(no_modify_path: bool, process: &Process) -> Result<String> {
764-
let cargo_home = utils::cargo_home(process)?;
765+
let cargo_home = process.cargo_home()?;
765766
let cargo_home_bin = cargo_home.join("bin");
766767
let rustup_home = home::rustup_home()?;
767768

@@ -824,7 +825,7 @@ fn current_install_opts(opts: &InstallOpts<'_>, process: &Process) -> String {
824825
}
825826

826827
fn install_bins(process: &Process) -> Result<()> {
827-
let bin_path = utils::cargo_home(process)?.join("bin");
828+
let bin_path = process.cargo_home()?.join("bin");
828829
let this_exe_path = utils::current_exe()?;
829830
let rustup_path = bin_path.join(format!("rustup{EXE_SUFFIX}"));
830831

@@ -840,7 +841,7 @@ fn install_bins(process: &Process) -> Result<()> {
840841
}
841842

842843
pub(crate) fn install_proxies(process: &Process) -> Result<()> {
843-
let bin_path = utils::cargo_home(process)?.join("bin");
844+
let bin_path = process.cargo_home()?.join("bin");
844845
let rustup_path = bin_path.join(format!("rustup{EXE_SUFFIX}"));
845846

846847
let rustup = Handle::from_path(&rustup_path)?;
@@ -938,7 +939,8 @@ async fn maybe_install_rust(
938939

939940
// If RUSTUP_HOME is not set, make sure it exists
940941
if process.var_os("RUSTUP_HOME").is_none() {
941-
let home = utils::home_dir(process)
942+
let home = process
943+
.home_dir()
942944
.map(|p| p.join(".rustup"))
943945
.ok_or_else(|| anyhow::anyhow!("could not find home dir to put .rustup in"))?;
944946

@@ -988,7 +990,7 @@ pub(crate) fn uninstall(no_prompt: bool, process: &Process) -> Result<utils::Exi
988990
return Ok(utils::ExitCode(1));
989991
}
990992

991-
let cargo_home = utils::cargo_home(process)?;
993+
let cargo_home = process.cargo_home()?;
992994

993995
if !cargo_home.join(format!("bin/rustup{EXE_SUFFIX}")).exists() {
994996
return Err(CLIError::NotSelfInstalled { p: cargo_home }.into());
@@ -1178,7 +1180,7 @@ fn parse_new_rustup_version(version: String) -> String {
11781180
}
11791181

11801182
pub(crate) async fn prepare_update(process: &Process) -> Result<Option<PathBuf>> {
1181-
let cargo_home = utils::cargo_home(process)?;
1183+
let cargo_home = process.cargo_home()?;
11821184
let rustup_path = cargo_home.join(format!("bin{MAIN_SEPARATOR}rustup{EXE_SUFFIX}"));
11831185
let setup_path = cargo_home.join(format!("bin{MAIN_SEPARATOR}rustup-init{EXE_SUFFIX}"));
11841186

@@ -1319,7 +1321,7 @@ pub(crate) async fn check_rustup_update(process: &Process) -> Result<()> {
13191321

13201322
#[cfg_attr(feature = "otel", tracing::instrument)]
13211323
pub(crate) fn cleanup_self_updater(process: &Process) -> Result<()> {
1322-
let cargo_home = utils::cargo_home(process)?;
1324+
let cargo_home = process.cargo_home()?;
13231325
let setup = cargo_home.join(format!("bin/rustup-init{EXE_SUFFIX}"));
13241326

13251327
if setup.exists() {

src/cli/self_update/shell.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ pub(crate) struct ShellScript {
4141

4242
impl ShellScript {
4343
pub(crate) fn write(&self, process: &Process) -> Result<()> {
44-
let home = utils::cargo_home(process)?;
44+
let home = process.cargo_home()?;
4545
let cargo_bin = format!("{}/bin", cargo_home_str(process)?);
4646
let env_name = home.join(self.name);
4747
let env_file = self.content.replace("{cargo_bin}", &cargo_bin);
@@ -52,9 +52,10 @@ impl ShellScript {
5252

5353
// TODO: Update into a bytestring.
5454
pub(crate) fn cargo_home_str(process: &Process) -> Result<Cow<'static, str>> {
55-
let path = utils::cargo_home(process)?;
55+
let path = process.cargo_home()?;
5656

57-
let default_cargo_home = utils::home_dir(process)
57+
let default_cargo_home = process
58+
.home_dir()
5859
.unwrap_or_else(|| PathBuf::from("."))
5960
.join(".cargo");
6061
Ok(if default_cargo_home == path {
@@ -117,7 +118,7 @@ impl UnixShell for Posix {
117118
}
118119

119120
fn rcfiles(&self, process: &Process) -> Vec<PathBuf> {
120-
match utils::home_dir(process) {
121+
match process.home_dir() {
121122
Some(dir) => vec![dir.join(".profile")],
122123
_ => vec![],
123124
}
@@ -142,7 +143,7 @@ impl UnixShell for Bash {
142143
// .profile as part of POSIX and always does setup for POSIX shells.
143144
[".bash_profile", ".bash_login", ".bashrc"]
144145
.iter()
145-
.filter_map(|rc| utils::home_dir(process).map(|dir| dir.join(rc)))
146+
.filter_map(|rc| process.home_dir().map(|dir| dir.join(rc)))
146147
.collect()
147148
}
148149

@@ -186,7 +187,7 @@ impl UnixShell for Zsh {
186187
}
187188

188189
fn rcfiles(&self, process: &Process) -> Vec<PathBuf> {
189-
[Zsh::zdotdir(process).ok(), utils::home_dir(process)]
190+
[Zsh::zdotdir(process).ok(), process.home_dir()]
190191
.iter()
191192
.filter_map(|dir| dir.as_ref().map(|p| p.join(".zshenv")))
192193
.collect()
@@ -226,7 +227,7 @@ impl UnixShell for Fish {
226227
path
227228
});
228229

229-
let p1 = utils::home_dir(process).map(|mut path| {
230+
let p1 = process.home_dir().map(|mut path| {
230231
path.push(".config/fish/conf.d/rustup.fish");
231232
path
232233
});
@@ -257,11 +258,11 @@ impl UnixShell for Fish {
257258
pub(crate) fn legacy_paths(process: &Process) -> impl Iterator<Item = PathBuf> + '_ {
258259
let zprofiles = Zsh::zdotdir(process)
259260
.into_iter()
260-
.chain(utils::home_dir(process))
261+
.chain(process.home_dir())
261262
.map(|d| d.join(".zprofile"));
262263
let profiles = [".bash_profile", ".profile"]
263264
.iter()
264-
.filter_map(|rc| utils::home_dir(process).map(|d| d.join(rc)));
265+
.filter_map(|rc| process.home_dir().map(|d| d.join(rc)));
265266

266267
profiles.chain(zprofiles)
267268
}

src/cli/self_update/unix.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub(crate) fn do_anti_sudo_check(no_prompt: bool, process: &Process) -> Result<u
4848
}
4949

5050
pub(crate) fn delete_rustup_and_cargo_home(process: &Process) -> Result<()> {
51-
let cargo_home = utils::cargo_home(process)?;
51+
let cargo_home = process.cargo_home()?;
5252
utils::remove_dir("cargo_home", &cargo_home, &|_: Notification<'_>| ())
5353
}
5454

src/cli/self_update/windows.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ pub fn complete_windows_uninstall(process: &Process) -> Result<utils::ExitCode>
279279
wait_for_parent()?;
280280

281281
// Now that the parent has exited there are hopefully no more files open in CARGO_HOME
282-
let cargo_home = utils::cargo_home(process)?;
282+
let cargo_home = process.cargo_home()?;
283283
utils::remove_dir("cargo_home", &cargo_home, &|_: Notification<'_>| ())?;
284284

285285
// Now, run a *system* binary to inherit the DELETE_ON_CLOSE
@@ -492,7 +492,7 @@ where
492492
F: FnOnce(Vec<u16>, Vec<u16>) -> Option<Vec<u16>>,
493493
{
494494
let windows_path = get_windows_path_var()?;
495-
let mut path_str = utils::cargo_home(process)?;
495+
let mut path_str = process.cargo_home()?;
496496
path_str.push("bin");
497497
Ok(windows_path
498498
.and_then(|old_path| f(old_path, OsString::from(path_str).encode_wide().collect())))
@@ -535,7 +535,7 @@ pub(crate) fn do_add_to_programs(process: &Process) -> Result<()> {
535535
}
536536
}
537537

538-
let mut path = utils::cargo_home(process)?;
538+
let mut path = process.cargo_home()?;
539539
path.push("bin\\rustup.exe");
540540
let mut uninstall_cmd = OsString::from("\"");
541541
uninstall_cmd.push(path);
@@ -657,7 +657,7 @@ pub(crate) fn delete_rustup_and_cargo_home(process: &Process) -> Result<()> {
657657
};
658658

659659
// CARGO_HOME, hopefully empty except for bin/rustup.exe
660-
let cargo_home = utils::cargo_home(process)?;
660+
let cargo_home = process.cargo_home()?;
661661
// The rustup.exe bin
662662
let rustup_path = cargo_home.join(format!("bin/rustup{EXE_SUFFIX}"));
663663

src/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ impl<'a> Cfg<'a> {
258258
process: &'a Process,
259259
) -> Result<Self> {
260260
// Set up the rustup home directory
261-
let rustup_dir = utils::rustup_home(process)?;
261+
let rustup_dir = process.rustup_home()?;
262262

263263
utils::ensure_dir_exists("home", &rustup_dir, notify_handler.as_ref())?;
264264

src/currentprocess.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use std::{
1212
sync::{Arc, Mutex},
1313
};
1414

15+
use anyhow::{Context, Result};
1516
#[cfg(feature = "test")]
1617
use tracing::subscriber::DefaultGuard;
1718
use tracing_subscriber::util::SubscriberInitExt;
@@ -45,6 +46,18 @@ impl Process {
4546
.map(String::from)
4647
}
4748

49+
pub(crate) fn home_dir(&self) -> Option<PathBuf> {
50+
home::env::home_dir_with_env(self)
51+
}
52+
53+
pub(crate) fn cargo_home(&self) -> Result<PathBuf> {
54+
home::env::cargo_home_with_env(self).context("failed to determine cargo home")
55+
}
56+
57+
pub(crate) fn rustup_home(&self) -> Result<PathBuf> {
58+
home::env::rustup_home_with_env(self).context("failed to determine rustup home dir")
59+
}
60+
4861
pub fn var(&self, key: &str) -> Result<String, env::VarError> {
4962
match self {
5063
Process::OSProcess(_) => env::var(key),

src/toolchain/toolchain.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ impl<'a> Toolchain<'a> {
185185
// versions of Cargo did. Rustup and Cargo should be in sync now (both
186186
// using the same `home` crate), but this is retained to ensure cargo
187187
// and rustup agree in older versions.
188-
if let Ok(cargo_home) = utils::cargo_home(self.cfg.process) {
188+
if let Ok(cargo_home) = self.cfg.process.cargo_home() {
189189
cmd.env("CARGO_HOME", &cargo_home);
190190
}
191191

@@ -243,7 +243,7 @@ impl<'a> Toolchain<'a> {
243243
// proxy bins don't exist. We'll just be running whatever happens to
244244
// be on the PATH.
245245
let mut path_entries = vec![];
246-
if let Ok(cargo_home) = utils::cargo_home(self.cfg.process) {
246+
if let Ok(cargo_home) = self.cfg.process.cargo_home() {
247247
path_entries.push(cargo_home.join("bin"));
248248
}
249249

src/utils/utils.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use std::path::{Path, PathBuf};
55
use std::process::ExitStatus;
66

77
use anyhow::{anyhow, bail, Context, Result};
8-
use home::env as home;
98
use retry::delay::{jitter, Fibonacci};
109
use retry::{retry, OperationResult};
1110
use sha2::Sha256;
@@ -483,18 +482,6 @@ pub fn current_exe() -> Result<PathBuf> {
483482
env::current_exe().context(RustupError::LocatingWorkingDir)
484483
}
485484

486-
pub(crate) fn home_dir(process: &Process) -> Option<PathBuf> {
487-
home::home_dir_with_env(process)
488-
}
489-
490-
pub(crate) fn cargo_home(process: &Process) -> Result<PathBuf> {
491-
home::cargo_home_with_env(process).context("failed to determine cargo home")
492-
}
493-
494-
pub(crate) fn rustup_home(process: &Process) -> Result<PathBuf> {
495-
home::rustup_home_with_env(process).context("failed to determine rustup home dir")
496-
}
497-
498485
pub(crate) fn format_path_for_display(path: &str) -> String {
499486
let unc_present = path.find(r"\\?\");
500487

0 commit comments

Comments
 (0)