Module: ImageOptim::Cmd

Defined in:
lib/image_optim/cmd.rb

Overview

Helper for running commands

Class Method Summary collapse

Class Method Details

.capture(cmd) ⇒ Object

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



43
44
45
46
47
48
49
# File 'lib/image_optim/cmd.rb', line 43

def capture(cmd)
  output = `#{cmd}`

  check_status!

  output
end

.run(*args) ⇒ Object

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/image_optim/cmd.rb', line 13

def run(*args)
  if args.last.is_a?(Hash) && (timeout = args.last.delete(:timeout))
    args.last[Gem.win_platform? ? :new_pgroup : :pgroup] = true

    pid = Process.spawn(*args)

    waiter = Process.detach(pid)
    if waiter.join(timeout.to_f)
      status = waiter.value

      check_status!(status)

      status.success?
    else
      cleanup(pid, waiter)

      fail Errors::TimeoutExceeded
    end
  else
    success = system(*args)

    check_status!

    success
  end
end