forked from nsoperations/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Simulator.swift
89 lines (79 loc) · 3.72 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
82
83
84
85
86
87
88
89
import Foundation
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.
static func selectAvailableSimulator(of sdk: SDK, from data: Data, validIdentifiers: Set<String>? = nil) -> 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
}
var allTargetSimulators = devices.reduce(into: [:], reducePlatformNames)
func sortedByVersion(_ osNames: [String]) -> [String] {
return osNames.sorted { lhs, rhs in
guard let lhsVersion = PinnedVersion(lhs).semanticVersion,
let rhsVersion = PinnedVersion(rhs).semanticVersion else {
return lhs < rhs
}
return lhsVersion < rhsVersion
}
}
if let validIdentifiers = validIdentifiers {
for (key, array) in allTargetSimulators {
allTargetSimulators[key] = array.filter { simulator -> Bool in
return validIdentifiers.contains(simulator.udid.description)
}
}
}
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.
static 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)"
}
}