How do I run Rake tasks within a Ruby script?

拜拜、爱过 提交于 2019-12-17 08:33:14

问题


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?


回答1:


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}



回答2:


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.




回答3:


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)"



回答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')


来源:https://stackoverflow.com/questions/3530/how-do-i-run-rake-tasks-within-a-ruby-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!