Matty
Matty

Reputation: 34433

What can rakefiles be used for?

I understand the utility of the rake command, but what kinds of custom actions are typically defined in the Rakefile? I'm new and trying to figure out when it's appropriate to use this feature. I've found out plenty about how to use it, but not much about "best practices".

Upvotes: 2

Views: 278

Answers (3)

peakxu
peakxu

Reputation: 6675

Since you're asking for best practices, check out this book: Ruby Best Practices. Pages 234-237 talks about Rake files.

To paraphrase, it enhances the discoverability of tasks in your project so that users unfamiliar with it can start doing useful things quickly.

A good practice is to add desc() strings for your Rake tasks so that rake -- tasks provides meaningful output.

Some applications for Rake that I've used personally:

  1. Test runs
  2. Gem packaging
  3. Documentation generation

Other examples include publishing code/documentation updates to Rubyforge automatically, etc. Basically, virtually anything that can be done at a command line can be done in Rake and your imagination's the limit.

Upvotes: 4

knut
knut

Reputation: 27845

Answering to your followup-question in Chads answer:

Just a followup - what is the advantage to using rake over calling a ruby file with the interpreter directly?

You could use rake as a method to define command line options.

I often use it like this:

require 'rake'  

#
# Your task definitions
#

task :default => :mytask



if $0 == __FILE__
  app = Rake.application
  app[:default].invoke
end

If I execute the script, my default task is running. But I may also start it via rake on command line.

Upvotes: 3

Chad
Chad

Reputation: 19609

Rake could be used for any kind of batch command, most people use things like setting up, installing, running, cleaning, or other application related actions.

Upvotes: 1

Related Questions