Class: RunLoop::Xcrun

Inherits:
Object
  • Object
show all
Defined in:
lib/run_loop/xcrun.rb

Defined Under Namespace

Classes: Error, TimeoutError

Constant Summary collapse

DEFAULT_OPTIONS =
{
      :timeout => 30,
      :log_cmd => false
}

Instance Method Summary collapse

Instance Method Details

#exec(args, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/run_loop/xcrun.rb', line 19

def exec(args, options={})

  merged_options = DEFAULT_OPTIONS.merge(options)

  timeout = merged_options[:timeout]

  unless args.is_a?(Array)
    raise ArgumentError,
          "Expected args '#{args}' to be an Array, but found '#{args.class}'"
  end

  args.each do |arg|
    unless arg.is_a?(String)
      raise ArgumentError,
%Q{Expected arg '#{arg}' to be a String, but found '#{arg.class}'
           IO.popen requires all arguments to be Strings.}
    end
  end

  cmd = "xcrun #{args.join(' ')}"

  # Don't see your log?
  # Commands are only logged when debugging.
  RunLoop.log_unix_cmd(cmd) if merged_options[:log_cmd]

  # Ruby < 2.0 support
  return exec_ruby19(args, merged_options) if RUBY_VERSION < '2.0'

  hash = {}

  begin

    start_time = Time.now
    command_output = CommandRunner.run(['xcrun'] + args, timeout: timeout)

    if command_output[:out]
      out = command_output[:out].force_encoding('utf-8').chomp
    else
      out = ''
    end

    process_status = command_output[:status]

    hash =
          {
                :out => out,
                :pid => process_status.pid,
                # nil if process was killed before completion
                :exit_status => process_status.exitstatus
          }

  rescue => e
    elapsed = "%0.2f" % (Time.now - start_time)
    raise Error, "Xcrun encountered an error after #{elapsed} seconds: #{e}"
  end

  if hash[:exit_status].nil?
    elapsed = "%0.2f" % (Time.now - start_time)
    raise TimeoutError,
          "Xcrun timed out after #{elapsed} seconds executing '#{cmd}' with a timeout of #{timeout}"
  end

  hash
end