forked from Carthage/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulator.swift
81 lines (73 loc) · 2.9 KB
/
Simulator.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import Foundation
import SPMUtility
import XCDBLD
internal struct Simulator: Decodable {
enum CodingKeys: String, CodingKey {
case name
case udid
case isAvailable
case availability
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
name = try container.decode(String.self, forKey: .name)
udid = try container.decode(UUID.self, forKey: .udid)
if let isAvailable = try? container.decode(Bool.self, forKey: .isAvailable) {
// Xcode 10.1 ~
self.isAvailable = isAvailable
} else if let availability = try container.decodeIfPresent(String.self, forKey: .availability), availability == "(available)" {
// <= Xcode 10.0
self.isAvailable = true
} else if let isAvailable = try container.decodeIfPresent(String.self, forKey: .isAvailable), isAvailable == "YES" {
// Xcode 10.1 beta
self.isAvailable = true
} else {
self.isAvailable = false
}
}
var isAvailable: Bool
var name: String
var udid: UUID
}
/// Select available simulator from output value of `simclt devices list`
/// If there are multiple OSs for the SDK, the latest one would be selected.
internal func selectAvailableSimulator(of sdk: SDK, from data: Data) -> Simulator? {
let decoder = JSONDecoder()
// simctl returns following JSON:
// {"devices": {"iOS 12.0": [<simulators...>]}]
guard let jsonObject = try? decoder.decode([String: [String: [Simulator]]].self, from: data),
let devices = jsonObject["devices"] else {
return nil
}
let platformName = sdk.platform.rawValue
func reducePlatformNames(_ result: inout [String: [Simulator]], _ entry: (key: String, value: [Simulator])) {
guard let platformVersion = parsePlatformVersion(for: platformName, from: entry.key) else { return }
guard entry.value.contains(where: { $0.isAvailable }) else { return }
result[platformVersion] = entry.value
}
let allTargetSimulators = devices.reduce(into: [:], reducePlatformNames)
func sortedByVersion(_ osNames: [String]) -> [String] {
return osNames.sorted { lhs, rhs in
guard let lhsVersion = Version.from(PinnedVersion(lhs)).value,
let rhsVersion = Version.from(PinnedVersion(rhs)).value else {
return lhs < rhs
}
return lhsVersion < rhsVersion
}
}
guard let latestOSName = sortedByVersion(Array(allTargetSimulators.keys)).last else {
return nil
}
return allTargetSimulators[latestOSName]?
.first { $0.isAvailable }
}
/// Parses a matching platform and version from a given identifier.
internal func parsePlatformVersion(for platformName: String, from identifier: String) -> String? {
guard let platformRange = identifier.range(of: platformName) else { return nil }
let nonDigitCharacters = CharacterSet.decimalDigits.inverted
let version = identifier
.suffix(from: platformRange.upperBound)
.split(whereSeparator: { $0.unicodeScalars.contains(where: { nonDigitCharacters.contains($0) }) })
.joined(separator: ".")
return "\(platformName) \(version)"
}