Skip to content

Commit 958e43a

Browse files
committed
Auto detect selected simulator in xcode
1 parent 356aaa6 commit 958e43a

File tree

3 files changed

+61
-1
lines changed

3 files changed

+61
-1
lines changed

lib/run_loop/detect_aut/xcode.rb

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
12
module RunLoop
23
# @!visibility private
34
module DetectAUT
45
# @!visibility private
56
module Xcode
7+
require "etc"
68

79
# @!visibility private
810
def xcode_project?
@@ -38,6 +40,19 @@ def find_xcodeproj
3840
xcode_projects
3941
end
4042

43+
# @!visibility private
44+
def self.find_user_state_file
45+
username = RunLoop::Environment.username
46+
xcworkspace = RunLoop::Environment.xcodeproj
47+
unless xcworkspace.nil?
48+
xcworkspace = xcworkspace.gsub("xcodeproj", "xcworkspace")
49+
file = Dir.glob("#{xcworkspace}/xcuserdata/#{username}.xcuserdatad/UserInterfaceState.xcuserstate")
50+
if !file.nil? && file.is_a?(Array)
51+
return file[0]
52+
end
53+
end
54+
end
55+
4156
# @!visibility private
4257
def ignore_xcodeproj?(path)
4358
path[/CordovaLib/, 0] ||
@@ -151,6 +166,40 @@ def xcode_preferences_plist
151166
def pbuddy
152167
@pbuddy ||= RunLoop::PlistBuddy.new
153168
end
169+
170+
# @!visibility private
171+
# @param [String] file the plist to read
172+
# @return [String] the UDID of device
173+
def self.plist_find_device(file)
174+
#TODO unfortunately i can use ony this solution
175+
file_content = `/usr/libexec/PlistBuddy -c Print "#{file}"`
176+
.encode('UTF-8', 'binary', invalid: :replace, undef: :replace, replace: '')
177+
if !file_content.nil? && !file_content.empty?
178+
lines = file_content.split("\n")
179+
lines.detect do |line|
180+
line[/dvtdevice.*:/, 0]
181+
end
182+
end
183+
end
184+
185+
# @!visibility private
186+
def self.detect_selected_device
187+
file_name = find_user_state_file
188+
selected_device = plist_find_device(file_name)
189+
if selected_device != '' && !selected_device.nil?
190+
udid = selected_device.split(':')[1]
191+
selected_device = RunLoop::Device.device_with_identifier(udid)
192+
#TODO now only returning detected device if simulator detected
193+
if selected_device.simulator?
194+
RunLoop.log_info2("Detected simulator selected in Xcode is: #{selected_device}")
195+
RunLoop.log_info2("If this is not desired simulator, set the DEVICE_TARGET variable")
196+
selected_device
197+
198+
else
199+
nil
200+
end
201+
end
202+
end
154203
end
155204
end
156205
end

lib/run_loop/device.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,13 @@ def self.device_with_identifier(udid_or_name, options={})
144144
# @raise [ArgumentError] If DEVICE_TARGET or options specify an identifier
145145
# that does not match an iOS Simulator or physical device.
146146
def self.detect_device(options, xcode, simctl, instruments)
147-
device = self.device_from_opts_or_env(options)
147+
detected_device = RunLoop::DetectAUT::Xcode.detect_selected_device
148+
if detected_device.is_a?(RunLoop::Device)
149+
device = detected_device
150+
else
151+
device = self.device_from_opts_or_env(options)
152+
end
153+
148154

149155
# Passed an instance of RunLoop::Device
150156
return device if device && device.is_a?(RunLoop::Device)

lib/run_loop/environment.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,11 @@ def self.ci?
290290
].any?
291291
end
292292

293+
# Returns current username
294+
def self.username
295+
Etc.getpwuid.name
296+
end
297+
293298
# !@visibility private
294299
def self.with_debugging(debug, &block)
295300
if debug

0 commit comments

Comments
 (0)