-
Notifications
You must be signed in to change notification settings - Fork 3
/
PackageDescription.swift
215 lines (179 loc) · 6.56 KB
/
PackageDescription.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import Foundation
import Releases
import ShellOut
protocol PackageDependency: Decodable {
var name: String { get }
var requirement: DependencyRequirement { get }
var url: URL { get }
}
private struct PackageDependencyV54: PackageDependency {
let name: String
let requirement: DependencyRequirement
let url: URL
}
private struct PackageDependencyV55: PackageDependency {
enum CodingKeys: String, CodingKey {
case name = "identity"
case requirement
case url = "location"
}
let name: String
let requirement: DependencyRequirement
let url: URL
}
private struct PackageDependencyV56: PackageDependency {
enum CodingKeys: String, CodingKey {
case name = "identity"
case requirement
case location
}
let name: String
let requirement: DependencyRequirement
let location: PackageLocation
var url: URL {
location.remote.first!
}
}
private struct PackageLocation: Decodable {
let remote: [URL]
}
enum DependencyRequirement: Decodable, Equatable {
case exact(version: Version)
case revision(revision: String)
case branch(name: String)
case range(lowerBound: Version, upperBound: Version)
enum CodingKeys: String, CodingKey {
case exact
case revision
case branch
case range
}
enum RangeCodingKeys: String, CodingKey {
case lowerBound
case upperBound
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
let key = container.allKeys.first
switch key {
case .exact:
var nestedUnkeyedContainer = try container.nestedUnkeyedContainer(forKey: .exact)
let versionString = try nestedUnkeyedContainer.decode(String.self)
let version = try Version(string: versionString)
self = .exact(version: version)
case .revision:
var nestedUnkeyedContainer = try container.nestedUnkeyedContainer(forKey: .revision)
let revision = try nestedUnkeyedContainer.decode(String.self)
self = .revision(revision: revision)
case .branch:
var nestedUnkeyedContainer = try container.nestedUnkeyedContainer(forKey: .branch)
let name = try nestedUnkeyedContainer.decode(String.self)
self = .branch(name: name)
case .range:
var nestedUnkeyedContainer = try container.nestedUnkeyedContainer(forKey: .range)
let nestedContainer = try nestedUnkeyedContainer.nestedContainer(keyedBy: RangeCodingKeys.self)
let lowerBoundString = try nestedContainer.decode(String.self, forKey: RangeCodingKeys.lowerBound)
let upperBoundString = try nestedContainer.decode(String.self, forKey: RangeCodingKeys.upperBound)
let lowerBound = try Version(string: lowerBoundString)
let upperBound = try Version(string: upperBoundString)
self = .range(lowerBound: lowerBound, upperBound: upperBound)
default:
throw DecodingError.dataCorrupted(
DecodingError.Context(
codingPath: container.codingPath,
debugDescription: "Unabled to decode enum"
)
)
}
}
}
enum PackageDescriptionError: Error, Equatable {
case loadingFailed(String)
case parsingFailed(String, String)
}
struct PackageDescriptionV54: PackageDescription {
enum CodingKeys: String, CodingKey {
case dependenciesArray = "dependencies"
}
private let dependenciesArray: [PackageDependencyV54]
var dependencies: [PackageDependency] {
dependenciesArray
}
}
struct PackageDescriptionV55: PackageDescription {
enum CodingKeys: String, CodingKey {
case dependencyMap = "dependencies"
}
private let dependencyMap: [[String: [PackageDependencyV55]]]
var dependencies: [PackageDependency] {
dependencyMap.flatMap { $0.values.flatMap { $0 } }
}
}
struct PackageDescriptionV56: PackageDescription {
enum CodingKeys: String, CodingKey {
case dependencyMap = "dependencies"
}
private let dependencyMap: [[String: [PackageDependencyV56]]]
var dependencies: [PackageDependency] {
dependencyMap.flatMap { $0.values.flatMap { $0 } }
}
}
enum PackageDescriptionFactory {
static func loadPackageDescription(from folder: URL) throws -> PackageDescription {
let json = try readPackageDescription(from: folder)
let data = json.data(using: .utf8)!
let decoder = JSONDecoder()
do {
let packageDescription = try decoder.decode(PackageDescriptionV56.self, from: data)
return packageDescription
} catch {
do {
let packageDescription = try decoder.decode(PackageDescriptionV55.self, from: data)
return packageDescription
} catch {
do {
let packageDescription = try decoder.decode(PackageDescriptionV54.self, from: data)
return packageDescription
} catch {
throw PackageDescriptionError.parsingFailed(String(describing: error), json)
}
}
}
}
private static func readPackageDescription(from folder: URL) throws -> String {
do {
let output = try shellOut(to: "swift", arguments: ["package", "dump-package", "--package-path", "\"\(folder.path)\"" ])
return output
} catch {
let error = error as! ShellOutError // swiftlint:disable:this force_cast
throw PackageDescriptionError.loadingFailed(error.message)
}
}
}
protocol PackageDescription: Decodable {
var dependencies: [PackageDependency] { get }
}
extension PackageDescriptionError: LocalizedError {
public var errorDescription: String? {
switch self {
case let .loadingFailed(error):
return "Could not get package data, swift package dump-package failed: \(error)"
case let .parsingFailed(error, packageData):
return "Could not parse package data: \(error)\n\nPackage Data: \(packageData)"
}
}
}
extension DependencyRequirement: CustomStringConvertible {
public var description: String {
switch self {
case let .exact(version):
return "\(version)"
case let .revision(revision):
return revision
case let .branch(name):
return name
case let .range(lowerBound, upperBound):
return "\(lowerBound)..<\(upperBound)"
}
}
}