Class: Snapshot::ResetSimulators

Inherits:
Object
  • Object
show all
Defined in:
snapshot/lib/snapshot/reset_simulators.rb

Class Method Summary collapse

Class Method Details

.clear_everything!(ios_versions, force = false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
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
# File 'snapshot/lib/snapshot/reset_simulators.rb', line 6

def self.clear_everything!(ios_versions, force = false)
  # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  # !! Warning: This script will remove all your existing simulators !!
  # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  sure = true if FastlaneCore::Env.truthy?("SNAPSHOT_FORCE_DELETE") || force
  begin
    sure = UI.confirm("Are you sure? All your simulators will be DELETED and new ones will be created! (You can use `SNAPSHOT_FORCE_DELETE` to skip this confirmation)") unless sure
  rescue => e
    UI.user_error!("Please make sure to pass the `--force` option to reset simulators when running in non-interactive mode") unless UI.interactive?
    raise e
  end

  UI.abort_with_message!("User cancelled action") unless sure

  if ios_versions
    ios_versions.each do |version|
      FastlaneCore::Simulator.delete_all_by_version(os_version: version)
    end
  else
    FastlaneCore::Simulator.delete_all
  end

  FastlaneCore::SimulatorTV.delete_all
  FastlaneCore::SimulatorWatch.delete_all

  all_runtime_type = runtimes
  # == Runtimes ==
  # iOS 9.3 (9.3 - 13E233) (com.apple.CoreSimulator.SimRuntime.iOS-9-3)
  # iOS 10.0 (10.0 - 14A345) (com.apple.CoreSimulator.SimRuntime.iOS-10-0)
  # iOS 10.1 (10.1 - 14B72) (com.apple.CoreSimulator.SimRuntime.iOS-10-1)
  # iOS 10.2 (10.2 - 14C89) (com.apple.CoreSimulator.SimRuntime.iOS-10-2)
  # tvOS 10.1 (10.1 - 14U591) (com.apple.CoreSimulator.SimRuntime.tvOS-10-1)
  # watchOS 3.1 (3.1 - 14S471a) (com.apple.CoreSimulator.SimRuntime.watchOS-3-1)
  #
  # Xcode 9 changed the format
  # == Runtimes ==
  # iOS 11.0 (11.0 - 15A5361a) - com.apple.CoreSimulator.SimRuntime.iOS-11-0
  # tvOS 11.0 (11.0 - 15J5368a) - com.apple.CoreSimulator.SimRuntime.tvOS-11-0
  # watchOS 4.0 (4.0 - 15R5363a) - com.apple.CoreSimulator.SimRuntime.watchOS-4-0
  ios_versions_ids = filter_runtimes(all_runtime_type, 'iOS', ios_versions)
  tv_version_ids = filter_runtimes(all_runtime_type, 'tvOS')
  watch_versions_ids = filter_runtimes(all_runtime_type, 'watchOS')

  all_device_types = `xcrun simctl list devicetypes`.scan(/(.*)\s\((.*)\)/)
  # == Device Types ==
  # iPhone 4s (com.apple.CoreSimulator.SimDeviceType.iPhone-4s)
  # iPhone 5 (com.apple.CoreSimulator.SimDeviceType.iPhone-5)
  # iPhone 5s (com.apple.CoreSimulator.SimDeviceType.iPhone-5s)
  # iPhone 6 (com.apple.CoreSimulator.SimDeviceType.iPhone-6)
  all_device_types.each do |device_type|
    if device_type.join(' ').include?("Watch")
      create(device_type, watch_versions_ids, 'watchOS')
    elsif device_type.join(' ').include?("TV")
      create(device_type, tv_version_ids, 'tvOS')
    else
      create(device_type, ios_versions_ids)
    end
  end

  make_phone_watch_pair
end

.create(device_type, os_versions, os_name = 'iOS') ⇒ Object



69
70
71
72
73
74
75
76
# File 'snapshot/lib/snapshot/reset_simulators.rb', line 69

def self.create(device_type, os_versions, os_name = 'iOS')
  os_versions.each do |os_version|
    puts("Creating #{device_type[0]} for #{os_name} version #{os_version[0]}")
    command = "xcrun simctl create '#{device_type[0]}' #{device_type[1]} #{os_version[1]}"
    UI.command(command) if FastlaneCore::Globals.verbose?
    `#{command}`
  end
end

.device_line_usable?(line) ⇒ Boolean

Returns:



118
119
120
# File 'snapshot/lib/snapshot/reset_simulators.rb', line 118

def self.device_line_usable?(line)
  !line.include?("unavailable")
end

.devicesObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'snapshot/lib/snapshot/reset_simulators.rb', line 82

def self.devices
  all_devices = Helper.backticks('xcrun simctl list devices', print: FastlaneCore::Globals.verbose?)
  # == Devices ==
  # -- iOS 9.0 --
  #   iPhone 4s (32246EBC-33B0-47F9-B7BB-5C23C550DF29) (Shutdown)
  #   iPhone 5 (4B56C101-6B95-43D1-9485-3FBA0E127FFA) (Shutdown)
  #   iPhone 5s (6379C204-E82A-4FBD-8A22-6A01C7791D62) (Shutdown)
  # -- Unavailable: com.apple.CoreSimulator.SimRuntime.iOS-8-4 --
  #   iPhone 4s (FE9D6F85-1C51-4FE6-8597-FCAB5286B869) (Shutdown) (unavailable, runtime profile not found)

  result = all_devices.lines.map do |line|
    (line.match(/\s+(.+?)\s\(([\w\-]+)\).*/) || []).to_a
  end

  result.select { |parsed| parsed.length == 3 } # we don't care about those headers
end

.filter_runtimes(all_runtimes, os = 'iOS', versions = []) ⇒ Object



78
79
80
# File 'snapshot/lib/snapshot/reset_simulators.rb', line 78

def self.filter_runtimes(all_runtimes, os = 'iOS', versions = [])
  all_runtimes.select { |v, id| v[/^#{os}/] }.select { |v, id| v[/#{versions.join("|")}$/] }.uniq
end

.make_phone_watch_pairObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'snapshot/lib/snapshot/reset_simulators.rb', line 103

def self.make_phone_watch_pair
  phones = []
  watches = []
  devices.each do |device|
    full_line, name, id = device
    phones << id if name.start_with?('iPhone 6') && device_line_usable?(full_line)
    watches << id if name.end_with?('mm') && device_line_usable?(full_line)
  end

  if phones.any? && watches.any?
    puts("Creating device pair of #{phones.last} and #{watches.last}")
    Helper.backticks("xcrun simctl pair #{watches.last} #{phones.last}", print: FastlaneCore::Globals.verbose?)
  end
end

.runtimesObject



99
100
101
# File 'snapshot/lib/snapshot/reset_simulators.rb', line 99

def self.runtimes
  Helper.backticks('xcrun simctl list runtimes', print: FastlaneCore::Globals.verbose?).scan(/(.*)\s\(\d.*(com\.apple[^)\s]*)/)
end