Class: Device

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, device_type:, runtime:, udid: nil, xc: XC.new) ⇒ Device

Returns a new instance of Device.



6
7
8
9
10
11
12
# File 'lib/peach/device.rb', line 6

def initialize(name:, device_type:, runtime:, udid: nil, xc: XC.new)
	@name = name
	@device_type = device_type
	@runtime = runtime
	@udid = udid
	@xc = xc
end

Instance Attribute Details

#device_typeObject (readonly)

Returns the value of attribute device_type.



4
5
6
# File 'lib/peach/device.rb', line 4

def device_type
  @device_type
end

#nameObject (readonly)

Returns the value of attribute name.



4
5
6
# File 'lib/peach/device.rb', line 4

def name
  @name
end

#runtimeObject (readonly)

Returns the value of attribute runtime.



4
5
6
# File 'lib/peach/device.rb', line 4

def runtime
  @runtime
end

#udidObject (readonly)

Returns the value of attribute udid.



4
5
6
# File 'lib/peach/device.rb', line 4

def udid
  @udid
end

Class Method Details

.current_devices(xc:) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/peach/device.rb', line 50

def self.current_devices(xc:)
	devices = Array.new

	simctl_json = JSON.parse(load_devices)
	device_list = simctl_json['devices']

	device_list.each do |type, ds|
		runtime = xc.runtime_name(type)

		ds.each do |d|
			device_name = d["name"]
			device_udid = d["udid"]
			device_type = device_type(udid: device_udid, xc: xc)
			devices << Device.new(name: device_name, device_type: device_type, runtime: runtime, udid: device_udid, xc: xc)
		end
	end

	devices
end

.destroy_all(xc:) ⇒ Object

class functions



43
44
45
46
47
48
# File 'lib/peach/device.rb', line 43

def self.destroy_all(xc:)
	devices = Device.current_devices(xc: xc) || []
	devices.each do |d|
		d.destroy
	end
end

.device_type(udid:, xc:) ⇒ Object



75
76
77
78
# File 'lib/peach/device.rb', line 75

def self.device_type(udid:, xc:)
	device_type_identifier = device_type_identifier(udid: udid, xc: xc)
	xc.device_name(device_type_identifier)
end

.device_type_identifier(udid:, xc:) ⇒ Object



70
71
72
73
# File 'lib/peach/device.rb', line 70

def self.device_type_identifier(udid:, xc:)
	plist = Plist.parse_xml("#{ENV['HOME']}/Library/Developer/CoreSimulator/Devices/#{udid}/device.plist")
	plist['deviceType']
end

.load_devicesObject



80
81
82
# File 'lib/peach/device.rb', line 80

def self.load_devices
	`xcrun simctl list devices -j`
end

Instance Method Details

#==(o) ⇒ Object



34
35
36
37
38
39
# File 'lib/peach/device.rb', line 34

def ==(o)
	o.class == self.class &&
	o.name == @name &&
	o.device_type == @device_type &&
	o.runtime == runtime
end

#createObject



18
19
20
21
22
23
24
25
26
27
# File 'lib/peach/device.rb', line 18

def create
	runtime_id = @xc.runtimes.key(@runtime)
	if @xc.runtime_invalid(@runtime)
		puts "Unable to create #{@name}. Invalid runtime: #{@runtime}. Run \"xcrun simctl list runtimes\" to list available runtimes"
	elsif @xc.device_type_invalid(@device_type)
		puts "Unable to create #{@name}. Invalid device_type: #{@device_type}. Run \"xcrun simctl list devicetypes\" to list available devicetypes"
	else
		@udid = `xcrun simctl create \"#{@name}\" \"#{@device_type}\" \"#{runtime_id}\"`.strip
	end
end

#destroyObject



29
30
31
# File 'lib/peach/device.rb', line 29

def destroy
	`xcrun simctl delete #{@udid}`
end

#eql?Object



33
# File 'lib/peach/device.rb', line 33

alias_method :eql?, :==

#exists?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/peach/device.rb', line 14

def exists?
	Device.current_devices(xc: @xc).include?(self)
end