Skip to content

Commit b55dfd9

Browse files
committed
PackageCollections: make some of the private API more public
This extends the visibility of some of the private implementation to the package level to allow use for testing without `@testable` imports.
1 parent 4d7e288 commit b55dfd9

20 files changed

+246
-110
lines changed

Sources/PackageCollections/Model/Collection.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import SourceControl
1919
public enum PackageCollectionsModel {}
2020

2121
// make things less verbose internally
22-
internal typealias Model = PackageCollectionsModel
22+
package typealias Model = PackageCollectionsModel
2323

2424
extension PackageCollectionsModel {
2525
/// A `Collection` is a collection of packages.
@@ -63,7 +63,7 @@ extension PackageCollectionsModel {
6363
}
6464

6565
/// Initializes a `Collection`
66-
init(
66+
package init(
6767
source: Source,
6868
name: String,
6969
overview: String?,
@@ -142,7 +142,7 @@ extension PackageCollectionsModel {
142142
case json(URL)
143143

144144
/// Creates an `Identifier` from `Source`
145-
init(from source: CollectionSource) {
145+
package init(from source: CollectionSource) {
146146
switch source.type {
147147
case .json:
148148
self = .json(source.url)
@@ -192,6 +192,10 @@ extension PackageCollectionsModel.Collection {
192192
public struct Author: Equatable, Codable {
193193
/// The name of the author
194194
public let name: String
195+
196+
package init(name: String) {
197+
self.name = name
198+
}
195199
}
196200
}
197201

Sources/PackageCollections/Model/License.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ extension PackageCollectionsModel {
2020

2121
/// URL of the license file
2222
public let url: URL
23+
24+
package init(type: LicenseType, url: URL) {
25+
self.type = type
26+
self.url = url
27+
}
2328
}
2429

2530
/// An enum of license types

Sources/PackageCollections/Model/PackageList.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,17 @@ extension PackageCollectionsModel {
2424

2525
/// Total number of packages
2626
public let total: Int
27+
28+
package init(
29+
items: [PackageCollectionsModel.Package],
30+
offset: Int,
31+
limit: Int,
32+
total: Int
33+
) {
34+
self.items = items
35+
self.offset = offset
36+
self.limit = limit
37+
self.total = total
38+
}
2739
}
2840
}

Sources/PackageCollections/Model/PackageTypes.swift

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ extension PackageCollectionsModel {
8888
public let languages: Set<String>?
8989

9090
/// Initializes a `Package`
91-
init(
91+
package init(
9292
identity: PackageIdentity,
9393
location: String,
9494
summary: String?,
@@ -150,6 +150,30 @@ extension PackageCollectionsModel.Package {
150150
/// When the package version was created
151151
public let createdAt: Date?
152152

153+
package init(
154+
version: TSCUtility.Version,
155+
title: String?,
156+
summary: String?,
157+
manifests: [ToolsVersion: Manifest],
158+
defaultToolsVersion: ToolsVersion,
159+
verifiedCompatibility: [PackageCollectionsModel.Compatibility]?,
160+
license: PackageCollectionsModel.License?,
161+
author: PackageCollectionsModel.Package.Author?,
162+
signer: PackageCollectionsModel.Signer?,
163+
createdAt: Date?
164+
) {
165+
self.version = version
166+
self.title = title
167+
self.summary = summary
168+
self.manifests = manifests
169+
self.defaultToolsVersion = defaultToolsVersion
170+
self.verifiedCompatibility = verifiedCompatibility
171+
self.license = license
172+
self.author = author
173+
self.signer = signer
174+
self.createdAt = createdAt
175+
}
176+
153177
public struct Manifest: Equatable, Codable {
154178
/// The Swift tools version specified in `Package.swift`.
155179
public let toolsVersion: ToolsVersion
@@ -167,6 +191,20 @@ extension PackageCollectionsModel.Package {
167191

168192
/// The package version's supported platforms
169193
public let minimumPlatformVersions: [SupportedPlatform]?
194+
195+
package init(
196+
toolsVersion: ToolsVersion,
197+
packageName: String,
198+
targets: [Target],
199+
products: [Product],
200+
minimumPlatformVersions: [SupportedPlatform]?
201+
) {
202+
self.toolsVersion = toolsVersion
203+
self.packageName = packageName
204+
self.targets = targets
205+
self.products = products
206+
self.minimumPlatformVersions = minimumPlatformVersions
207+
}
170208
}
171209
}
172210
}
@@ -179,6 +217,11 @@ extension PackageCollectionsModel {
179217

180218
/// Target module name
181219
public let moduleName: String?
220+
221+
package init(name: String, moduleName: String?) {
222+
self.name = name
223+
self.moduleName = moduleName
224+
}
182225
}
183226
}
184227

@@ -193,6 +236,12 @@ extension PackageCollectionsModel {
193236

194237
/// The product's targets
195238
public let targets: [Target]
239+
240+
package init(name: String, type: ProductType, targets: [Target]) {
241+
self.name = name
242+
self.type = type
243+
self.targets = targets
244+
}
196245
}
197246
}
198247

@@ -204,6 +253,11 @@ extension PackageCollectionsModel {
204253

205254
/// The Swift version
206255
public let swiftVersion: SwiftLanguageVersion
256+
257+
package init(platform: PackageModel.Platform, swiftVersion: SwiftLanguageVersion) {
258+
self.platform = platform
259+
self.swiftVersion = swiftVersion
260+
}
207261
}
208262
}
209263

@@ -219,10 +273,20 @@ extension PackageCollectionsModel.Package {
219273
/// Service that provides the user information
220274
public let service: Service?
221275

276+
package init(username: String, url: URL?, service: Service?) {
277+
self.username = username
278+
self.url = url
279+
self.service = service
280+
}
281+
222282
/// A representation of user service
223283
public struct Service: Equatable, Codable {
224284
/// The service name
225285
public let name: String
286+
287+
package init(name: String) {
288+
self.name = name
289+
}
226290
}
227291
}
228292
}
@@ -272,13 +336,13 @@ extension PackageCollectionsModel.Package.Version: Comparable {
272336
}
273337

274338
extension Array where Element == PackageCollectionsModel.Package.Version {
275-
var latestRelease: PackageCollectionsModel.Package.Version? {
339+
package var latestRelease: PackageCollectionsModel.Package.Version? {
276340
self.filter { $0.version.prereleaseIdentifiers.isEmpty }
277341
.sorted(by: >)
278342
.first
279343
}
280344

281-
var latestPrerelease: PackageCollectionsModel.Package.Version? {
345+
package var latestPrerelease: PackageCollectionsModel.Package.Version? {
282346
self.filter { !$0.version.prereleaseIdentifiers.isEmpty }
283347
.sorted(by: >)
284348
.first

Sources/PackageCollections/Model/Search.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ extension PackageCollectionsModel {
1818
/// Result items of the search
1919
public let items: [Item]
2020

21+
package init(items: [Item]) {
22+
self.items = items
23+
}
24+
2125
/// Represents a search result item
2226
public struct Item: Encodable {
2327
// Merged package metadata from across collections
@@ -30,7 +34,7 @@ extension PackageCollectionsModel {
3034
/// Package indexes that contain the package
3135
public internal(set) var indexes: [URL]
3236

33-
init(
37+
package init(
3438
package: PackageCollectionsModel.Package,
3539
collections: [PackageCollectionsModel.CollectionIdentifier] = [],
3640
indexes: [URL] = []

Sources/PackageCollections/PackageCollections+CertificatePolicy.swift

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import PackageCollectionsSigning
1717
/// Configuration in this file is intended for package collection sources to define certificate policies
1818
/// that are more restrictive. For example, a source may want to require that all their package
1919
/// collections be signed using certificate that belongs to certain subject user ID.
20-
internal struct PackageCollectionSourceCertificatePolicy {
20+
package struct PackageCollectionSourceCertificatePolicy {
2121
private static let defaultSourceCertPolicies: [String: [CertificatePolicyConfig]] = [
2222
"developer.apple.com": [
2323
CertificatePolicyConfig(
@@ -38,35 +38,40 @@ internal struct PackageCollectionSourceCertificatePolicy {
3838

3939
private let sourceCertPolicies: [String: [CertificatePolicyConfig]]
4040

41-
var allRootCerts: Set<String>? {
41+
package var allRootCerts: Set<String>? {
4242
let allRootCerts = self.sourceCertPolicies.values
4343
.flatMap { configs in configs.compactMap(\.base64EncodedRootCerts) }
4444
.flatMap { $0 }
4545
return allRootCerts.isEmpty ? nil : Set(allRootCerts)
4646
}
4747

48-
init(sourceCertPolicies: [String: [CertificatePolicyConfig]]? = nil) {
48+
package init(sourceCertPolicies: [String: [CertificatePolicyConfig]]? = nil) {
4949
guard sourceCertPolicies?.values.first(where: { $0.isEmpty }) == nil else {
5050
preconditionFailure("CertificatePolicyConfig array must not be empty")
5151
}
5252
self.sourceCertPolicies = sourceCertPolicies ?? Self.defaultSourceCertPolicies
5353
}
5454

55-
func mustBeSigned(source: Model.CollectionSource) -> Bool {
55+
package func mustBeSigned(source: Model.CollectionSource) -> Bool {
5656
source.certPolicyConfigKey.map { self.sourceCertPolicies[$0] != nil } ?? false
5757
}
5858

59-
func certificatePolicyKeys(for source: Model.CollectionSource) -> [CertificatePolicyKey]? {
59+
package func certificatePolicyKeys(for source: Model.CollectionSource) -> [CertificatePolicyKey]? {
6060
// Certificate policy is associated to a collection host
6161
source.certPolicyConfigKey.flatMap { self.sourceCertPolicies[$0]?.map(\.certPolicyKey) }
6262
}
6363

64-
struct CertificatePolicyConfig {
64+
package struct CertificatePolicyConfig {
6565
let certPolicyKey: CertificatePolicyKey
6666

6767
/// Root CAs of the signing certificates. Each item is the base64-encoded string
6868
/// of the DER representation of a root CA.
6969
let base64EncodedRootCerts: [String]?
70+
71+
package init(certPolicyKey: CertificatePolicyKey, base64EncodedRootCerts: [String]?) {
72+
self.certPolicyKey = certPolicyKey
73+
self.base64EncodedRootCerts = base64EncodedRootCerts
74+
}
7075
}
7176
}
7277

Sources/PackageCollections/PackageCollections+Storage.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,19 @@
1313
import protocol TSCBasic.Closable
1414

1515
extension PackageCollections {
16-
struct Storage: Closable {
17-
let sources: PackageCollectionsSourcesStorage
18-
let collections: PackageCollectionsStorage
16+
package struct Storage: Closable {
17+
package let sources: PackageCollectionsSourcesStorage
18+
package let collections: PackageCollectionsStorage
1919

20-
init(sources: PackageCollectionsSourcesStorage, collections: PackageCollectionsStorage) {
20+
package init(sources: PackageCollectionsSourcesStorage, collections: PackageCollectionsStorage) {
2121
self.sources = sources
2222
self.collections = collections
2323
}
2424
}
2525
}
2626

2727
extension PackageCollections.Storage {
28-
func close() throws {
28+
package func close() throws {
2929
var errors = [Error]()
3030

3131
let tryClose = { (item: Any) in

Sources/PackageCollections/PackageCollections+Validation.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import struct TSCUtility.Version
2020
// MARK: - Model validations
2121

2222
extension Model.CollectionSource {
23-
func validate(fileSystem: FileSystem) -> [ValidationMessage]? {
23+
package func validate(fileSystem: FileSystem) -> [ValidationMessage]? {
2424
var messages: [ValidationMessage]?
2525
let appendMessage = { (message: ValidationMessage) in
2626
if messages == nil {
@@ -197,11 +197,11 @@ public struct ValidationMessage: Equatable, CustomStringConvertible {
197197
self.property = property
198198
}
199199

200-
static func error(_ message: String, property: String? = nil) -> ValidationMessage {
200+
package static func error(_ message: String, property: String? = nil) -> ValidationMessage {
201201
.init(message, level: .error, property: property)
202202
}
203203

204-
static func warning(_ message: String, property: String? = nil) -> ValidationMessage {
204+
package static func warning(_ message: String, property: String? = nil) -> ValidationMessage {
205205
.init(message, level: .warning, property: property)
206206
}
207207

@@ -216,7 +216,7 @@ public struct ValidationMessage: Equatable, CustomStringConvertible {
216216
}
217217

218218
extension Array where Element == ValidationMessage {
219-
func errors(include levels: Set<ValidationMessage.Level> = [.error]) -> [ValidationError]? {
219+
package func errors(include levels: Set<ValidationMessage.Level> = [.error]) -> [ValidationError]? {
220220
let errors = self.filter { levels.contains($0.level) }
221221

222222
guard !errors.isEmpty else { return nil }

Sources/PackageCollections/PackageCollections.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ import protocol TSCBasic.Closable
2121
public struct PackageCollections: PackageCollectionsProtocol, Closable {
2222
// Check JSONPackageCollectionProvider.isSignatureCheckSupported before updating or removing this
2323
#if os(macOS) || os(Linux) || os(Windows) || os(Android)
24-
static let isSupportedPlatform = true
24+
package static let isSupportedPlatform = true
2525
#else
26-
static let isSupportedPlatform = false
26+
package static let isSupportedPlatform = false
2727
#endif
2828

29-
let configuration: Configuration
29+
package let configuration: Configuration
3030
private let fileSystem: FileSystem
3131
private let observabilityScope: ObservabilityScope
3232
private let storageContainer: (storage: Storage, owned: Bool)
3333
private let collectionProviders: [Model.CollectionSourceType: PackageCollectionProvider]
34-
let metadataProvider: PackageMetadataProvider
34+
package let metadataProvider: PackageMetadataProvider
3535

3636
private var storage: Storage {
3737
self.storageContainer.storage
@@ -92,7 +92,7 @@ public struct PackageCollections: PackageCollectionsProtocol, Closable {
9292
}
9393

9494
// internal initializer for testing
95-
init(configuration: Configuration = .init(),
95+
package init(configuration: Configuration = .init(),
9696
fileSystem: FileSystem,
9797
observabilityScope: ObservabilityScope,
9898
storage: Storage,
@@ -545,8 +545,8 @@ public struct PackageCollections: PackageCollectionsProtocol, Closable {
545545
}
546546
}
547547

548-
internal static func mergedPackageMetadata(package: Model.Package,
549-
basicMetadata: Model.PackageBasicMetadata?) -> Model.Package {
548+
package static func mergedPackageMetadata(package: Model.Package,
549+
basicMetadata: Model.PackageBasicMetadata?) -> Model.Package {
550550
// This dictionary contains recent releases and might not contain everything that's in package.versions.
551551
let basicVersionMetadata = basicMetadata.map { Dictionary($0.versions.map { ($0.version, $0) }, uniquingKeysWith: { first, _ in first }) } ?? [:]
552552
var versions = package.versions.map { packageVersion -> Model.Package.Version in

Sources/PackageCollections/PackageIndex+Configuration.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public struct PackageIndexConfiguration: Equatable {
2121
public var cacheMaxSizeInMegabytes: Int
2222

2323
// TODO: rdar://87575573 remove feature flag
24-
public internal(set) var enabled = ProcessInfo.processInfo.environment["SWIFTPM_ENABLE_PACKAGE_INDEX"] == "1"
24+
public package(set) var enabled = ProcessInfo.processInfo.environment["SWIFTPM_ENABLE_PACKAGE_INDEX"] == "1"
2525

2626
public init(
2727
url: URL? = nil,

0 commit comments

Comments
 (0)