Questions tagged [rake]

Ruby build utility similar to make with build commands defined in pure Ruby.

Rake uses Rakefiles (cf. Makefiles) defined in pure Ruby, the build utility created by the late Jim Weirich. Rule patterns to synthesize implicit tasks are supported and developers can easily specify pre- and post-task dependency chains. Rake includes a library of pre-packaged tasks to make creating Rakefiles easier.

Rake uses Ruby's anonymous function blocks to define various tasks, allowing the use of Ruby syntax. It has a library of common tasks: for example, functions to do common file-manipulation tasks and a library to remove compiled files (the "clean" task). Like Make, Rake can also synthesize tasks based on patterns: for example, automatically building a file compilation task based on filename patterns. Rake is now part of the standard library from Ruby version 1.9 onward.

Sample Rake file

    namespace :pick do
      desc "Pick a random user as the winner"
      task :winner => :environment do
        puts "Winner: #{pick(User).name}"
      end

      desc "Pick a random product as the prize"
      task :prize => :environment do
        puts "Prize: #{pick(Product).name}"
      end

      desc "Pick a random prize and winner"
      task :all => [:prize, :winner]

      def pick(model_class)
        model_class.find(:first, :order => 'RAND()')
      end
    end

Useful links

Implementation specific tags

You can specify your question by adding the implementation you used as a tag.

4366 questions
91
votes
3 answers

Use bundle exec rake or just rake?

I learned Rails using just the rake command like rake db:migrate; however, I read that I should be using the bundle exec rake ... instead of just plain rake. Now I'm confused about which to use. Should I be using bundle exec rake instead of just…
ab217
  • 16,900
  • 25
  • 74
  • 92
91
votes
8 answers

Default task for namespace in Rake

Given something like: namespace :my_tasks do task :foo do do_something end task :bar do do_something_else end task :all => [:foo, :bar] end How do I make :all be the default task, so that running rake my_tasks will call it…
agentofuser
  • 8,987
  • 11
  • 54
  • 85
86
votes
5 answers

Execute bash commands from a Rakefile

I would like to execute a number of bash commands from a Rakefile. I have tried the following in my Rakefile task :hello do %{echo "World!"} end but upon executing rake hello there is no output? How do I execute bash commands from a…
rudolph9
  • 8,021
  • 9
  • 50
  • 80
86
votes
4 answers

What exactly is Rake?

In simple terms, what does Rake do? What purposes does it have? I understand it's a build tool but I'm looking a bit more detail.
Skip
  • 925
  • 1
  • 6
  • 6
86
votes
5 answers

Global access to Rake DSL methods is deprecated

I am working through the Ruby on Rails 3 tutorial book and typed the following on the command line: rake db:migrate which produced the following warning. WARNING: Global access to Rake DSL methods is deprecated. Please Include ... Rake::DSL…
chell
  • 7,646
  • 16
  • 74
  • 140
85
votes
5 answers

Running ruby debug in rspec?

I'm trying to get Ruby debugger running in one of my specs: describe User do it "should be valid" do debugger User.new.should be_valid end end When I run rspec though, I get: debugger statement ignored, use -d or --debug option to…
Allyl Isocyanate
  • 13,306
  • 17
  • 79
  • 130
84
votes
2 answers

How do I use "gets" on a rake task?

I get an error whenever I try to use the function gets within a rake task. Is there a way to make it work? The error says, "no such file or directory - (rake task name)"
foo
83
votes
3 answers

Overriding rails' default rake tasks

I have a Rails 2.2 project in which I want to override the functionality of the rake db:test:prepare task. I thought this would work, but it doesn't: #lib/tasks/db.rake namespace :db do namespace :test do desc "Overridden version of rails'…
Max Williams
  • 32,435
  • 31
  • 130
  • 197
83
votes
4 answers

How can I generate a git diff of what's changed since the last time I pulled?

I'd like to script, preferably in rake, the following actions into a single command: Get the version of my local git repository. Git pull the latest code. Git diff from the version I extracted in step #1 to what is now in my local repository. In…
Teflon Ted
  • 8,696
  • 19
  • 64
  • 78
83
votes
4 answers

Run rake task in controller

I'd like to run a rake task in my controller. Is there any way to do this?
user143482
  • 2,341
  • 3
  • 15
  • 7
80
votes
3 answers

Adding a custom seed file

I want to populate a new feature with dummy data, but don't want to use the db/seeds.rb file as it already has seeds other data irrelevant for this feature. To run the default seeds.rb file, you run the command rake db:seed. If I create a file in…
Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
78
votes
3 answers

Why is my custom rake task in lib/tasks not discovered in Rails 3?

Build-in rake tasks work fine, but my new custom one, in Project/lib/tasks/payments.rb doesn't get loaded: namespace :payments do desc "Tally payments at the end of the month" task :compute => :environment do …
zambezi
  • 1,505
  • 2
  • 11
  • 14
77
votes
7 answers

Where are rake tasks defined?

On a freshly created Rails project (generated by rails someName), one can run some 'default' rake tasks like: rake test rake db:migrate etc Question is, where does these tasks get described? The default Rakefile doesn't have all these…
ryanprayogo
  • 11,587
  • 11
  • 51
  • 66
72
votes
8 answers

I have a Rails task: should I use script/runner or rake?

For ad hoc Rails tasks we have a few implementation alternatives, chief among which would seem to be: script/runner some_useful_thing and: rake some:other_useful_thing Which option should I prefer? If there's a clear favourite then when, if ever,…
Mike Woodhouse
  • 51,832
  • 12
  • 88
  • 127
72
votes
6 answers

Why is rake db:migrate:reset not listed in rake -T?

Why are some rake tasks not listed by rake -T? Like db:migrate:reset? I can execute it without a problem, but why is it not listed there? Is there a way to get a real full list of rake tasks? % rake -T (in /home/zeus/projects/my_project) rake about…
medihack
  • 16,045
  • 21
  • 90
  • 134