Skip to content

Commit 4bfc7aa

Browse files
authored
Move PackageRegistryTool to async/await (#7190)
### Motivation: Continue replacing older Dispatch-based concurrency primitives with native `async`/`await`. ### Modifications: Replace all usage of `temp_await` with actual `async`/`await` in the `PackageRegistryTool`. ### Result: One additional executable target that is natively `async`/`await`.
1 parent 6bed023 commit 4bfc7aa

File tree

5 files changed

+47
-58
lines changed

5 files changed

+47
-58
lines changed

Sources/PackageRegistryTool/PackageRegistryTool+Auth.swift

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ private func readpassword(_ prompt: String) throws -> String {
8989
#endif
9090

9191
extension SwiftPackageRegistryTool {
92-
struct Login: SwiftCommand {
92+
struct Login: AsyncSwiftCommand {
9393

9494
static func loginURL(from registryURL: URL, loginAPIPath: String?) throws -> URL {
9595
// Login URL must be HTTPS
@@ -144,7 +144,7 @@ extension SwiftPackageRegistryTool {
144144

145145
private static let PLACEHOLDER_TOKEN_USER = "token"
146146

147-
func run(_ swiftTool: SwiftTool) throws {
147+
func run(_ swiftTool: SwiftTool) async throws {
148148
// We need to be able to read/write credentials
149149
// Make sure credentials store is available before proceeding
150150
let authorizationProvider: AuthorizationProvider?
@@ -220,15 +220,12 @@ extension SwiftPackageRegistryTool {
220220
}
221221

222222
// Save in cache so we can try the credentials and persist to storage only if login succeeds
223-
try temp_await { callback in
224-
authorizationWriter?.addOrUpdate(
225-
for: registryURL,
226-
user: storeUsername,
227-
password: storePassword,
228-
persist: false,
229-
callback: callback
230-
)
231-
}
223+
try await authorizationWriter?.addOrUpdate(
224+
for: registryURL,
225+
user: storeUsername,
226+
password: storePassword,
227+
persist: false
228+
)
232229

233230
// `url` can either be base URL of the registry, in which case the login API
234231
// is assumed to be at /login, or the full URL of the login API.
@@ -258,15 +255,13 @@ extension SwiftPackageRegistryTool {
258255
)
259256

260257
// Try logging in
261-
try temp_await { callback in
262-
registryClient.login(
263-
loginURL: loginURL,
264-
timeout: .seconds(5),
265-
observabilityScope: swiftTool.observabilityScope,
266-
callbackQueue: .sharedConcurrent,
267-
completion: callback
268-
)
269-
}
258+
try await registryClient.login(
259+
loginURL: loginURL,
260+
timeout: .seconds(5),
261+
observabilityScope: swiftTool.observabilityScope,
262+
callbackQueue: .sharedConcurrent
263+
)
264+
270265
print("Login successful.")
271266

272267
// Login successful. Persist credentials to storage.
@@ -296,15 +291,12 @@ extension SwiftPackageRegistryTool {
296291
}
297292

298293
if saveChanges {
299-
try temp_await { callback in
300-
authorizationWriter?.addOrUpdate(
301-
for: registryURL,
302-
user: storeUsername,
303-
password: storePassword,
304-
persist: true,
305-
callback: callback
306-
)
307-
}
294+
try await authorizationWriter?.addOrUpdate(
295+
for: registryURL,
296+
user: storeUsername,
297+
password: storePassword,
298+
persist: true
299+
)
308300

309301
if osStore {
310302
print("\nCredentials have been saved to the operating system's secure credential store.")
@@ -323,7 +315,7 @@ extension SwiftPackageRegistryTool {
323315
}
324316
}
325317

326-
struct Logout: SwiftCommand {
318+
struct Logout: AsyncSwiftCommand {
327319
static let configuration = CommandConfiguration(
328320
abstract: "Log out from a registry"
329321
)
@@ -338,7 +330,7 @@ extension SwiftPackageRegistryTool {
338330
self.url
339331
}
340332

341-
func run(_ swiftTool: SwiftTool) throws {
333+
func run(_ swiftTool: SwiftTool) async throws {
342334
// Auth config is in user-level registries config only
343335
let configuration = try getRegistriesConfig(swiftTool, global: true)
344336

@@ -359,7 +351,7 @@ extension SwiftPackageRegistryTool {
359351

360352
// Only OS credential store supports deletion
361353
if osStore {
362-
try temp_await { callback in authorizationWriter?.remove(for: registryURL, callback: callback) }
354+
try await authorizationWriter?.remove(for: registryURL)
363355
print("Credentials have been removed from operating system's secure credential store.")
364356
} else {
365357
print("netrc file not updated. Please remove credentials from the file manually.")

Sources/PackageRegistryTool/PackageRegistryTool+Publish.swift

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import struct TSCBasic.SHA256
3333
import struct TSCUtility.Version
3434

3535
extension SwiftPackageRegistryTool {
36-
struct Publish: SwiftCommand {
36+
struct Publish: AsyncSwiftCommand {
3737
static let metadataFilename = "package-metadata.json"
3838

3939
static let configuration = CommandConfiguration(
@@ -85,7 +85,7 @@ extension SwiftPackageRegistryTool {
8585
@Flag(help: "Dry run only; prepare the archive and sign it but do not publish to the registry.")
8686
var dryRun: Bool = false
8787

88-
func run(_ swiftTool: SwiftTool) throws {
88+
func run(_ swiftTool: SwiftTool) async throws {
8989
// Require both local and user-level registries config
9090
let configuration = try getRegistriesConfig(swiftTool, global: false).configuration
9191

@@ -207,22 +207,19 @@ extension SwiftPackageRegistryTool {
207207

208208
swiftTool.observabilityScope
209209
.emit(info: "publishing \(self.packageIdentity) archive at '\(archivePath)' to \(registryURL)")
210-
let result = try temp_await {
211-
registryClient.publish(
212-
registryURL: registryURL,
213-
packageIdentity: self.packageIdentity,
214-
packageVersion: self.packageVersion,
215-
packageArchive: archivePath,
216-
packageMetadata: metadataLocation?.path,
217-
signature: archiveSignature,
218-
metadataSignature: metadataSignature,
219-
signatureFormat: self.signatureFormat,
220-
fileSystem: localFileSystem,
221-
observabilityScope: swiftTool.observabilityScope,
222-
callbackQueue: .sharedConcurrent,
223-
completion: $0
224-
)
225-
}
210+
let result = try await registryClient.publish(
211+
registryURL: registryURL,
212+
packageIdentity: self.packageIdentity,
213+
packageVersion: self.packageVersion,
214+
packageArchive: archivePath,
215+
packageMetadata: metadataLocation?.path,
216+
signature: archiveSignature,
217+
metadataSignature: metadataSignature,
218+
signatureFormat: self.signatureFormat,
219+
fileSystem: localFileSystem,
220+
observabilityScope: swiftTool.observabilityScope,
221+
callbackQueue: .sharedConcurrent
222+
)
226223

227224
switch result {
228225
case .published(.none):

Sources/PackageRegistryTool/PackageRegistryTool.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import PackageRegistry
1919
import Workspace
2020

2121
@available(macOS 10.15, macCatalyst 13, iOS 13, tvOS 13, watchOS 6, *)
22-
public struct SwiftPackageRegistryTool: ParsableCommand {
22+
public struct SwiftPackageRegistryTool: AsyncParsableCommand {
2323
public static var configuration = CommandConfiguration(
2424
commandName: "package-registry",
2525
_superCommandName: "swift",
@@ -41,7 +41,7 @@ public struct SwiftPackageRegistryTool: ParsableCommand {
4141

4242
public init() {}
4343

44-
struct Set: SwiftCommand {
44+
struct Set: AsyncSwiftCommand {
4545
static let configuration = CommandConfiguration(
4646
abstract: "Set a custom registry"
4747
)
@@ -62,7 +62,7 @@ public struct SwiftPackageRegistryTool: ParsableCommand {
6262
self.url
6363
}
6464

65-
func run(_ swiftTool: SwiftTool) throws {
65+
func run(_ swiftTool: SwiftTool) async throws {
6666
try self.registryURL.validateRegistryURL()
6767

6868
let scope = try scope.map(PackageIdentity.Scope.init(validating:))
@@ -84,7 +84,7 @@ public struct SwiftPackageRegistryTool: ParsableCommand {
8484
}
8585
}
8686

87-
struct Unset: SwiftCommand {
87+
struct Unset: AsyncSwiftCommand {
8888
static let configuration = CommandConfiguration(
8989
abstract: "Remove a configured registry"
9090
)
@@ -98,7 +98,7 @@ public struct SwiftPackageRegistryTool: ParsableCommand {
9898
@Option(help: "Associate the registry with a given scope")
9999
var scope: String?
100100

101-
func run(_ swiftTool: SwiftTool) throws {
101+
func run(_ swiftTool: SwiftTool) async throws {
102102
let scope = try scope.map(PackageIdentity.Scope.init(validating:))
103103

104104
let unset: (inout RegistryConfiguration) throws -> Void = { configuration in

Sources/swift-package-manager/SwiftPM.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ struct SwiftPM {
3737
case "swift-package-collection":
3838
await SwiftPackageCollectionsTool.main()
3939
case "swift-package-registry":
40-
SwiftPackageRegistryTool.main()
40+
await SwiftPackageRegistryTool.main()
4141
default:
4242
fatalError("swift-package-manager launched with unexpected name: \(execName ?? "(unknown)")")
4343
}

Sources/swift-package-registry/runner.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import PackageRegistryTool
1515

1616
@main
1717
struct Runner {
18-
static func main() {
19-
SwiftPackageRegistryTool.main()
18+
static func main() async {
19+
await SwiftPackageRegistryTool.main()
2020
}
2121
}

0 commit comments

Comments
 (0)