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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2020-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics
import PackageModel
import protocol TSCBasic.Closable
// TODO: is there a better name? this conflicts with the module name which is okay in this case but not ideal in Swift
public struct PackageCollections: PackageCollectionsProtocol, Closable {
// Check JSONPackageCollectionProvider.isSignatureCheckSupported before updating or removing this
#if os(macOS) || os(Linux) || os(Windows) || os(Android)
static let isSupportedPlatform = true
#else
static let isSupportedPlatform = false
#endif
let configuration: Configuration
private let fileSystem: FileSystem
private let observabilityScope: ObservabilityScope
private let storageContainer: (storage: Storage, owned: Bool)
private let collectionProviders: [Model.CollectionSourceType: PackageCollectionProvider]
let metadataProvider: PackageMetadataProvider
private var storage: Storage {
self.storageContainer.storage
}
// initialize with defaults
public init(
configuration: Configuration = .init(),
fileSystem: FileSystem,
observabilityScope: ObservabilityScope
) {
self.init(
configuration: configuration,
customMetadataProvider: nil,
fileSystem: fileSystem,
observabilityScope: observabilityScope
)
}
init(
configuration: Configuration = .init(),
customMetadataProvider: PackageMetadataProvider?,
fileSystem: FileSystem,
observabilityScope: ObservabilityScope
) {
let storage = Storage(
sources: FilePackageCollectionsSourcesStorage(
fileSystem: fileSystem,
path: configuration.configurationDirectory?.appending("collections.json")
),
collections: SQLitePackageCollectionsStorage(
location: configuration.cacheDirectory.map { .path($0.appending(components: "package-collection.db")) },
observabilityScope: observabilityScope
)
)
let collectionProviders = [
Model.CollectionSourceType.json: JSONPackageCollectionProvider(
fileSystem: fileSystem,
observabilityScope: observabilityScope
)
]
let metadataProvider = customMetadataProvider ?? GitHubPackageMetadataProvider(
configuration: .init(
authTokens: configuration.authTokens,
cacheDir: configuration.cacheDirectory?.appending(components: "package-metadata")
),
observabilityScope: observabilityScope
)
self.configuration = configuration
self.fileSystem = fileSystem
self.observabilityScope = observabilityScope
self.storageContainer = (storage, true)
self.collectionProviders = collectionProviders
self.metadataProvider = metadataProvider
}
// internal initializer for testing
init(configuration: Configuration = .init(),
fileSystem: FileSystem,
observabilityScope: ObservabilityScope,
storage: Storage,
collectionProviders: [Model.CollectionSourceType: PackageCollectionProvider],
metadataProvider: PackageMetadataProvider
) {
self.configuration = configuration
self.fileSystem = fileSystem
self.observabilityScope = observabilityScope
self.storageContainer = (storage, false)
self.collectionProviders = collectionProviders
self.metadataProvider = metadataProvider
}
public func shutdown() throws {
if self.storageContainer.owned {
try self.storageContainer.storage.close()
}
if let metadataProvider = self.metadataProvider as? Closable {
try metadataProvider.close()
}
}
public func close() throws {
try self.shutdown()
}
// MARK: - Collections
public func listCollections(identifiers: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
callback: @escaping (Result<[PackageCollectionsModel.Collection], Error>) -> Void) {
guard Self.isSupportedPlatform else {
return callback(.failure(PackageCollectionError.unsupportedPlatform))
}
self.storage.sources.list { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success(let sources):
let identiferSource = sources.reduce(into: [PackageCollectionsModel.CollectionIdentifier: PackageCollectionsModel.CollectionSource]()) { result, source in
result[.init(from: source)] = source
}
let identifiersToFetch = identiferSource.keys.filter { identifiers?.contains($0) ?? true }
if identifiersToFetch.isEmpty {
return callback(.success([]))
}
self.storage.collections.list(identifiers: identifiersToFetch) { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success(var collections):
let sourceOrder = sources.enumerated().reduce(into: [Model.CollectionIdentifier: Int]()) { result, item in
result[.init(from: item.element)] = item.offset
}
// re-order by profile order which reflects the user's election
let sort = { (lhs: PackageCollectionsModel.Collection, rhs: PackageCollectionsModel.Collection) -> Bool in
sourceOrder[lhs.identifier] ?? 0 < sourceOrder[rhs.identifier] ?? 0
}
// We've fetched all the wanted collections and we're done
if collections.count == identifiersToFetch.count {
collections.sort(by: sort)
return callback(.success(collections))
}
// Some of the results are missing. This happens when deserialization of stored collections fail,
// so we will try refreshing the missing collections to update data in storage.
let missingSources = Set(identifiersToFetch.compactMap { identiferSource[$0] }).subtracting(Set(collections.map { $0.source }))
let refreshResults = ThreadSafeArrayStore<Result<Model.Collection, Error>>()
missingSources.forEach { source in
self.refreshCollectionFromSource(source: source, trustConfirmationProvider: nil) { refreshResult in
let count = refreshResults.append(refreshResult)
if count == missingSources.count {
var result = collections + refreshResults.compactMap { $0.success } // best-effort; not returning errors
result.sort(by: sort)
callback(.success(result))
}
}
}
}
}
}
}
}
public func refreshCollections(callback: @escaping (Result<[PackageCollectionsModel.CollectionSource], Error>) -> Void) {
guard Self.isSupportedPlatform else {
return callback(.failure(PackageCollectionError.unsupportedPlatform))
}
self.storage.sources.list { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success(let sources):
if sources.isEmpty {
return callback(.success([]))
}
let refreshResults = ThreadSafeArrayStore<Result<Model.Collection, Error>>()
sources.forEach { source in
self.refreshCollectionFromSource(source: source, trustConfirmationProvider: nil) { refreshResult in
let count = refreshResults.append(refreshResult)
if count == sources.count {
let errors = refreshResults.compactMap { $0.failure }
callback(errors.isEmpty ? .success(sources) : .failure(MultipleErrors(errors)))
}
}
}
}
}
}
public func refreshCollection(_ source: PackageCollectionsModel.CollectionSource,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void) {
guard Self.isSupportedPlatform else {
return callback(.failure(PackageCollectionError.unsupportedPlatform))
}
self.storage.sources.list { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success(let sources):
guard let savedSource = sources.first(where: { $0 == source }) else {
return callback(.failure(NotFoundError("\(source)")))
}
self.refreshCollectionFromSource(source: savedSource, trustConfirmationProvider: nil, callback: callback)
}
}
}
public func addCollection(_ source: PackageCollectionsModel.CollectionSource,
order: Int? = nil,
trustConfirmationProvider: ((PackageCollectionsModel.Collection, @escaping (Bool) -> Void) -> Void)? = nil,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void) {
guard Self.isSupportedPlatform else {
return callback(.failure(PackageCollectionError.unsupportedPlatform))
}
if let errors = source.validate(fileSystem: self.fileSystem)?.errors() {
return callback(.failure(MultipleErrors(errors)))
}
// first record the registration
self.storage.sources.add(source: source, order: order) { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success:
// next try to fetch the collection from the network and store it locally so future operations dont need to access the network
self.refreshCollectionFromSource(source: source, trustConfirmationProvider: trustConfirmationProvider) { collectionResult in
switch collectionResult {
case .failure(let error):
// Don't delete the source if we are either pending user confirmation or have recorded user's preference.
// It is also possible that we can't verify signature (yet) due to config issue, which user can fix and we retry later.
if let error = error as? PackageCollectionError, error == .trustConfirmationRequired || error == .untrusted || error == .cannotVerifySignature {
return callback(.failure(error))
}
// Otherwise remove source since it fails to be fetched
self.storage.sources.remove(source: source) { _ in
// Whether removal succeeds or not, return the refresh error
callback(.failure(error))
}
case .success(let collection):
callback(.success(collection))
}
}
}
}
}
public func removeCollection(_ source: PackageCollectionsModel.CollectionSource,
callback: @escaping (Result<Void, Error>) -> Void) {
guard Self.isSupportedPlatform else {
return callback(.failure(PackageCollectionError.unsupportedPlatform))
}
self.storage.sources.remove(source: source) { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success:
self.storage.collections.remove(identifier: .init(from: source), callback: callback)
}
}
}
public func moveCollection(_ source: PackageCollectionsModel.CollectionSource,
to order: Int,
callback: @escaping (Result<Void, Error>) -> Void) {
guard Self.isSupportedPlatform else {
return callback(.failure(PackageCollectionError.unsupportedPlatform))
}
self.storage.sources.move(source: source, to: order, callback: callback)
}
public func updateCollection(_ source: PackageCollectionsModel.CollectionSource,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void) {
guard Self.isSupportedPlatform else {
return callback(.failure(PackageCollectionError.unsupportedPlatform))
}
self.storage.sources.update(source: source) { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success:
self.refreshCollectionFromSource(source: source, trustConfirmationProvider: nil, callback: callback)
}
}
}
// Returns information about a package collection.
// The collection is not required to be in the configured list.
// If not found locally (storage), the collection will be fetched from the source.
public func getCollection(_ source: PackageCollectionsModel.CollectionSource,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void) {
guard Self.isSupportedPlatform else {
return callback(.failure(PackageCollectionError.unsupportedPlatform))
}
self.storage.collections.get(identifier: .init(from: source)) { result in
switch result {
case .failure:
// The collection is not in storage. Validate the source before fetching it.
if let errors = source.validate(fileSystem: self.fileSystem)?.errors() {
return callback(.failure(MultipleErrors(errors)))
}
guard let provider = self.collectionProviders[source.type] else {
return callback(.failure(UnknownProvider(source.type)))
}
provider.get(source, callback: callback)
case .success(let collection):
callback(.success(collection))
}
}
}
// MARK: - Packages
public func findPackages(_ query: String,
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void) {
guard Self.isSupportedPlatform else {
return callback(.failure(PackageCollectionError.unsupportedPlatform))
}
self.storage.sources.list { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success(let sources):
let identifiers = sources.map { .init(from: $0) }.filter { collections?.contains($0) ?? true }
if identifiers.isEmpty {
return callback(.success(Model.PackageSearchResult(items: [])))
}
self.storage.collections.searchPackages(identifiers: identifiers, query: query, callback: callback)
}
}
}
public func listPackages(collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void) {
self.listCollections(identifiers: collections) { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success(let collections):
var packageCollections = [PackageIdentity: (package: Model.Package, collections: Set<Model.CollectionIdentifier>)]()
// Use package data from the most recently processed collection
collections.sorted(by: { $0.lastProcessedAt > $1.lastProcessedAt }).forEach { collection in
collection.packages.forEach { package in
var entry = packageCollections.removeValue(forKey: package.identity)
if entry == nil {
entry = (package, .init())
}
if var entry = entry {
entry.collections.insert(collection.identifier)
packageCollections[package.identity] = entry
}
}
}
let result = PackageCollectionsModel.PackageSearchResult(
items: packageCollections.sorted { $0.value.package.displayName < $1.value.package.displayName }
.map { entry in
.init(package: entry.value.package, collections: Array(entry.value.collections))
}
)
callback(.success(result))
}
}
}
// MARK: - Package Metadata
public func getPackageMetadata(identity: PackageIdentity,
location: String? = .none,
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void) {
self.getPackageMetadata(identity: identity, location: location, collections: .none, callback: callback)
}
public func getPackageMetadata(identity: PackageIdentity,
location: String? = .none,
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void) {
guard Self.isSupportedPlatform else {
return callback(.failure(PackageCollectionError.unsupportedPlatform))
}
// first find in storage
self.findPackage(identity: identity, location: location, collections: collections) { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success(let packageSearchResult):
// then try to get more metadata from provider (optional)
self.metadataProvider.get(identity: packageSearchResult.package.identity, location: packageSearchResult.package.location) { result, provider in
switch result {
case .failure(let error):
self.observabilityScope.emit(
warning: "Failed fetching information about \(identity) from \(self.metadataProvider.self)",
underlyingError: error
)
let metadata = Model.PackageMetadata(
package: Self.mergedPackageMetadata(package: packageSearchResult.package, basicMetadata: nil),
collections: packageSearchResult.collections,
provider: provider
)
callback(.success(metadata))
case .success(let basicMetadata):
// finally merge the results
let metadata = Model.PackageMetadata(
package: Self.mergedPackageMetadata(package: packageSearchResult.package, basicMetadata: basicMetadata),
collections: packageSearchResult.collections,
provider: provider
)
callback(.success(metadata))
}
}
}
}
}
// MARK: - Targets
public func listTargets(collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
callback: @escaping (Result<PackageCollectionsModel.TargetListResult, Error>) -> Void) {
guard Self.isSupportedPlatform else {
return callback(.failure(PackageCollectionError.unsupportedPlatform))
}
self.listCollections(identifiers: collections) { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success(let collections):
let targets = self.targetListResultFromCollections(collections)
callback(.success(targets))
}
}
}
public func findTargets(_ query: String,
searchType: PackageCollectionsModel.TargetSearchType? = nil,
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void) {
guard Self.isSupportedPlatform else {
return callback(.failure(PackageCollectionError.unsupportedPlatform))
}
let searchType = searchType ?? .exactMatch
self.storage.sources.list { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success(let sources):
let identifiers = sources.map { .init(from: $0) }.filter { collections?.contains($0) ?? true }
if identifiers.isEmpty {
return callback(.success(.init(items: [])))
}
self.storage.collections.searchTargets(identifiers: identifiers, query: query, type: searchType, callback: callback)
}
}
}
// MARK: - Private
// Fetch the collection from the network and store it in local storage
// This helps avoid network access in normal operations
private func refreshCollectionFromSource(
source: PackageCollectionsModel.CollectionSource,
trustConfirmationProvider: ((PackageCollectionsModel.Collection, @escaping (Bool) -> Void) -> Void)?,
callback: @escaping (Result<Model.Collection, Error>) -> Void
) {
guard let provider = self.collectionProviders[source.type] else {
return callback(.failure(UnknownProvider(source.type)))
}
provider.get(source) { result in
switch result {
case .failure(let error):
// Remove the unavailable/invalid collection (if previously saved) from storage before calling back
self.storage.collections.remove(identifier: PackageCollectionsModel.CollectionIdentifier(from: source)) { _ in
callback(.failure(error))
}
case .success(let collection):
// If collection is signed and signature is valid, save to storage. `provider.get`
// would have failed if signature were invalid.
if collection.isSigned {
return self.storage.collections.put(collection: collection, callback: callback)
}
// If collection is not signed, check if it's trusted by user and prompt user if needed.
if let isTrusted = source.isTrusted {
if isTrusted {
return self.storage.collections.put(collection: collection, callback: callback)
} else {
// Try to remove the untrusted collection (if previously saved) from storage before calling back
return self.storage.collections.remove(identifier: collection.identifier) { _ in
callback(.failure(PackageCollectionError.untrusted))
}
}
}
// No user preference recorded, so we need to prompt if we can.
guard let trustConfirmationProvider = trustConfirmationProvider else {
// Try to remove the untrusted collection (if previously saved) from storage before calling back
return self.storage.collections.remove(identifier: collection.identifier) { _ in
callback(.failure(PackageCollectionError.trustConfirmationRequired))
}
}
trustConfirmationProvider(collection) { userTrusted in
var source = source
source.isTrusted = userTrusted
// Record user preference then save collection to storage
self.storage.sources.update(source: source) { updateSourceResult in
switch updateSourceResult {
case .failure(let error):
callback(.failure(error))
case .success:
if userTrusted {
var collection = collection
collection.source = source
self.storage.collections.put(collection: collection, callback: callback)
} else {
// Try to remove the untrusted collection (if previously saved) from storage before calling back
return self.storage.collections.remove(identifier: collection.identifier) { _ in
callback(.failure(PackageCollectionError.untrusted))
}
}
}
}
}
}
}
}
func findPackage(identity: PackageIdentity,
location: String?,
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult.Item, Error>) -> Void) {
self.storage.sources.list { result in
let notFoundError = NotFoundError("identity: \(identity), location: \(location ?? "none")")
switch result {
case .failure(is NotFoundError):
callback(.failure(notFoundError))
case .failure(let error):
callback(.failure(error))
case .success(let sources):
var collectionIdentifiers = sources.map { Model.CollectionIdentifier(from: $0) }
if let collections {
collectionIdentifiers = collectionIdentifiers.filter { collections.contains($0) }
}
if collectionIdentifiers.isEmpty {
return callback(.failure(notFoundError))
}
self.storage.collections.findPackage(identifier: identity, collectionIdentifiers: collectionIdentifiers) { findPackageResult in
switch findPackageResult {
case .failure(is NotFoundError):
callback(.failure(notFoundError))
case .failure(let error):
callback(.failure(error))
case .success(let packagesCollections):
let matches: [PackageCollectionsModel.Package]
if let location {
// A package identity can be associated with multiple repository URLs
matches = packagesCollections.packages.filter { CanonicalPackageLocation($0.location) == CanonicalPackageLocation(location) }
}
else {
matches = packagesCollections.packages
}
guard let package = matches.first else {
return callback(.failure(notFoundError))
}
callback(.success(.init(package: package, collections: packagesCollections.collections)))
}
}
}
}
}
private func targetListResultFromCollections(_ collections: [Model.Collection]) -> Model.TargetListResult {
var packageCollections = [PackageIdentity: (package: Model.Package, collections: Set<Model.CollectionIdentifier>)]()
var targetsPackages = [String: (target: Model.Target, packages: Set<PackageIdentity>)]()
collections.forEach { collection in
collection.packages.forEach { package in
// Avoid copy-on-write: remove entry from dictionary before mutating
var entry = packageCollections.removeValue(forKey: package.identity) ?? (package, .init())
entry.collections.insert(collection.identifier)
packageCollections[package.identity] = entry
package.versions.forEach { version in
version.manifests.values.forEach { manifest in
manifest.targets.forEach { target in
// Avoid copy-on-write: remove entry from dictionary before mutating
var entry = targetsPackages.removeValue(forKey: target.name) ?? (target: target, packages: .init())
entry.packages.insert(package.identity)
targetsPackages[target.name] = entry
}
}
}
}
}
return targetsPackages.map { _, pair in
let targetPackages = pair.packages
.compactMap { packageCollections[$0] }
.map { pair -> Model.TargetListResult.Package in
let versions = pair.package.versions.flatMap { version in
version.manifests.values.map { manifest in
Model.TargetListResult.PackageVersion(
version: version.version,
toolsVersion: manifest.toolsVersion,
packageName: manifest.packageName
)
}
}
return .init(identity: pair.package.identity,
location: pair.package.location,
summary: pair.package.summary,
versions: versions,
collections: Array(pair.collections))
}
return Model.TargetListItem(target: pair.target, packages: targetPackages)
}
}
internal static func mergedPackageMetadata(package: Model.Package,
basicMetadata: Model.PackageBasicMetadata?) -> Model.Package {
// This dictionary contains recent releases and might not contain everything that's in package.versions.
let basicVersionMetadata = basicMetadata.map { Dictionary($0.versions.map { ($0.version, $0) }, uniquingKeysWith: { first, _ in first }) } ?? [:]
var versions = package.versions.map { packageVersion -> Model.Package.Version in
let versionMetadata = basicVersionMetadata[packageVersion.version]
return .init(version: packageVersion.version,
title: versionMetadata?.title ?? packageVersion.title,
summary: versionMetadata?.summary ?? packageVersion.summary,
manifests: packageVersion.manifests,
defaultToolsVersion: packageVersion.defaultToolsVersion,
verifiedCompatibility: packageVersion.verifiedCompatibility,
license: packageVersion.license,
author: versionMetadata?.author ?? packageVersion.author,
signer: packageVersion.signer,
createdAt: versionMetadata?.createdAt ?? packageVersion.createdAt)
}
versions.sort(by: >)
return Model.Package(
identity: package.identity,
location: package.location,
summary: basicMetadata?.summary ?? package.summary,
keywords: basicMetadata?.keywords ?? package.keywords,
versions: versions,
watchersCount: basicMetadata?.watchersCount,
readmeURL: basicMetadata?.readmeURL ?? package.readmeURL,
license: basicMetadata?.license ?? package.license,
authors: basicMetadata?.authors ?? package.authors,
languages: basicMetadata?.languages ?? package.languages
)
}
}
private struct UnknownProvider: Error {
let sourceType: Model.CollectionSourceType
init(_ sourceType: Model.CollectionSourceType) {
self.sourceType = sourceType
}
}
|