Class: RunLoop::Device

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, version, udid, state = nil) ⇒ Device

Create a new device.

Parameters:

  • name (String)

    The name of the device. For sims this should be ‘iPhone 5s’ and for physical devices it will be the name the user gave to the device.

  • version (String, RunLoop::Version)

    The iOS version that is running on the device. Can be a string or a Version instance.

  • udid (String)

    The device identifier.

  • state (String) (defaults to: nil)

    (nil) This a simulator only value. It refers to the Booted/Shutdown/Creating state of the simulator. For pre-Xcode 6 simulators, this value should be nil.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/run_loop/device.rb', line 26

def initialize(name, version, udid, state=nil)
  @name = name
  @udid = udid
  @state = state

  if version.is_a? String
    @version = RunLoop::Version.new version
  else
    @version = version
  end
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/run_loop/device.rb', line 6

def name
  @name
end

#stateObject (readonly)

Returns the value of attribute state.



9
10
11
# File 'lib/run_loop/device.rb', line 9

def state
  @state
end

#udidObject (readonly)

Returns the value of attribute udid.



8
9
10
# File 'lib/run_loop/device.rb', line 8

def udid
  @udid
end

#versionObject (readonly)

Returns the value of attribute version.



7
8
9
# File 'lib/run_loop/device.rb', line 7

def version
  @version
end

Class Method Details

.device_with_identifier(udid_or_name, options = {}) ⇒ RunLoop::Device

Returns a device given a udid or name. In the case of a physical device, the udid is the device identifier. In the case of a simulator the name is the _instruments identifier_ as reported by ‘$ xcrun instruments -s devices` - this is the identifier that can be passed to instruments.

Note that if you have a device and simulator with the same name, the simulator will always be selected.

Examples:

RunLoop::Device.device_with_identifier('iPhone 4s (8.3 Simulator'))
RunLoop::Device.device_with_identifier('6E43E3CF-25F5-41CC-A833-588F043AE749')
RunLoop::Device.device_with_identifier('denis') # Simulator or device named 'denis'
RunLoop::Device.device_with_identifier('893688959205dc7eb48d603c558ede919ad8dd0c')

Parameters:

  • udid_or_name (String)

    A name or udid that identifies the device you are looking for.

  • options (Hash) (defaults to: {})

    Allows callers to pass runtime models that might optimize performance (via memoization).

Options Hash (options):

Returns:

Raises:

  • (ArgumentError)

    If no matching device can be found.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/run_loop/device.rb', line 64

def self.device_with_identifier(udid_or_name, options={})
  if options.is_a?(RunLoop::SimControl)
    RunLoop.deprecated('1.5.0', %q(
The 'sim_control' argument has been deprecated.  It has been replaced by an
options hash with two keys: :sim_control and :instruments.
Please update your sources.))
    merged_options = {
          :sim_control => options,
          :instruments => RunLoop::Instruments.new
    }
  else
    default_options = {
          :sim_control => RunLoop::SimControl.new,
          :instruments => RunLoop::Instruments.new
    }
    merged_options = default_options.merge(options)
  end

  instruments = merged_options[:instruments]
  sim_control = merged_options[:sim_control]

  xcode = sim_control.xcode
  simulator = sim_control.simulators.detect do |sim|
    sim.instruments_identifier(xcode) == udid_or_name ||
          sim.udid == udid_or_name
  end

  return simulator if !simulator.nil?

  physical_device = instruments.physical_devices.detect do |device|
    device.name == udid_or_name ||
          device.udid == udid_or_name
  end

  return physical_device if !physical_device.nil?

  raise ArgumentError, "Could not find a device with a UDID or name matching '#{udid_or_name}'"
end

Instance Method Details

#instruction_setString

Note:

Finding the instruction set of a device requires a third-party tool like ideviceinfo. Example: ‘$ ideviceinfo -u 89b59 < snip > ab7ba –key ’CPUArchitecture’ => arm64`

Return the instruction set for this device.

Simulator The simulator instruction set will be i386 or x86_64 depending on the the (marketing) name of the device.

Returns:

  • (String)

    An instruction set.

Raises:

  • (RuntimeError)

    Raises an error if this device is a physical device.



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/run_loop/device.rb', line 179

def instruction_set
  if simulator?
    if ['iPhone 4s', 'iPhone 5', 'iPad 2', 'iPad Retina'].include?(self.name)
      'i386'
    else
      'x86_64'
    end
  else
    raise 'Finding the instruction set of a device requires a third-party tool like ideviceinfo'
  end
end

#instruments_identifier(xcode = SIM_CONTROL.xcode) ⇒ String

Note:

As of 1.5.0, the XCTools optional argument has become a non-optional Xcode argument.

Returns and instruments-ready device identifier that is a suitable value for DEVICE_TARGET environment variable.

Parameters:

Returns:

  • (String)

    An instruments-ready device identifier.

Raises:

  • (RuntimeError)

    If trying to obtain a instruments-ready identifier for a simulator when Xcode < 6.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/run_loop/device.rb', line 128

def instruments_identifier(xcode=SIM_CONTROL.xcode)
  if xcode.is_a?(RunLoop::XCTools)
    RunLoop.deprecated('1.5.0',
                       %q(
RunLoop::XCTools has been replaced with a non-optional RunLoop::Xcode argument.
Please update your sources to pass an instance of RunLoop::Xcode))
  end

  if physical_device?
    udid
  else
    if version == RunLoop::Version.new('7.0.3')
      version_part = version.to_s
    else
      version_part = "#{version.major}.#{version.minor}"
    end

    if xcode.version_gte_7?
      "#{name} (#{version_part})"
    elsif xcode.version_gte_6?
      "#{name} (#{version_part} Simulator)"
    else
      udid
    end
  end
end

#physical_device?Boolean

Is this a physical device?

Returns:

  • (Boolean)

    Returns true if this is a device.



157
158
159
# File 'lib/run_loop/device.rb', line 157

def physical_device?
  not udid[DEVICE_UDID_REGEX, 0].nil?
end

#simulator?Boolean

Is this a simulator?

Returns:

  • (Boolean)

    Returns true if this is a simulator.



163
164
165
# File 'lib/run_loop/device.rb', line 163

def simulator?
  not physical_device?
end