lib/eco-rake/shell/command.rb in eco-rake-0.2.3 vs lib/eco-rake/shell/command.rb in eco-rake-0.2.4

- old
+ new

@@ -40,10 +40,22 @@ # @param comm [String] the command line def sh_continue(comm, &block) sh(comm, &sh_default_block(comm, &block)) end + # It exits if there is an error + # @note if doesn't raise (prevents) a RuntimeError + # @param comm [String] the command line + def sh_exit_on_fail(comm) + callback = middlewared_callback(sh_default_block(comm)) do |ok, res| + next if ok + + exit(1) + end + sh(comm, &callback) + end + # Returns the default block for `sh` native method. # @note it wraps `block` if given # @return [Proc] def sh_default_block(comm) proc do |ok, res| @@ -51,9 +63,17 @@ unless ok msg = "Command failed (status = #{res.exitstatus})" puts "#{msg}\n • #{comm}" end res.exitstatus + end + end + + def middlewared_callback(original) + proc do |*args, **kargs| + yield(*args, **kargs) + ensure + original.call(*args, **kargs) end end end end end