Skip to content

remove profile #3068

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
Nov 19, 2020
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
34 changes: 1 addition & 33 deletions Sources/PackageCollections/API.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@ import PackageModel
import SourceControl

public protocol PackageCollectionsProtocol {
// MARK: - Package collection profile APIs

/// Lists package collection profiles.
///
/// The result of this API does not include `PackageCollection` data. All other APIs in this
/// protocol require the context of a profile. Implementations should support a "default"
/// profile such that the `profile` API parameter can be optional.
///
/// - Parameters:
/// - callback: The closure to invoke when result becomes available
func listProfiles(callback: @escaping (Result<[PackageCollectionsModel.Profile], Error>) -> Void)

// MARK: - Package set APIs

/// Returns packages organized into collections.
Expand All @@ -33,48 +21,38 @@ public protocol PackageCollectionsProtocol {
///
/// - Parameters:
/// - identifiers: Optional. If specified, only `PackageCollection`s with matching identifiers will be returned.
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
/// - callback: The closure to invoke when result becomes available
func listCollections(
identifiers: Set<PackageCollectionsModel.CollectionIdentifier>?,
in profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<[PackageCollectionsModel.Collection], Error>) -> Void
)

/// Refreshes all configured package collections.
///
/// - Parameters:
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
/// - callback: The closure to invoke after triggering a refresh for the configured package collections.
func refreshCollections(
in profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<[PackageCollectionsModel.CollectionSource], Error>) -> Void
)
func refreshCollections(callback: @escaping (Result<[PackageCollectionsModel.CollectionSource], Error>) -> Void)

/// Adds a package collection.
///
/// - Parameters:
/// - source: The package collection's source
/// - 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).
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
/// - callback: The closure to invoke with the updated `PackageCollection`s
func addCollection(
_ source: PackageCollectionsModel.CollectionSource,
order: Int?,
to profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void
)

/// Removes a package collection.
///
/// - Parameters:
/// - source: The package collection's source
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
/// - callback: The closure to invoke with the updated `PackageCollection`s
func removeCollection(
_ source: PackageCollectionsModel.CollectionSource,
from profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<Void, Error>) -> Void
)

Expand All @@ -83,12 +61,10 @@ public protocol PackageCollectionsProtocol {
/// - Parameters:
/// - id: The identifier of the `PackageCollection` to be moved
/// - order: The new order that the `PackageCollection` should be positioned after the move
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
/// - callback: The closure to invoke with the updated `PackageCollection`s
func moveCollection(
_ source: PackageCollectionsModel.CollectionSource,
to order: Int,
in profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<Void, Error>) -> Void
)

Expand All @@ -112,11 +88,9 @@ public protocol PackageCollectionsProtocol {
///
/// - Parameters:
/// - reference: The package reference
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
/// - callback: The closure to invoke when result becomes available
func getPackageMetadata(
_ reference: PackageReference,
profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void
)

Expand All @@ -130,11 +104,9 @@ public protocol PackageCollectionsProtocol {
///
/// - Parameters:
/// - collections: Optional. If specified, only list targets within these collections.
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
/// - callback: The closure to invoke when result becomes available
func listTargets(
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
in profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<PackageCollectionsModel.TargetListResult, Error>) -> Void
)

Expand All @@ -148,12 +120,10 @@ public protocol PackageCollectionsProtocol {
/// - Parameters:
/// - query: The search query
/// - collections: Optional. If specified, only search within these collections.
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
/// - callback: The closure to invoke when result becomes available
func findPackages(
_ query: String,
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void
)

Expand All @@ -167,13 +137,11 @@ 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.
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
/// - callback: The closure to invoke when result becomes available
func findTargets(
_ query: String,
searchType: PackageCollectionsModel.TargetSearchType?,
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
profile: PackageCollectionsModel.Profile?,
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void
)
}
3 changes: 1 addition & 2 deletions Sources/PackageCollections/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@ add_library(PackageCollections
Model/Collection.swift
Model/License.swift
Model/Package.swift
Model/Profile.swift
Model/Search.swift
Model/TargetListResult.swift
Providers/GitHubPackageMetadataProvider.swift
Providers/JSONPackageCollectionProvider.swift
Providers/PackageCollectionProvider.swift
Providers/PackageMetadataProvider.swift
Storage/PackageCollectionsProfileStorage.swift
Storage/PackageCollectionsSourcesStorage.swift
Storage/PackageCollectionsStorage.swift
API.swift
PackageCollections.swift
Expand Down
24 changes: 0 additions & 24 deletions Sources/PackageCollections/Model/Profile.swift

This file was deleted.

8 changes: 4 additions & 4 deletions Sources/PackageCollections/PackageCollections+Storage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ import TSCBasic

extension PackageCollections {
public struct Storage: Closable {
let collectionsProfiles: PackageCollectionsProfileStorage
let sources: PackageCollectionsSourcesStorage
let collections: PackageCollectionsStorage

public init(collectionsProfiles: PackageCollectionsProfileStorage, collections: PackageCollectionsStorage) {
self.collectionsProfiles = collectionsProfiles
public init(sources: PackageCollectionsSourcesStorage, collections: PackageCollectionsStorage) {
self.sources = sources
self.collections = collections
}
}
Expand All @@ -36,7 +36,7 @@ extension PackageCollections.Storage {
}
}

tryClose(self.collectionsProfiles)
tryClose(self.sources)
tryClose(self.collections)

if !errors.isEmpty {
Expand Down
76 changes: 15 additions & 61 deletions Sources/PackageCollections/PackageCollections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,11 @@ public struct PackageCollections: PackageCollectionsProtocol {
self.metadataProvider = metadataProvider
}

// MARK: - Profiles

public func listProfiles(callback: @escaping (Result<[PackageCollectionsModel.Profile], Error>) -> Void) {
self.storage.collectionsProfiles.listProfiles(callback: callback)
}

// MARK: - Collections

public func listCollections(identifiers: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
in profile: PackageCollectionsModel.Profile? = nil,
callback: @escaping (Result<[PackageCollectionsModel.Collection], Error>) -> Void) {
let profile = profile ?? .default

self.storage.collectionsProfiles.listSources(in: profile) { result in
self.storage.sources.list { result in
switch result {
case .failure(let error):
callback(.failure(error))
Expand Down Expand Up @@ -69,11 +60,8 @@ public struct PackageCollections: PackageCollectionsProtocol {
}
}

public func refreshCollections(in profile: PackageCollectionsModel.Profile? = nil,
callback: @escaping (Result<[PackageCollectionsModel.CollectionSource], Error>) -> Void) {
let profile = profile ?? .default

self.storage.collectionsProfiles.listSources(in: profile) { result in
public func refreshCollections(callback: @escaping (Result<[PackageCollectionsModel.CollectionSource], Error>) -> Void) {
self.storage.sources.list { result in
switch result {
case .failure(let error):
callback(.failure(error))
Expand All @@ -84,7 +72,7 @@ public struct PackageCollections: PackageCollectionsProtocol {
let lock = Lock()
var refreshResults = [Result<PackageCollectionsModel.Collection, Error>]()
sources.forEach { source in
self.refreshCollectionFromSource(source: source, profile: profile) { refreshResult in
self.refreshCollectionFromSource(source: source) { refreshResult in
lock.withLock { refreshResults.append(refreshResult) }
if refreshResults.count == (lock.withLock { sources.count }) {
let errors = refreshResults.compactMap { $0.failure }
Expand All @@ -98,60 +86,39 @@ public struct PackageCollections: PackageCollectionsProtocol {

public func addCollection(_ source: PackageCollectionsModel.CollectionSource,
order: Int? = nil,
to profile: PackageCollectionsModel.Profile? = nil,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void) {
let profile = profile ?? .default

if let errors = source.validate() {
return callback(.failure(MultipleErrors(errors)))
}

// first record the registration
self.storage.collectionsProfiles.add(source: source, order: order, to: profile) { result in
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, order: order, profile: profile, callback: callback)
self.refreshCollectionFromSource(source: source, order: order, callback: callback)
}
}
}

public func removeCollection(_ source: PackageCollectionsModel.CollectionSource,
from profile: PackageCollectionsModel.Profile? = nil,
callback: @escaping (Result<Void, Error>) -> Void) {
let profile = profile ?? .default

self.storage.collectionsProfiles.remove(source: source, from: profile) { result in
self.storage.sources.remove(source: source) { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success:
// check to see if the collection is used in some other profile,
// if not delete it from storage to reduce disk space
self.storage.collectionsProfiles.exists(source: source, in: nil) { result in
switch result {
case .failure(let error):
callback(.failure(error))
case .success(let exists):
if exists {
callback(.success(()))
} else {
self.storage.collections.remove(identifier: .init(from: source), callback: callback)
}
}
}
self.storage.collections.remove(identifier: .init(from: source), callback: callback)
}
}
}

public func moveCollection(_ source: PackageCollectionsModel.CollectionSource,
to order: Int, in profile: PackageCollectionsModel.Profile? = nil,
to order: Int,
callback: @escaping (Result<Void, Error>) -> Void) {
let profile = profile ?? .default

self.storage.collectionsProfiles.move(source: source, to: order, in: profile, callback: callback)
self.storage.sources.move(source: source, to: order, callback: callback)
}

// Returns information about a package collection.
Expand Down Expand Up @@ -181,12 +148,9 @@ public struct PackageCollections: PackageCollectionsProtocol {
public func findPackages(
_ query: String,
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
profile: PackageCollectionsModel.Profile? = nil,
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void
) {
let profile = profile ?? .default

self.storage.collectionsProfiles.listSources(in: profile) { result in
self.storage.sources.list { result in
switch result {
case .failure(let error):
callback(.failure(error))
Expand All @@ -203,12 +167,9 @@ public struct PackageCollections: PackageCollectionsProtocol {
// MARK: - Package Metadata

public func getPackageMetadata(_ reference: PackageReference,
profile: PackageCollectionsModel.Profile? = nil,
callback: @escaping (Result<PackageCollectionsModel.PackageMetadata, Error>) -> Void) {
let profile = profile ?? .default

// first find in storage
self.findPackage(identifier: reference.identity, profile: profile) { result in
self.findPackage(identifier: reference.identity) { result in
switch result {
case .failure(let error):
callback(.failure(error))
Expand Down Expand Up @@ -241,10 +202,9 @@ public struct PackageCollections: PackageCollectionsProtocol {

public func listTargets(
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
in profile: PackageCollectionsModel.Profile? = nil,
callback: @escaping (Result<PackageCollectionsModel.TargetListResult, Error>) -> Void
) {
self.listCollections(identifiers: collections, in: profile) { result in
self.listCollections(identifiers: collections) { result in
switch result {
case .failure(let error):
callback(.failure(error))
Expand All @@ -259,13 +219,11 @@ public struct PackageCollections: PackageCollectionsProtocol {
_ query: String,
searchType: PackageCollectionsModel.TargetSearchType? = nil,
collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil,
profile: PackageCollectionsModel.Profile? = nil,
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void
) {
let profile = profile ?? .default
let searchType = searchType ?? .exactMatch

self.storage.collectionsProfiles.listSources(in: profile) { result in
self.storage.sources.list { result in
switch result {
case .failure(let error):
callback(.failure(error))
Expand All @@ -285,7 +243,6 @@ public struct PackageCollections: PackageCollectionsProtocol {
// This helps avoid network access in normal operations
private func refreshCollectionFromSource(source: PackageCollectionsModel.CollectionSource,
order _: Int? = nil,
profile _: PackageCollectionsModel.Profile? = nil,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void) {
if let errors = source.validate() {
return callback(.failure(MultipleErrors(errors)))
Expand All @@ -305,12 +262,9 @@ public struct PackageCollections: PackageCollectionsProtocol {

func findPackage(
identifier: PackageIdentity,
profile: PackageCollectionsModel.Profile? = nil,
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult.Item, Error>) -> Void
) {
let profile = profile ?? .default

self.storage.collectionsProfiles.listSources(in: profile) { result in
self.storage.sources.list { result in
switch result {
case .failure(let error):
callback(.failure(error))
Expand Down
Loading