Skip to content

Move PackageRegistryTool to async/await #7190

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
Dec 12, 2023
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
56 changes: 24 additions & 32 deletions Sources/PackageRegistryTool/PackageRegistryTool+Auth.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ private func readpassword(_ prompt: String) throws -> String {
#endif

extension SwiftPackageRegistryTool {
struct Login: SwiftCommand {
struct Login: AsyncSwiftCommand {

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

private static let PLACEHOLDER_TOKEN_USER = "token"

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

// Save in cache so we can try the credentials and persist to storage only if login succeeds
try temp_await { callback in
authorizationWriter?.addOrUpdate(
for: registryURL,
user: storeUsername,
password: storePassword,
persist: false,
callback: callback
)
}
try await authorizationWriter?.addOrUpdate(
for: registryURL,
user: storeUsername,
password: storePassword,
persist: false
)

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

// Try logging in
try temp_await { callback in
registryClient.login(
loginURL: loginURL,
timeout: .seconds(5),
observabilityScope: swiftTool.observabilityScope,
callbackQueue: .sharedConcurrent,
completion: callback
)
}
try await registryClient.login(
loginURL: loginURL,
timeout: .seconds(5),
observabilityScope: swiftTool.observabilityScope,
callbackQueue: .sharedConcurrent
)

print("Login successful.")

// Login successful. Persist credentials to storage.
Expand Down Expand Up @@ -296,15 +291,12 @@ extension SwiftPackageRegistryTool {
}

if saveChanges {
try temp_await { callback in
authorizationWriter?.addOrUpdate(
for: registryURL,
user: storeUsername,
password: storePassword,
persist: true,
callback: callback
)
}
try await authorizationWriter?.addOrUpdate(
for: registryURL,
user: storeUsername,
password: storePassword,
persist: true
)

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

struct Logout: SwiftCommand {
struct Logout: AsyncSwiftCommand {
static let configuration = CommandConfiguration(
abstract: "Log out from a registry"
)
Expand All @@ -338,7 +330,7 @@ extension SwiftPackageRegistryTool {
self.url
}

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

Expand All @@ -359,7 +351,7 @@ extension SwiftPackageRegistryTool {

// Only OS credential store supports deletion
if osStore {
try temp_await { callback in authorizationWriter?.remove(for: registryURL, callback: callback) }
try await authorizationWriter?.remove(for: registryURL)
print("Credentials have been removed from operating system's secure credential store.")
} else {
print("netrc file not updated. Please remove credentials from the file manually.")
Expand Down
33 changes: 15 additions & 18 deletions Sources/PackageRegistryTool/PackageRegistryTool+Publish.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import struct TSCBasic.SHA256
import struct TSCUtility.Version

extension SwiftPackageRegistryTool {
struct Publish: SwiftCommand {
struct Publish: AsyncSwiftCommand {
static let metadataFilename = "package-metadata.json"

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

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

Expand Down Expand Up @@ -207,22 +207,19 @@ extension SwiftPackageRegistryTool {

swiftTool.observabilityScope
.emit(info: "publishing \(self.packageIdentity) archive at '\(archivePath)' to \(registryURL)")
let result = try temp_await {
registryClient.publish(
registryURL: registryURL,
packageIdentity: self.packageIdentity,
packageVersion: self.packageVersion,
packageArchive: archivePath,
packageMetadata: metadataLocation?.path,
signature: archiveSignature,
metadataSignature: metadataSignature,
signatureFormat: self.signatureFormat,
fileSystem: localFileSystem,
observabilityScope: swiftTool.observabilityScope,
callbackQueue: .sharedConcurrent,
completion: $0
)
}
let result = try await registryClient.publish(
registryURL: registryURL,
packageIdentity: self.packageIdentity,
packageVersion: self.packageVersion,
packageArchive: archivePath,
packageMetadata: metadataLocation?.path,
signature: archiveSignature,
metadataSignature: metadataSignature,
signatureFormat: self.signatureFormat,
fileSystem: localFileSystem,
observabilityScope: swiftTool.observabilityScope,
callbackQueue: .sharedConcurrent
)

switch result {
case .published(.none):
Expand Down
10 changes: 5 additions & 5 deletions Sources/PackageRegistryTool/PackageRegistryTool.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import PackageRegistry
import Workspace

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

public init() {}

struct Set: SwiftCommand {
struct Set: AsyncSwiftCommand {
static let configuration = CommandConfiguration(
abstract: "Set a custom registry"
)
Expand All @@ -62,7 +62,7 @@ public struct SwiftPackageRegistryTool: ParsableCommand {
self.url
}

func run(_ swiftTool: SwiftTool) throws {
func run(_ swiftTool: SwiftTool) async throws {
try self.registryURL.validateRegistryURL()

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

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

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

let unset: (inout RegistryConfiguration) throws -> Void = { configuration in
Expand Down
2 changes: 1 addition & 1 deletion Sources/swift-package-manager/SwiftPM.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ struct SwiftPM {
case "swift-package-collection":
SwiftPackageCollectionsTool.main()
case "swift-package-registry":
SwiftPackageRegistryTool.main()
await SwiftPackageRegistryTool.main()
default:
fatalError("swift-package-manager launched with unexpected name: \(execName ?? "(unknown)")")
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/swift-package-registry/runner.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import PackageRegistryTool

@main
struct Runner {
static func main() {
SwiftPackageRegistryTool.main()
static func main() async {
await SwiftPackageRegistryTool.main()
}
}