Skip to content

Commit

Permalink
Implement chruby for Fish Shell
Browse files Browse the repository at this point in the history
  • Loading branch information
JeanMertz committed Jun 4, 2013
1 parent 5884a05 commit 5869c05
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 7 deletions.
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Changes the current Ruby.
* Fuzzy matching of Rubies by name.
* Defaults to the system Ruby.
* Optionally supports auto-switching and the `.ruby-version` file.
* Supports [bash] and [zsh].
* Supports [bash], [zsh] and [fish].
* Small (~90 LOC).
* Has tests.

Expand All @@ -38,7 +38,7 @@ Changes the current Ruby.
### PGP

All releases are [PGP] signed for security. Instructions on how to import my
PGP key can be found on my [blog][1]. To verify that a release was not tampered
PGP key can be found on my [blog][1]. To verify that a release was not tampered
with:

wget https://raw.github.com/postmodern/chruby/master/pkg/chruby-0.3.5.tar.gz.asc
Expand All @@ -47,7 +47,7 @@ with:
### setup.sh

chruby also includes a `setup.sh` script, which installs chruby and the latest
releases of [Ruby], [JRuby] and [Rubinius]. Simply run the script as root or
releases of [Ruby], [JRuby] and [Rubinius]. Simply run the script as root or
via `sudo`:

sudo ./scripts/setup.sh
Expand All @@ -63,7 +63,7 @@ chruby can also be installed with [homebrew]:
chruby is already included in the [AUR]:

yaourt -S chruby

### FreeBSD

chruby is included in the official [FreeBSD ports collection]:
Expand Down Expand Up @@ -102,6 +102,8 @@ Installing to `/opt/rubies`:

## Configuration

### Bash and Zsh

Add the following to the `/etc/profile.d/chruby.sh`, `~/.bashrc` or
`~/.zshenv` file:

Expand All @@ -116,6 +118,19 @@ By default chruby will search for Rubies installed into `/opt/rubies/` or
$HOME/src/rubinius
)

### Fish shell

Add the following to the `/etc/fish/config.fish` or `~/.config/fish/config.fish`
file:

. /usr/local/share/chruby/chruby.fish

Setting the `RUBIES` variable in Fish is done in a slightly different way. Add
the following to the above mentioned `config.fish` file before you source
`chruby.fish`:

set -xU RUBIES /opt/jruby-1.7.0 $HOME/src/rubinius

### Migrating

If you are migrating from another Ruby manager, set `RUBIES` accordingly:
Expand All @@ -130,7 +145,7 @@ If you wish to enable chruby system-wide, add the following to
`/etc/profile.d/chruby.sh`:

[ -n "$BASH_VERSION" ] || [ -n "$ZSH_VERSION" ] || return

source /usr/local/share/chruby/chruby.sh

### Auto-Switching
Expand Down Expand Up @@ -241,12 +256,12 @@ Switch to an arbitrary Ruby on the fly:
## Endorsements

> yeah `chruby` is nice, does the limited thing of switching really good,
> the only hope it never grows
> the only hope it never grows
-- [Michal Papis](https://twitter.com/mpapis/status/258049391791841280) of [RVM]

> I just looooove [chruby](#readme) For the first time I'm in total control of
> all aspects of my Ruby installation.
> all aspects of my Ruby installation.
-- [Marius Mathiesen](https://twitter.com/zmalltalker/status/271192206268829696)

Expand All @@ -270,6 +285,7 @@ Switch to an arbitrary Ruby on the fly:

[bash]: http://www.gnu.org/software/bash/
[zsh]: http://www.zsh.org/
[fish]: http://fishshell.com/
[PGP]: http://en.wikipedia.org/wiki/Pretty_Good_Privacy
[homebrew]: http://mxcl.github.com/homebrew/
[AUR]: https://aur.archlinux.org/packages/chruby/
Expand Down
28 changes: 28 additions & 0 deletions share/chruby/auto.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
set -e RUBY_VERSION_FILE

function chruby_auto --on-event fish_prompt
set -l cwd (pwd)
set -l dir (pwd)
set -l version_file ''

while not test "$dir" = '/'
set version_file "$dir/.ruby-version"

if test "$version_file" = "$RUBY_VERSION_FILE"; return
else if test -f "$version_file"
chruby (cat "$version_file"); or return 1

set -gx RUBY_VERSION_FILE "$version_file"
return
end

cd $dir/..
set dir (pwd)
end
cd $cwd

if test -n "$RUBY_VERSION_FILE"
chruby_reset
set -e RUBY_VERSION_FILE
end
end
107 changes: 107 additions & 0 deletions share/chruby/chruby.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
set CHRUBY_VERSION '0.3.5'

set -eg RUBIES
test -d "$PREFIX/opt/rubies/"; and set -xg RUBIES $RUBIES $PREFIX/opt/rubies/*
test -d "$HOME/.rubies/"; and set -xg RUBIES $RUBIES $HOME/.rubies/*

function chruby_reset
test -z $RUBY_ROOT; and return

for arg in $PATH
test "$arg" = "$RUBY_ROOT/bin"; and continue

if test "$UID" != "0"
test -n "$GEM_HOME"; and test "$arg" = "$GEM_HOME/bin"; and continue
test -n "$GEM_ROOT"; and test "$arg" = "$GEM_ROOT/bin"; and continue
end

set -g NEW_PATH $arg $NEW_PATH
end

set -x PATH $NEW_PATH
set -e NEW_PATH

if test "$UID" != "0"
for arg in $GEM_PATH
test "$arg" = "$GEM_HOME"; and continue
test "$arg" = "$GEM_ROOT"; and continue
set -g NEW_GEM_PATH $arg $NEW_GEM_PATH
end

set -x GEM_PATH $NEW_GEM_PATH
set -e NEW_GEM_PATH
set -e GEM_ROOT
set -e GEM_HOME
end

set -e RUBY_ROOT
set -e RUBY_ENGINE
set -e RUBY_VERSION
set -e RUBYOPT
return 0
end

function chruby_use
echo $argv | read -l ruby_path opts

if not test -x "$ruby_path/bin/ruby"
echo "chruby: $ruby_path/bin/ruby not executable" >&2
return 1
end

test -n "$RUBY_ROOT"; and chruby_reset

set -gx RUBY_ROOT $ruby_path
set -gx RUBYOPT $opts
set -x PATH $RUBY_ROOT/bin $PATH

set -gx RUBY_ENGINE (eval "$RUBY_ROOT/bin/ruby -e 'print RUBY_ENGINE'")
set -gx RUBY_VERSION (eval "$RUBY_ROOT/bin/ruby -e 'print RUBY_VERSION'")
set -gx GEM_ROOT (eval "$RUBY_ROOT/bin/ruby -e 'print Gem.default_dir'")

if test "$UID" != "0"
set -gx GEM_HOME "$HOME/.gem/$RUBY_ENGINE/$RUBY_VERSION"
set -gx GEM_PATH $GEM_HOME $GEM_ROOT $GEM_PATH
set PATH "$GEM_HOME/bin" $PATH
set -q $GEM_ROOT; and set PATH "$GEM_ROOT/bin" $PATH
end

status -i; and echo "Using $RUBY_ENGINE-$RUBY_VERSION"
return 0
end

function chruby
echo $argv | read -l arg
if test "$arg" = ""
for dir in $RUBIES
test "$dir" = "$RUBY_ROOT"; and set star '*'; or set star ' '
set dir (basename $dir); echo " $star $dir"
end
return 0
end

switch $argv[1]
case '-h' '--help'
echo "usage: chruby [RUBY|VERSION|system] [RUBY_OPTS]"
case '-v' '--version'
echo "chruby version $CHRUBY_VERSION"
case 'system'
chruby_reset
case '*'
echo $argv | read -l ruby opts

set -l match ''

for dir in $RUBIES
set basedir (basename $dir)
test "$basedir" = "$ruby"; and set match "$dir"
end

if test -z "$match"
echo "chruby: unknown Ruby: $ruby" >&2
return 1
end

chruby_use "$match" "$opts"
end
end

0 comments on commit 5869c05

Please sign in to comment.