@@ -34,19 +34,36 @@ def find_ios_simulator
3434end
3535
3636# 辅助方法:获取构建 destination
37- def get_build_destination ( boot_simulator : false )
37+ def get_build_destination ( boot_simulator : false , prefer_generic : false )
38+ # 如果偏好使用通用 destination,直接返回
39+ if prefer_generic
40+ UI . important ( "Using generic iOS Simulator destination" )
41+ return "platform=iOS Simulator"
42+ end
43+
3844 simulator = find_ios_simulator
3945
4046 if simulator
4147 if boot_simulator
4248 UI . message ( "Booting simulator: #{ simulator [ :name ] } (#{ simulator [ :udid ] } )" )
43- `xcrun simctl boot "#{ simulator [ :udid ] } " 2>&1`
49+ boot_result = `xcrun simctl boot "#{ simulator [ :udid ] } " 2>&1`
50+ unless $?. success?
51+ UI . important ( "Failed to boot simulator, may already be booted: #{ boot_result } " )
52+ end
4453 sleep ( 5 )
4554 end
46- "platform=iOS Simulator,id=#{ simulator [ :udid ] } "
55+
56+ # 验证模拟器是否存在
57+ device_list = `xcrun simctl list devices 2>&1`
58+ if device_list . include? ( simulator [ :udid ] )
59+ "platform=iOS Simulator,id=#{ simulator [ :udid ] } "
60+ else
61+ UI . important ( "Simulator #{ simulator [ :udid ] } not found, using name-based destination" )
62+ "platform=iOS Simulator,name=#{ simulator [ :name ] } "
63+ end
4764 else
4865 UI . important ( "No available iPhone simulator found, using generic iOS Simulator" )
49- "platform=iOS Simulator,name=iPhone 15 Pro "
66+ "platform=iOS Simulator"
5067 end
5168end
5269
@@ -79,10 +96,37 @@ platform :ios do
7996 if !has_test_action
8097 UI . important ( "No test action configured in scheme, skipping tests and just building the project" )
8198
82- destination = get_build_destination ( boot_simulator : false )
99+ # 在 CI 环境中,优先使用通用 destination 以避免模拟器 ID 问题
100+ is_ci = ENV [ "CI" ] == "true" || ENV [ "GITHUB_ACTIONS" ] == "true"
101+ destination = get_build_destination ( boot_simulator : false , prefer_generic : is_ci )
83102
84103 Dir . chdir ( project_root ) do
85- sh ( "xcodebuild clean build -project Smth.xcodeproj -scheme Smth -destination '#{ destination } ' -sdk iphonesimulator -skipPackagePluginValidation" )
104+ # 尝试构建,如果失败则使用更通用的 destination
105+ build_success = false
106+ attempts = [
107+ destination ,
108+ "platform=iOS Simulator" ,
109+ "generic/platform=iOS Simulator"
110+ ] . uniq
111+
112+ attempts . each_with_index do |dest , index |
113+ begin
114+ UI . message ( "Build attempt #{ index + 1 } /#{ attempts . length } with destination: #{ dest } " )
115+ sh ( "xcodebuild clean build -project Smth.xcodeproj -scheme Smth -destination '#{ dest } ' -sdk iphonesimulator -skipPackagePluginValidation" )
116+ build_success = true
117+ break
118+ rescue => ex
119+ if index < attempts . length - 1
120+ UI . important ( "Build failed with destination '#{ dest } ', trying next: #{ ex . message } " )
121+ else
122+ UI . user_error! ( "All build attempts failed. Last error: #{ ex . message } " )
123+ end
124+ end
125+ end
126+
127+ unless build_success
128+ UI . user_error! ( "Failed to build project after #{ attempts . length } attempts" )
129+ end
86130 end
87131
88132 UI . success ( "Build completed successfully (no tests to run)" )
0 commit comments