-
Notifications
You must be signed in to change notification settings - Fork 3
/
PackageDescription.swift
121 lines (109 loc) · 4.33 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
import Foundation
import Releases
import ShellOut
struct PackageDependency: Decodable {
let name: String
let requirement: DependencyRequirement
let url: 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 PackageDescription: Decodable {
let dependencies: [PackageDependency]
static func loadPackageDescription(from folder: URL) throws -> Self {
let json = try readPackageDescription(from: folder)
let data = json.data(using: .utf8)!
let decoder = JSONDecoder()
do {
let packageDescription = try decoder.decode(Self.self, from: data)
return packageDescription
} catch {
throw PackageDescriptionError.parsingFailed(error.localizedDescription, 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)
}
}
}
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)"
}
}
}