61

I have a Rakefile with a Rake task that I would normally call from the command line:

rake blog:post Title

I'd like to write a Ruby script that calls that Rake task multiple times, but the only solution I see is shelling out using `` (backticks) or system.

What's the right way to do this?

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Mando Escamilla
  • 1,560
  • 1
  • 10
  • 17

4 Answers4

45

from timocracy.com:

require 'rake'

def capture_stdout
  s = StringIO.new
  oldstdout = $stdout
  $stdout = s
  yield
  s.string
ensure
  $stdout = oldstdout
end

Rake.application.rake_require 'metric_fetcher', ['../../lib/tasks']
results = capture_stdout {Rake.application['metric_fetcher'].invoke}
Csa77
  • 649
  • 13
  • 19
titanous
  • 3,668
  • 3
  • 27
  • 26
  • 1
    With Rails 3.1 the rake/rdoctask has been deprecated and tasks/rails is missing. The above works just fine with just the first require statement. – jwadsack Jan 09 '12 at 21:07
  • For changing stdout, I suggest saving the original stream via `#dup`, then `#reopen` to a `Tempfile` which is read after reopening to the original. Merely assigning `$stdout` won't work if the task uses the `STDOUT` constant, or runs an external program. – Kelvin Mar 06 '13 at 22:29
  • Be aware that `rake_require` always joins the given path with each path from the `$LOAD_PATH` array and checks for existence of a file. So the first argument should be a **relative path**. It will be treated as relative even if it contains a leading slash (or backslash on non-Unix systems). – siefca Dec 04 '13 at 15:55
23

This works with Rake version 10.0.3:

require 'rake'
app = Rake.application
app.init
# do this as many times as needed
app.add_import 'some/other/file.rake'
# this loads the Rakefile and other imports
app.load_rakefile

app['sometask'].invoke

As knut said, use reenable if you want to invoke multiple times.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Kelvin
  • 20,119
  • 3
  • 60
  • 68
  • 1
    Hi @JasonFB, you can access the gem with something like `app.add_import "#{Gem::Specification.find_by_name('statesman').gem_dir}/lib/tasks/statesman.rake"` – spikeheap Feb 17 '17 at 15:07
  • somehow my rake task is executing twice . i have puts "hello" in rake task and hello is printing twice... – mrtechmaker Jan 04 '23 at 16:40
17

You can use invoke and reenable to execute the task a second time.

Your example call rake blog:post Title seems to have a parameter. This parameter can be used as a parameter in invoke:

Example:

require 'rake'
task 'mytask', :title do |tsk, args|
  p "called #{tsk} (#{args[:title]})"
end



Rake.application['mytask'].invoke('one')
Rake.application['mytask'].reenable
Rake.application['mytask'].invoke('two')

Please replace mytask with blog:post and instead the task definition you can require your rakefile.

This solution will write the result to stdout - but you did not mention, that you want to suppress output.


Interesting experiment:

You can call the reenable also inside the task definition. This allows a task to reenable himself.

Example:

require 'rake'
task 'mytask', :title do |tsk, args|
  p "called #{tsk} (#{args[:title]})"
  tsk.reenable  #<-- HERE
end

Rake.application['mytask'].invoke('one')
Rake.application['mytask'].invoke('two')

The result (tested with rake 10.4.2):

"called mytask (one)"
"called mytask (two)"
knut
  • 27,320
  • 6
  • 84
  • 112
4

In a script with Rails loaded (e.g. rails runner script.rb)

def rake(*tasks)
  tasks.each do |task|
    Rake.application[task].tap(&:invoke).tap(&:reenable)
  end
end

rake('db:migrate', 'cache:clear', 'cache:warmup')
Dorian
  • 22,759
  • 8
  • 120
  • 116