Skip to content

Commit ad2f81b

Browse files
authored
Support fish shell (#20)
* ✨ make the command(init) support fish * 💚 fix install script for fish * 💚 add fish completions to the release zip file * 📝 add how to install to README.md for fish user
1 parent eb2f9b0 commit ad2f81b

File tree

5 files changed

+57
-24
lines changed

5 files changed

+57
-24
lines changed

.ci/install.sh

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,18 +169,20 @@ setup_shell() {
169169
echo '# To use completion, run `compinit` after adding $fpath' >>$CONF_FILE
170170
echo '# compinit' >>$CONF_FILE
171171

172-
# elif [ "$CURRENT_SHELL" = "fish" ]; then
173-
# CONF_FILE=$HOME/.config/fish/conf.d/phpup.fish
174-
# ensure_containing_dir_exists "$CONF_FILE"
175-
# echo "Installing for Fish. Appending the following to $CONF_FILE:"
176-
# echo ""
177-
# echo ' # PHP-UP'
178-
# echo ' set PATH '"$INSTALL_DIR"' $PATH'
179-
# echo ' phpup init --auto --recursive | source'
180-
181-
# echo '# PHP-UP' >>$CONF_FILE
182-
# echo 'set PATH '"$INSTALL_DIR"' $PATH' >>$CONF_FILE
183-
# echo 'phpup init --auto --recursive | source' >>$CONF_FILE
172+
elif [ "$CURRENT_SHELL" = "fish" ]; then
173+
CONF_FILE=$HOME/.config/fish/conf.d/phpup.fish
174+
ensure_containing_dir_exists "$CONF_FILE"
175+
echo "Installing for Fish. Appending the following to $CONF_FILE:"
176+
echo ""
177+
echo ' # PHP-UP'
178+
echo ' set PATH '$INSTALL_DIR/bin' $PATH'
179+
echo ' phpup init --auto --recursive | source'
180+
echo ' set -gx fish_complete_path '$INSTALL_DIR/completions/fish' $fish_complete_path'
181+
182+
echo '# PHP-UP' >>$CONF_FILE
183+
echo 'set PATH '$INSTALL_DIR/bin' $PATH' >>$CONF_FILE
184+
echo 'phpup init --auto --recursive | source' >>$CONF_FILE
185+
echo 'set -gx fish_complete_path '$INSTALL_DIR/completions/fish' $fish_complete_path' >>$CONF_FILE
184186

185187
elif [ "$CURRENT_SHELL" = "bash" ]; then
186188
if [ "$OS" = "Darwin" ]; then

.github/workflows/release.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,10 @@ jobs:
138138
- name: Generate completion files (x86_64)
139139
if: matrix.arch == 'x86_64'
140140
run: |
141-
mkdir -p completions/{bash,zsh}
141+
mkdir -p completions/{bash,zsh,fish}
142142
./target/${{ matrix.target }}/release/phpup completions --shell bash > completions/bash/_phpup
143143
./target/${{ matrix.target }}/release/phpup completions --shell zsh > completions/zsh/_phpup
144+
./target/${{ matrix.target }}/release/phpup completions --shell fish > completions/fish/phpup.fish
144145
145146
- name: Generate completion files (arm)
146147
if: matrix.arch == 'armv7' || matrix.arch == 'aarch64'
@@ -153,9 +154,10 @@ jobs:
153154
-v $PWD/target:/target:Z \
154155
-v $PWD/completions:/completions:Z
155156
run: |
156-
mkdir -p completions/{bash,zsh}
157+
mkdir -p completions/{bash,zsh,fish}
157158
./target/${{ matrix.target }}/release/phpup completions --shell bash > completions/bash/_phpup
158159
./target/${{ matrix.target }}/release/phpup completions --shell zsh > completions/zsh/_phpup
160+
./target/${{ matrix.target }}/release/phpup completions --shell fish > completions/fish/phpup.fish
159161
160162
- name: Build archive
161163
shell: bash

README.md

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
## Quick Start (Linux, macOS)
1616

17-
For bash, zsh, there's a [installation script](./.ci/install.sh)
17+
For `bash`, `zsh` and `fish` there's a [installation script](./.ci/install.sh)
1818

1919
```sh
2020
curl -fsSL https://phpup.vercel.app/install | bash
@@ -25,7 +25,7 @@ curl -fsSL https://phpup.vercel.app/install | bash
2525
To prevent duplication in your shell config file, add `--skip-shell` option to install command.
2626

2727
```sh
28-
curl -fsSL https://phpup.vercel.app/install | bash -s -- --skip-shell
28+
curl -fsSL https://phpup.vercel.app/install | bash -s -- --skip-shell
2929
```
3030

3131
#### Uninstall
@@ -38,8 +38,8 @@ You should also edit your shell configuration to remove any references to phpup.
3838
### Requirements
3939

4040
- OS: Linux, macOS, Windows[WIP]
41-
- shell: bash, zsh, fish[WIP], powershell[WIP]
42-
- `curl`/`ps` installation
41+
- shell: `bash`, `zsh`, `fish` or `powershell`[WIP]
42+
- `curl`, `ps` and `make` installation
4343

4444
### Installation
4545

@@ -56,11 +56,20 @@ cargo install phpup
5656

5757
### Shell setup
5858

59+
#### Bash, Zsh
60+
5961
Add the following to your `.bashrc` or `.zshrc`
6062

61-
```bash
63+
```sh
6264
eval "$(phpup init --auto --recursive)"
6365
```
66+
#### Fish
67+
68+
Create `~/.config/fish/conf.d/phpup.fish` and add the following to it
69+
70+
```fish
71+
phpup init --auto --recursive | source
72+
```
6473

6574
- To automatically run `phpup use` when a directory contains a `.php-version` file, add the `--auto` (long: `--auto-switch`) option.
6675
- To search recursively for a `.php-version` file in a parent directory when running `phpup use` automatically, add the `--recursive` (long: `--recursive-version-file`) option.

TODO.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
## Cross-platform support
3535

3636
- [x] bash
37-
- [ ] fish
37+
- [x] fish
3838
- [ ] windows
3939

4040
## Test

src/shell.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,21 @@ use thiserror::Error;
99
pub enum Shell {
1010
Bash,
1111
Zsh,
12+
Fish,
1213
}
1314

1415
pub const fn available_shells() -> &'static [&'static str] {
15-
&["bash", "zsh"]
16+
&["bash", "zsh", "fish"]
1617
}
1718

1819
#[derive(Debug, Error)]
1920
pub enum ShellDetectError {
2021
#[error("parent process tracing count reached the limit: {MAX_SEARCH_ITERATIONS}")]
2122
TooManyTracing,
23+
2224
#[error("reached first process PID=0 when tracing processes")]
2325
ReachedFirstProcess,
26+
2427
#[error(transparent)]
2528
FailedGetProcessInfo(#[from] ProcessInfoError),
2629
}
@@ -60,13 +63,15 @@ impl Shell {
6063
Bash | Zsh => {
6164
format!("export PATH={}:$PATH", path.as_ref().display())
6265
}
66+
Fish => format!("set -gx PATH {} $PATH;", path.as_ref().display()),
6367
}
6468
}
6569
pub fn set_env(&self, name: impl Display, value: impl Display) -> String {
6670
match &self {
6771
Bash | Zsh => {
6872
format!("export {}={}", name, value)
6973
}
74+
Fish => format!("set -gx {} {};", name, value),
7075
}
7176
}
7277
pub fn auto_switch_hook(&self, version_file: &version::File) -> String {
@@ -83,7 +88,8 @@ impl Shell {
8388

8489
match &self {
8590
Bash => {
86-
formatdoc! {r#"
91+
formatdoc! {
92+
r#"
8793
__phpup_use() {{
8894
{phpup_use}
8995
}}
@@ -97,7 +103,8 @@ impl Shell {
97103
}
98104
}
99105
Zsh => {
100-
formatdoc! {r#"
106+
formatdoc! {
107+
r#"
101108
autoload -U add-zsh-hook
102109
_phpup_autoload_hook () {{
103110
{phpup_use}
@@ -107,18 +114,30 @@ impl Shell {
107114
phpup_use = phpup_use
108115
}
109116
}
117+
Fish => {
118+
formatdoc!(
119+
r#"
120+
function _phpup_autoload_hook --on-variable PWD --description 'Change PHP version on directory change'
121+
status --is-command-substitution; and return
122+
{phpup_use}
123+
end
124+
_phpup_autoload_hook"#,
125+
phpup_use = phpup_use
126+
)
127+
}
110128
}
111129
}
112130
pub fn rehash(&self) -> Option<String> {
113131
match &self {
114-
Bash => None,
132+
Bash | Fish => None,
115133
Zsh => Some("rehash".to_string()),
116134
}
117135
}
118136
pub fn to_clap_shell(&self) -> clap_complete::Shell {
119137
match &self {
120138
Bash => clap_complete::Shell::Bash,
121139
Zsh => clap_complete::Shell::Zsh,
140+
Fish => clap_complete::Shell::Fish,
122141
}
123142
}
124143
}
@@ -135,6 +154,7 @@ impl FromStr for Shell {
135154
match s {
136155
"bash" | "dash" => Ok(Bash),
137156
"zsh" => Ok(Zsh),
157+
"fish" => Ok(Fish),
138158
_ => Err(ParseShellError::UnknownShell(s.to_owned())),
139159
}
140160
}

0 commit comments

Comments
 (0)