Skip to content

Instantly share code, notes, and snippets.

@mpasternacki
Created May 28, 2013 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpasternacki/5661786 to your computer and use it in GitHub Desktop.
Save mpasternacki/5661786 to your computer and use it in GitHub Desktop.
Script to activate Ruby Gem Bundler environment in current shell session.
#!/bin/sh
#
# This script activates a Gem Bundler environment in current shell
# session. This lets you type commands without `bundle exec` all the
# time, even if you leave the project's directory, and without complex
# magic that runs with every command.
#
# Usage: save this file somewhere. Use following command to activate
# current directory's bundle in current shell session:
#
# eval `/path/to/bundle-activate`
#
# For convenience you may add alias to your shell's initialization
# file (e.g. ~/.profile):
#
# alias ba='eval `path/to/bundle-activate`'
#
set -e
old=`mktemp env.unbundled.XXXXX`
new=`mktemp env.bundled.XXXXX`
out=`mktemp env.changed.XXXXX`
env > $old
bundle exec env > $new
fgrep -v -f $old $new \
| grep -v '^_=' \
| sed "s/'/'\\\\''/g; s/^/'/; s/$/'/" \
> $out
prj="`grep '^BUNDLE_GEMFILE=' $new | cut -d= -f2-`"
prj="`dirname $prj`"
prj="`basename $prj`"
echo "PS1=\"(bundle@$prj)\$PS1\"" >> $out
tr \\n \\0 < $out | xargs -0 echo export
rm $old $new $out
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment