Skip to content

Commit aae361d

Browse files
committed
more backward compat
1 parent dcb79b3 commit aae361d

File tree

4 files changed

+60
-17
lines changed

4 files changed

+60
-17
lines changed

Sources/Commands/SwiftPackageCollectionsTool.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,10 +265,9 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
265265
mutating func run() throws {
266266
try with { collections in
267267
let identity = PackageIdentity(url: packageURL)
268-
let reference = PackageReference.remote(identity: identity, location: packageURL)
269268

270269
do { // assume URL is for a package in an imported collection
271-
let result = try tsc_await { collections.getPackageMetadata(reference, callback: $0) }
270+
let result = try tsc_await { collections.getPackageMetadata(identity: identity, location: packageURL, callback: $0) }
272271

273272
if let versionString = version {
274273
guard let version = TSCUtility.Version(versionString), let result = result.package.versions.first(where: { $0.version == version }), let printedResult = printVersion(result) else {

Sources/PackageCollections/API.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ public protocol PackageCollectionsProtocol {
111111
/// - Parameters:
112112
/// - reference: The package reference
113113
/// - callback: The closure to invoke when result becomes available
114+
@available(*, deprecated, message: "user getPackageMetadata(identity:) instead")
114115
func getPackageMetadata(
115116
_ reference: PackageReference,
116117
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void
@@ -126,12 +127,46 @@ public protocol PackageCollectionsProtocol {
126127
/// - collections: Optional. If specified, only look for package in these collections. Data from the most recently
127128
/// processed collection will be used.
128129
/// - callback: The closure to invoke when result becomes available
130+
@available(*, deprecated, message: "user getPackageMetadata(identity:) instead")
129131
func getPackageMetadata(
130132
_ reference: PackageReference,
131133
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
132134
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void
133135
)
134136

137+
/// Returns metadata for the package identified by the given `PackageIdentity`, along with the
138+
/// identifiers of `PackageCollection`s where the package is found.
139+
///
140+
/// A failure is returned if the package is not found.
141+
///
142+
/// - Parameters:
143+
/// - identity: The package identity
144+
/// - location: The package location (optional for deduplication)
145+
/// - callback: The closure to invoke when result becomes available
146+
func getPackageMetadata(
147+
identity: PackageIdentity,
148+
location: String?,
149+
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void
150+
)
151+
152+
/// Returns metadata for the package identified by the given `PackageIdentity`, along with the
153+
/// identifiers of `PackageCollection`s where the package is found.
154+
///
155+
/// A failure is returned if the package is not found.
156+
///
157+
/// - Parameters:
158+
/// - identity: The package identity
159+
/// - location: The package location (optional for deduplication)
160+
/// - collections: Optional. If specified, only look for package in these collections. Data from the most recently
161+
/// processed collection will be used.
162+
/// - callback: The closure to invoke when result becomes available
163+
func getPackageMetadata(
164+
identity: PackageIdentity,
165+
location: String?,
166+
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
167+
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void
168+
)
169+
135170
/// Lists packages from the specified collections.
136171
///
137172
/// - Parameters:

Sources/PackageCollections/PackageCollections.swift

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -353,23 +353,25 @@ public struct PackageCollections: PackageCollectionsProtocol {
353353
public func getPackageMetadata(_ reference: PackageReference,
354354
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
355355
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void) {
356-
self.getPackageMetadata(reference.identity, collections: nil, callback: callback)
356+
self.getPackageMetadata(identity: reference.identity, location: reference.location, collections: .none, callback: callback)
357357
}
358358

359-
public func getPackageMetadata(_ identity: PackageIdentity,
359+
public func getPackageMetadata(identity: PackageIdentity,
360+
location: String? = .none,
360361
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void) {
361-
self.getPackageMetadata(identity, collections: nil, callback: callback)
362+
self.getPackageMetadata(identity: identity, location: location, collections: .none, callback: callback)
362363
}
363364

364-
public func getPackageMetadata(_ identity: PackageIdentity,
365+
public func getPackageMetadata(identity: PackageIdentity,
366+
location: String? = .none,
365367
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
366368
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void) {
367369
guard Self.isSupportedPlatform else {
368370
return callback(.failure(PackageCollectionError.unsupportedPlatform))
369371
}
370372

371373
// first find in storage
372-
self.findPackage(identity: identity, collections: collections) { result in
374+
self.findPackage(identity: identity, location: location, collections: collections) { result in
373375
switch result {
374376
case .failure(let error):
375377
callback(.failure(error))
@@ -532,6 +534,7 @@ public struct PackageCollections: PackageCollectionsProtocol {
532534
}
533535

534536
func findPackage(identity: PackageIdentity,
537+
location: String?,
535538
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
536539
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult.Item, Error>) -> Void) {
537540
self.storage.sources.list { result in
@@ -551,10 +554,16 @@ public struct PackageCollections: PackageCollectionsProtocol {
551554
case .failure(let error):
552555
callback(.failure(error))
553556
case .success(let packagesCollections):
554-
// A package identity can be associated with multiple repository URLs
555-
let matches = packagesCollections.packages.filter { $0.identity == identity }
557+
let matches: [PackageCollectionsModel.Package]
558+
if let location = location {
559+
// A package identity can be associated with multiple repository URLs
560+
matches = packagesCollections.packages.filter { CanonicalPackageIdentity($0.location) == CanonicalPackageIdentity(location) }
561+
}
562+
else {
563+
matches = packagesCollections.packages
564+
}
556565
guard let package = matches.first else {
557-
return callback(.failure(NotFoundError("\(identity)")))
566+
return callback(.failure(NotFoundError("\(identity), \(location ?? "none")")))
558567
}
559568
callback(.success(.init(package: package, collections: packagesCollections.collections)))
560569
}

Tests/PackageCollectionsTests/PackageCollectionsTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,7 +1178,7 @@ final class PackageCollectionsTests: XCTestCase {
11781178
XCTAssertEqual(list.count, mockCollections.count, "list count should match")
11791179
}
11801180

1181-
let metadata = try tsc_await { callback in packageCollections.getPackageMetadata(mockPackage.identity, callback: callback) }
1181+
let metadata = try tsc_await { callback in packageCollections.getPackageMetadata(identity: mockPackage.identity, location: mockPackage.location, callback: callback) }
11821182

11831183
let expectedCollections = Set(mockCollections.filter { $0.packages.map { $0.identity }.contains(mockPackage.identity) }.map { $0.identifier })
11841184
XCTAssertEqual(Set(metadata.collections), expectedCollections, "collections should match")
@@ -1216,7 +1216,7 @@ final class PackageCollectionsTests: XCTestCase {
12161216
XCTAssertEqual(list.count, mockCollections.count, "list count should match")
12171217
}
12181218

1219-
let metadata = try tsc_await { callback in packageCollections.getPackageMetadata(mockPackage.identity, callback: callback) }
1219+
let metadata = try tsc_await { callback in packageCollections.getPackageMetadata(identity: mockPackage.identity, location: mockPackage.location, callback: callback) }
12201220

12211221
let expectedCollections = Set(mockCollections.filter { $0.packages.map { $0.identity }.contains(mockPackage.identity) }.map { $0.identifier })
12221222
XCTAssertEqual(Set(metadata.collections), expectedCollections, "collections should match")
@@ -1255,7 +1255,7 @@ final class PackageCollectionsTests: XCTestCase {
12551255
}
12561256

12571257
let collectionIdentifiers: Set<Model.CollectionIdentifier> = [mockCollections.last!.identifier]
1258-
let metadata = try tsc_await { callback in packageCollections.getPackageMetadata(mockPackage.identity, collections: collectionIdentifiers, callback: callback) }
1258+
let metadata = try tsc_await { callback in packageCollections.getPackageMetadata(identity: mockPackage.identity, location: mockPackage.location, collections: collectionIdentifiers, callback: callback) }
12591259
XCTAssertEqual(Set(metadata.collections), collectionIdentifiers, "collections should match")
12601260

12611261
let expectedMetadata = PackageCollections.mergedPackageMetadata(package: mockPackage, basicMetadata: nil)
@@ -1373,7 +1373,7 @@ final class PackageCollectionsTests: XCTestCase {
13731373
XCTAssertEqual(list.count, 0, "list should be empty")
13741374
}
13751375

1376-
XCTAssertThrowsError(try tsc_await { callback in packageCollections.getPackageMetadata(mockPackage.identity, callback: callback) }, "expected error") { error in
1376+
XCTAssertThrowsError(try tsc_await { callback in packageCollections.getPackageMetadata(identity: mockPackage.identity, location: mockPackage.location, callback: callback) }, "expected error") { error in
13771377
XCTAssert(error is NotFoundError)
13781378
}
13791379
}
@@ -1404,7 +1404,7 @@ final class PackageCollectionsTests: XCTestCase {
14041404
XCTAssertEqual(list.count, mockCollections.count, "list count should match")
14051405
}
14061406

1407-
let metadata = try tsc_await { callback in packageCollections.getPackageMetadata(mockPackage.identity, callback: callback) }
1407+
let metadata = try tsc_await { callback in packageCollections.getPackageMetadata(identity: mockPackage.identity, location: mockPackage.location, callback: callback) }
14081408

14091409
let expectedCollections = Set(mockCollections.filter { $0.packages.map { $0.identity }.contains(mockPackage.identity) }.map { $0.identifier })
14101410
XCTAssertEqual(Set(metadata.collections), expectedCollections, "collections should match")
@@ -1459,7 +1459,7 @@ final class PackageCollectionsTests: XCTestCase {
14591459
}
14601460

14611461
// Despite metadata provider error we should still get back data from storage
1462-
let metadata = try tsc_await { callback in packageCollections.getPackageMetadata(mockPackage.identity, callback: callback) }
1462+
let metadata = try tsc_await { callback in packageCollections.getPackageMetadata(identity: mockPackage.identity, location: mockPackage.location, callback: callback) }
14631463
let expectedMetadata = PackageCollections.mergedPackageMetadata(package: mockPackage, basicMetadata: nil)
14641464
XCTAssertEqual(metadata.package, expectedMetadata, "package should match")
14651465

@@ -1496,7 +1496,7 @@ final class PackageCollectionsTests: XCTestCase {
14961496
sync.wait()
14971497

14981498
let start = Date()
1499-
let metadata = try tsc_await { callback in packageCollections.getPackageMetadata(mockPackage.identity, callback: callback) }
1499+
let metadata = try tsc_await { callback in packageCollections.getPackageMetadata(identity: mockPackage.identity, location: mockPackage.location, callback: callback) }
15001500
XCTAssertNotNil(metadata)
15011501
let delta = Date().timeIntervalSince(start)
15021502
XCTAssert(delta < 1.0, "should fetch quickly, took \(delta)")

0 commit comments

Comments
 (0)