forked from Carthage/Carthage
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulator.swift
More file actions
57 lines (53 loc) · 2.06 KB
/
Copy pathSimulator.swift
File metadata and controls
57 lines (53 loc) · 2.06 KB
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
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)
// To Xcode 10.0, Return values of `xcrun simctl list devices --json` contains `availability` field.
// Its value is possible to be `(available)` or `(unavailable)`.
// Since Xcode 10.1, `availability` field is obsolated.
// Using `isAvailable` instead. its value is possible to be `YES` or `NO`.
let availability = try container.decodeIfPresent(String.self, forKey: .availability)
let isAvailable = try container.decodeIfPresent(String.self, forKey: .isAvailable)
self.isAvailable = isAvailable == "YES" || availability == "(available)"
}
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
let allTargetSimulators = devices
.filter { $0.key.hasPrefix(platformName) }
func sortedByVersion(_ osNames: [String]) -> [String] {
return osNames.sorted { lhs, rhs in
guard let lhsVersion = SemanticVersion.from(PinnedVersion(lhs)).value,
let rhsVersion = SemanticVersion.from(PinnedVersion(rhs)).value else {
return lhs < rhs
}
return lhsVersion < rhsVersion
}
}
guard let latestOSName = sortedByVersion(Array(allTargetSimulators.keys)).last else {
return nil
}
return devices[latestOSName]?
.first { $0.isAvailable }
}