Thursday, February 3, 2011

How do I rake tasks within a ruby script?

I've got 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?

  • 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}
    
    From Titanous
  • Works like a charm. Now to fix my rake task :).

    From mando

0 comments:

Post a Comment