Home

Add Rake To Any Project

Rake is an awesome tool. You may want to use it in a non-Ruby project or a project that isn't configured for it.

Rake is a really powerful tool for automating any type of task. It can come in handy even in a project that doesn't have it installed. Heck, you can even use it in a non-ruby project if you want.

01: Install Rake

While you could use system gems to get this setup, I'm going to take the approach that we want this to work consistently for multiple developers. That means we're going to wrap the gems we need up in the project.

So, first, make sure you have Bundler installed.

Next, create a Gemfile in your project's root if it doesn't already have one.

If you had to create the Gemfile, then you'll need to add a source call to the top of it.

Gemfile

source 'https://rubygems.org'

Then we're going to add rake. (You can skip this if you already had a Gemfile that has this line.)

Gemfile

source 'https://rubygems.org'

gem 'rake'

Then use Bundler to install the gem.

$ bundle install

This will create a .bundle directory at your project's root. If you aren't already, it's good to ignore this with git (add .bundle/ to .gitignore).

02: Configure Rake

Next, create a Rakefile in your project's root, and add this one-liner:

Rakefile

Dir.glob(File.join('lib/tasks/**/*.rake')).each { |file| load file }

This is telling Rake to load every .rake file it finds in the lib/tasks directory.

03: Test It

Last, let's make sure it works. Let's add a dummy task to lib/tasks/hello.rake.

lib/tasks/hello.rake

task :hello do
puts 'hello'
end

Now, from within your project, run the following command.

$ bundle exec rake hello

It should say "hello" to you. If it does, you've set it up correctly!

Let's Connect

Keep Reading

Bulk Resize Images Using Rake and ImageMagick

Got a set of images you need all to conform to the same size? Hate doing it manually? Me too. Let's write a rake task to solve our challenge.

Feb 15, 2016

4 Ways to Pass Arguments to a Rake Task

Always googling and forgetting how to pass arguments to rake tasks? Here's a up list of the various methods.

Dec 13, 2014

A Quicker Way to Compare Multiple Equals Operators in Ruby

When you attempt to write several predictable comparisons in one statement, it gets ugly fast. Here are some methods for cleaning it up.

Apr 20, 2015