Skip to content

Commit 5e89a8d

Browse files
committed
update collections APIs to use package identity
motivation: adoption of SE-0292 package registry chnages: * migrate APIs that take PackageRefernec to take PackageIdentity * adjust and update tests rdar://83073308 rdar://82954076
1 parent ac5d561 commit 5e89a8d

14 files changed

+244
-237
lines changed

Sources/Commands/SwiftPackageCollectionsTool.swift

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/*
22
This source file is part of the Swift.org open source project
3-
3+
44
Copyright 2020-2021 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
6-
6+
77
See http://swift.org/LICENSE.txt for license information
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
@@ -67,22 +67,22 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
6767
],
6868
helpNames: [.short, .long, .customLong("help", withSingleDash: true)]
6969
)
70-
70+
7171
public init() {}
72-
72+
7373
// MARK: Collections
74-
74+
7575
struct List: ParsableCommand {
7676
static let configuration = CommandConfiguration(abstract: "List configured collections")
77-
77+
7878
@OptionGroup
7979
var jsonOptions: JSONOptions
80-
80+
8181
mutating func run() throws {
8282
let collections = try with { collections in
8383
try tsc_await { collections.listCollections(identifiers: nil, callback: $0) }
8484
}
85-
85+
8686
if self.jsonOptions.json {
8787
try JSONEncoder.makeWithDefaults().print(collections)
8888
} else {
@@ -92,36 +92,36 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
9292
}
9393
}
9494
}
95-
95+
9696
struct Refresh: ParsableCommand {
9797
static let configuration = CommandConfiguration(abstract: "Refresh configured collections")
98-
98+
9999
mutating func run() throws {
100100
let collections = try with { collections in
101101
try tsc_await { collections.refreshCollections(callback: $0) }
102102
}
103103
print("Refreshed \(collections.count) configured package collection\(collections.count == 1 ? "" : "s").")
104104
}
105105
}
106-
106+
107107
struct Add: ParsableCommand {
108108
static let configuration = CommandConfiguration(abstract: "Add a new collection")
109-
109+
110110
@Argument(help: "URL of the collection to add")
111111
var collectionURL: String
112-
112+
113113
@Option(name: .long, help: "Sort order for the added collection")
114114
var order: Int?
115-
115+
116116
@Flag(name: .long, help: "Trust the collection even if it is unsigned")
117117
var trustUnsigned: Bool = false
118-
118+
119119
@Flag(name: .long, help: "Skip signature check if the collection is signed")
120120
var skipSignatureCheck: Bool = false
121-
121+
122122
mutating func run() throws {
123123
let collectionURL = try url(self.collectionURL)
124-
124+
125125
let source = PackageCollectionsModel.CollectionSource(type: .json, url: collectionURL, skipSignatureCheck: self.skipSignatureCheck)
126126
let collection: PackageCollectionsModel.Collection = try with { collections in
127127
do {
@@ -144,20 +144,20 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
144144
throw CollectionsError.missingSignature
145145
}
146146
}
147-
147+
148148
print("Added \"\(collection.name)\" to your package collections.")
149149
}
150150
}
151-
151+
152152
struct Remove: ParsableCommand {
153153
static let configuration = CommandConfiguration(abstract: "Remove a configured collection")
154-
154+
155155
@Argument(help: "URL of the collection to remove")
156156
var collectionURL: String
157-
157+
158158
mutating func run() throws {
159159
let collectionURL = try url(self.collectionURL)
160-
160+
161161
let source = PackageCollectionsModel.CollectionSource(type: .json, url: collectionURL)
162162
try with { collections in
163163
let collection = try tsc_await { collections.getCollection(source, callback: $0) }
@@ -166,115 +166,115 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
166166
}
167167
}
168168
}
169-
169+
170170
// MARK: Search
171-
171+
172172
enum SearchMethod: String, EnumerableFlag {
173173
case keywords
174174
case module
175175
}
176-
176+
177177
struct Search: ParsableCommand {
178178
static var configuration = CommandConfiguration(abstract: "Search for packages by keywords or module names")
179-
179+
180180
@OptionGroup
181181
var jsonOptions: JSONOptions
182-
182+
183183
@Flag(help: "Pick the method for searching")
184184
var searchMethod: SearchMethod
185-
185+
186186
@Argument(help: "Search query")
187187
var searchQuery: String
188-
188+
189189
mutating func run() throws {
190190
try with { collections in
191191
switch searchMethod {
192192
case .keywords:
193193
let results = try tsc_await { collections.findPackages(searchQuery, collections: nil, callback: $0) }
194-
194+
195195
if jsonOptions.json {
196196
try JSONEncoder.makeWithDefaults().print(results.items)
197197
} else {
198198
results.items.forEach {
199-
print("\($0.package.repository.url): \($0.package.summary ?? "")")
199+
print("\($0.package.identity): \($0.package.summary ?? "")")
200200
}
201201
}
202-
202+
203203
case .module:
204204
let results = try tsc_await { collections.findTargets(searchQuery, searchType: .exactMatch, collections: nil, callback: $0) }
205-
205+
206206
let packages = Set(results.items.flatMap { $0.packages })
207207
if jsonOptions.json {
208208
try JSONEncoder.makeWithDefaults().print(packages)
209209
} else {
210210
packages.forEach {
211-
print("\($0.repository.url): \($0.summary ?? "")")
211+
print("\($0.identity): \($0.summary ?? "")")
212212
}
213213
}
214214
}
215215
}
216216
}
217217
}
218-
218+
219219
// MARK: Packages
220-
220+
221221
struct Describe: ParsableCommand {
222222
static var configuration = CommandConfiguration(abstract: "Get metadata for a collection or a package included in an imported collection")
223-
223+
224224
@OptionGroup
225225
var jsonOptions: JSONOptions
226-
226+
227227
@Argument(help: "URL of the package or collection to get information for")
228228
var packageURL: String
229-
229+
230230
@Option(name: .long, help: "Version of the package to get information for")
231231
var version: String?
232-
232+
233233
private func printVersion(_ version: PackageCollectionsModel.Package.Version?) -> String? {
234234
guard let version = version else {
235235
return nil
236236
}
237237
guard let defaultManifest = version.defaultManifest else {
238238
return nil
239239
}
240-
240+
241241
let manifests = version.manifests.values.filter { $0.toolsVersion != version.defaultToolsVersion }.map { printManifest($0) }.joined(separator: "\n")
242242
let compatibility = optionalRow(
243243
"Verified Compatibility (Platform, Swift Version)",
244244
version.verifiedCompatibility?.map { "(\($0.platform.name), \($0.swiftVersion.rawValue))" }.joined(separator: ", ")
245245
)
246246
let license = optionalRow("License", version.license?.type.description)
247-
247+
248248
return """
249249
\(version.version)
250250
\(self.printManifest(defaultManifest))\(manifests)\(compatibility)\(license)
251251
"""
252252
}
253-
253+
254254
private func printManifest(_ manifest: PackageCollectionsModel.Package.Version.Manifest) -> String {
255255
let modules = manifest.targets.compactMap { $0.moduleName }.joined(separator: ", ")
256256
let products = optionalRow("Products", manifest.products.isEmpty ? nil : manifest.products.compactMap { $0.name }.joined(separator: ", "), indentationLevel: 3)
257-
257+
258258
return """
259259
Tools Version: \(manifest.toolsVersion.description)
260260
Package Name: \(manifest.packageName)
261261
Modules: \(modules)\(products)
262262
"""
263263
}
264-
264+
265265
mutating func run() throws {
266266
try with { collections in
267267
let identity = PackageIdentity(url: packageURL)
268268
let reference = PackageReference.remote(identity: identity, location: packageURL)
269-
269+
270270
do { // assume URL is for a package in an imported collection
271271
let result = try tsc_await { collections.getPackageMetadata(reference, callback: $0) }
272-
272+
273273
if let versionString = version {
274274
guard let version = TSCUtility.Version(versionString), let result = result.package.versions.first(where: { $0.version == version }), let printedResult = printVersion(result) else {
275275
throw CollectionsError.invalidVersionString(versionString)
276276
}
277-
277+
278278
if jsonOptions.json {
279279
try JSONEncoder.makeWithDefaults().print(result)
280280
} else {
@@ -289,7 +289,7 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
289289
let license = optionalRow("License", result.package.license.map { "\($0.type) (\($0.url))" })
290290
let languages = optionalRow("Languages", result.package.languages?.joined(separator: ", "))
291291
let latestVersion = optionalRow("\(String(repeating: "-", count: 60))\n\(indent())Latest Version", printVersion(result.package.latestVersion))
292-
292+
293293
if jsonOptions.json {
294294
try JSONEncoder.makeWithDefaults().print(result.package)
295295
} else {
@@ -304,23 +304,23 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
304304
if version != nil {
305305
throw error
306306
}
307-
307+
308308
let collectionURL = try url(self.packageURL)
309-
309+
310310
do {
311311
let source = PackageCollectionsModel.CollectionSource(type: .json, url: collectionURL)
312312
let collection = try tsc_await { collections.getCollection(source, callback: $0) }
313-
313+
314314
let description = optionalRow("Description", collection.overview)
315315
let keywords = optionalRow("Keywords", collection.keywords?.joined(separator: ", "))
316316
let createdAt = optionalRow("Created At", DateFormatter().string(from: collection.createdAt))
317-
let packages = collection.packages.map { "\($0.repository.url)" }.joined(separator: "\n\(indent(levels: 2))")
318-
317+
let packages = collection.packages.map { "\($0.identity)" }.joined(separator: "\n\(indent(levels: 2))")
318+
319319
if jsonOptions.json {
320320
try JSONEncoder.makeWithDefaults().print(collection)
321321
} else {
322322
let signature = optionalRow("Signed By", collection.signature.map { "\($0.certificate.subject.commonName ?? "Unspecified") (\($0.isVerified ? "" : "not ")verified)" })
323-
323+
324324
print("""
325325
Name: \(collection.name)
326326
Source: \(collection.source.url)\(description)\(keywords)\(createdAt)
@@ -367,10 +367,10 @@ private extension ParsableCommand {
367367
Self.exit(withError: error)
368368
}
369369
}
370-
370+
371371
return try handler(collections)
372372
}
373-
373+
374374
func url(_ urlString: String) throws -> Foundation.URL {
375375
guard let url = URL(string: urlString) else {
376376
let filePrefix = "file://"

Sources/PackageCollections/Model/Package.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ import SourceControl
1717
extension PackageCollectionsModel {
1818
/// Package metadata
1919
public struct Package: Codable, Equatable {
20-
/// Package reference
21-
public let reference: PackageReference
20+
/// Package identity
21+
public let identity: PackageIdentity
2222

23-
/// Package's repository address
24-
public let repository: RepositorySpecifier
23+
/// Package location
24+
public let location: String
2525

2626
/// Package description
2727
public let summary: String?
@@ -85,7 +85,8 @@ extension PackageCollectionsModel {
8585

8686
/// Initializes a `Package`
8787
init(
88-
repository: RepositorySpecifier,
88+
identity: PackageIdentity,
89+
location: String,
8990
summary: String?,
9091
keywords: [String]?,
9192
versions: [Version],
@@ -95,8 +96,8 @@ extension PackageCollectionsModel {
9596
authors: [Author]?,
9697
languages: Set<String>?
9798
) {
98-
self.reference = .init(repository: repository)
99-
self.repository = repository
99+
self.identity = identity
100+
self.location = location
100101
self.summary = summary
101102
self.keywords = keywords
102103
self.versions = versions
@@ -271,6 +272,6 @@ extension PackageCollectionsModel.Package.Version {
271272

272273
extension Model.Package {
273274
var displayName: String {
274-
self.latestVersion?.packageName ?? self.reference.identity.description
275+
self.latestVersion?.packageName ?? self.identity.description
275276
}
276277
}

Sources/PackageCollections/Model/TargetListResult.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ extension PackageCollectionsModel.TargetListResult {
3333
public struct Package: Hashable, Encodable {
3434
public typealias Version = PackageCollectionsModel.TargetListResult.PackageVersion
3535

36-
/// Package's repository address
37-
public let repository: RepositorySpecifier
36+
/// Package's identity
37+
public let identity: PackageIdentity
38+
39+
/// Package's location
40+
public let location: String
3841

3942
/// Package description
4043
public let summary: String?

0 commit comments

Comments
 (0)