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

Optional auto-switching #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -12,6 +12,7 @@ Changes your `$GEM_HOME`.
* Prepends the new `$GEM_HOME/bin` to `$PATH` so it takes precedence.
* Compartmentalizes gems into `.gem/$ruby_engine/$ruby_version`.
* Plays nicely with [RVM] and [chruby].
* Optional auto-switching.
* Supports [bash] and [zsh].
* Small (~90 LOC).
* Has tests.
Expand Down Expand Up @@ -100,6 +101,19 @@ fi
This will prevent `gem_home` from accidentally being loaded by `/bin/sh`, which
is not always the same as `/bin/bash`.

### Auto-switching

This feature's workflow is similar to [chruby's `auth.sh`](https://github.com/postmodern/chruby/blob/master/share/chruby/auto.sh):

If you want to auto-switch current `$GEM_HOME` when you cd between your different projects, simply load `auto.sh` in `~/.bashrc` or `~/.zshrc`:

``` bash
source /usr/local/share/gem_home/gem_home.sh
source /usr/local/share/gem_home/auto.sh
```

gem_home will check the current and parent directories for a `.ruby-gemhome` file, which contains a path to the custom $GEM_HOME.

## Uninstall

After removing the `gem_home` configuration:
Expand Down
35 changes: 35 additions & 0 deletions share/gem_home/auto.sh
@@ -0,0 +1,35 @@
unset GEM_HOME_AUTO

if [ -z "$GEM_HOME_FILENAME" ]; then
export GEM_HOME_FILENAME=".ruby-gemhome"
fi

function gem_home_auto() {
local dir="$PWD/" gem_dir

until [[ -z "$dir" ]]; do
dir="${dir%/*}"

if { read -e gem_dir <"$dir/$GEM_HOME_FILENAME"; } 2>/dev/null || [[ -n "$gem_dir" ]]; then
if [[ "$gem_dir" == "$GEM_HOME_AUTO" ]]; then return
else
GEM_HOME_AUTO="$gem_dir"
gem_home "$gem_dir"
return $?
fi
fi
done

if [[ -n "$GEM_HOME_AUTO" ]]; then
gem_home -
unset GEM_HOME_AUTO
fi
}

if [[ -n "$ZSH_VERSION" ]]; then
if [[ ! "$preexec_functions" == *chruby_auto* ]]; then

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be

if [[ ! "$preexec_functions" == *gem_home_auto* ]]; then

preexec_functions+=("gem_home_auto")
fi
elif [[ -n "$BASH_VERSION" ]]; then
trap '[[ "$BASH_COMMAND" != "$PROMPT_COMMAND" ]] && gem_home_auto' DEBUG
fi