@@ -314,26 +314,26 @@ public struct PackageCollections: PackageCollectionsProtocol {
314
314
case . failure( let error) :
315
315
callback ( . failure( error) )
316
316
case . success( let collections) :
317
- var packageCollections = [ PackageReference : ( package : Model . Package , collections: Set < Model . CollectionIdentifier > ) ] ( )
317
+ var packageCollections = [ PackageIdentity : ( package : Model . Package , collections: Set < Model . CollectionIdentifier > ) ] ( )
318
318
// Use package data from the most recently processed collection
319
319
collections. sorted ( by: { $0. lastProcessedAt > $1. lastProcessedAt } ) . forEach { collection in
320
320
collection. packages. forEach { package in
321
- var entry = packageCollections. removeValue ( forKey: package . reference )
321
+ var entry = packageCollections. removeValue ( forKey: package . identity )
322
322
if entry == nil {
323
323
entry = ( package , . init( ) )
324
324
}
325
325
326
326
if var entry = entry {
327
327
entry. collections. insert ( collection. identifier)
328
- packageCollections [ package . reference ] = entry
328
+ packageCollections [ package . identity ] = entry
329
329
}
330
330
}
331
331
}
332
332
333
333
let result = PackageCollectionsModel . PackageSearchResult (
334
334
items: packageCollections. sorted { $0. value. package . displayName < $1. value. package . displayName }
335
335
. map { entry in
336
- . init( package : entry. value. package , collections: Array ( entry. value. collections) )
336
+ . init( package : entry. value. package , collections: Array ( entry. value. collections) )
337
337
}
338
338
)
339
339
callback ( . success( result) )
@@ -343,32 +343,48 @@ public struct PackageCollections: PackageCollectionsProtocol {
343
343
344
344
// MARK: - Package Metadata
345
345
346
+ @available ( * , deprecated, message: " user identity based API instead " )
346
347
public func getPackageMetadata( _ reference: PackageReference ,
347
348
callback: @escaping ( Result < PackageCollectionsModel . PackageMetadata , Error > ) -> Void ) {
348
349
self . getPackageMetadata ( reference, collections: nil , callback: callback)
349
350
}
350
351
352
+ @available ( * , deprecated, message: " user identity based API instead " )
351
353
public func getPackageMetadata( _ reference: PackageReference ,
352
354
collections: Set < PackageCollectionsModel . CollectionIdentifier > ? ,
353
355
callback: @escaping ( Result < PackageCollectionsModel . PackageMetadata , Error > ) -> Void ) {
356
+ self . getPackageMetadata ( identity: reference. identity, location: reference. location, collections: . none, callback: callback)
357
+ }
358
+
359
+ public func getPackageMetadata( identity: PackageIdentity ,
360
+ location: String ? = . none,
361
+ callback: @escaping ( Result < PackageCollectionsModel . PackageMetadata , Error > ) -> Void ) {
362
+ self . getPackageMetadata ( identity: identity, location: location, collections: . none, callback: callback)
363
+ }
364
+
365
+ public func getPackageMetadata( identity: PackageIdentity ,
366
+ location: String ? = . none,
367
+ collections: Set < PackageCollectionsModel . CollectionIdentifier > ? ,
368
+ callback: @escaping ( Result < PackageCollectionsModel . PackageMetadata , Error > ) -> Void ) {
354
369
guard Self . isSupportedPlatform else {
355
370
return callback ( . failure( PackageCollectionError . unsupportedPlatform) )
356
371
}
357
372
358
373
// first find in storage
359
- self . findPackage ( reference : reference , collections: collections) { result in
374
+ self . findPackage ( identity : identity , location : location , collections: collections) { result in
360
375
switch result {
361
376
case . failure( let error) :
362
377
callback ( . failure( error) )
363
378
case . success( let packageSearchResult) :
379
+
364
380
// then try to get more metadata from provider (optional)
365
- let authTokenType = self . metadataProvider. getAuthTokenType ( for: reference )
381
+ let authTokenType = self . metadataProvider. getAuthTokenType ( for: packageSearchResult . package . location )
366
382
let isAuthTokenConfigured = authTokenType. flatMap { self . configuration. authTokens ( ) ? [ $0] } != nil
367
383
368
- self . metadataProvider. get ( reference ) { result in
384
+ self . metadataProvider. get ( identity : packageSearchResult . package . identity , location : packageSearchResult . package . location ) { result in
369
385
switch result {
370
386
case . failure( let error) :
371
- self . diagnosticsEngine? . emit ( warning: " Failed fetching information about \( reference ) from \( self . metadataProvider. name) : \( error) " )
387
+ self . diagnosticsEngine? . emit ( warning: " Failed fetching information about \( identity ) from \( self . metadataProvider. name) : \( error) " )
372
388
373
389
let provider : PackageMetadataProviderContext ?
374
390
switch error {
@@ -517,7 +533,8 @@ public struct PackageCollections: PackageCollectionsProtocol {
517
533
}
518
534
}
519
535
520
- func findPackage( reference: PackageReference ,
536
+ func findPackage( identity: PackageIdentity ,
537
+ location: String ? ,
521
538
collections: Set < PackageCollectionsModel . CollectionIdentifier > ? ,
522
539
callback: @escaping ( Result < PackageCollectionsModel . PackageSearchResult . Item , Error > ) -> Void ) {
523
540
self . storage. sources. list { result in
@@ -530,17 +547,23 @@ public struct PackageCollections: PackageCollectionsProtocol {
530
547
collectionIdentifiers = collectionIdentifiers. filter { collections. contains ( $0) }
531
548
}
532
549
if collectionIdentifiers. isEmpty {
533
- return callback ( . failure( NotFoundError ( " \( reference ) " ) ) )
550
+ return callback ( . failure( NotFoundError ( " \( identity ) " ) ) )
534
551
}
535
- self . storage. collections. findPackage ( identifier: reference . identity, collectionIdentifiers: collectionIdentifiers) { findPackageResult in
552
+ self . storage. collections. findPackage ( identifier: identity, collectionIdentifiers: collectionIdentifiers) { findPackageResult in
536
553
switch findPackageResult {
537
554
case . failure( let error) :
538
555
callback ( . failure( error) )
539
556
case . success( let packagesCollections) :
540
- // A package identity can be associated with multiple repository URLs
541
- let matches = packagesCollections. packages. filter { $0. reference. canonicalLocation == reference. canonicalLocation }
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
+ }
542
565
guard let package = matches. first else {
543
- return callback ( . failure( NotFoundError ( " \( reference ) " ) ) )
566
+ return callback ( . failure( NotFoundError ( " \( identity ) , \( location ?? " none " ) " ) ) )
544
567
}
545
568
callback ( . success( . init( package : package , collections: packagesCollections. collections) ) )
546
569
}
@@ -550,22 +573,22 @@ public struct PackageCollections: PackageCollectionsProtocol {
550
573
}
551
574
552
575
private func targetListResultFromCollections( _ collections: [ Model . Collection ] ) -> Model . TargetListResult {
553
- var packageCollections = [ PackageReference : ( package : Model . Package , collections: Set < Model . CollectionIdentifier > ) ] ( )
554
- var targetsPackages = [ String : ( target: Model . Target , packages: Set < PackageReference > ) ] ( )
576
+ var packageCollections = [ PackageIdentity : ( package : Model . Package , collections: Set < Model . CollectionIdentifier > ) ] ( )
577
+ var targetsPackages = [ String : ( target: Model . Target , packages: Set < PackageIdentity > ) ] ( )
555
578
556
579
collections. forEach { collection in
557
580
collection. packages. forEach { package in
558
581
// Avoid copy-on-write: remove entry from dictionary before mutating
559
- var entry = packageCollections. removeValue ( forKey: package . reference ) ?? ( package , . init( ) )
582
+ var entry = packageCollections. removeValue ( forKey: package . identity ) ?? ( package , . init( ) )
560
583
entry. collections. insert ( collection. identifier)
561
- packageCollections [ package . reference ] = entry
584
+ packageCollections [ package . identity ] = entry
562
585
563
586
package . versions. forEach { version in
564
587
version. manifests. values. forEach { manifest in
565
588
manifest. targets. forEach { target in
566
589
// Avoid copy-on-write: remove entry from dictionary before mutating
567
590
var entry = targetsPackages. removeValue ( forKey: target. name) ?? ( target: target, packages: . init( ) )
568
- entry. packages. insert ( package . reference )
591
+ entry. packages. insert ( package . identity )
569
592
targetsPackages [ target. name] = entry
570
593
}
571
594
}
@@ -586,7 +609,8 @@ public struct PackageCollections: PackageCollectionsProtocol {
586
609
)
587
610
}
588
611
}
589
- return . init( repository: pair. package . repository,
612
+ return . init( identity: pair. package . identity,
613
+ location: pair. package . location,
590
614
summary: pair. package . summary,
591
615
versions: versions,
592
616
collections: Array ( pair. collections) )
@@ -614,7 +638,8 @@ public struct PackageCollections: PackageCollectionsProtocol {
614
638
versions. sort ( by: > )
615
639
616
640
return Model . Package (
617
- repository: package . repository,
641
+ identity: package . identity,
642
+ location: package . location,
618
643
summary: basicMetadata? . summary ?? package . summary,
619
644
keywords: basicMetadata? . keywords ?? package . keywords,
620
645
versions: versions,
@@ -634,9 +659,3 @@ private struct UnknownProvider: Error {
634
659
self . sourceType = sourceType
635
660
}
636
661
}
637
-
638
- private extension PackageReference {
639
- var canonicalLocation : String {
640
- ( self . location. hasSuffix ( " .git " ) ? String ( self . location. dropLast ( 4 ) ) : self . location) . lowercased ( )
641
- }
642
- }
0 commit comments