File: PackageCollections.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (587 lines) | stat: -rw-r--r-- 26,527 bytes parent folder | download | duplicates (2)
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
//===----------------------------------------------------------------------===//
//
// 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 _Concurrency
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) async throws -> [PackageCollectionsModel.Collection] {
        guard Self.isSupportedPlatform else {
            throw PackageCollectionError.unsupportedPlatform
        }

        let sources = try await self.storage.sources.list()
        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 []
        }

        var collections = try await self.storage.collections.list(identifiers: identifiersToFetch)

        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 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 }))
        var refreshResults = [Model.Collection]()
        for source in missingSources {
            guard let refreshResult = try? await self.refreshCollectionFromSource(source: source, trustConfirmationProvider: nil) else {
                continue
            }
            refreshResults.append(refreshResult)
        }
        var result = collections + refreshResults
        result.sort(by: sort)
        return result
    }

    public func refreshCollections() async throws -> [PackageCollectionsModel.CollectionSource] {
        guard Self.isSupportedPlatform else {
            throw PackageCollectionError.unsupportedPlatform
        }

        let sources = try await self.storage.sources.list()
        guard !sources.isEmpty else {
            return []
        }

        var refreshResults = [Result<Model.Collection, Error>]()
        for source in sources {
            do {
                try await refreshResults.append(.success(self.refreshCollectionFromSource(source: source, trustConfirmationProvider: nil)))
            } catch {
                refreshResults.append(.failure(error))
            }
        }
        let failures = refreshResults.compactMap { $0.failure }
        guard failures.isEmpty else {
            throw MultipleErrors(failures)
        }
        return sources
    }

    public func refreshCollection(_ source: PackageCollectionsModel.CollectionSource) async throws -> PackageCollectionsModel.Collection {
        guard Self.isSupportedPlatform else {
            throw PackageCollectionError.unsupportedPlatform
        }

        let sources = try await self.storage.sources.list()
        guard let savedSource = sources.first(where: { $0 == source }) else {
            throw NotFoundError("\(source)")
        }
        return try await self.refreshCollectionFromSource(source: savedSource, trustConfirmationProvider: nil)
    }

    public func addCollection(_ source: PackageCollectionsModel.CollectionSource, order: Int? = nil, trustConfirmationProvider: ((PackageCollectionsModel.Collection, @escaping (Bool) -> Void) -> Void)? = nil) async throws -> PackageCollectionsModel.Collection {
        guard Self.isSupportedPlatform else {
            throw PackageCollectionError.unsupportedPlatform
        }

        if let errors = source.validate(fileSystem: self.fileSystem)?.errors() {
            throw MultipleErrors(errors)
        }

        // first record the registration
        try await self.storage.sources.add(source: source, order: order)
        // next try to fetch the collection from the network and store it locally so future operations dont need to access the network
        do {
            return try await self.refreshCollectionFromSource(source: source, trustConfirmationProvider: trustConfirmationProvider)
        } catch {
            // 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 {
                throw error
            }
            // Otherwise remove source since it fails to be fetched
            try? await self.storage.sources.remove(source: source)
            // Whether removal succeeds or not, return the refresh error
            throw error
        }

    }

    public func removeCollection(_ source: PackageCollectionsModel.CollectionSource) async throws {
        guard Self.isSupportedPlatform else {
            throw PackageCollectionError.unsupportedPlatform
        }

        try await self.storage.sources.remove(source: source)
        try await self.storage.collections.remove(identifier: .init(from: source))
    }

    public func moveCollection(_ source: PackageCollectionsModel.CollectionSource, to order: Int) async throws {
        guard Self.isSupportedPlatform else {
            throw PackageCollectionError.unsupportedPlatform
        }

        try await self.storage.sources.move(source: source, to: order)
    }

    public func updateCollection(_ source: PackageCollectionsModel.CollectionSource) async throws -> PackageCollectionsModel.Collection {
        guard Self.isSupportedPlatform else {
            throw PackageCollectionError.unsupportedPlatform
        }

        try await self.storage.sources.update(source: source)
        return try await self.refreshCollectionFromSource(source: source, trustConfirmationProvider: nil)
    }

    // 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) async throws -> PackageCollectionsModel.Collection {
        guard Self.isSupportedPlatform else {
            throw PackageCollectionError.unsupportedPlatform
        }

        do {
            return try await self.storage.collections.get(identifier: .init(from: source))
        } catch {
            // The collection is not in storage. Validate the source before fetching it.
            if let errors = source.validate(fileSystem: self.fileSystem)?.errors() {
                throw MultipleErrors(errors)
            }
            guard let provider = self.collectionProviders[source.type] else {
                throw UnknownProvider(source.type)
            }
            return try await provider.get(source)
        }
    }

    // MARK: - Packages

    public func findPackages(
        _ query: String,
        collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
    ) async throws -> PackageCollectionsModel.PackageSearchResult{
        guard Self.isSupportedPlatform else {
            throw PackageCollectionError.unsupportedPlatform
        }
        let sources = try await self.storage.sources.list()

        let identifiers = sources.map { .init(from: $0) }.filter { collections?.contains($0) ?? true }
        if identifiers.isEmpty {
            return Model.PackageSearchResult(items: [])
        }
        return try await self.storage.collections.searchPackages(identifiers: identifiers, query: query)
    }

    public func listPackages(collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil) async throws -> PackageCollectionsModel.PackageSearchResult {
        let collections = try await self.listCollections(identifiers: 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
                }
            }
        }

        return 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))
                }
        )
    }

    // MARK: - Package Metadata

    public func getPackageMetadata(
        identity: PackageModel.PackageIdentity,
        location: String? = nil,
        collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
    ) async throws -> PackageCollectionsModel.PackageMetadata {
        guard Self.isSupportedPlatform else {
            throw PackageCollectionError.unsupportedPlatform
        }

        // first find in storage
        let packageSearchResult = try await self.findPackage(identity: identity, location: location, collections: collections)
        // then try to get more metadata from provider (optional)
        let (basicMetadata, provider) = await self.metadataProvider.get(identity: packageSearchResult.package.identity, location: packageSearchResult.package.location)
        do {
            return try Model.PackageMetadata(
                package: Self.mergedPackageMetadata(package: packageSearchResult.package, basicMetadata: basicMetadata.get()),
                collections: packageSearchResult.collections,
                provider: provider
            )
        } catch {
            self.observabilityScope.emit(
                warning: "Failed fetching information about \(identity) from \(self.metadataProvider.self)",
                underlyingError: error
            )
            return Model.PackageMetadata(
                package: Self.mergedPackageMetadata(package: packageSearchResult.package, basicMetadata: nil),
                collections: packageSearchResult.collections,
                provider: provider
            )
        }
    }

    // MARK: - Targets

    public func listTargets(collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil) async throws -> PackageCollectionsModel.TargetListResult {
        guard Self.isSupportedPlatform else {
            throw PackageCollectionError.unsupportedPlatform
        }

        let collections = try await self.listCollections(identifiers: collections)
        return self.targetListResultFromCollections(collections)
    }

    public func findTargets(
        _ query: String,
        searchType: PackageCollectionsModel.TargetSearchType? = nil,
        collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
    ) async throws -> PackageCollectionsModel.TargetSearchResult {
        guard Self.isSupportedPlatform else {
            throw PackageCollectionError.unsupportedPlatform
        }

        let searchType = searchType ?? .exactMatch

        let sources = try await self.storage.sources.list()
        let identifiers = sources.map { .init(from: $0) }.filter { collections?.contains($0) ?? true }
        if identifiers.isEmpty {
            return PackageCollectionsModel.TargetSearchResult(items: [])
        }
        return try await self.storage.collections.searchTargets(identifiers: identifiers, query: query, type: searchType)
    }

    // 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)? = nil
    ) async throws -> Model.Collection {
        guard let provider = self.collectionProviders[source.type] else {
            throw UnknownProvider(source.type)
        }
        var collection: Model.Collection
        do {
            collection = try await provider.get(source)
        } catch {
            // Remove the unavailable/invalid collection (if previously saved) from storage before calling back
            try? await self.storage.collections.remove(identifier: PackageCollectionsModel.CollectionIdentifier(from: source))
            throw error
        }
        // If collection is signed and signature is valid, save to storage. `provider.get`
        // would have failed if signature were invalid.
        if collection.isSigned {
            return try await self.storage.collections.put(collection: collection)
        }

        // If collection is not signed, check if it's trusted by user and prompt user if needed.
        if let isTrusted = source.isTrusted {
            guard isTrusted else {
                // Try to remove the untrusted collection (if previously saved) from storage before calling back
                try? await self.storage.collections.remove(identifier: collection.identifier)
                throw PackageCollectionError.untrusted
            }
            return try await self.storage.collections.put(collection: collection)
        }


        // No user preference recorded, so we need to prompt if we can.
        guard let trustConfirmationProvider else {
            // Try to remove the untrusted collection (if previously saved) from storage before calling back
            try? await self.storage.collections.remove(identifier: collection.identifier)
            throw PackageCollectionError.trustConfirmationRequired
        }
        let userTrusted = await withCheckedContinuation { continuation in
            trustConfirmationProvider(collection) { result in
                continuation.resume(returning: result)
            }
        }
        var source = source
        source.isTrusted = userTrusted
        // Record user preference then save collection to storage
        try await self.storage.sources.update(source: source)

        guard userTrusted else {
            // Try to remove the untrusted collection (if previously saved) from storage before calling back
            try? await self.storage.collections.remove(identifier: collection.identifier)
            throw PackageCollectionError.untrusted
        }
        collection.source = source
        return try await self.storage.collections.put(collection: collection)
    }

    func findPackage(identity: PackageIdentity,
                     location: String? = nil,
                     collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
    ) async throws -> PackageCollectionsModel.PackageSearchResult.Item {
        let notFoundError = NotFoundError("identity: \(identity), location: \(location ?? "none")")

        let sources: [PackageCollectionsModel.CollectionSource]
        do {
            sources = try await self.storage.sources.list()
        } catch is NotFoundError {
            throw notFoundError
        }

        var collectionIdentifiers = sources.map { Model.CollectionIdentifier(from: $0) }
        if let collections {
            collectionIdentifiers = collectionIdentifiers.filter { collections.contains($0) }
        }
        guard !collectionIdentifiers.isEmpty else {
            throw notFoundError
        }
        let packagesCollections: (packages: [PackageCollectionsModel.Package], collections: [PackageCollectionsModel.CollectionIdentifier])
        do {
            packagesCollections = try await self.storage.collections.findPackage(identifier: identity, collectionIdentifiers: collectionIdentifiers)
        } catch is NotFoundError {
            throw notFoundError
        }

        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 {
            throw notFoundError
        }
        return PackageCollectionsModel.PackageSearchResult.Item(
            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
    }
}