Skip to content

Commit d4a1621

Browse files
Cleanup refactoring
Factor out get_{remove,add}_path_methods Factor out secret "write_env" fn Run rustfmt on everything
1 parent 187aec3 commit d4a1621

File tree

3 files changed

+71
-74
lines changed

3 files changed

+71
-74
lines changed

src/cli/self_update.rs

Lines changed: 3 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,8 @@ mod os {
6161
pub use crate::self_update::windows::*;
6262
}
6363

64-
pub use os::{
65-
complete_windows_uninstall, delete_rustup_and_cargo_home, run_update, self_replace,
66-
};
6764
use os::*;
65+
pub use os::{complete_windows_uninstall, delete_rustup_and_cargo_home, run_update, self_replace};
6866

6967
pub struct InstallOpts<'a> {
7068
pub default_host_triple: Option<String>,
@@ -323,11 +321,8 @@ pub fn install(no_prompt: bool, verbose: bool, quiet: bool, mut opts: InstallOpt
323321
quiet,
324322
)?;
325323

326-
if cfg!(unix) {
327-
let env_file = utils::cargo_home()?.join("env");
328-
let env_str = format!("{}\n", shell_export_string()?);
329-
utils::write_file("env", &env_file, &env_str)?;
330-
}
324+
#[cfg(unix)]
325+
write_env()?;
331326

332327
Ok(())
333328
})();
@@ -853,69 +848,6 @@ pub fn uninstall(no_prompt: bool) -> Result<()> {
853848
process::exit(0);
854849
}
855850

856-
/// Decide which rcfiles we're going to update, so we
857-
/// can tell the user before they confirm.
858-
fn get_add_path_methods() -> Vec<PathUpdateMethod> {
859-
if cfg!(windows) {
860-
return vec![PathUpdateMethod::Windows];
861-
}
862-
863-
let home_dir = utils::home_dir().unwrap();
864-
let profile = home_dir.join(".profile");
865-
let mut profiles = vec![profile];
866-
867-
if let Ok(shell) = env::var("SHELL") {
868-
if shell.contains("zsh") {
869-
let var = env::var_os("ZDOTDIR");
870-
let zdotdir = var.as_deref().map_or_else(|| home_dir.as_path(), Path::new);
871-
let zprofile = zdotdir.join(".zprofile");
872-
profiles.push(zprofile);
873-
}
874-
}
875-
876-
let bash_profile = home_dir.join(".bash_profile");
877-
// Only update .bash_profile if it exists because creating .bash_profile
878-
// will cause .profile to not be read
879-
if bash_profile.exists() {
880-
profiles.push(bash_profile);
881-
}
882-
883-
profiles.into_iter().map(PathUpdateMethod::RcFile).collect()
884-
}
885-
886-
fn shell_export_string() -> Result<String> {
887-
let path = format!("{}/bin", canonical_cargo_home()?);
888-
// The path is *prepended* in case there are system-installed
889-
// rustc's that need to be overridden.
890-
Ok(format!(r#"export PATH="{}:$PATH""#, path))
891-
}
892-
893-
894-
895-
/// Decide which rcfiles we're going to update, so we
896-
/// can tell the user before they confirm.
897-
fn get_remove_path_methods() -> Result<Vec<PathUpdateMethod>> {
898-
if cfg!(windows) {
899-
return Ok(vec![PathUpdateMethod::Windows]);
900-
}
901-
902-
let profile = utils::home_dir().map(|p| p.join(".profile"));
903-
let bash_profile = utils::home_dir().map(|p| p.join(".bash_profile"));
904-
905-
let rcfiles = vec![profile, bash_profile];
906-
let existing_rcfiles = rcfiles.into_iter().filter_map(|f| f).filter(|f| f.exists());
907-
908-
let export_str = shell_export_string()?;
909-
let matching_rcfiles = existing_rcfiles.filter(|f| {
910-
let file = utils::read_file("rcfile", f).unwrap_or_default();
911-
let addition = format!("\n{}", export_str);
912-
file.contains(&addition)
913-
});
914-
915-
Ok(matching_rcfiles.map(PathUpdateMethod::RcFile).collect())
916-
}
917-
918-
919851
/// Self update downloads rustup-init to `CARGO_HOME`/bin/rustup-init
920852
/// and runs it.
921853
///
@@ -1092,7 +1024,6 @@ pub fn prepare_update() -> Result<Option<PathBuf>> {
10921024
Ok(Some(setup_path))
10931025
}
10941026

1095-
10961027
pub fn cleanup_self_updater() -> Result<()> {
10971028
let cargo_home = utils::cargo_home()?;
10981029
let setup = cargo_home.join(&format!("bin/rustup-init{}", EXE_SUFFIX));

src/cli/self_update/unix.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use rustup::utils::Notification;
44
use std::env;
55
use std::path::{Path, PathBuf};
66
use std::process::{self, Command};
7+
use types::PathUpdateMethod;
78

89
// If the user is trying to install with sudo, on some systems this will
910
// result in writing root-owned files to the user's home directory, because
@@ -79,6 +80,13 @@ pub fn do_remove_from_path(methods: &[PathUpdateMethod]) -> Result<()> {
7980
Ok(())
8081
}
8182

83+
pub fn write_env() -> Result<()> {
84+
let env_file = utils::cargo_home()?.join("env");
85+
let env_str = format!("{}\n", shell_export_string()?);
86+
utils::write_file("env", &env_file, &env_str)?;
87+
Ok(())
88+
}
89+
8290
pub fn shell_export_string() -> Result<String> {
8391
let path = format!("{}/bin", canonical_cargo_home()?);
8492
// The path is *prepended* in case there are system-installed
@@ -140,3 +148,48 @@ pub fn self_replace() -> Result<()> {
140148

141149
Ok(())
142150
}
151+
152+
/// Decide which rcfiles we're going to update, so we
153+
/// can tell the user before they confirm.
154+
pub fn get_add_path_methods() -> Vec<PathUpdateMethod> {
155+
let home_dir = utils::home_dir().unwrap();
156+
let profile = home_dir.join(".profile");
157+
let mut profiles = vec![profile];
158+
159+
if let Ok(shell) = env::var("SHELL") {
160+
if shell.contains("zsh") {
161+
let var = env::var_os("ZDOTDIR");
162+
let zdotdir = var.as_deref().map_or_else(|| home_dir.as_path(), Path::new);
163+
let zprofile = zdotdir.join(".zprofile");
164+
profiles.push(zprofile);
165+
}
166+
}
167+
168+
let bash_profile = home_dir.join(".bash_profile");
169+
// Only update .bash_profile if it exists because creating .bash_profile
170+
// will cause .profile to not be read
171+
if bash_profile.exists() {
172+
profiles.push(bash_profile);
173+
}
174+
175+
profiles.into_iter().map(PathUpdateMethod::RcFile).collect()
176+
}
177+
178+
/// Decide which rcfiles we're going to update, so we
179+
/// can tell the user before they confirm.
180+
pub fn get_remove_path_methods() -> Result<Vec<PathUpdateMethod>> {
181+
let profile = utils::home_dir().map(|p| p.join(".profile"));
182+
let bash_profile = utils::home_dir().map(|p| p.join(".bash_profile"));
183+
184+
let rcfiles = vec![profile, bash_profile];
185+
let existing_rcfiles = rcfiles.into_iter().filter_map(|f| f).filter(|f| f.exists());
186+
187+
let export_str = shell_export_string()?;
188+
let matching_rcfiles = existing_rcfiles.filter(|f| {
189+
let file = utils::read_file("rcfile", f).unwrap_or_default();
190+
let addition = format!("\n{}", export_str);
191+
file.contains(&addition)
192+
});
193+
194+
Ok(matching_rcfiles.map(PathUpdateMethod::RcFile).collect())
195+
}

src/cli/self_update/windows.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use super::*;
2-
use rustup::dist::dist::{TargetTriple};
2+
use rustup::dist::dist::TargetTriple;
33
use rustup::utils::utils;
44
use rustup::utils::Notification;
55
use std::env;
66
use std::env::consts::EXE_SUFFIX;
7-
use std::path::{Path};
7+
use std::path::Path;
88
use std::process::{self, Command};
9+
use types::PathUpdateMethod;
910

1011
// Provide guidance about setting up MSVC if it doesn't appear to be
1112
// installed
@@ -415,3 +416,15 @@ pub fn delete_rustup_and_cargo_home() -> Result<()> {
415416

416417
Ok(())
417418
}
419+
420+
/// Decide which rcfiles we're going to update, so we
421+
/// can tell the user before they confirm.
422+
pub fn get_add_path_methods() -> Vec<PathUpdateMethod> {
423+
vec![PathUpdateMethod::Windows]
424+
}
425+
426+
/// Decide which rcfiles we're going to update, so we
427+
/// can tell the user before they confirm.
428+
pub fn get_remove_path_methods() -> Result<Vec<PathUpdateMethod>> {
429+
Ok(vec![PathUpdateMethod::Windows])
430+
}

0 commit comments

Comments
 (0)