-
Notifications
You must be signed in to change notification settings - Fork 3
/
ResolvedPackage.swift
119 lines (103 loc) · 3.64 KB
/
ResolvedPackage.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import Foundation
import Releases
import ShellOut
struct ResolvedVersion: Decodable {
enum CodingKeys: String, CodingKey {
case branch
case revision
case version
}
let branch: String?
let revision: String
let version: Version?
public var versionNumberOrRevision: String {
if let version = version {
return "\(version)"
} else {
return "\(revision)"
}
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
branch = try container.decode(String?.self, forKey: .branch)
revision = try container.decode(String.self, forKey: .revision)
if let versionString = try container.decode(String?.self, forKey: .version) {
version = try Version(string: versionString)
} else {
version = nil
}
}
}
struct ResolvedDependency: Decodable {
enum CodingKeys: String, CodingKey {
case name = "package"
case url = "repositoryURL"
case version = "state"
}
let name: String
let url: URL
let version: ResolvedVersion
}
private struct Wrapper: Decodable {
let object: ResolvedPackage
}
enum ResolvedPackageError: Error, Equatable {
case resolvingFailed(String)
case readingFailed(String)
case parsingFailed(String, String)
}
struct ResolvedPackage: Decodable {
enum CodingKeys: String, CodingKey {
case dependencies = "pins"
}
let dependencies: [ResolvedDependency]
static func resolveAndLoadResolvedPackage(from folder: URL) throws -> ResolvedPackage {
do {
try shellOut(to: "swift", arguments: ["package", "resolve", "--package-path", "\"\(folder.path)\"" ])
} catch {
let error = error as! ShellOutError // swiftlint:disable:this force_cast
throw ResolvedPackageError.resolvingFailed(error.message)
}
return try loadResolvedPackage(from: folder)
}
static func loadResolvedPackage(from folder: URL) throws -> ResolvedPackage {
let data = try readResolvedPackageData(from: folder)
let decoder = JSONDecoder()
do {
let resolvedPackage = try decoder.decode(Wrapper.self, from: data).object
return resolvedPackage
} catch {
throw ResolvedPackageError.parsingFailed(error.localizedDescription, String(decoding: data, as: UTF8.self))
}
}
private static func readResolvedPackageData(from folder: URL) throws -> Data {
let resolvedPackage = folder.appendingPathComponent("Package.resolved", isDirectory: false)
do {
let contents = try Data(contentsOf: resolvedPackage)
return contents
} catch {
throw ResolvedPackageError.readingFailed(error.localizedDescription)
}
}
}
extension ResolvedPackageError: LocalizedError {
public var errorDescription: String? {
switch self {
case let .resolvingFailed(error):
return "Running swift package resolved failed: \(error)"
case let .readingFailed(error):
return "Could not read Package.resolved file: \(error)"
case let .parsingFailed(error, packageData):
return "Could not parse package data: \(error)\n\nPackage Data: \(packageData)"
}
}
}
extension ResolvedVersion: CustomStringConvertible {
public var description: String {
if let version = version {
return "\(version) (\(revision)\(branch != nil ? ", branch: \(branch!)" : ""))"
} else {
return "\(revision)\(branch != nil ? " (branch: \(branch!))" : "")"
}
}
}