Yesterday I got sick of typing
rake test
and
rake db:migrate
and being told
You have already activated rake 0.9.2, but your Gemfile requires rake 0.8.7. Consider using bundle exec.
I know you should always run
bundle exec
, but my unconscious memory has not caught up with my conscious one on that aspect, so I always forget to run
rake
under
bundle exec
.
So
I wondered aloud on twitter if I could just alias
rake
to
bundle exec rake
, but confine that setting to specific directories (with bash being my shell).
Turns out, it is possible with the help of another tool that
Calvin Spealman pointed me towards:
capn.
To successfully run the commands I've listed below, you need to have
python and
homebrew already installed. If you already have
libyaml installed or have another way of getting it, there's no need for homebrew.
The section beginning with the line that starts out with
echo
and ending with
-unalias rake"...
creates the capn config file. It's just YAML, so if you'd rather create it with a text editor, you can surely do so. See the
capn project for details on the config possibilities.
Either way, you'll want to change the paths I've used to the places
you want to do the aliasing.
From the terminal, run the following commands:
curl -O http://python-distribute.org/distribute_setup.py
sudo python distribute_setup.py
sudo easy_install pip
sudo brew install libyaml
sudo easy_install -U pyyaml
sudo pip install capn
echo "
settings:
default_type: path
hooks:
- path: ~/workspace #change this to the path where you want to alias rake
type: tree # if you don't want the whole tree under the path above, remove this line
enter:
- echo aliasing rake to 'bundle exec rake'
- alias rake='bundle exec rake'
exit:
- echo unaliasing rake from 'bundle exec rake'
- unalias rake" > ~/.capnhooks
source capn # put this line in your .bash_profile if you want capn to work when you enter the shell
# to deactivate the hooks, use: unhook
Enjoy the silence now that you don't have to hear the whining.
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
Leave a comment
At work we discovered that some gems or plugins would actually call rake for us, with no option to use bundle exec. So we actually patched the rake binary to boot up Bundler if it sees a Gemfile.
I really wish that all gem-provided binaries would come with Bundler-awareness.
Posted by
Henning Koch
on Jun 26, 2011 at 03:54 PM UTC - 6 hrs
That's a cool idea. I'd also contact the authors of those gems so they can fix their work for everyone else. =)
Posted by
Sammy Larbi
on Jul 06, 2011 at 06:00 AM UTC - 6 hrs
@Graham - thanks for pointing that out to me. I had read the top part of that blog post before, but I guess I didn't make it down to binstubs.
Either way, this was a fun diversion for me on the day I did it.
I wonder why they didn't do binstubs by default?
Posted by
Sammy Larbi
on Sep 20, 2011 at 06:16 AM UTC - 6 hrs
Leave a comment