Now that Rails 3 is out and everyone has switched to it, there’s a new bit of fun to get used to also: Bundler. If you want to run a command with your gems loaded, you’ll need to run bundle exec to get it to run. This can become quite repetitive
Like most programmers, I’m lazy. So lazy, in fact, I just couldn’t bear the thought of typing bundle exec (or be which I aliased to) so many times. Here’s a bash shortcut I came up with:
bundle_commands=( spec rspec cucumber cap watchr rails rackup ) function run_bundler_cmd () { if [ -e ./Gemfile ]; then echo "bundle exec $@" bundle exec $@ else echo "$@" $@ fi } for cmd in $bundle_commands do alias $cmd="run_bundler_cmd $cmd" done
It looks for a Gemfile in the current working directory. If present, when you run one of the commands on in the bundle_commands variable then it will echo the command and then run it with bundle exec. It’s worked pretty well for me.
Update: As noted by Peter Wagenet in the comments, you can do bundle install --binstubs which will create a bin directory in the project root that does the same thing. Adding ./bin to your $PATH should accomplish the same thing.
Great idea, takes all the usual extra thought out of running those commands, ill give it a try
I usually just use be as shortcut, so “be rspec” would run rspec.
Only drawback I can see is that one cannot opt-out from it, e.g. if I do not want to bundle cap and just use system cap.
@grosser You can opt out; just type $(which rake) instead of rake…
Nice script. I made something similar a while back, but beyond posting it to the relevant bundler bug report I didn’t bother to promote it. Sorry about that.
http://github.com/gma/bundler-exec
It’s ever so slightly cleverer in that it searches for a Gemfile in the current directory’s parents too.
Better yet, use “bundle install –binstubs” which will make a bin directory in your project with the right binaries.
Thanks for the heads-up. I’ve updated the post.
Adding relative paths to $PATH is a bad habit (security-wise). In this particular case you’re already running binaries from random gems and their dogs so it won’t make matter worse – but it’s still a bad habit.
…so after switching from rvm to rbenv I’m going with your bash shortcut thank you very much (-:
Glad no one sees any problem with this insanity.