Module: Calabash::Cucumber::XcodeTools

Included in:
SimulatorAccessibility
Defined in:
lib/calabash-cucumber/utils/xctools.rb

Overview

methods for interacting with the xcode tools

Instance Method Summary collapse

Instance Method Details

#installed_simulatorsArray<String>

returns a list of installed simulators by calling:

$ instruments -s devices

and parsing the output

Returns:

  • (Array<String>)

    an array of simulator names suitable for passing to instruments or xcodebuild

Raises:

  • (RuntimeError)

    if the currently active instruments version does not support the -s flag



86
87
88
89
90
91
# File 'lib/calabash-cucumber/utils/xctools.rb', line 86

def installed_simulators
  unless instruments_supports_hyphen_s?
    raise(RuntimeError, "instruments '#{instruments(:version)}' does not support '-s devices' arguments")
  end
  instruments(:sims)
end

#instruments(cmd = nil) ⇒ String

method for interacting with instruments

          instruments #=> /Applications/Xcode.app/Contents/Developer/usr/bin/instruments
instruments(:version) #=> 5.1.1
   instruments(:sims) #=> < list of known simulators >

Parameters:

  • cmd (String) (defaults to: nil)

    controls the return value. currently accepts nil, :sims, and :version as valid parameters

Returns:

  • (String)

    based on the value of cmd version, a list known simulators, or the path to the instruments binary

Raises:

  • (ArgumentError)

    if invalid cmd is passed



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/calabash-cucumber/utils/xctools.rb', line 41

def instruments(cmd=nil)
  instruments = "#{xcode_bin_dir}/instruments"
  return instruments if cmd == nil

  case cmd
    when :version
      # instruments, version 5.1.1 (55045)
      # noinspection RubyUnusedLocalVariable
      Open3.popen3("#{instruments}") do |stdin, stdout, stderr, wait_thr|
        stderr.read.chomp.split(' ')[2]
      end
    when :sims
      devices = `#{instruments} -s devices`.chomp.split("\n")
      devices.select { |device| device.downcase.include?('simulator') }
    else
      candidates = [:version, :sims]
      raise(ArgumentError, "expected '#{cmd}' to be one of '#{candidates}'")
  end
end

#instruments_supports_hyphen_s?(version = instruments(:version)) ⇒ Boolean

does the instruments version accept the -s (devices) flag?

instruments_supports_hyphen_s?('4.6.3') #=> false
instruments_supports_hyphen_s?('5.0.2') #=> true
  instruments_supports_hyphen_s?('5.1') #=> true

Parameters:

  • version (String) (defaults to: instruments(:version))

    a major.minor.patch version string - defaults to the currently active instruments binary

Returns:

  • (Boolean)

    true iff the version is >= 5.*



70
71
72
73
74
75
# File 'lib/calabash-cucumber/utils/xctools.rb', line 70

def instruments_supports_hyphen_s?(version=instruments(:version))
  tokens = version.split('.')
  return false if tokens[0].to_i < 5
  return false if tokens[1].to_i < 1
  true
end

#xcode_bin_dirString

returns the path to the current developer usr/bin directory

Returns:

  • (String)

    path to the current xcode binaries



26
27
28
# File 'lib/calabash-cucumber/utils/xctools.rb', line 26

def xcode_bin_dir
  "#{xcode_developer_dir}/usr/bin"
end

#xcode_developer_dirString

returns the path to the current developer directory

$ man xcode-select DEVELOPER_DIR Overrides the active developer directory. When DEVELOPER_DIR is set, its value will be used instead of the system-wide active developer directory.

Returns:

  • (String)

    path to current developer directory



17
18
19
20
21
22
# File 'lib/calabash-cucumber/utils/xctools.rb', line 17

def xcode_developer_dir
  # respect DEVELOPER_DIR
  return ENV['DEVELOPER_DIR'] if ENV['DEVELOPER_DIR']
  # fall back to xcode-select
  `xcode-select --print-path`.chomp
end