Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does `rake` know where to look for Rakefiles?

Tags:

rake

I'm trying to better understand how rake works. I've looked on the rake website to see how it works but there isn't a clear explanation for how rake searches for Rakefiles and the steps it goes through in resolving dependencies. Can someone explain how rake works?

like image 967
Avery Avatar asked Oct 08 '11 05:10

Avery


People also ask

How does rake work in Ruby?

Rake is a tool you can use with Ruby projects. It allows you to use ruby code to define "tasks" that can be run in the command line. Rake can be downloaded and included in ruby projects as a ruby gem. Once installed, you define tasks in a file named "Rakefile" that you add to your project.

What is the difference between rake and Ruby?

rake is a Make-like program implemented in Ruby. rails is a web framework, which also has some rake tasks. This means that you can have a ruby program with rake but without rails, but not the other way around. By itself, rake will be faster because you don't need to load the whole rails application.

What does rake do in Rails?

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.

Where are rake tasks defined?

User defined rake tasks live inside the lib/tasks folder. Any file ending in ". rake" will be automatically picked up and loaded by Rails.


1 Answers

By default rake will look for one of these files under the directory you execute it from:

  • rakefile
  • Rakefile
  • rakefile.rb
  • Rakefile.rb

You can look at Rake's Application docs to see this list

Additionally, any ruby file including other rakefiles can be included with a standard Ruby require command:

require 'rake/loaders/external-rakefile'

alternatively, you can import them:

import 'rake/loaders/external-rakefile'

To make a set of Rake tasks available for use from any directory, create a .rake subdirectory within your home directory, and place the appropriate Rake files there. Any rake command with the -g option will use these global Rake files (read more here):

rake -g -T

Additionally, if -g option is set, Rake will first try to load the files form RAKE_SYSTEM environment variable, if that is not set, it will default to a home user directory/.rake/*.rake. These files will be loaded/imported in addition to one of the default files listed above.

Otherwise it will load the first default file (from the above list), and additionally import all the rake files from the rakelib directory (under location you run rake from), OR this directory can be specified using:

--rakelibdir=RAKELIBDIR or -R RAKELIBDIR: Auto-import any .rake files in RAKELIBDIR. (default is 'rakelib')  
like image 105
tolitius Avatar answered Nov 01 '22 00:11

tolitius