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

Support fish shell #20

Merged
merged 6 commits into from
Mar 27, 2022
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
26 changes: 14 additions & 12 deletions .ci/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,20 @@ setup_shell() {
echo '# To use completion, run `compinit` after adding $fpath' >>$CONF_FILE
echo '# compinit' >>$CONF_FILE

# elif [ "$CURRENT_SHELL" = "fish" ]; then
# CONF_FILE=$HOME/.config/fish/conf.d/phpup.fish
# ensure_containing_dir_exists "$CONF_FILE"
# echo "Installing for Fish. Appending the following to $CONF_FILE:"
# echo ""
# echo ' # PHP-UP'
# echo ' set PATH '"$INSTALL_DIR"' $PATH'
# echo ' phpup init --auto --recursive | source'

# echo '# PHP-UP' >>$CONF_FILE
# echo 'set PATH '"$INSTALL_DIR"' $PATH' >>$CONF_FILE
# echo 'phpup init --auto --recursive | source' >>$CONF_FILE
elif [ "$CURRENT_SHELL" = "fish" ]; then
CONF_FILE=$HOME/.config/fish/conf.d/phpup.fish
ensure_containing_dir_exists "$CONF_FILE"
echo "Installing for Fish. Appending the following to $CONF_FILE:"
echo ""
echo ' # PHP-UP'
echo ' set PATH '$INSTALL_DIR/bin' $PATH'
echo ' phpup init --auto --recursive | source'
echo ' set -gx fish_complete_path '$INSTALL_DIR/completions/fish' $fish_complete_path'

echo '# PHP-UP' >>$CONF_FILE
echo 'set PATH '$INSTALL_DIR/bin' $PATH' >>$CONF_FILE
echo 'phpup init --auto --recursive | source' >>$CONF_FILE
echo 'set -gx fish_complete_path '$INSTALL_DIR/completions/fish' $fish_complete_path' >>$CONF_FILE

elif [ "$CURRENT_SHELL" = "bash" ]; then
if [ "$OS" = "Darwin" ]; then
Expand Down
6 changes: 4 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ jobs:
- name: Generate completion files (x86_64)
if: matrix.arch == 'x86_64'
run: |
mkdir -p completions/{bash,zsh}
mkdir -p completions/{bash,zsh,fish}
./target/${{ matrix.target }}/release/phpup completions --shell bash > completions/bash/_phpup
./target/${{ matrix.target }}/release/phpup completions --shell zsh > completions/zsh/_phpup
./target/${{ matrix.target }}/release/phpup completions --shell fish > completions/fish/phpup.fish

- name: Generate completion files (arm)
if: matrix.arch == 'armv7' || matrix.arch == 'aarch64'
Expand All @@ -153,9 +154,10 @@ jobs:
-v $PWD/target:/target:Z \
-v $PWD/completions:/completions:Z
run: |
mkdir -p completions/{bash,zsh}
mkdir -p completions/{bash,zsh,fish}
./target/${{ matrix.target }}/release/phpup completions --shell bash > completions/bash/_phpup
./target/${{ matrix.target }}/release/phpup completions --shell zsh > completions/zsh/_phpup
./target/${{ matrix.target }}/release/phpup completions --shell fish > completions/fish/phpup.fish

- name: Build archive
shell: bash
Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

## Quick Start (Linux, macOS)

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

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

```sh
curl -fsSL https://phpup.vercel.app/install | bash -s -- --skip-shell
curl -fsSL https://phpup.vercel.app/install | bash -s -- --skip-shell
```

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

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

### Installation

Expand All @@ -56,11 +56,20 @@ cargo install phpup

### Shell setup

#### Bash, Zsh

Add the following to your `.bashrc` or `.zshrc`

```bash
```sh
eval "$(phpup init --auto --recursive)"
```
#### Fish

Create `~/.config/fish/conf.d/phpup.fish` and add the following to it

```fish
phpup init --auto --recursive | source
```

- To automatically run `phpup use` when a directory contains a `.php-version` file, add the `--auto` (long: `--auto-switch`) option.
- 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.
Expand Down
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
## Cross-platform support

- [x] bash
- [ ] fish
- [x] fish
- [ ] windows

## Test
Expand Down
28 changes: 24 additions & 4 deletions src/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,21 @@ use thiserror::Error;
pub enum Shell {
Bash,
Zsh,
Fish,
}

pub const fn available_shells() -> &'static [&'static str] {
&["bash", "zsh"]
&["bash", "zsh", "fish"]
}

#[derive(Debug, Error)]
pub enum ShellDetectError {
#[error("parent process tracing count reached the limit: {MAX_SEARCH_ITERATIONS}")]
TooManyTracing,

#[error("reached first process PID=0 when tracing processes")]
ReachedFirstProcess,

#[error(transparent)]
FailedGetProcessInfo(#[from] ProcessInfoError),
}
Expand Down Expand Up @@ -60,13 +63,15 @@ impl Shell {
Bash | Zsh => {
format!("export PATH={}:$PATH", path.as_ref().display())
}
Fish => format!("set -gx PATH {} $PATH;", path.as_ref().display()),
}
}
pub fn set_env(&self, name: impl Display, value: impl Display) -> String {
match &self {
Bash | Zsh => {
format!("export {}={}", name, value)
}
Fish => format!("set -gx {} {};", name, value),
}
}
pub fn auto_switch_hook(&self, version_file: &version::File) -> String {
Expand All @@ -83,7 +88,8 @@ impl Shell {

match &self {
Bash => {
formatdoc! {r#"
formatdoc! {
r#"
__phpup_use() {{
{phpup_use}
}}
Expand All @@ -97,7 +103,8 @@ impl Shell {
}
}
Zsh => {
formatdoc! {r#"
formatdoc! {
r#"
autoload -U add-zsh-hook
_phpup_autoload_hook () {{
{phpup_use}
Expand All @@ -107,18 +114,30 @@ impl Shell {
phpup_use = phpup_use
}
}
Fish => {
formatdoc!(
r#"
function _phpup_autoload_hook --on-variable PWD --description 'Change PHP version on directory change'
status --is-command-substitution; and return
{phpup_use}
end
_phpup_autoload_hook"#,
phpup_use = phpup_use
)
}
}
}
pub fn rehash(&self) -> Option<String> {
match &self {
Bash => None,
Bash | Fish => None,
Zsh => Some("rehash".to_string()),
}
}
pub fn to_clap_shell(&self) -> clap_complete::Shell {
match &self {
Bash => clap_complete::Shell::Bash,
Zsh => clap_complete::Shell::Zsh,
Fish => clap_complete::Shell::Fish,
}
}
}
Expand All @@ -135,6 +154,7 @@ impl FromStr for Shell {
match s {
"bash" | "dash" => Ok(Bash),
"zsh" => Ok(Zsh),
"fish" => Ok(Fish),
_ => Err(ParseShellError::UnknownShell(s.to_owned())),
}
}
Expand Down