1

I have a Rake task which looks something like the following. What I’m trying to do run a system command, and return its error-value. Before returning I’d like to display a message saying something like “[OK]” or “[FAILED]".

With this code, Rake returns success every time.

How do I get the Rake task to return the correct error value?

task :build do
  status = system BUILD_SHELL_COMMAND
  puts status ? "[OK]" : "[FAILED]"
  status
end
Debajit
  • 46,327
  • 33
  • 91
  • 100

1 Answers1

2

It appears there isn’t a way to specify a “return value” from a rake task. The task should fail if the system() method fails.

The standard way to do this would be to use Rake’s sh utility method:

task :build do
  sh BUILD_SHELL_COMMAND
end

To display an error/success message however, for the case in question, the following would not work:

task :build do
  sh BUILD_SHELL_COMMAND or fail “[FAILED]”
  puts “[OK]"
end

because as soon as the shell command fails, it would not display the failure message (which in reality would be a longer non-trivial message :), which is what we want.

This works:

task :build do
  system BUILD_SHELL_COMMAND or fail “[FAILED]”
  puts “[OK]"
end
Debajit
  • 46,327
  • 33
  • 91
  • 100