How to explicitly fail a task in ruby rake?

RubyRake

Ruby Problem Overview


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

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.

Ruby Solutions


Solution 1 - Ruby

Use the raise or fail method as you would for any other Ruby script (fail is an alias for raise). This method takes a string or exception as an argument which is used as the error message displayed at termination of the script. This will also cause the script to return the value 1 to the calling shell. It is documented here and other places.

Solution 2 - Ruby

You can use abort("message") to gracefully fail rake task.

It will print message to stdout and exit with code 1.

Exit code 1 is a failure in Unix-like systems.

See Kernel#abort for details.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondahpgjgamganView Question on Stackoverflow
Solution 1 - RubyRichard CookView Answer on Stackoverflow
Solution 2 - RubySenidView Answer on Stackoverflow