@@ -28,69 +28,87 @@ jobs:
2828 run : |
2929 set -euo pipefail
3030
31- # 런타임 키를 오름차순으로 정렬하고, iPhone 시뮬레이터가 있으면 선택
32- RESULT=$(python3 - <<'PY'
33- import json, subprocess, sys
34-
35- devices = json.loads(subprocess.check_output(["xcrun", "simctl", "list", "devices", "-j"], text=True)).get("devices", {})
36-
37- def is_available(d):
38- return d.get("isAvailable") or d.get("availability") == "(available)"
39-
40- def parse_runtime(runtime_key):
41- # com.apple.CoreSimulator.SimRuntime.iOS-17-2 -> ([17, 2], "17.2")
42- if "iOS-" not in runtime_key:
43- return None
44- parts = runtime_key.split("iOS-")[-1].split("-")
45- try:
46- nums = [int(p) for p in parts]
47- except ValueError:
48- return None
49- return nums, ".".join(str(n) for n in nums)
50-
51- runtimes = []
52- for key in devices.keys():
53- parsed = parse_runtime(key)
54- if parsed is None:
55- continue
56- nums, ver = parsed
57- runtimes.append((nums, ver, key))
58-
59- for _, ver, key in sorted(runtimes, key=lambda x: x[0]):
60- for d in devices.get(key, []):
61- if is_available(d) and "iPhone" in d.get("name", ""):
62- print(f"{ver}|{d['name']}")
63- sys.exit(0)
64-
65- sys.exit(1)
31+ # iPhone 시뮬레이터가 존재하는 런타임 중 가장 낮은 버전 선택 (패치 버전 포함)
32+ chosen=$(python3 - <<'PY'
33+ import json, re, subprocess
34+
35+ runtime_text = subprocess.check_output(["xcrun", "simctl", "runtime", "list"], text=True, stderr=subprocess.DEVNULL)
36+ versions = re.findall(r'iOS ([0-9]+(?:\.[0-9]+)*)', runtime_text)
37+ def ver_key(v):
38+ return [int(x) for x in v.split(".")]
39+ versions = sorted(set(versions), key=ver_key)
40+
41+ devices = json.loads(subprocess.check_output(["xcrun", "simctl", "list", "devices", "available", "-j"], text=True)).get("devices", {})
42+
43+ def has_iphone(version):
44+ prefix = f"com.apple.CoreSimulator.SimRuntime.iOS-{version.replace('.', '-') }"
45+ for runtime, devs in devices.items():
46+ if not runtime.startswith(prefix):
47+ continue
48+ for d in devs:
49+ if d.get("isAvailable") and "iPhone" in d.get("name", ""):
50+ return True
51+ return False
52+
53+ for v in versions:
54+ if has_iphone(v):
55+ print(v)
56+ raise SystemExit(0)
57+
58+ raise SystemExit(1)
6659 PY
67- )
60+ )
6861
69- if [ -z "${RESULT :-}" ]; then
70- echo "No iPhone simulator devices detected." >&2
62+ if [ -z "${chosen :-}" ]; then
63+ echo "No iPhone simulator runtimes detected." >&2
7164 exit 1
7265 fi
7366
74- IFS='|' read -r IOS_VER DEVICE_NAME <<< "$RESULT "
67+ echo "Chosen iOS runtime version (iPhone): $chosen "
7568
76- echo "Chosen iOS runtime version (iPhone): $IOS_VER"
77- echo "Chosen simulator: $DEVICE_NAME"
78-
79- echo "ios_version=$IOS_VER" >> "$GITHUB_OUTPUT"
80- echo "device_name=$DEVICE_NAME" >> "$GITHUB_OUTPUT"
69+ echo "ios_version=$chosen" >> "$GITHUB_OUTPUT"
8170
8271 - name : Build
8372 shell : bash
8473 run : |
8574 set -euo pipefail
8675 set -x
8776 IOS_VER="${{ steps.pick_ios.outputs.ios_version }}"
88- DEVICE_NAME="${{ steps.pick_ios.outputs.device_name }}"
77+ export IOS_VER
8978
9079 xcodebuild -version
9180
9281 echo "Using scheme: $SCHEME"
9382
83+ # 선택된 iOS 런타임에서 'iPhone'이 포함된 첫 번째 시뮬레이터 이름 선택 (JSON 파싱)
84+ DEVICE_INFO=$(python3 - <<'PY'
85+ import json, os, subprocess, sys
86+
87+ ios_ver = os.environ['IOS_VER']
88+ prefix = f"com.apple.CoreSimulator.SimRuntime.iOS-{ios_ver.replace('.', '-') }"
89+ data = subprocess.check_output(["xcrun", "simctl", "list", "devices", "available", "-j"], text=True)
90+ j = json.loads(data)
91+ for runtime, devices in j.get("devices", {}).items():
92+ if not runtime.startswith(prefix):
93+ continue
94+ for d in devices:
95+ if d.get("isAvailable") and "iPhone" in d.get("name", ""):
96+ os_ver = runtime.split("iOS-")[-1].replace("-", ".")
97+ print(f"{d['name']}|{os_ver}")
98+ sys.exit(0)
99+ sys.exit(1)
100+ PY
101+ )
102+
103+ IFS='|' read -r DEVICE_NAME DEVICE_OS <<< "$DEVICE_INFO"
104+ IOS_VER="$DEVICE_OS"
105+ export IOS_VER
106+
107+ if [ -z "${DEVICE_NAME:-}" ] || [ -z "${IOS_VER:-}" ]; then
108+ echo "No available simulator device found for iOS ${IOS_VER}." >&2
109+ exit 1
110+ fi
111+
94112 echo "Using simulator: $DEVICE_NAME (iOS ${IOS_VER})"
95113
96114 # 예시: 시뮬레이터 대상으로 빌드/테스트 시 destination에서 OS 지정
@@ -101,4 +119,4 @@ jobs:
101119 -destination "platform=iOS Simulator,OS=${IOS_VER},name=${DEVICE_NAME}" \
102120 -showBuildTimingSummary \
103121 build \
104- | cat
122+ | cat
0 commit comments