bundle exec bash shortcut

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.

Tags: , ,

This is the posts comments

  1. grosser September 17, 2010 at 7:45 pm #

    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.

  2. Graham Ashton September 17, 2010 at 8:39 pm #

    @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.

  3. Peter Wagenet September 18, 2010 at 5:36 am #

    Better yet, use “bundle install –binstubs” which will make a bin directory in your project with the right binaries.

    • jason September 23, 2010 at 4:19 pm #

      Thanks for the heads-up. I’ve updated the post.

      • Wojtek Kruszewski December 16, 2011 at 5:34 pm #

        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.

        • Wojtek Kruszewski December 16, 2011 at 5:39 pm #

          …so after switching from rvm to rbenv I’m going with your bash shortcut thank you very much (-:

  4. Rick October 25, 2011 at 5:33 am #

    Glad no one sees any problem with this insanity.

  5. Johne90 September 18, 2014 at 1:12 pm #

    Do you mind if I quote a few of your posts as long as I provide credit and sources back to your weblog? My blog is in the very same niche as yours and my users would truly benefit from a lot of the information you present here. Please let me know if this alright with you. Thank you! edkcdfeceaef

  6. Pharme4 September 21, 2014 at 11:02 pm #

    Very nice site! cheap goods

Trackbacks/Pingbacks

  1. Link dump for September 17th | The Queue Blog - September 18, 2010

    [...] bundle exec bash shortcut | Twistedmind – a bit of bash hackery that looks for a specific set of commands, given it's in a directory with bundler, to intercept and use bundle exec "said command" instead [...]

Post your comment