Skip to content

Commit a35a9bc

Browse files
author
Ryan Kopf
committed
Update to show the message relevant to your system
1 parent 5100b80 commit a35a9bc

2 files changed

Lines changed: 74 additions & 30 deletions

File tree

src/cli/self_update.rs

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -445,36 +445,20 @@ but will not be added automatically."
445445
};
446446
}
447447

448-
#[cfg(not(windows))]
449-
macro_rules! post_install_msg_unix_source_env {
450-
() => {
451-
r#"To configure your current shell, you need to source
452-
the corresponding `env` file under {cargo_home}.
453-
454-
This is usually done by running one of the following (note the leading DOT):
455-
. "{cargo_home}/env" # For sh/bash/zsh/ash/dash/pdksh
456-
source "{cargo_home}/env.fish" # For fish
457-
source "{cargo_home_nushell}/env.nu" # For nushell
458-
source "{cargo_home}/env.tcsh" # For tcsh
459-
. "{cargo_home}/env.ps1" # For pwsh
460-
source "{cargo_home}/env.xsh" # For xonsh
461-
"#
462-
};
463-
}
464-
465448
#[cfg(not(windows))]
466449
macro_rules! post_install_msg_unix {
467450
() => {
468-
concat!(
469-
r"# Rust is installed now. Great!
451+
r"# Rust is installed now. Great!
470452
471453
To get started you may need to restart your current shell.
472454
This would reload your `PATH` environment variable to include
473455
Cargo's bin directory ({cargo_home}/bin).
474456
475-
",
476-
post_install_msg_unix_source_env!(),
477-
)
457+
To configure your current shell, you need to source
458+
the corresponding `env` file under {cargo_home}.
459+
460+
This is usually done by running one of the following (note the leading DOT):
461+
{source_env_lines}"
478462
};
479463
}
480464

@@ -494,15 +478,16 @@ Cargo's bin directory ({cargo_home}\\bin).
494478
#[cfg(not(windows))]
495479
macro_rules! post_install_msg_unix_no_modify_path {
496480
() => {
497-
concat!(
498-
r"# Rust is installed now. Great!
481+
r"# Rust is installed now. Great!
499482
500483
To get started you need Cargo's bin directory ({cargo_home}/bin) in your `PATH`
501484
environment variable. This has not been done automatically.
502485
503-
",
504-
post_install_msg_unix_source_env!(),
505-
)
486+
To configure your current shell, you need to source
487+
the corresponding `env` file under {cargo_home}.
488+
489+
This is usually done by running one of the following (note the leading DOT):
490+
{source_env_lines}"
506491
};
507492
}
508493

@@ -661,19 +646,19 @@ pub(crate) async fn install(
661646
format!(post_install_msg_win!(), cargo_home = cargo_home)
662647
};
663648
#[cfg(not(windows))]
664-
let cargo_home_nushell = Nu.cargo_home_str(cfg.process)?;
649+
let source_env_lines = shell::build_source_env_lines(cfg.process);
665650
#[cfg(not(windows))]
666651
let msg = if no_modify_path {
667652
format!(
668653
post_install_msg_unix_no_modify_path!(),
669654
cargo_home = cargo_home,
670-
cargo_home_nushell = cargo_home_nushell,
655+
source_env_lines = source_env_lines,
671656
)
672657
} else {
673658
format!(
674659
post_install_msg_unix!(),
675660
cargo_home = cargo_home,
676-
cargo_home_nushell = cargo_home_nushell,
661+
source_env_lines = source_env_lines,
677662
)
678663
};
679664
md(&mut term, msg);

src/cli/self_update/shell.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,30 @@ fn enumerate_shells() -> Vec<Shell> {
7373
]
7474
}
7575

76+
/// Builds the shell source lines for the post-install message, showing only
77+
/// shells that are available on the current system. Shells sharing the same
78+
/// env file are grouped onto one line (e.g. sh/bash/zsh all use `env`).
79+
pub(crate) fn build_source_env_lines(process: &Process) -> String {
80+
let mut groups = Vec::<(String, Vec<&'static str>)>::new();
81+
for shell in get_available_shells(process) {
82+
let Ok(src) = shell.source_string(process) else {
83+
continue;
84+
};
85+
if let Some(names) = groups
86+
.iter_mut()
87+
.find_map(|(s, names)| (*s == src).then_some(names))
88+
{
89+
names.push(shell.name());
90+
} else {
91+
groups.push((src, vec![shell.name()]));
92+
}
93+
}
94+
groups
95+
.into_iter()
96+
.map(|(src, names)| format!(" {} # For {}\n", src, names.join("/")))
97+
.collect()
98+
}
99+
76100
pub(crate) fn get_available_shells(process: &Process) -> impl Iterator<Item = Shell> + '_ {
77101
enumerate_shells()
78102
.into_iter()
@@ -84,6 +108,9 @@ pub(crate) trait UnixShell {
84108
// heuristic should be used, assuming shells exist if any traces do.
85109
fn does_exist(&self, process: &Process) -> bool;
86110

111+
// Returns the display name of the shell, used in post-install messages.
112+
fn name(&self) -> &'static str;
113+
87114
// Gives all rcfiles of a given shell that Rustup is concerned with.
88115
// Used primarily in checking rcfiles for cleanup.
89116
fn rcfiles(&self, process: &Process) -> Vec<PathBuf>;
@@ -128,6 +155,10 @@ impl UnixShell for Posix {
128155
true
129156
}
130157

158+
fn name(&self) -> &'static str {
159+
"sh/ash/dash/pdksh"
160+
}
161+
131162
fn rcfiles(&self, process: &Process) -> Vec<PathBuf> {
132163
match process.home_dir() {
133164
Some(dir) => vec![dir.join(".profile")],
@@ -149,6 +180,10 @@ impl UnixShell for Bash {
149180
!self.update_rcs(process).is_empty()
150181
}
151182

183+
fn name(&self) -> &'static str {
184+
"bash"
185+
}
186+
152187
fn rcfiles(&self, process: &Process) -> Vec<PathBuf> {
153188
// Bash also may read .profile, however Rustup already includes handling
154189
// .profile as part of POSIX and always does setup for POSIX shells.
@@ -197,6 +232,10 @@ impl UnixShell for Zsh {
197232
|| utils::find_cmd(&["zsh"], process).is_some()
198233
}
199234

235+
fn name(&self) -> &'static str {
236+
"zsh"
237+
}
238+
200239
fn rcfiles(&self, process: &Process) -> Vec<PathBuf> {
201240
[Zsh::zdotdir(process).ok(), process.home_dir()]
202241
.iter()
@@ -229,6 +268,10 @@ impl UnixShell for Fish {
229268
|| utils::find_cmd(&["fish"], process).is_some()
230269
}
231270

271+
fn name(&self) -> &'static str {
272+
"fish"
273+
}
274+
232275
// > "$XDG_CONFIG_HOME/fish/conf.d" (or "~/.config/fish/conf.d" if that variable is unset) for the user
233276
// from <https://github.com/fish-shell/fish-shell/issues/3170#issuecomment-228311857>
234277
fn rcfiles(&self, process: &Process) -> Vec<PathBuf> {
@@ -278,6 +321,10 @@ impl UnixShell for Nu {
278321
|| utils::find_cmd(&["nu"], process).is_some()
279322
}
280323

324+
fn name(&self) -> &'static str {
325+
"nushell"
326+
}
327+
281328
fn rcfiles(&self, process: &Process) -> Vec<PathBuf> {
282329
let mut paths = vec![];
283330

@@ -329,6 +376,10 @@ impl UnixShell for Tcsh {
329376
|| utils::find_cmd(&["tcsh"], process).is_some()
330377
}
331378

379+
fn name(&self) -> &'static str {
380+
"tcsh"
381+
}
382+
332383
fn rcfiles(&self, process: &Process) -> Vec<PathBuf> {
333384
let mut paths = vec![];
334385

@@ -378,6 +429,10 @@ impl UnixShell for Pwsh {
378429
|| utils::find_cmd(&["pwsh"], process).is_some()
379430
}
380431

432+
fn name(&self) -> &'static str {
433+
"pwsh"
434+
}
435+
381436
fn rcfiles(&self, process: &Process) -> Vec<PathBuf> {
382437
let mut paths = vec![];
383438

@@ -453,6 +508,10 @@ impl UnixShell for Xonsh {
453508
process.var("XONSHRC").is_ok() || utils::find_cmd(&["xonsh"], process).is_some()
454509
}
455510

511+
fn name(&self) -> &'static str {
512+
"xonsh"
513+
}
514+
456515
fn rcfiles(&self, process: &Process) -> Vec<PathBuf> {
457516
let mut paths = vec![];
458517

0 commit comments

Comments
 (0)