Class: RunLoop::SimControl
- Inherits:
-
Object
- Object
- RunLoop::SimControl
- Defined in:
- lib/run_loop/sim_control.rb
Overview
‘puts` calls need to be replaced with proper logging
All command line tools are run in the context of ‘xcrun`.
One class interact with the iOS Simulators.
Throughout this class’s documentation, there are references to the _current version of Xcode_. The current Xcode version is the one returned by ‘xcrun xcodebuild`. The current Xcode version can be set using `xcode-select` or overridden using the `DEVELOPER_DIR`.
Class Method Summary collapse
-
.terminate_all_sims ⇒ Object
Terminates all simulators.
Instance Method Summary collapse
-
#launch_sim(opts = {}) ⇒ Object
If it is not already running, launch the simulator for the current version of Xcode.
-
#pbuddy ⇒ RunLoop::PlistBuddy
Return an instance of PlistBuddy.
-
#quit_sim(opts = {}) ⇒ Object
If it is running, quit the simulator for the current version of Xcode.
-
#relaunch_sim(opts = {}) ⇒ Object
Relaunch the simulator for the current version of Xcode.
-
#reset_sim_content_and_settings(opts = {}) ⇒ Object
Resets the simulator content and settings.
-
#sim_is_running? ⇒ Boolean
Is the simulator for the current version of Xcode running?.
-
#sim_udid?(udid) ⇒ Boolean
Is the arg a valid Xcode >= 6.0 simulator udid?.
- #simulators ⇒ Object
-
#xctools ⇒ RunLoop::XCTools
Returns an instance of XCTools.
Class Method Details
.terminate_all_sims ⇒ Object
Sends ‘kill -9` to all Simulator processes. Use sparingly or not at all.
Terminates all simulators.
SimulatorBridge launchd_sim ScriptAgent
There can be only one simulator running at a time. However, during gem testing, situations can arise where multiple simulators are active.
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 154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/run_loop/sim_control.rb', line 128 def self.terminate_all_sims # @todo Throwing SpringBoard crashed UI dialog. # Tried the gentle approach first; it did not work. # SimControl.new.quit_sim({:post_quit_wait => 0.5}) processes = ['iPhone Simulator.app', 'iOS Simulator.app', # Multiple launchd_sim processes have been causing problems. This # is a first pass at investigating what it would mean to kill the # launchd_sim process. 'launchd_sim' # RE: Throwing SpringBoard crashed UI dialog # These are children of launchd_sim. I tried quiting them # to suppress related UI dialogs about crashing processes. Killing # them can throw 'launchd_sim' UI Dialogs #'SimulatorBridge', 'SpringBoard', 'ScriptAgent', 'configd_sim', 'xpcproxy_sim' ] # @todo Maybe should try to send -TERM first and -KILL if TERM fails. # @todo Needs benchmarking. processes.each do |process_name| descripts = `xcrun ps x -o pid,command | grep "#{process_name}" | grep -v grep`.strip.split("\n") descripts.each do |process_desc| pid = process_desc.split(' ').first Open3.popen3("xcrun kill -9 #{pid} && xcrun wait #{pid}") do |_, stdout, stderr, _| if ENV['DEBUG_UNIX_CALLS'] == '1' out = stdout.read.strip err = stderr.read.strip next if out.to_s.empty? and err.to_s.empty? puts "kill process '#{pid}' => stdout: '#{out}' | stderr: '#{err}'" end end end end end |
Instance Method Details
#launch_sim(opts = {}) ⇒ Object
Consider migrating apple script call to xctools.
If it is not already running, launch the simulator for the current version of Xcode.
83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/run_loop/sim_control.rb', line 83 def launch_sim(opts={}) unless sim_is_running? default_opts = {:post_launch_wait => 2.0, :hide_after => false} merged_opts = default_opts.merge(opts) `xcrun open -a "#{sim_app_path}"` if merged_opts[:hide_after] `xcrun /usr/bin/osascript -e 'tell application "System Events" to keystroke "h" using command down'` end sleep(merged_opts[:post_launch_wait]) if merged_opts[:post_launch_wait] end end |
#pbuddy ⇒ RunLoop::PlistBuddy
Return an instance of PlistBuddy.
45 46 47 |
# File 'lib/run_loop/sim_control.rb', line 45 def pbuddy @pbuddy ||= RunLoop::PlistBuddy.new end |
#quit_sim(opts = {}) ⇒ Object
Consider migrating apple script call to xctools.
If it is running, quit the simulator for the current version of Xcode.
62 63 64 65 66 67 68 69 |
# File 'lib/run_loop/sim_control.rb', line 62 def quit_sim(opts={}) if sim_is_running? default_opts = {:post_quit_wait => 1.0 } merged_opts = default_opts.merge(opts) `echo 'application "#{sim_name}" quit' | xcrun osascript` sleep(merged_opts[:post_quit_wait]) if merged_opts[:post_quit_wait] end end |
#relaunch_sim(opts = {}) ⇒ Object
Relaunch the simulator for the current version of Xcode. If that simulator is already running, it is quit.
108 109 110 111 112 113 114 115 |
# File 'lib/run_loop/sim_control.rb', line 108 def relaunch_sim(opts={}) default_opts = {:post_quit_wait => 1.0, :post_launch_wait => 2.0, :hide_after => false} merged_opts = default_opts.merge(opts) quit_sim(merged_opts) launch_sim(merged_opts) end |
#reset_sim_content_and_settings(opts = {}) ⇒ Object
Resets the simulator content and settings.
In Xcode < 6, it is analogous to touching the menu item _for every
simulator_, regardless of SDK.
In Xcode 6, the default is the same; the content and settings for every
simulator is erased. However, in Xcode 6 it is possible to pass
a `:sim_udid` as a option to erase an individual simulator.
On Xcode 5, it works by deleting the following directories:
-
~/Library/Application Support/iPhone Simulator/Library
-
~/Library/Application Support/iPhone Simulator/Library/<sdk>
and relaunching the iOS Simulator which will recreate the Library directory and the latest SDK directory.
On Xcode 6, it uses the ‘simctl erase <udid>` command line tool.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/run_loop/sim_control.rb', line 200 def reset_sim_content_and_settings(opts={}) default_opts = {:post_quit_wait => 1.0, :post_launch_wait => 3.0, :hide_after => false, :sim_udid => nil} merged_opts = default_opts.merge(opts) quit_sim(merged_opts) # WARNING - DO NOT TRY TO DELETE Developer/CoreSimulator/Devices! # Very bad things will happen. Unlike Xcode < 6, the re-launching the # simulator will _not_ recreate the SDK (aka Devices) directories. if xcode_version_gte_6? simctl_reset(merged_opts[:sim_udid]) else sim_lib_path = File.join(sim_app_support_dir, 'Library') FileUtils.rm_rf(sim_lib_path) existing_sim_sdk_or_device_data_dirs.each do |dir| FileUtils.rm_rf(dir) end launch_sim(merged_opts) # This is tricky because we need to wait for the simulator to recreate # the directories. Specifically, we need the Accessibility plist to be # exist so subsequent calabash launches will be able to enable # accessibility. # # The directories take ~3.0 - ~5.0 to create. counter = 0 loop do break if counter == 80 dirs = existing_sim_sdk_or_device_data_dirs if dirs.count == 0 sleep(0.2) else break if dirs.all? { |dir| plist = File.("#{dir}/Library/Preferences/com.apple.Accessibility.plist") File.exist?(plist) } sleep(0.2) end counter = counter + 1 end end end |
#sim_is_running? ⇒ Boolean
Is the simulator for the current version of Xcode running?
51 52 53 |
# File 'lib/run_loop/sim_control.rb', line 51 def sim_is_running? not sim_pid.nil? end |
#sim_udid?(udid) ⇒ Boolean
Is the arg a valid Xcode >= 6.0 simulator udid?
310 311 312 |
# File 'lib/run_loop/sim_control.rb', line 310 def sim_udid?(udid) udid.length == 36 and udid[XCODE_6_SIM_UDID_REGEX,0] != nil end |
#simulators ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
# File 'lib/run_loop/sim_control.rb', line 314 def simulators unless xcode_version_gte_51? raise RuntimeError, 'simctl is only available on Xcode >= 6' end if xcode_version_gte_6? hash = simctl_list :devices sims = [] hash.each_pair do |sdk, list| list.each do |details| sims << RunLoop::Device.new(details[:name], sdk, details[:udid]) end end sims else raise NotImplementedError, 'the simulators method is not available yet for Xcode 5.1.1' end end |
#xctools ⇒ RunLoop::XCTools
Returns an instance of XCTools.
19 20 21 |
# File 'lib/run_loop/sim_control.rb', line 19 def xctools @xctools ||= RunLoop::XCTools.new end |