reset-simulators.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #!/usr/bin/ruby
  2. require 'json'
  3. def platform_for_runtime(runtime)
  4. runtime['identifier'].gsub(/com.apple.CoreSimulator.SimRuntime.([^-]+)-.*/, '\1')
  5. end
  6. def platform_for_device_type(device_type)
  7. case device_type['identifier']
  8. when /Watch/
  9. 'watchOS'
  10. when /TV/
  11. 'tvOS'
  12. else
  13. 'iOS'
  14. end
  15. end
  16. def wait_for_core_simulator_service
  17. # Run until we get a result since switching simulator versions often causes CoreSimulatorService to throw an exception.
  18. while `xcrun simctl list devices`.empty?
  19. end
  20. end
  21. def running_devices(devices)
  22. devices.select { |device| device['state'] != 'Shutdown' }
  23. end
  24. def shutdown_simulator_devices(devices)
  25. # Shut down any simulators that need it.
  26. running_devices(devices).each do |device|
  27. puts "Shutting down simulator #{device['udid']}"
  28. system("xcrun simctl shutdown #{device['udid']}") or puts " Failed to shut down simulator #{device['udid']}"
  29. end
  30. end
  31. attempts = 0
  32. begin
  33. # Kill all the current simulator processes as they may be from a different Xcode version
  34. print 'Killing running Simulator processes...'
  35. while system('pgrep -q Simulator')
  36. system('pkill Simulator 2>/dev/null')
  37. # CoreSimulatorService doesn't exit when sent SIGTERM
  38. system('pkill -9 Simulator 2>/dev/null')
  39. end
  40. puts ' done!'
  41. wait_for_core_simulator_service
  42. # Shut down any running simulator devices. This may take multiple attempts if some
  43. # simulators are currently in the process of booting or being created.
  44. all_available_devices = []
  45. (0..5).each do |shutdown_attempt|
  46. begin
  47. devices_json = `xcrun simctl list devices -j`
  48. all_devices = JSON.parse(devices_json)['devices'].flat_map { |_, devices| devices }
  49. rescue JSON::ParserError
  50. sleep shutdown_attempt if shutdown_attempt > 0
  51. next
  52. end
  53. # Exclude devices marked as unavailable as they're from a different version of Xcode.
  54. all_available_devices = all_devices.reject { |device| device['availability'] =~ /unavailable/ }
  55. break if running_devices(all_available_devices).empty?
  56. shutdown_simulator_devices all_available_devices
  57. sleep shutdown_attempt if shutdown_attempt > 0
  58. end
  59. # Delete all simulators.
  60. print 'Deleting all simulators...'
  61. all_available_devices.each do |device|
  62. system("xcrun simctl delete #{device['udid']}") or raise "Failed to delete simulator #{device['udid']}"
  63. end
  64. puts ' done!'
  65. # Recreate all simulators.
  66. runtimes = JSON.parse(`xcrun simctl list runtimes -j`)['runtimes']
  67. device_types = JSON.parse(`xcrun simctl list devicetypes -j`)['devicetypes']
  68. runtimes_by_platform = Hash.new { |hash, key| hash[key] = [] }
  69. runtimes.each do |runtime|
  70. next unless runtime['availability'] == '(available)'
  71. runtimes_by_platform[platform_for_runtime(runtime)] << runtime
  72. end
  73. print 'Creating fresh simulators...'
  74. device_types.each do |device_type|
  75. platform = platform_for_device_type(device_type)
  76. runtimes_by_platform[platform].each do |runtime|
  77. output = `xcrun simctl create '#{device_type['name']}' '#{device_type['identifier']}' '#{runtime['identifier']}' 2>&1`
  78. next if $? == 0
  79. # Error code 161 and 162 indicate that the given device is not supported by the runtime, such as the iPad 2 and
  80. # iPhone 4s not being supported by the iOS 10 simulator runtime.
  81. next if output =~ /(domain=com.apple.CoreSimulator.SimError, code=16[12])/
  82. puts "Failed to create device of type #{device_type['identifier']} with runtime #{runtime['identifier']}:"
  83. output.each_line do |line|
  84. puts " #{line}"
  85. end
  86. end
  87. end
  88. puts ' done!'
  89. rescue => e
  90. if (attempts += 1) < 5
  91. puts ''
  92. puts e.message
  93. e.backtrace.each { |line| puts line }
  94. puts ''
  95. puts 'Retrying...'
  96. retry
  97. end
  98. system('ps auxwww')
  99. system('xcrun simctl list')
  100. raise
  101. end