Skip to content

Move PackageCollectionsProtocol to async/await #7726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
258 changes: 25 additions & 233 deletions Sources/PackageCollections/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,30 +27,20 @@ public protocol PackageCollectionsProtocol {
///
/// - Parameters:
/// - identifiers: Optional. If specified, only `PackageCollection`s with matching identifiers will be returned.
/// - callback: The closure to invoke when result becomes available
@available(*, noasync, message: "Use the async alternative")
func listCollections(
identifiers: Set<PackageCollectionsModel.CollectionIdentifier>?,
callback: @escaping (Result<[PackageCollectionsModel.Collection], Error>) -> Void
)
identifiers: Set<PackageCollectionsModel.CollectionIdentifier>?
) async throws -> [PackageCollectionsModel.Collection]

/// Refreshes all configured package collections.
///
/// - Parameters:
/// - callback: The closure to invoke after triggering a refresh for the configured package collections.
@available(*, noasync, message: "Use the async alternative")
func refreshCollections(callback: @escaping (Result<[PackageCollectionsModel.CollectionSource], Error>) -> Void)
func refreshCollections() async throws -> [PackageCollectionsModel.CollectionSource]

/// Refreshes a package collection.
///
/// - Parameters:
/// - source: The package collection to be refreshed
/// - callback: The closure to invoke with the refreshed `PackageCollection`
@available(*, noasync, message: "Use the async alternative")
func refreshCollection(
_ source: PackageCollectionsModel.CollectionSource,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void
)
_ source: PackageCollectionsModel.CollectionSource
) async throws -> PackageCollectionsModel.Collection

/// Adds a package collection.
///
Expand All @@ -59,77 +49,46 @@ public protocol PackageCollectionsProtocol {
/// - order: Optional. The order that the `PackageCollection` should take after being added to the list.
/// By default the new collection is appended to the end (i.e., the least relevant order).
/// - trustConfirmationProvider: The closure to invoke when the collection is not signed and user confirmation is required to proceed
/// - callback: The closure to invoke with the newly added `PackageCollection`
@available(*, noasync, message: "Use the async alternative")
func addCollection(
_ source: PackageCollectionsModel.CollectionSource,
order: Int?,
trustConfirmationProvider: ((PackageCollectionsModel.Collection, @escaping (Bool) -> Void) -> Void)?,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void
)
trustConfirmationProvider: ((PackageCollectionsModel.Collection, @escaping (Bool) -> Void) -> Void)?
) async throws -> PackageCollectionsModel.Collection

/// Removes a package collection.
///
/// - Parameters:
/// - source: The package collection's source
/// - callback: The closure to invoke with the result becomes available
@available(*, noasync, message: "Use the async alternative")
func removeCollection(
_ source: PackageCollectionsModel.CollectionSource,
callback: @escaping (Result<Void, Error>) -> Void
)
_ source: PackageCollectionsModel.CollectionSource
) async throws

/// Moves a package collection to a different order.
///
/// - Parameters:
/// - source: The source of the `PackageCollection` to be reordered
/// - order: The new order that the `PackageCollection` should be positioned after the move
/// - callback: The closure to invoke with the result becomes available
@available(*, noasync, message: "Use the async alternative")
func moveCollection(
_ source: PackageCollectionsModel.CollectionSource,
to order: Int,
callback: @escaping (Result<Void, Error>) -> Void
)
to order: Int
) async throws

/// Updates settings of a `PackageCollection` source (e.g., if it is trusted or not).
///
/// - Parameters:
/// - source: The `PackageCollection` source to be updated
/// - callback: The closure to invoke when result becomes available
@available(*, noasync, message: "Use the async alternative")
func updateCollection(
_ source: PackageCollectionsModel.CollectionSource,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void
)
_ source: PackageCollectionsModel.CollectionSource
) async throws -> PackageCollectionsModel.Collection

/// Returns information about a package collection. The collection is not required to be in the configured list. If
/// not found locally, the collection will be fetched from the source.
///
/// - Parameters:
/// - source: The package collection's source
/// - callback: The closure to invoke with the `PackageCollection`
@available(*, noasync, message: "Use the async alternative")
func getCollection(
_ source: PackageCollectionsModel.CollectionSource,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void
)

/// Returns metadata for the package identified by the given `PackageIdentity`, along with the
/// identifiers of `PackageCollection`s where the package is found.
///
/// A failure is returned if the package is not found.
///
/// - Parameters:
/// - identity: The package identity
/// - location: The package location (optional for deduplication)
/// - callback: The closure to invoke when result becomes available
@available(*, noasync, message: "Use the async alternative")
func getPackageMetadata(
identity: PackageIdentity,
location: String?,
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void
)
_ source: PackageCollectionsModel.CollectionSource
) async throws -> PackageCollectionsModel.Collection

/// Returns metadata for the package identified by the given `PackageIdentity`, along with the
/// identifiers of `PackageCollection`s where the package is found.
Expand All @@ -141,25 +100,19 @@ public protocol PackageCollectionsProtocol {
/// - location: The package location (optional for deduplication)
/// - collections: Optional. If specified, only look for package in these collections. Data from the most recently
/// processed collection will be used.
/// - callback: The closure to invoke when result becomes available
@available(*, noasync, message: "Use the async alternative")
func getPackageMetadata(
identity: PackageIdentity,
location: String?,
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void
)
collections: Set<PackageCollectionsModel.CollectionIdentifier>?
) async throws -> PackageCollectionsModel.PackageMetadata

/// Lists packages from the specified collections.
///
/// - Parameters:
/// - collections: Optional. If specified, only packages in these collections are included.
/// - callback: The closure to invoke when result becomes available
@available(*, noasync, message: "Use the async alternative")
func listPackages(
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void
)
collections: Set<PackageCollectionsModel.CollectionIdentifier>?
) async throws -> PackageCollectionsModel.PackageSearchResult

// MARK: - Target (Module) APIs

Expand All @@ -171,12 +124,9 @@ public protocol PackageCollectionsProtocol {
///
/// - Parameters:
/// - collections: Optional. If specified, only list targets within these collections.
/// - callback: The closure to invoke when result becomes available
@available(*, noasync, message: "Use the async alternative")
func listTargets(
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
callback: @escaping (Result<PackageCollectionsModel.TargetListResult, Error>) -> Void
)
collections: Set<PackageCollectionsModel.CollectionIdentifier>?
) async throws -> PackageCollectionsModel.TargetListResult

// MARK: - Search APIs

Expand All @@ -188,13 +138,10 @@ public protocol PackageCollectionsProtocol {
/// - Parameters:
/// - query: The search query
/// - collections: Optional. If specified, only search within these collections.
/// - callback: The closure to invoke when result becomes available
@available(*, noasync, message: "Use the async alternative")
func findPackages(
_ query: String,
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void
)
collections: Set<PackageCollectionsModel.CollectionIdentifier>?
) async throws -> PackageCollectionsModel.PackageSearchResult

/// Finds targets by name and returns the corresponding packages.
///
Expand All @@ -206,168 +153,13 @@ public protocol PackageCollectionsProtocol {
/// - searchType: Optional. Target names must either match exactly or contain the prefix.
/// For more flexibility, use the `findPackages` API instead.
/// - collections: Optional. If specified, only search within these collections.
/// - callback: The closure to invoke when result becomes available
@available(*, noasync, message: "Use the async alternative")
func findTargets(
_ query: String,
searchType: PackageCollectionsModel.TargetSearchType?,
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void
)
collections: Set<PackageCollectionsModel.CollectionIdentifier>?
) async throws -> PackageCollectionsModel.TargetSearchResult
}

public extension PackageCollectionsProtocol {
func listCollections(
identifiers: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
) async throws -> [PackageCollectionsModel.Collection] {
try await safe_async {
self.listCollections(identifiers: identifiers, callback: $0)
}
}

func refreshCollections() async throws -> [PackageCollectionsModel.CollectionSource] {
try await safe_async {
self.refreshCollections(callback: $0)
}
}

func refreshCollection(
_ source: PackageCollectionsModel.CollectionSource
) async throws -> PackageCollectionsModel.Collection {
try await safe_async {
self.refreshCollection(
source,
callback: $0
)
}
}

func addCollection(
_ source: PackageCollectionsModel.CollectionSource,
order: Int? = nil,
trustConfirmationProvider: ((PackageCollectionsModel.Collection, @escaping (Bool) -> Void) -> Void)? = nil
) async throws -> PackageCollectionsModel.Collection {
try await safe_async {
self.addCollection(
source,
order: order,
trustConfirmationProvider:trustConfirmationProvider,
callback: $0
)
}
}

func removeCollection(
_ source: PackageCollectionsModel.CollectionSource
) async throws {
try await safe_async {
self.removeCollection(
source,
callback: $0
)
}
}

func moveCollection(
_ source: PackageCollectionsModel.CollectionSource,
to order: Int
) async throws {
try await safe_async {
self.moveCollection(
source,
to: order,
callback: $0
)
}
}

func updateCollection(
_ source: PackageCollectionsModel.CollectionSource
) async throws -> PackageCollectionsModel.Collection {
try await safe_async {
self.updateCollection(
source,
callback: $0
)
}
}

func getCollection(
_ source: PackageCollectionsModel.CollectionSource
) async throws -> PackageCollectionsModel.Collection {
try await safe_async {
self.getCollection(
source,
callback: $0
)
}
}

func getPackageMetadata(
identity: PackageIdentity,
location: String? = nil,
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
) async throws -> PackageCollectionsModel.PackageMetadata {
try await safe_async {
self.getPackageMetadata(
identity: identity,
location: location,
collections: collections,
callback: $0
)
}
}

func listPackages(
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
) async throws -> PackageCollectionsModel.PackageSearchResult {
try await safe_async {
self.listPackages(
collections: collections,
callback: $0
)
}
}

func listTargets(
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
) async throws -> PackageCollectionsModel.TargetListResult {
try await safe_async {
self.listTargets(
collections: collections,
callback: $0
)
}
}

func findPackages(
_ query: String,
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
) async throws -> PackageCollectionsModel.PackageSearchResult {
try await safe_async {
self.findPackages(
query,
collections: collections,
callback: $0
)
}
}

func findTargets(
_ query: String,
searchType: PackageCollectionsModel.TargetSearchType? = nil,
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
) async throws -> PackageCollectionsModel.TargetSearchResult {
try await safe_async {
self.findTargets(
query,
searchType: searchType,
collections: collections,
callback: $0
)
}
}
}

public enum PackageCollectionError: Equatable, Error {
/// Package collection is not signed and there is no record of user's trust selection
Expand Down
Loading