Skip to content

Instantly share code, notes, and snippets.

@nyarly
Created February 12, 2013 23:42
Show Gist options
  • Select an option

  • Save nyarly/4774570 to your computer and use it in GitHub Desktop.

Select an option

Save nyarly/4774570 to your computer and use it in GitHub Desktop.
export PROMPT_DIRTRIM=2
if [[ ! $PROMPT_COMMAND =~ __bundler_prompt_command ]]; then
export PROMPT_COMMAND="${PROMPT_COMMAND:-:} ; __bundler_prompt_command"
fi
# Put your fun stuff here.
PS1='\[\033[01;31m\]$(__bundler_ps1)\[\033[00;34m\]\[\033[32m\]\u@\h\[\033[34m\] \w\[\033[01;32m\]$(__git_ps1) \[\033[34m\]\$\[\033[00m\] '
#!/bin/sh
BUNDLE_BASE=~/ruby/bundle-paths/rails3
if [ -f .bundle/config ]; then
echo "Bundle config file already exists"
bundle install
else
projname=$(basename $(pwd))
BUNDLE_PATH="$BUNDLE_BASE/lib"
BINSTUBS=".bundle/bin"
echo "Setting up bundle for $projname"
bundle install --path=$BUNDLE_PATH --binstubs=$BINSTUBS
fi
export bundle_dir=""
export bundle_bin=""
export orig_path=$PATH
function find_bundle_dir() {
bundle_dir=$(pwd)
while [ "$bundle_dir" != "/" ]; do
if [ -e "$bundle_dir/.bundle/config" ]; then
return 0
fi
bundle_dir=$(dirname "$bundle_dir")
done
bundle_dir=""
export bundle_dir
return 1
}
function bundle_config_changed() {
local old_dir=$bundle_dir
find_bundle_dir
if [ "$bundle_dir" != "$old_dir" ]; then
return 0
else
return 1
fi
}
function __bundler_prompt_command(){
if bundle_config_changed; then
if [ ! -z "$bundle_bin" ]; then
export PATH=$(echo $PATH | sed "s!${bundle_bin}\:!!")
fi
if [ -e "$bundle_dir/.bundle/config" ]; then
export bundle_bin=$(readlink -f $(cat "$bundle_dir/.bundle/config" | grep BUNDLE_BIN | awk '{ print $2 }'))
else
export bundle_bin=""
fi
fi
if echo $PATH | grep -q -v "$bundle_bin"; then
export PATH=$bundle_bin:$PATH
fi
}
function __bundler_ps1(){
if [ ! -z "$bundle_bin" ]; then
if echo $PATH | grep -q "$bundle_bin"; then
echo -n 'B '
fi
fi
}
@nyarly
Copy link
Copy Markdown
Author

nyarly commented Feb 12, 2013

Upshot here is: use bundle_setup (put in your PATH somewhere) in a new project with a Gemfile. It'll create binstubs for all the gems it installs.

The __bundler_prompt_command checks for a .bundle directory and uses it to modify your PATH so that those binstubs are available.

The PS1 (apart from naming the current git branch) adds a 'B' to the prompt when this setup is active, so you can be sure that things are running properly.

Upshot is that each project gets a gemset, reused gems are shared across projects (although it may be that that slows gem loading ... easy enough to change that behavior), and we're not adding another tool to the chain (i.e. rbenv/rvm)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment