Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for fish shell #3108

Merged
merged 4 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/cli/self_update/env.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# rustup shell setup
if not contains "{cargo_bin}" $PATH
# Prepending path in case a system-installed rustc needs to be overridden
set -x PATH "{cargo_bin}" $PATH
end
49 changes: 48 additions & 1 deletion src/cli/self_update/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,12 @@ pub(crate) fn cargo_home_str() -> Result<Cow<'static, str>> {
// TODO?: Make a decision on Ion Shell, Power Shell, Nushell
// Cross-platform non-POSIX shells have not been assessed for integration yet
fn enumerate_shells() -> Vec<Shell> {
vec![Box::new(Posix), Box::new(Bash), Box::new(Zsh)]
vec![
Box::new(Posix),
Box::new(Bash),
Box::new(Zsh),
Box::new(Fish),
]
}

pub(crate) fn get_available_shells() -> impl Iterator<Item = Shell> {
Expand Down Expand Up @@ -201,6 +206,48 @@ impl UnixShell for Zsh {
}
}

struct Fish;

impl UnixShell for Fish {
fn does_exist(&self) -> bool {
// fish has to either be the shell or be callable for fish setup.
matches!(process().var("SHELL"), Ok(sh) if sh.contains("fish"))
|| matches!(utils::find_cmd(&["fish"]), Some(_))
}

// > "$XDG_CONFIG_HOME/fish/conf.d" (or "~/.config/fish/conf.d" if that variable is unset) for the user
// from <https://github.com/fish-shell/fish-shell/issues/3170#issuecomment-228311857>
fn rcfiles(&self) -> Vec<PathBuf> {
let p0 = process().var("XDG_CONFIG_HOME").ok().map(|p| {
let mut path = PathBuf::from(p);
path.push("fish/conf.d/rustup.fish");
path
});

let p1 = utils::home_dir().map(|mut path| {
path.push(".config/fish/conf.d/rustup.fish");
path
});

p0.into_iter().chain(p1).collect()
}

fn update_rcs(&self) -> Vec<PathBuf> {
self.rcfiles()
}

fn env_script(&self) -> ShellScript {
ShellScript {
name: "env.fish",
content: include_str!("env.fish"),
}
}

fn source_string(&self) -> Result<String> {
Ok(format!(r#". "{}/env.fish""#, cargo_home_str()?))
}
}

pub(crate) fn legacy_paths() -> impl Iterator<Item = PathBuf> {
let zprofiles = Zsh::zdotdir()
.into_iter()
Expand Down