Skip to content

Commit

Permalink
Auto detect selected simulator in xcode
Browse files Browse the repository at this point in the history
  • Loading branch information
ark-konopacki committed Oct 7, 2016
1 parent 356aaa6 commit 958e43a
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
49 changes: 49 additions & 0 deletions lib/run_loop/detect_aut/xcode.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@

module RunLoop
# @!visibility private
module DetectAUT
# @!visibility private
module Xcode
require "etc"

# @!visibility private
def xcode_project?
Expand Down Expand Up @@ -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] ||
Expand Down Expand Up @@ -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
Expand Down
8 changes: 7 additions & 1 deletion lib/run_loop/device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions lib/run_loop/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,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
Expand Down

0 comments on commit 958e43a

Please sign in to comment.