How do I rake tasks within a ruby script? *

Question

I have a Rakefile with a rake task that I would normally call from the commandline (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 (`` or system).

What's the right way to do this?

Answer

from timocracy.com:

require 'rake'
require 'rake/rdoctask'
require 'rake/testtask'
require 'tasks/rails'

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

Rake.application.rake_require '../../lib/tasks/metric_fetcher'
results = capture_stdout {Rake.application['metric_fetcher'].invoke}
< br > via < a class="StackLink" href=" http://stackoverflow.com/questions/3530/" >How do I rake tasks within a ruby script?< /a>
Share on Google Plus

About Cinema Guy

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment