Skip to content

Commit aaab855

Browse files
tomerdyim-lee
andauthored
package-collections storage (#3017)
motivation: continue the implementation of pacakge-collections feature changes: * add protocol + implementations + tests for collections-profiles storage, this is based on a file in ~/.swiftpm * add protocol + implementations + tests for collections cache storage, this is based on sqlite table Co-authored-by: Yim Lee <[email protected]>
1 parent 36206ad commit aaab855

File tree

12 files changed

+1297
-12
lines changed

12 files changed

+1297
-12
lines changed

Package.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,9 @@ let package = Package(
239239
.testTarget(
240240
name: "PackageGraphPerformanceTests",
241241
dependencies: ["PackageGraph", "SPMTestSupport"]),
242+
.testTarget(
243+
name: "PackageCollectionsTests",
244+
dependencies: ["SPMTestSupport", "PackageCollections"]),
242245
.testTarget(
243246
name: "SourceControlTests",
244247
dependencies: ["SourceControl", "SPMTestSupport"]),

Sources/PackageCollections/Model/License.swift

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import struct Foundation.URL
1212

1313
extension PackageCollectionsModel {
1414
/// A representation of a package license
15-
public struct License {
15+
public struct License: Codable {
1616
/// License type
1717
public let type: LicenseType
1818

@@ -100,3 +100,92 @@ extension PackageCollectionsModel {
100100
}
101101
}
102102
}
103+
104+
extension PackageCollectionsModel.LicenseType: Codable {
105+
public enum DiscriminatorKeys: String, Codable {
106+
case Apache2_0
107+
case BSD2Clause
108+
case BSD3Clause
109+
case GPL2_0
110+
case GPL3_0
111+
case LGPL2_0
112+
case LGPL2_1
113+
case LGPL3_0
114+
case MIT
115+
case MPL2_0
116+
case CDDL1_0
117+
case EPL2_0
118+
case other
119+
}
120+
121+
public enum CodingKeys: CodingKey {
122+
case _case
123+
case name
124+
}
125+
126+
public init(from decoder: Decoder) throws {
127+
let container = try decoder.container(keyedBy: CodingKeys.self)
128+
switch try container.decode(DiscriminatorKeys.self, forKey: ._case) {
129+
case .Apache2_0:
130+
self = .Apache2_0
131+
case .BSD2Clause:
132+
self = .BSD2Clause
133+
case .BSD3Clause:
134+
self = .BSD3Clause
135+
case .GPL2_0:
136+
self = .GPL2_0
137+
case .GPL3_0:
138+
self = .GPL3_0
139+
case .LGPL2_0:
140+
self = .LGPL2_0
141+
case .LGPL2_1:
142+
self = .LGPL2_1
143+
case .LGPL3_0:
144+
self = .LGPL3_0
145+
case .MIT:
146+
self = .MIT
147+
case .MPL2_0:
148+
self = .MPL2_0
149+
case .CDDL1_0:
150+
self = .CDDL1_0
151+
case .EPL2_0:
152+
self = .EPL2_0
153+
case .other:
154+
let name = try container.decode(String.self, forKey: .name)
155+
self = .other(name)
156+
}
157+
}
158+
159+
public func encode(to encoder: Encoder) throws {
160+
var container = encoder.container(keyedBy: CodingKeys.self)
161+
switch self {
162+
case .Apache2_0:
163+
try container.encode(DiscriminatorKeys.Apache2_0, forKey: ._case)
164+
case .BSD2Clause:
165+
try container.encode(DiscriminatorKeys.BSD2Clause, forKey: ._case)
166+
case .BSD3Clause:
167+
try container.encode(DiscriminatorKeys.BSD3Clause, forKey: ._case)
168+
case .GPL2_0:
169+
try container.encode(DiscriminatorKeys.GPL2_0, forKey: ._case)
170+
case .GPL3_0:
171+
try container.encode(DiscriminatorKeys.GPL3_0, forKey: ._case)
172+
case .LGPL2_0:
173+
try container.encode(DiscriminatorKeys.LGPL2_0, forKey: ._case)
174+
case .LGPL2_1:
175+
try container.encode(DiscriminatorKeys.LGPL2_1, forKey: ._case)
176+
case .LGPL3_0:
177+
try container.encode(DiscriminatorKeys.LGPL3_0, forKey: ._case)
178+
case .MIT:
179+
try container.encode(DiscriminatorKeys.MIT, forKey: ._case)
180+
case .MPL2_0:
181+
try container.encode(DiscriminatorKeys.MPL2_0, forKey: ._case)
182+
case .CDDL1_0:
183+
try container.encode(DiscriminatorKeys.CDDL1_0, forKey: ._case)
184+
case .EPL2_0:
185+
try container.encode(DiscriminatorKeys.EPL2_0, forKey: ._case)
186+
case .other(let name):
187+
try container.encode(DiscriminatorKeys.other, forKey: ._case)
188+
try container.encode(name, forKey: .name)
189+
}
190+
}
191+
}

Sources/PackageCollections/Model/Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ extension PackageCollectionsModel.Package {
124124

125125
extension PackageCollectionsModel {
126126
/// A representation of package target
127-
public struct PackageTarget {
127+
public struct PackageTarget: Codable {
128128
/// The target name
129129
public let name: String
130130

@@ -135,15 +135,15 @@ extension PackageCollectionsModel {
135135

136136
extension PackageCollectionsModel {
137137
/// A representation of package product
138-
public struct PackageProduct {
138+
public struct PackageProduct: Codable {
139139
/// The product name
140140
public let name: String
141141

142142
/// The product type
143143
public let type: ProductType
144144

145145
/// The product's targets
146-
public let targets: [Target]
146+
public let targets: [PackageTarget]
147147
}
148148
}
149149

Sources/PackageCollections/Model/PackageSet.swift

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import TSCUtility
1818
public enum PackageCollectionsModel {}
1919

2020
extension PackageCollectionsModel {
21-
/// A `PackageCollection` is a grouping of package metadata.
22-
public struct PackageCollection {
21+
/// A `PackageCollection` is a collection of packages.
22+
public struct PackageCollection: Codable {
2323
public typealias Identifier = PackageCollectionIdentifier
2424
public typealias Source = PackageCollectionSource
2525

@@ -71,12 +71,41 @@ extension PackageCollectionsModel {
7171

7272
extension PackageCollectionsModel {
7373
/// Represents the source of a `PackageCollection`
74-
public enum PackageCollectionSource {
74+
public enum PackageCollectionSource: Equatable {
7575
/// Package feed at URL
7676
case feed(URL)
7777
}
7878
}
7979

80+
extension PackageCollectionsModel.PackageCollectionSource: Codable {
81+
public enum DiscriminatorKeys: String, Codable {
82+
case feed
83+
}
84+
85+
public enum CodingKeys: CodingKey {
86+
case _case
87+
case url
88+
}
89+
90+
public init(from decoder: Decoder) throws {
91+
let container = try decoder.container(keyedBy: CodingKeys.self)
92+
switch try container.decode(DiscriminatorKeys.self, forKey: ._case) {
93+
case .feed:
94+
let url = try container.decode(URL.self, forKey: .url)
95+
self = .feed(url)
96+
}
97+
}
98+
99+
public func encode(to encoder: Encoder) throws {
100+
var container = encoder.container(keyedBy: CodingKeys.self)
101+
switch self {
102+
case .feed(let url):
103+
try container.encode(DiscriminatorKeys.feed, forKey: ._case)
104+
try container.encode(url, forKey: .url)
105+
}
106+
}
107+
}
108+
80109
extension PackageCollectionsModel {
81110
/// Represents the identifier of a `PackageCollection`
82111
public enum PackageCollectionIdentifier: Hashable, Comparable {
@@ -100,9 +129,38 @@ extension PackageCollectionsModel {
100129
}
101130
}
102131

132+
extension PackageCollectionsModel.PackageCollection.Identifier: Codable {
133+
public enum DiscriminatorKeys: String, Codable {
134+
case feed
135+
}
136+
137+
public enum CodingKeys: CodingKey {
138+
case _case
139+
case url
140+
}
141+
142+
public init(from decoder: Decoder) throws {
143+
let container = try decoder.container(keyedBy: CodingKeys.self)
144+
switch try container.decode(DiscriminatorKeys.self, forKey: ._case) {
145+
case .feed:
146+
let url = try container.decode(URL.self, forKey: .url)
147+
self = .feed(url)
148+
}
149+
}
150+
151+
public func encode(to encoder: Encoder) throws {
152+
var container = encoder.container(keyedBy: CodingKeys.self)
153+
switch self {
154+
case .feed(let url):
155+
try container.encode(DiscriminatorKeys.feed, forKey: ._case)
156+
try container.encode(url, forKey: .url)
157+
}
158+
}
159+
}
160+
103161
extension PackageCollectionsModel.PackageCollection {
104162
/// A representation of package metadata
105-
public struct Package {
163+
public struct Package: Codable {
106164
public typealias Version = PackageVersion
107165

108166
/// Package reference
@@ -138,7 +196,7 @@ extension PackageCollectionsModel.PackageCollection {
138196

139197
extension PackageCollectionsModel.PackageCollection {
140198
/// A representation of package version
141-
public struct PackageVersion {
199+
public struct PackageVersion: Codable {
142200
public typealias Target = PackageCollectionsModel.PackageTarget
143201
public typealias Product = PackageCollectionsModel.PackageProduct
144202

Sources/PackageCollections/Model/Profile.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
extension PackageCollectionsModel {
1212
/// A `PackageGroupProfile` is a grouping of `PackageGroup`s.
13-
public struct Profile {
13+
public struct Profile: Hashable {
1414
/// The default profile; this should be used when a profile is required but not specified.
1515
public static let `default` = Profile(name: "default")
1616

0 commit comments

Comments
 (0)