Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to explicitly fail a task in ruby rake?

Tags:

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 

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.

like image 352
dahpgjgamgan Avatar asked Sep 20 '10 16:09

dahpgjgamgan


People also ask

What does Rakefile do?

It allows you to use ruby code to define "tasks" that can be run in the command line. Rake can be downloaded and included in ruby projects as a ruby gem. Once installed, you define tasks in a file named "Rakefile" that you add to your project.

What is task in Ruby?

Rake is a popular task runner for Ruby and Rails applications. For example, Rails provides the predefined Rake tasks for creating databases, running migrations, and performing tests. You can also create custom tasks to automate specific actions - run code analysis tools, backup databases, and so on.


1 Answers

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.

like image 117
Richard Cook Avatar answered Sep 29 '22 03:09

Richard Cook