module ImageOptim::Cmd

Helper for running commands

Public Class Methods

capture(cmd) click to toggle source

Run using backtick Return captured output Will raise SignalException if process was interrupted

# File lib/image_optim/cmd.rb, line 21
def capture(cmd)
  output = `#{cmd}`

  check_status!

  output
end
run(*args) click to toggle source

Run using ‘system` Return success status Will raise SignalException if process was interrupted

# File lib/image_optim/cmd.rb, line 10
def run(*args)
  success = system(*args)

  check_status!

  success
end

Private Class Methods

check_status!() click to toggle source
# File lib/image_optim/cmd.rb, line 31
def check_status!
  status = $CHILD_STATUS

  return unless status.signaled?

  # jruby incorrectly returns true for `signaled?` if process exits with
  # non zero status. For following code
  #
  #     `sh -c 'exit 66'`
  #     p [$?.signaled?, $?.exitstatus, $?.termsig]
  #
  # jruby outputs `[true, 66, 66]` instead of expected `[false, 66, nil]`
  return if defined?(JRUBY_VERSION) && status.exitstatus == status.termsig

  fail SignalException, status.termsig
end