|
| 1 | +//! Paths and Unix shells |
| 2 | +//! |
| 3 | +//! MacOS, Linux, FreeBSD, and many other OS model their design on Unix, |
| 4 | +//! so handling them is relatively consistent. But only relatively. |
| 5 | +//! POSIX postdates Unix by 20 years, and each "Unix-like" shell develops |
| 6 | +//! unique quirks over time. |
| 7 | +//! |
| 8 | +//! |
| 9 | +//! Windowing Managers, Desktop Environments, GUI Terminals, and PATHs |
| 10 | +//! |
| 11 | +//! Duplicating paths in PATH can cause performance issues when the OS searches |
| 12 | +//! the same place multiple times. Traditionally, Unix configurations have |
| 13 | +//! resolved this by setting up PATHs in the shell's login profile. |
| 14 | +//! |
| 15 | +//! This has its own issues. Login profiles are only intended to run once, but |
| 16 | +//! changing the PATH is common enough that people may run it twice. Desktop |
| 17 | +//! environments often choose to NOT start login shells in GUI terminals. Thus, |
| 18 | +//! a trend has emerged to place PATH updates in other run-commands (rc) files, |
| 19 | +//! leaving Rustup with few assumptions to build on for fulfilling its promise |
| 20 | +//! to set up PATH appropriately. |
| 21 | +//! |
| 22 | +//! Rustup addresses this by: |
| 23 | +//! 1) using a shell script that updates PATH if the path is not in PATH |
| 24 | +//! 2) sourcing this script in any known and appropriate rc file |
| 25 | +
|
| 26 | +use std::path::PathBuf; |
| 27 | + |
| 28 | +use error_chain::bail; |
| 29 | + |
| 30 | +use super::*; |
| 31 | +use crate::process; |
| 32 | + |
| 33 | +pub type Shell = Box<dyn UnixShell>; |
| 34 | + |
| 35 | +#[derive(Debug, PartialEq)] |
| 36 | +pub struct ShellScript { |
| 37 | + content: &'static str, |
| 38 | + name: &'static str, |
| 39 | +} |
| 40 | + |
| 41 | +impl ShellScript { |
| 42 | + pub fn write(&self) -> Result<()> { |
| 43 | + let home = utils::cargo_home()?; |
| 44 | + let cargo_bin = format!("{}/bin", cargo_home_str()?); |
| 45 | + let env_name = home.join(self.name); |
| 46 | + let env_file = self.content.replace("{cargo_bin}", &cargo_bin); |
| 47 | + utils::write_file(self.name, &env_name, &env_file)?; |
| 48 | + Ok(()) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +// TODO: Update into a bytestring. |
| 53 | +pub fn cargo_home_str() -> Result<Cow<'static, str>> { |
| 54 | + let path = utils::cargo_home()?; |
| 55 | + |
| 56 | + let default_cargo_home = utils::home_dir() |
| 57 | + .unwrap_or_else(|| PathBuf::from(".")) |
| 58 | + .join(".cargo"); |
| 59 | + Ok(if default_cargo_home == path { |
| 60 | + "$HOME/.cargo".into() |
| 61 | + } else { |
| 62 | + match path.to_str() { |
| 63 | + Some(p) => p.to_owned().into(), |
| 64 | + None => bail!("Non-Unicode path!"), |
| 65 | + } |
| 66 | + }) |
| 67 | +} |
| 68 | + |
| 69 | +// TODO: Tcsh (BSD) |
| 70 | +// TODO?: Make a decision on Ion Shell, Power Shell, Nushell |
| 71 | +// Cross-platform non-POSIX shells have not been assessed for integration yet |
| 72 | +fn enumerate_shells() -> Vec<Shell> { |
| 73 | + vec![Box::new(Posix), Box::new(Bash), Box::new(Zsh)] |
| 74 | +} |
| 75 | + |
| 76 | +pub fn get_available_shells() -> impl Iterator<Item = Shell> { |
| 77 | + enumerate_shells().into_iter().filter(|sh| sh.does_exist()) |
| 78 | +} |
| 79 | + |
| 80 | +pub trait UnixShell { |
| 81 | + // Detects if a shell "exists". Users have multiple shells, so an "eager" |
| 82 | + // heuristic should be used, assuming shells exist if any traces do. |
| 83 | + fn does_exist(&self) -> bool; |
| 84 | + |
| 85 | + // Gives all rcfiles of a given shell that rustup is concerned with. |
| 86 | + // Used primarily in checking rcfiles for cleanup. |
| 87 | + fn rcfiles(&self) -> Vec<PathBuf>; |
| 88 | + |
| 89 | + // Gives rcs that should be written to. |
| 90 | + fn update_rcs(&self) -> Vec<PathBuf>; |
| 91 | + |
| 92 | + // Writes the relevant env file. |
| 93 | + fn env_script(&self) -> ShellScript { |
| 94 | + ShellScript { |
| 95 | + name: "env", |
| 96 | + content: include_str!("env.sh"), |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + fn source_string(&self) -> Result<String> { |
| 101 | + Ok(format!(r#"source "{}/env""#, cargo_home_str()?)) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +struct Posix; |
| 106 | +impl UnixShell for Posix { |
| 107 | + fn does_exist(&self) -> bool { |
| 108 | + true |
| 109 | + } |
| 110 | + |
| 111 | + fn rcfiles(&self) -> Vec<PathBuf> { |
| 112 | + match utils::home_dir() { |
| 113 | + Some(dir) => vec![dir.join(".profile")], |
| 114 | + _ => vec![], |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + fn update_rcs(&self) -> Vec<PathBuf> { |
| 119 | + // Write to .profile even if it doesn't exist. It's the only rc in the |
| 120 | + // POSIX spec so it should always be set up. |
| 121 | + self.rcfiles() |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +struct Bash; |
| 126 | + |
| 127 | +impl UnixShell for Bash { |
| 128 | + fn does_exist(&self) -> bool { |
| 129 | + self.update_rcs().len() > 0 |
| 130 | + } |
| 131 | + |
| 132 | + fn rcfiles(&self) -> Vec<PathBuf> { |
| 133 | + // Bash also may read .profile, however Rustup already includes handling |
| 134 | + // .profile as part of POSIX and always does setup for POSIX shells. |
| 135 | + [".bash_profile", ".bash_login", ".bashrc"] |
| 136 | + .iter() |
| 137 | + .filter_map(|rc| utils::home_dir().map(|dir| dir.join(rc))) |
| 138 | + .collect() |
| 139 | + } |
| 140 | + |
| 141 | + fn update_rcs(&self) -> Vec<PathBuf> { |
| 142 | + self.rcfiles() |
| 143 | + .into_iter() |
| 144 | + .filter(|rc| rc.is_file()) |
| 145 | + .collect() |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +struct Zsh; |
| 150 | + |
| 151 | +impl Zsh { |
| 152 | + fn zdotdir() -> Result<PathBuf> { |
| 153 | + use std::ffi::OsStr; |
| 154 | + use std::os::unix::ffi::OsStrExt; |
| 155 | + |
| 156 | + if matches!(process().var("SHELL"), Ok(sh) if sh.contains("zsh")) { |
| 157 | + match process().var("ZDOTDIR") { |
| 158 | + Ok(dir) if dir.len() > 0 => Ok(PathBuf::from(dir)), |
| 159 | + _ => bail!("Zsh setup failed."), |
| 160 | + } |
| 161 | + } else { |
| 162 | + match std::process::Command::new("zsh") |
| 163 | + .args(&["-c", "'echo $ZDOTDIR'"]) |
| 164 | + .output() |
| 165 | + { |
| 166 | + Ok(io) if io.stdout.len() > 0 => Ok(PathBuf::from(OsStr::from_bytes(&io.stdout))), |
| 167 | + _ => bail!("Zsh setup failed."), |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | +} |
| 172 | + |
| 173 | +impl UnixShell for Zsh { |
| 174 | + fn does_exist(&self) -> bool { |
| 175 | + // zsh has to either be the shell or be callable for zsh setup. |
| 176 | + matches!(process().var("SHELL"), Ok(sh) if sh.contains("zsh")) |
| 177 | + || matches!(utils::find_cmd(&["zsh"]), Some(_)) |
| 178 | + } |
| 179 | + |
| 180 | + fn rcfiles(&self) -> Vec<PathBuf> { |
| 181 | + [Zsh::zdotdir().ok(), utils::home_dir()] |
| 182 | + .iter() |
| 183 | + .filter_map(|dir| dir.as_ref().map(|p| p.join(".zshenv"))) |
| 184 | + .collect() |
| 185 | + } |
| 186 | + |
| 187 | + fn update_rcs(&self) -> Vec<PathBuf> { |
| 188 | + // zsh can change $ZDOTDIR both _before_ AND _during_ reading .zshenv, |
| 189 | + // so we: write to $ZDOTDIR/.zshenv if-exists ($ZDOTDIR changes before) |
| 190 | + // OR write to $HOME/.zshenv if it exists (change-during) |
| 191 | + // if neither exist, we create it ourselves, but using the same logic, |
| 192 | + // because we must still respond to whether $ZDOTDIR is set or unset. |
| 193 | + // In any case we only write once. |
| 194 | + self.rcfiles() |
| 195 | + .into_iter() |
| 196 | + .filter(|env| env.is_file()) |
| 197 | + .chain(self.rcfiles().into_iter()) |
| 198 | + .take(1) |
| 199 | + .collect() |
| 200 | + } |
| 201 | +} |
| 202 | + |
| 203 | +pub fn legacy_paths() -> impl Iterator<Item = PathBuf> { |
| 204 | + let zprofiles = Zsh::zdotdir() |
| 205 | + .into_iter() |
| 206 | + .chain(utils::home_dir()) |
| 207 | + .map(|d| d.join(".zprofile")); |
| 208 | + let profiles = [".bash_profile", ".profile"] |
| 209 | + .iter() |
| 210 | + .filter_map(|rc| utils::home_dir().map(|d| d.join(rc))); |
| 211 | + |
| 212 | + profiles.chain(zprofiles) |
| 213 | +} |
0 commit comments