Skip to content

Commit 057d0f5

Browse files
committed
Use PackageIdentity.Scope instead of String where applicable
1 parent ef4fb75 commit 057d0f5

File tree

5 files changed

+48
-18
lines changed

5 files changed

+48
-18
lines changed

Sources/Commands/SwiftPackageRegistryTool.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import Foundation
2424
import PackageRegistry
2525

2626
private enum RegistryConfigurationError: Swift.Error {
27-
case missingScope(String? = nil)
27+
case missingScope(PackageIdentity.Scope? = nil)
2828
case invalidURL(String)
2929
}
3030

@@ -90,6 +90,8 @@ public struct SwiftPackageRegistryTool: ParsableCommand {
9090
throw RegistryConfigurationError.invalidURL(self.url)
9191
}
9292

93+
let scope = try scope.map(PackageIdentity.Scope.init(validating:))
94+
9395
// TODO: Require login if password is specified
9496

9597
let set: (inout RegistryConfiguration) throws -> Void = { configuration in
@@ -125,6 +127,8 @@ public struct SwiftPackageRegistryTool: ParsableCommand {
125127
var scope: String?
126128

127129
func run(_ swiftTool: SwiftTool) throws {
130+
let scope = try scope.map(PackageIdentity.Scope.init(validating:))
131+
128132
let unset: (inout RegistryConfiguration) throws -> Void = { configuration in
129133
if let scope = scope {
130134
guard let _ = configuration.scopedRegistries[scope] else {
@@ -151,7 +155,6 @@ public struct SwiftPackageRegistryTool: ParsableCommand {
151155

152156
// MARK: -
153157

154-
155158
private extension SwiftTool {
156159
func getRegistriesConfig() throws -> Workspace.Configuration.Registries {
157160
let localRegistriesFile = try Workspace.DefaultLocations.registriesConfigurationFile(forRootPackage: self.getPackageRoot())

Sources/PackageModel/PackageIdentity.swift

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ public struct PackageIdentity: Hashable, CustomStringConvertible {
5959
}
6060

6161
// TODO: formalize package registry identifier
62-
public var scopeAndName: (String, String)? {
62+
public var scopeAndName: (Scope, Name)? {
6363
let components = description.split(separator: ".", maxSplits: 1, omittingEmptySubsequences: true)
64-
guard let scope = components.first,
65-
let name = components.last,
66-
components.count == 2
64+
guard components.count == 2,
65+
let scope = Scope(components.first),
66+
let name = Name(components.last)
6767
else { return nil }
6868

69-
return (String(scope), String(name))
69+
return (scope, name)
7070
}
7171
}
7272

@@ -136,6 +136,11 @@ extension PackageIdentity {
136136
self = scope
137137
}
138138

139+
fileprivate init?(_ substring: String.SubSequence?) {
140+
guard let substring = substring else { return nil }
141+
self.init(String(substring))
142+
}
143+
139144
// MARK: - Equatable & Comparable
140145

141146
private func compare(to other: Scope) -> ComparisonResult {
@@ -213,6 +218,11 @@ extension PackageIdentity {
213218
self = name
214219
}
215220

221+
fileprivate init?(_ substring: String.SubSequence?) {
222+
guard let substring = substring else { return nil }
223+
self.init(String(substring))
224+
}
225+
216226
// MARK: - Equatable & Comparable
217227

218228
private func compare(to other: Name) -> ComparisonResult {

Sources/PackageRegistry/RegistryConfiguration.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,17 @@
99
*/
1010

1111
import Foundation
12+
import PackageModel
1213

1314
public struct RegistryConfiguration: Hashable {
14-
public typealias Scope = String
15-
1615
public enum Version: Int, Codable {
1716
case v1 = 1
1817
}
1918

2019
public static let version: Version = .v1
2120

2221
public var defaultRegistry: Registry?
23-
public var scopedRegistries: [Scope: Registry]
22+
public var scopedRegistries: [PackageIdentity.Scope: Registry]
2423

2524
public init() {
2625
self.defaultRegistry = nil
@@ -41,7 +40,7 @@ public struct RegistryConfiguration: Hashable {
4140
}
4241
}
4342

44-
public func registry(for scope: Scope) -> Registry? {
43+
public func registry(for scope: PackageIdentity.Scope) -> Registry? {
4544
return scopedRegistries[scope] ?? defaultRegistry
4645
}
4746
}
@@ -79,9 +78,10 @@ extension RegistryConfiguration: Codable {
7978

8079
self.defaultRegistry = try nestedContainer.decodeIfPresent(Registry.self, forKey: .default)
8180

82-
var scopedRegistries: [Scope: Registry] = [:]
81+
var scopedRegistries: [PackageIdentity.Scope: Registry] = [:]
8382
for key in nestedContainer.allKeys where key != .default {
84-
scopedRegistries[key.stringValue] = try nestedContainer.decode(Registry.self, forKey: key)
83+
let scope = try PackageIdentity.Scope(validating: key.stringValue)
84+
scopedRegistries[scope] = try nestedContainer.decode(Registry.self, forKey: key)
8585
}
8686
self.scopedRegistries = scopedRegistries
8787
case nil:
@@ -99,7 +99,7 @@ extension RegistryConfiguration: Codable {
9999
try nestedContainer.encodeIfPresent(defaultRegistry, forKey: .default)
100100

101101
for (scope, registry) in scopedRegistries {
102-
let key = ScopeCodingKey(stringValue: scope)
102+
let key = ScopeCodingKey(stringValue: scope.description)
103103
try nestedContainer.encode(registry, forKey: key)
104104
}
105105
}

Sources/PackageRegistry/RegistryManager.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import struct Foundation.URLQueryItem
2222
import Dispatch
2323

2424
public enum RegistryError: Error {
25-
case registryNotConfigured(scope: String)
25+
case registryNotConfigured(scope: PackageIdentity.Scope)
2626
case invalidPackage(PackageReference)
2727
case invalidOperation
2828
case invalidResponse
@@ -69,7 +69,7 @@ public final class RegistryManager {
6969
}
7070

7171
var components = URLComponents(url: registry.url, resolvingAgainstBaseURL: true)
72-
components?.appendPathComponents(scope, name)
72+
components?.appendPathComponents("\(scope)", "\(name)")
7373

7474
guard let url = components?.url else {
7575
return completion(.failure(RegistryError.invalidURL))
@@ -123,7 +123,7 @@ public final class RegistryManager {
123123
}
124124

125125
var components = URLComponents(url: registry.url, resolvingAgainstBaseURL: true)
126-
components?.appendPathComponents(scope, name, "\(version)", "Package.swift")
126+
components?.appendPathComponents("\(scope)", "\(name)", "\(version)", "Package.swift")
127127
if let swiftLanguageVersion = swiftLanguageVersion {
128128
components?.queryItems = [
129129
URLQueryItem(name: "swift-version", value: swiftLanguageVersion.rawValue)
@@ -207,7 +207,7 @@ public final class RegistryManager {
207207
}
208208

209209
var components = URLComponents(url: registry.url, resolvingAgainstBaseURL: true)
210-
components?.appendPathComponents(scope, name, "\(version).zip")
210+
components?.appendPathComponents("\(scope)", "\(name)", "\(version).zip")
211211

212212
guard let url = components?.url else {
213213
return completion(.failure(RegistryError.invalidURL))

Tests/CommandsTests/PackageRegistryToolTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,23 @@ final class PackageRegistryToolTests: XCTestCase {
161161
}
162162
}
163163

164+
func testSetInvalidScope() throws {
165+
fixture(name: "DependencyResolution/External/Simple") { prefix in
166+
let packageRoot = prefix.appending(component: "Bar")
167+
let configurationFilePath = packageRoot.appending(RelativePath(".swiftpm/configuration/registries.json"))
168+
169+
XCTAssertFalse(localFileSystem.exists(configurationFilePath))
170+
171+
// Set default registry
172+
do {
173+
let result = try execute(["set", "--scope", "_invalid_", "\(defaultRegistryBaseURL)"], packagePath: packageRoot)
174+
XCTAssertNotEqual(result.exitStatus, .terminated(code: 0))
175+
}
176+
177+
XCTAssertFalse(localFileSystem.exists(configurationFilePath))
178+
}
179+
}
180+
164181
func testUnsetMissingEntry() throws {
165182
fixture(name: "DependencyResolution/External/Simple") { prefix in
166183
let packageRoot = prefix.appending(component: "Bar")

0 commit comments

Comments
 (0)