diff --git a/lib/run_loop/detect_aut/xcode.rb b/lib/run_loop/detect_aut/xcode.rb index 1e7e4511..e5662aac 100644 --- a/lib/run_loop/detect_aut/xcode.rb +++ b/lib/run_loop/detect_aut/xcode.rb @@ -1,8 +1,10 @@ + module RunLoop # @!visibility private module DetectAUT # @!visibility private module Xcode + require "etc" # @!visibility private def xcode_project? @@ -38,6 +40,19 @@ def find_xcodeproj xcode_projects end + # @!visibility private + def self.find_user_state_file + username = RunLoop::Environment.username + xcworkspace = RunLoop::Environment.xcodeproj + unless xcworkspace.nil? + xcworkspace = xcworkspace.gsub("xcodeproj", "xcworkspace") + file = Dir.glob("#{xcworkspace}/xcuserdata/#{username}.xcuserdatad/UserInterfaceState.xcuserstate") + if !file.nil? && file.is_a?(Array) + return file[0] + end + end + end + # @!visibility private def ignore_xcodeproj?(path) path[/CordovaLib/, 0] || @@ -151,6 +166,40 @@ def xcode_preferences_plist def pbuddy @pbuddy ||= RunLoop::PlistBuddy.new end + + # @!visibility private + # @param [String] file the plist to read + # @return [String] the UDID of device + def self.plist_find_device(file) + #TODO unfortunately i can use ony this solution + file_content = `/usr/libexec/PlistBuddy -c Print "#{file}"` + .encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '') + if !file_content.nil? && !file_content.empty? + lines = file_content.split("\n") + lines.detect do |line| + line[/dvtdevice.*:/, 0] + end + end + end + + # @!visibility private + def self.detect_selected_device + file_name = find_user_state_file + selected_device = plist_find_device(file_name) + if selected_device != '' && !selected_device.nil? + udid = selected_device.split(':')[1] + selected_device = RunLoop::Device.device_with_identifier(udid) + #TODO now only returning detected device if simulator detected + if selected_device.simulator? + RunLoop.log_info2("Detected simulator selected in Xcode is: #{selected_device}") + RunLoop.log_info2("If this is not desired simulator, set the DEVICE_TARGET variable") + selected_device + + else + nil + end + end + end end end end diff --git a/lib/run_loop/device.rb b/lib/run_loop/device.rb index d83c469e..26348644 100644 --- a/lib/run_loop/device.rb +++ b/lib/run_loop/device.rb @@ -144,7 +144,13 @@ def self.device_with_identifier(udid_or_name, options={}) # @raise [ArgumentError] If DEVICE_TARGET or options specify an identifier # that does not match an iOS Simulator or physical device. def self.detect_device(options, xcode, simctl, instruments) - device = self.device_from_opts_or_env(options) + detected_device = RunLoop::DetectAUT::Xcode.detect_selected_device + if detected_device.is_a?(RunLoop::Device) + device = detected_device + else + device = self.device_from_opts_or_env(options) + end + # Passed an instance of RunLoop::Device return device if device && device.is_a?(RunLoop::Device) diff --git a/lib/run_loop/environment.rb b/lib/run_loop/environment.rb index 219dafea..e5ce1412 100644 --- a/lib/run_loop/environment.rb +++ b/lib/run_loop/environment.rb @@ -300,6 +300,11 @@ def self.ci? ].any? end + # Returns current username + def self.username + Etc.getpwuid.name + end + # !@visibility private def self.with_debugging(debug, &block) if debug