Skip to content

Commit 93f850f

Browse files
tomerdneonichuyim-lee
authored
implement package-collections business logic (#3028)
motivation: continue the implmentation of package-collections changes: * implemented PackageCollectionsProtocol which is the main business logic of the feature * added basic configuration model for the feature * added basic validation rules for collection source * simplfied model names to be shorter * added relevants tests Co-authored-by: Boris Bügling <[email protected]> Co-authored-by: Yim Lee <[email protected]>
1 parent c10c9b8 commit 93f850f

19 files changed

+1449
-143
lines changed

Sources/PackageCollections/API.swift

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,23 @@ public protocol PackageCollectionsProtocol {
3232
/// the ordering of `PackageCollection`s should be preserved and respected during conflict resolution.
3333
///
3434
/// - Parameters:
35-
/// - identifers: Optional. If specified, only `PackageCollection`s with matching identifiers will be returned.
35+
/// - identifiers: Optional. If specified, only `PackageCollection`s with matching identifiers will be returned.
3636
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
3737
/// - callback: The closure to invoke when result becomes available
38-
func listPackageCollections(
39-
identifers: Set<PackageCollectionsModel.PackageCollectionIdentifier>?,
38+
func listCollections(
39+
identifiers: Set<PackageCollectionsModel.CollectionIdentifier>?,
4040
in profile: PackageCollectionsModel.Profile?,
41-
callback: @escaping (Result<[PackageCollectionsModel.PackageCollection], Error>) -> Void
41+
callback: @escaping (Result<[PackageCollectionsModel.Collection], Error>) -> Void
4242
)
4343

4444
/// Refreshes all configured package collections.
4545
///
4646
/// - Parameters:
4747
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
4848
/// - callback: The closure to invoke after triggering a refresh for the configured package collections.
49-
func refreshPackageCollections(
49+
func refreshCollections(
5050
in profile: PackageCollectionsModel.Profile?,
51-
callback: @escaping (Result<[PackageCollectionsModel.PackageCollectionSource], Error>) -> Void
51+
callback: @escaping (Result<[PackageCollectionsModel.CollectionSource], Error>) -> Void
5252
)
5353

5454
/// Adds a package collection.
@@ -59,11 +59,11 @@ public protocol PackageCollectionsProtocol {
5959
/// By default the new collection is appended to the end (i.e., the least relevant order).
6060
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
6161
/// - callback: The closure to invoke with the updated `PackageCollection`s
62-
func addPackageCollection(
63-
_ source: PackageCollectionsModel.PackageCollectionSource,
62+
func addCollection(
63+
_ source: PackageCollectionsModel.CollectionSource,
6464
order: Int?,
6565
to profile: PackageCollectionsModel.Profile?,
66-
callback: @escaping (Result<PackageCollectionsModel.PackageCollection, Error>) -> Void
66+
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void
6767
)
6868

6969
/// Removes a package collection.
@@ -72,8 +72,8 @@ public protocol PackageCollectionsProtocol {
7272
/// - source: The package collection's source
7373
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
7474
/// - callback: The closure to invoke with the updated `PackageCollection`s
75-
func removePackageCollection(
76-
_ source: PackageCollectionsModel.PackageCollectionSource,
75+
func removeCollection(
76+
_ source: PackageCollectionsModel.CollectionSource,
7777
from profile: PackageCollectionsModel.Profile?,
7878
callback: @escaping (Result<Void, Error>) -> Void
7979
)
@@ -85,8 +85,8 @@ public protocol PackageCollectionsProtocol {
8585
/// - order: The new order that the `PackageCollection` should be positioned after the move
8686
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
8787
/// - callback: The closure to invoke with the updated `PackageCollection`s
88-
func movePackageCollection(
89-
_ source: PackageCollectionsModel.PackageCollectionSource,
88+
func moveCollection(
89+
_ source: PackageCollectionsModel.CollectionSource,
9090
to order: Int,
9191
in profile: PackageCollectionsModel.Profile?,
9292
callback: @escaping (Result<Void, Error>) -> Void
@@ -98,9 +98,9 @@ public protocol PackageCollectionsProtocol {
9898
/// - Parameters:
9999
/// - source: The package collection's source
100100
/// - callback: The closure to invoke with the `PackageCollection`
101-
func getPackageCollection(
102-
_ source: PackageCollectionsModel.PackageCollectionSource,
103-
callback: @escaping (Result<PackageCollectionsModel.PackageCollection, Error>) -> Void
101+
func getCollection(
102+
_ source: PackageCollectionsModel.CollectionSource,
103+
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void
104104
)
105105

106106
// MARK: - Package APIs
@@ -133,7 +133,7 @@ public protocol PackageCollectionsProtocol {
133133
/// - profile: Optional. The `PackageCollectionProfile` context. By default the `default` profile is used.
134134
/// - callback: The closure to invoke when result becomes available
135135
func listTargets(
136-
collections: Set<PackageCollectionsModel.PackageCollectionIdentifier>?,
136+
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
137137
in profile: PackageCollectionsModel.Profile?,
138138
callback: @escaping (Result<PackageCollectionsModel.TargetListResult, Error>) -> Void
139139
)
@@ -152,7 +152,7 @@ public protocol PackageCollectionsProtocol {
152152
/// - callback: The closure to invoke when result becomes available
153153
func findPackages(
154154
_ query: String,
155-
collections: Set<PackageCollectionsModel.PackageCollectionIdentifier>?,
155+
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
156156
profile: PackageCollectionsModel.Profile?,
157157
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void
158158
)
@@ -172,7 +172,7 @@ public protocol PackageCollectionsProtocol {
172172
func findTargets(
173173
_ query: String,
174174
searchType: PackageCollectionsModel.TargetSearchType?,
175-
collections: Set<PackageCollectionsModel.PackageCollectionIdentifier>?,
175+
collections: Set<PackageCollectionsModel.CollectionIdentifier>?,
176176
profile: PackageCollectionsModel.Profile?,
177177
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void
178178
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# This source file is part of the Swift.org open source project
2+
#
3+
# Copyright (c) 2020 Apple Inc. and the Swift project authors
4+
# Licensed under Apache License v2.0 with Runtime Library Exception
5+
#
6+
# See http://swift.org/LICENSE.txt for license information
7+
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
8+
9+
add_library(PackageCollections
10+
Model/CVE.swift
11+
Model/Collection.swift
12+
Model/License.swift
13+
Model/Package.swift
14+
Model/Profile.swift
15+
Model/Search.swift
16+
Model/TargetListResult.swift
17+
PackageCollectionProvider.swift
18+
PackageCollections+Configuration.swift
19+
PackageCollections+Storage.swift
20+
PackageCollections+Vallidation.swift
21+
PackageCollections.swift
22+
Storage/PackageCollectionsProfileStorage.swift
23+
Storage/PackageCollectionsStorage.swift
24+
Utility.swift)
25+
target_link_libraries(PackageCollections PUBLIC
26+
TSCBasic
27+
TSCUtility
28+
PackageModel
29+
SourceControl)
30+
# NOTE(compnerd) workaround for CMake not setting up include flags yet
31+
set_target_properties(PackageCollections PROPERTIES
32+
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})
33+
34+
if(USE_CMAKE_INSTALL)
35+
install(TARGETS PackageCollections
36+
ARCHIVE DESTINATION lib
37+
LIBRARY DESTINATION lib
38+
RUNTIME DESTINATION bin)
39+
endif()
40+
set_property(GLOBAL APPEND PROPERTY SwiftPM_EXPORTS PackageCollections)

Sources/PackageCollections/Model/PackageSet.swift renamed to Sources/PackageCollections/Model/Collection.swift

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public enum PackageCollectionsModel {}
1919

2020
extension PackageCollectionsModel {
2121
/// A `PackageCollection` is a collection of packages.
22-
public struct PackageCollection: Codable {
23-
public typealias Identifier = PackageCollectionIdentifier
24-
public typealias Source = PackageCollectionSource
22+
public struct Collection: Equatable, Codable {
23+
public typealias Identifier = CollectionIdentifier
24+
public typealias Source = CollectionSource
2525

2626
/// The identifier of the group
2727
public let identifier: Identifier
@@ -57,7 +57,7 @@ extension PackageCollectionsModel {
5757
createdAt: Date,
5858
lastProcessedAt: Date = Date()
5959
) {
60-
self.identifier = .init(source: source)
60+
self.identifier = .init(from: source)
6161
self.source = source
6262
self.name = name
6363
self.description = description
@@ -71,52 +71,69 @@ extension PackageCollectionsModel {
7171

7272
extension PackageCollectionsModel {
7373
/// Represents the source of a `PackageCollection`
74-
public enum PackageCollectionSource: Equatable {
75-
/// Package feed at URL
76-
case feed(URL)
74+
public struct CollectionSource: Equatable, Hashable, Codable {
75+
/// Source type
76+
public let type: CollectionSourceType
77+
78+
/// URL of the source file
79+
public let url: URL
80+
81+
public init(type: CollectionSourceType, url: URL) {
82+
self.type = type
83+
self.url = url
84+
}
7785
}
78-
}
7986

80-
extension PackageCollectionsModel.PackageCollectionSource: Codable {
81-
public enum DiscriminatorKeys: String, Codable {
87+
/// Represents the source type of a `PackageCollection`
88+
public enum CollectionSourceType: Equatable, CaseIterable {
8289
case feed
8390
}
91+
}
8492

93+
extension PackageCollectionsModel.CollectionSourceType: Codable {
8594
public enum CodingKeys: CodingKey {
8695
case _case
87-
case url
8896
}
8997

9098
public init(from decoder: Decoder) throws {
9199
let container = try decoder.container(keyedBy: CodingKeys.self)
92-
switch try container.decode(DiscriminatorKeys.self, forKey: ._case) {
93-
case .feed:
94-
let url = try container.decode(URL.self, forKey: .url)
95-
self = .feed(url)
100+
let value = try container.decode(String.self, forKey: ._case)
101+
switch value {
102+
case "feed":
103+
self = .feed
104+
default:
105+
throw UnknownType(value)
96106
}
97107
}
98108

99109
public func encode(to encoder: Encoder) throws {
100110
var container = encoder.container(keyedBy: CodingKeys.self)
101111
switch self {
102-
case .feed(let url):
103-
try container.encode(DiscriminatorKeys.feed, forKey: ._case)
104-
try container.encode(url, forKey: .url)
112+
case .feed:
113+
try container.encode("feed", forKey: ._case)
114+
}
115+
}
116+
117+
struct UnknownType: Error {
118+
let type: String
119+
120+
init(_ type: String) {
121+
self.type = type
105122
}
106123
}
107124
}
108125

109126
extension PackageCollectionsModel {
110127
/// Represents the identifier of a `PackageCollection`
111-
public enum PackageCollectionIdentifier: Hashable, Comparable {
128+
public enum CollectionIdentifier: Hashable, Comparable {
112129
/// Package feed at URL
113130
case feed(URL)
114131

115132
/// Creates an `Identifier` from `Source`
116-
init(source: PackageCollectionSource) {
117-
switch source {
118-
case .feed(let url):
119-
self = .feed(url)
133+
init(from source: CollectionSource) {
134+
switch source.type {
135+
case .feed:
136+
self = .feed(source.url)
120137
}
121138
}
122139

@@ -129,7 +146,7 @@ extension PackageCollectionsModel {
129146
}
130147
}
131148

132-
extension PackageCollectionsModel.PackageCollection.Identifier: Codable {
149+
extension PackageCollectionsModel.CollectionIdentifier: Codable {
133150
public enum DiscriminatorKeys: String, Codable {
134151
case feed
135152
}
@@ -158,9 +175,9 @@ extension PackageCollectionsModel.PackageCollection.Identifier: Codable {
158175
}
159176
}
160177

161-
extension PackageCollectionsModel.PackageCollection {
178+
extension PackageCollectionsModel.Collection {
162179
/// A representation of package metadata
163-
public struct Package: Codable {
180+
public struct Package: Equatable, Codable {
164181
public typealias Version = PackageVersion
165182

166183
/// Package reference
@@ -194,9 +211,9 @@ extension PackageCollectionsModel.PackageCollection {
194211
}
195212
}
196213

197-
extension PackageCollectionsModel.PackageCollection {
214+
extension PackageCollectionsModel.Collection {
198215
/// A representation of package version
199-
public struct PackageVersion: Codable {
216+
public struct PackageVersion: Equatable, Codable {
200217
public typealias Target = PackageCollectionsModel.PackageTarget
201218
public typealias Product = PackageCollectionsModel.PackageProduct
202219

Sources/PackageCollections/Model/License.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import struct Foundation.URL
1212

1313
extension PackageCollectionsModel {
1414
/// A representation of a package license
15-
public struct License: Codable {
15+
public struct License: Equatable, Codable {
1616
/// License type
1717
public let type: LicenseType
1818

@@ -21,7 +21,7 @@ extension PackageCollectionsModel {
2121
}
2222

2323
/// An enum of license types
24-
public enum LicenseType: CaseIterable, CustomStringConvertible {
24+
public enum LicenseType: Equatable, CaseIterable, CustomStringConvertible {
2525
// This list is taken from https://opensource.org/licenses
2626

2727
/// Apache License 2.0

Sources/PackageCollections/Model/Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ extension PackageCollectionsModel.Package {
124124

125125
extension PackageCollectionsModel {
126126
/// A representation of package target
127-
public struct PackageTarget: Codable {
127+
public struct PackageTarget: Equatable, Codable {
128128
/// The target name
129129
public let name: String
130130

@@ -135,7 +135,7 @@ extension PackageCollectionsModel {
135135

136136
extension PackageCollectionsModel {
137137
/// A representation of package product
138-
public struct PackageProduct: Codable {
138+
public struct PackageProduct: Equatable, Codable {
139139
/// The product name
140140
public let name: String
141141

@@ -168,5 +168,5 @@ extension PackageCollectionsModel.Package {
168168
}
169169

170170
extension PackageCollectionsModel {
171-
public typealias PackageMetadata = (package: PackageCollectionsModel.Package, collections: [PackageCollectionsModel.PackageCollectionIdentifier])
171+
public typealias PackageMetadata = (package: PackageCollectionsModel.Package, collections: [PackageCollectionsModel.CollectionIdentifier])
172172
}

Sources/PackageCollections/Model/Search.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ extension PackageCollectionsModel {
2121
public let package: PackageCollectionsModel.Package
2222

2323
/// Package collections that contain the package
24-
public let collections: [PackageCollectionsModel.PackageCollectionIdentifier]
24+
public let collections: [PackageCollectionsModel.CollectionIdentifier]
2525
}
2626
}
2727

@@ -32,5 +32,8 @@ extension PackageCollectionsModel {
3232
}
3333

3434
/// A representation of target in search result
35-
public typealias TargetSearchResult = TargetListResult
35+
public struct TargetSearchResult {
36+
/// Result items of the search
37+
public let items: TargetListResult
38+
}
3639
}

Sources/PackageCollections/Model/TargetListResult.swift

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ import SourceControl
1414
import TSCUtility
1515

1616
extension PackageCollectionsModel {
17-
/// Target metadata
1817
public typealias TargetListResult = [TargetListItem]
1918

2019
public struct TargetListItem {
20+
public typealias Target = PackageCollectionsModel.PackageTarget
21+
public typealias Package = PackageCollectionsModel.TargetListResult.Package
22+
2123
/// Target
22-
public let target: PackageCollectionsModel.PackageTarget
24+
public let target: Target
2325

2426
/// Packages where the target is found
2527
public let packages: [Package]
@@ -28,8 +30,8 @@ extension PackageCollectionsModel {
2830

2931
extension PackageCollectionsModel.TargetListResult {
3032
/// Metadata of package that contains the target
31-
public struct Package {
32-
public typealias Version = PackageVersion
33+
public struct Package: Hashable {
34+
public typealias Version = PackageCollectionsModel.TargetListResult.PackageVersion
3335

3436
/// Package's repository address
3537
public let repository: RepositorySpecifier
@@ -41,13 +43,13 @@ extension PackageCollectionsModel.TargetListResult {
4143
public let versions: [Version]
4244

4345
/// Package collections that contain this package and at least one of the `versions`
44-
public let collections: [PackageCollectionsModel.PackageCollectionIdentifier]
46+
public let collections: [PackageCollectionsModel.CollectionIdentifier]
4547
}
4648
}
4749

4850
extension PackageCollectionsModel.TargetListResult {
4951
/// Represents a package version
50-
public struct PackageVersion {
52+
public struct PackageVersion: Hashable {
5153
/// The version
5254
public let version: TSCUtility.Version
5355

0 commit comments

Comments
 (0)