How to explicitly fail a task in ruby rake?

dah*_*gan 47 ruby rake

Let's say I have a rakefile like this:

file 'file1' => some_dependencies do
  sh 'external tool I do not have control over, which sometimes fail to create the file'
  ???
end

task :default => 'file1' do
  puts "everything's OK"
end
Run Code Online (Sandbox Code Playgroud)

Now if I put nothing in place of ???, I get the OK message, even if the external tool fails to generate file. What is the proper way to informing rake, that 'file1' task has failed and it should abort (hopefully presenting a meaningful message - like which task did fail) - the only think I can think of now is raising an exception there, but that just doesn't seem right.

P.S The tool always returns 0 as exit code.

Ric*_*ook 59

像使用任何其他Ruby脚本一样使用raiseor fail方法(fail是别名raise).此方法将字符串或异常作为参数,该参数用作脚本终止时显示的错误消息.这也将导致脚本将值1返回给调用shell.它在这里和其他地方都有记录.

  • [`fail`](http://ruby-doc.org/core-2.0/Kernel.html#method-i-fail)只是[`raise`]的别名(http://ruby-doc.org /core-2.0/Kernel.html#method-i-raise) (5认同)

小智 17

您可以使用abort("message")优雅地使rake任务失败.

它将打印message到stdout并退出代码1.

退出代码1是类Unix系统中的失败.

有关详细信息,请参阅内核#abort.