-
Notifications
You must be signed in to change notification settings - Fork 172
Expand file tree
/
Copy pathModels.swift
More file actions
99 lines (82 loc) · 3.4 KB
/
Models.swift
File metadata and controls
99 lines (82 loc) · 3.4 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
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
90
91
92
93
94
95
96
97
98
99
import Foundation
import Path
import Version
public struct InstalledXcode: Equatable {
public let path: Path
/// Composed of the bundle short version from Info.plist and the product build version from version.plist
public let version: Version
init(path: Path, version: Version) {
self.path = path
self.version = version
}
public init?(path: Path) {
self.path = path
let infoPlistPath = path.join("Contents").join("Info.plist")
let versionPlistPath = path.join("Contents").join("version.plist")
guard
let infoPlistData = Current.files.contents(atPath: infoPlistPath.string),
let infoPlist = try? PropertyListDecoder().decode(InfoPlist.self, from: infoPlistData),
let bundleShortVersion = infoPlist.bundleShortVersion,
let bundleVersion = Version(tolerant: bundleShortVersion),
let versionPlistData = Current.files.contents(atPath: versionPlistPath.string),
let versionPlist = try? PropertyListDecoder().decode(VersionPlist.self, from: versionPlistData)
else { return nil }
// Installed betas don't include the beta number anywhere, so try to parse it from the filename or fall back to simply "beta"
var prereleaseIdentifiers = bundleVersion.prereleaseIdentifiers
if let filenameVersion = Version(path.basename(dropExtension: true).replacingOccurrences(of: "Xcode-", with: "")) {
prereleaseIdentifiers = filenameVersion.prereleaseIdentifiers
}
else if infoPlist.bundleIconName == "XcodeBeta", !prereleaseIdentifiers.contains("beta") {
prereleaseIdentifiers = ["beta"]
}
self.version = Version(major: bundleVersion.major,
minor: bundleVersion.minor,
patch: bundleVersion.patch,
prereleaseIdentifiers: prereleaseIdentifiers,
buildMetadataIdentifiers: [versionPlist.productBuildVersion].compactMap { $0 })
}
}
public struct Xcode: Codable, Equatable {
public let version: Version
public let url: URL
public let filename: String
public let releaseDate: Date?
public let architectures: [String]?
public var downloadPath: String {
return url.path
}
public init(version: Version, url: URL, filename: String, releaseDate: Date?, architectures: [String]? = nil) {
self.version = version
self.url = url
self.filename = filename
self.releaseDate = releaseDate
self.architectures = architectures
}
}
struct Downloads: Codable {
let downloads: [Download]
}
public struct Download: Codable {
public let name: String
public let files: [File]
public let dateModified: Date
public struct File: Codable {
public let remotePath: String
}
}
public struct InfoPlist: Decodable {
public let bundleID: String?
public let bundleShortVersion: String?
public let bundleIconName: String?
public enum CodingKeys: String, CodingKey {
case bundleID = "CFBundleIdentifier"
case bundleShortVersion = "CFBundleShortVersionString"
case bundleIconName = "CFBundleIconName"
}
}
public struct VersionPlist: Decodable {
public let productBuildVersion: String
public enum CodingKeys: String, CodingKey {
case productBuildVersion = "ProductBuildVersion"
}
}