Class: Device

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid, type, name, state, runtime) ⇒ Device

Returns a new instance of Device.



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

def initialize(uuid, type, name, state, runtime)
  @UUID = uuid
  @type = type
  @name = name
  @state = state
  @runtime = runtime
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



5
6
7
# File 'lib/israkel/device.rb', line 5

def name
  @name
end

#runtimeObject

Returns the value of attribute runtime.



5
6
7
# File 'lib/israkel/device.rb', line 5

def runtime
  @runtime
end

#stateObject

Returns the value of attribute state.



5
6
7
# File 'lib/israkel/device.rb', line 5

def state
  @state
end

#typeObject

Returns the value of attribute type.



5
6
7
# File 'lib/israkel/device.rb', line 5

def type
  @type
end

#UUIDObject

Returns the value of attribute UUID.



5
6
7
# File 'lib/israkel/device.rb', line 5

def UUID
  @UUID
end

Class Method Details

.allObject



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/israkel/device.rb', line 41

def self.all
  devices = []
  dirs = Dir.entries(Device.sim_root_path).reject { |entry| File.directory? entry }
  dirs.sort.each do |simulator_dir|
    plist_path = "#{Device.sim_root_path}/#{simulator_dir}/device.plist"
    if File.exists?(plist_path)
      plist = CFPropertyList::List.new(:file => plist_path)
      devices << Device.from_plist(plist)
    end
  end
  devices
end

.edit_plist(path) {|content || {}| ... } ⇒ Object

Yields:

  • (content || {})


54
55
56
57
58
59
60
61
62
63
64
# File 'lib/israkel/device.rb', line 54

def self.edit_plist(path, &block)
  if File.exists?(path)
    plist = CFPropertyList::List.new(:file => path)
    content = CFPropertyList.native_types(plist.value)
  end
  yield content || {}
  if plist
    plist.value = CFPropertyList.guess(content)
    plist.save(path, CFPropertyList::List::FORMAT_BINARY)
  end
end

.from_hash(hash) ⇒ Object



15
16
17
# File 'lib/israkel/device.rb', line 15

def self.from_hash(hash)
  self.new(hash['UDID'], hash['deviceType'], hash['name'], hash['state'], hash['runtime'])
end

.from_plist(plist) ⇒ Object



19
20
21
# File 'lib/israkel/device.rb', line 19

def self.from_plist(plist)
  self.from_hash(CFPropertyList.native_types(plist.value))
end

.shutdown_devicesObject



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

def self.shutdown_devices
  SIMCTL.booted_devices_uuids.each do |uuid|
    SIMCTL.shutdown(uuid)
  end
end

.sim_root_pathObject



108
109
110
# File 'lib/israkel/device.rb', line 108

def self.sim_root_path
  File.join(ENV['HOME'], 'Library', 'Developer', 'CoreSimulator', 'Devices')
end

.stopObject



30
31
32
33
# File 'lib/israkel/device.rb', line 30

def self.stop
  self.shutdown_devices
  system 'killall', '-m', '-TERM', 'iOS Simulator'
end

.with_sdk_and_type(sdk_version, type) ⇒ Object



23
24
25
26
27
28
# File 'lib/israkel/device.rb', line 23

def self.with_sdk_and_type(sdk_version, type)
  Device.all.each do |device|
    return device if device.os == sdk_version && device.type.split('.').last == type
  end
  nil
end

Instance Method Details

#allow_addressbook_access(bundle_id) ⇒ Object



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

def allow_addressbook_access(bundle_id)
  allow_tcc_access('kTCCServiceAddressBook', bundle_id)
end

#allow_gps_access(bundle_id) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/israkel/device.rb', line 78

def allow_gps_access(bundle_id)
  directory = File.join(path, 'Library', 'Caches', 'locationd')
  FileUtils.mkdir_p(directory) unless Dir.exists?(directory)
  Device.edit_plist(File.join(directory, 'clients.plist')) do |content|
    set_gps_access(content, bundle_id)
  end
end

#allow_photos_access(bundle_id) ⇒ Object



74
75
76
# File 'lib/israkel/device.rb', line 74

def allow_photos_access(bundle_id)
  allow_tcc_access('kTCCServicePhotos', bundle_id)
end

#edit_global_preferences(&block) ⇒ Object



116
117
118
119
# File 'lib/israkel/device.rb', line 116

def edit_global_preferences(&block)
  pref_path = File.join(path, 'Library', 'Preferences')
  Device.edit_plist( File.join(pref_path, '.GlobalPreferences.plist'), &block )
end

#edit_preferences(&block) ⇒ Object



121
122
123
124
# File 'lib/israkel/device.rb', line 121

def edit_preferences(&block)
  pref_path = File.join(path, 'Library', 'Preferences')
  Device.edit_plist( File.join(pref_path, 'com.apple.Preferences.plist'), &block )
end

#osObject



112
113
114
# File 'lib/israkel/device.rb', line 112

def os
  runtime.gsub('com.apple.CoreSimulator.SimRuntime.iOS-', '').gsub('-', '.')
end

#resetObject



104
105
106
# File 'lib/israkel/device.rb', line 104

def reset
  SIMCTL.erase @UUID
end

#set_language(language) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/israkel/device.rb', line 86

def set_language(language)
  edit_global_preferences do |p|
    if p['AppleLanguages']
      if p['AppleLanguages'].include?(language)
        p['AppleLanguages'].unshift(language).uniq!
      else
        fail "#{language} is not a valid language"
      end
    else
      p['AppleLanguages'] = [language]
    end
  end
end

#startObject



100
101
102
# File 'lib/israkel/device.rb', line 100

def start
  system "ios-sim start --devicetypeid \"#{device_type}\""
end

#tcc_pathObject



126
127
128
# File 'lib/israkel/device.rb', line 126

def tcc_path
  File.join(path, 'Library', 'TCC', 'TCC.db')
end

#to_sObject



66
67
68
# File 'lib/israkel/device.rb', line 66

def to_s
  "#{name} #{pretty_runtime}"
end