What is a Rakefile?
It is an alternative to Makefile
with Ruby syntax.
What is rake and how it is used in rails?
Rake is a "software task management tool", similar to Make, etc. in other systems.
See: http://guides.rubyonrails.org/command_line.html#rake
Rake is Ruby Make, a standalone Ruby utility that replaces the Unix utility 'make', and uses a 'Rakefile' and .rake files to build up a list of tasks. In Rails, Rake is used for common administration tasks, especially sophisticated ones that build off of each other.
You can get a list of Rake tasks available to you, which will often depend on your current directory, by typing rake --tasks. Each task has a description, and should help you find the thing you need.
It is most often used for administration level tasks that can be scripted. The benefit to using Rake over Make or similar, is that it is a Ruby tool and can interface with your RoR app natively, so Models, data constraints and business rules are all available for use.
Rails comes with a set of predefined Rake tasks that allow you to perform database migrations, generate Rails scaffold files, etc.
What exactly is Rake?
Try Martin Fowler's article on Rake for more information:
http://martinfowler.com/articles/rake.html
His pre-amble is:
Rake is a build language, similar in
purpose to make and ant. Like make and
ant it's a Domain Specific Language,
unlike those two it's an internal DSL
programmed in the Ruby language. In
this article I introduce rake and
describe some interesting things that
came out of my use of rake to build
this web site: dependency models,
synthesized tasks, custom build
routines and debugging the build
script.
There is more information available on or linked from the project's home page as well:
http://rake.rubyforge.org/
What is the purpose of RakeFile in the application root directory
The Rakefile
is a ruby-written file which contains the definition of Rake tasks.
Here you can find a small introduction to Rake.
The Rakefile
can include other Ruby files. This is the case of Rails projects.
In fact, in a Rails project you shouldn't change the Rakefile directly. Instead, you can add more rake tasks by creating .rake files in the lib/tasks
folder of your Rails project.
How do you create a Rakefile?
Name the file Test.rake and put it in your lib/task file
What is the purpose of Rake?
Rake lets you script certain tasks on a per-project basis, much as a Makefile allows a Unix developer to script their compile and build process. The defined tasks you've used Rake with so far were included with the packages they came with (e.g. rake db:migrate
comes with Rails, or at least with ActiveRecord) and automate certain tasks related to those packages (e.g. installing required gems for a Rails project).
If your project has certain tasks which are performed repeatedly, you can write a rake task to run those tasks which gets included in the SCM tree for the project and runs in the context of that project; in Rails they're in lib/tasks
. You could write a Rake task to purge stale session records from your database, for example, and then set up a cron job to run it.
How to execute Rake file tasks in parallel
You can create threads and you can run your files in new thread.
For example
CC = "gcc"
task :default => "hello"
file "hello" => ["hello.o", "message.o"] do
Thread.new do
sh "#{CC} -o hello hello.o message.o"
end
end
file "hello.o" => "hello.c" do
Thread.new do
sh "#{CC} -c hello.c"
end
end
file "message.o" => "message.c" do
Thread.new do
sh "#{CC} -c message.c"
end
end
Related Topics
Ruby 1.9, Force_Encoding, But Check
Error Installing Homebrew - Brew Command Not Found
Does Dependency Injection Exist in Rails
Where to Put Common Code Found in Multiple Models
Upgrading from Rails 3 to Rails 3.1
Concatenating String with Number in Ruby
How to Run Selenium (Used Through Capybara) at a Lower Speed
How to Debug a Slow Rails App Boot Time
Monkey-Patching VS. S.O.L.I.D. Principles
Rails Fields_For Form Not Showing Up, Nested Form
Setting Request Headers in Ruby
How to Password-Protect My /Sidekiq Route (I.E. Require Authentication for the Sidekiq::Web Tool)
Aws-Sdk for Ruby Access Folder Within Bucket