Skip to content

Commit 056f9f8

Browse files
Move SwiftPackageCollectionsTool to async/await (#7183)
### Motivation: Take another step to `async`/`await` in SwiftPM without breaking its API. ### Modifications: Move code associated with `SwiftPackageCollectionsTool` to `async`/`await`. ### Result: No Dispatch or `temp_await` usage in `SwiftPackageCollectionsTool` --------- Co-authored-by: Max Desiatov <[email protected]>
1 parent 351222f commit 056f9f8

File tree

4 files changed

+57
-38
lines changed

4 files changed

+57
-38
lines changed

Sources/PackageCollectionsTool/SwiftPackageCollectionsTool.swift

Lines changed: 31 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct JSONOptions: ParsableArguments {
5454
var json: Bool = false
5555
}
5656

57-
public struct SwiftPackageCollectionsTool: ParsableCommand {
57+
public struct SwiftPackageCollectionsTool: AsyncParsableCommand {
5858
public static var configuration = CommandConfiguration(
5959
commandName: "package-collection",
6060
_superCommandName: "swift",
@@ -76,7 +76,7 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
7676

7777
// MARK: Collections
7878

79-
struct List: SwiftCommand {
79+
struct List: AsyncSwiftCommand {
8080
static let configuration = CommandConfiguration(abstract: "List configured collections")
8181

8282
@OptionGroup
@@ -85,9 +85,9 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
8585
@OptionGroup(visibility: .hidden)
8686
var globalOptions: GlobalOptions
8787

88-
func run(_ swiftTool: SwiftTool) throws {
89-
let collections = try with(swiftTool) { collections in
90-
try temp_await { collections.listCollections(identifiers: nil, callback: $0) }
88+
func run(_ swiftTool: SwiftTool) async throws {
89+
let collections = try await with(swiftTool) { collections in
90+
try await collections.listCollections(identifiers: nil)
9191
}
9292

9393
if self.jsonOptions.json {
@@ -100,21 +100,21 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
100100
}
101101
}
102102

103-
struct Refresh: SwiftCommand {
103+
struct Refresh: AsyncSwiftCommand {
104104
static let configuration = CommandConfiguration(abstract: "Refresh configured collections")
105105

106106
@OptionGroup(visibility: .hidden)
107107
var globalOptions: GlobalOptions
108108

109-
func run(_ swiftTool: SwiftTool) throws {
110-
let collections = try with(swiftTool) { collections in
111-
try temp_await { collections.refreshCollections(callback: $0) }
109+
func run(_ swiftTool: SwiftTool) async throws {
110+
let collections = try await with(swiftTool) { collections in
111+
try await collections.refreshCollections()
112112
}
113113
print("Refreshed \(collections.count) configured package collection\(collections.count == 1 ? "" : "s").")
114114
}
115115
}
116116

117-
struct Add: SwiftCommand {
117+
struct Add: AsyncSwiftCommand {
118118
static let configuration = CommandConfiguration(abstract: "Add a new collection")
119119

120120
@Argument(help: "URL of the collection to add")
@@ -132,20 +132,15 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
132132
@OptionGroup(visibility: .hidden)
133133
var globalOptions: GlobalOptions
134134

135-
func run(_ swiftTool: SwiftTool) throws {
135+
func run(_ swiftTool: SwiftTool) async throws {
136136
let collectionURL = try url(self.collectionURL)
137137

138138
let source = PackageCollectionsModel.CollectionSource(type: .json, url: collectionURL, skipSignatureCheck: self.skipSignatureCheck)
139-
let collection: PackageCollectionsModel.Collection = try with(swiftTool) { collections in
139+
let collection: PackageCollectionsModel.Collection = try await with(swiftTool) { collections in
140140
do {
141141
let userTrusted = self.trustUnsigned
142-
return try temp_await {
143-
collections.addCollection(
144-
source,
145-
order: order,
146-
trustConfirmationProvider: { _, callback in callback(userTrusted) },
147-
callback: $0
148-
)
142+
return try await collections.addCollection(source, order: order) { _, callback in
143+
callback(userTrusted)
149144
}
150145
} catch PackageCollectionError.trustConfirmationRequired, PackageCollectionError.untrusted {
151146
throw CollectionsError.unsigned
@@ -162,7 +157,7 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
162157
}
163158
}
164159

165-
struct Remove: SwiftCommand {
160+
struct Remove: AsyncSwiftCommand {
166161
static let configuration = CommandConfiguration(abstract: "Remove a configured collection")
167162

168163
@Argument(help: "URL of the collection to remove")
@@ -171,13 +166,13 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
171166
@OptionGroup(visibility: .hidden)
172167
var globalOptions: GlobalOptions
173168

174-
func run(_ swiftTool: SwiftTool) throws {
169+
func run(_ swiftTool: SwiftTool) async throws {
175170
let collectionURL = try url(self.collectionURL)
176171

177172
let source = PackageCollectionsModel.CollectionSource(type: .json, url: collectionURL)
178-
try with(swiftTool) { collections in
179-
let collection = try temp_await { collections.getCollection(source, callback: $0) }
180-
_ = try temp_await { collections.removeCollection(source, callback: $0) }
173+
try await with(swiftTool) { collections in
174+
let collection = try await collections.getCollection(source)
175+
_ = try await collections.removeCollection(source)
181176
print("Removed \"\(collection.name)\" from your package collections.")
182177
}
183178
}
@@ -190,7 +185,7 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
190185
case module
191186
}
192187

193-
struct Search: SwiftCommand {
188+
struct Search: AsyncSwiftCommand {
194189
static var configuration = CommandConfiguration(abstract: "Search for packages by keywords or module names")
195190

196191
@OptionGroup
@@ -205,11 +200,11 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
205200
@OptionGroup(visibility: .hidden)
206201
var globalOptions: GlobalOptions
207202

208-
func run(_ swiftTool: SwiftTool) throws {
209-
try with(swiftTool) { collections in
203+
func run(_ swiftTool: SwiftTool) async throws {
204+
try await with(swiftTool) { collections in
210205
switch searchMethod {
211206
case .keywords:
212-
let results = try temp_await { collections.findPackages(searchQuery, collections: nil, callback: $0) }
207+
let results = try await collections.findPackages(searchQuery, collections: nil)
213208

214209
if jsonOptions.json {
215210
try JSONEncoder.makeWithDefaults().print(results.items)
@@ -220,7 +215,7 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
220215
}
221216

222217
case .module:
223-
let results = try temp_await { collections.findTargets(searchQuery, searchType: .exactMatch, collections: nil, callback: $0) }
218+
let results = try await collections.findTargets(searchQuery, searchType: .exactMatch, collections: nil)
224219

225220
let packages = Set(results.items.flatMap { $0.packages })
226221
if jsonOptions.json {
@@ -237,7 +232,7 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
237232

238233
// MARK: Packages
239234

240-
struct Describe: SwiftCommand {
235+
struct Describe: AsyncSwiftCommand {
241236
static var configuration = CommandConfiguration(abstract: "Get metadata for a collection or a package included in an imported collection")
242237

243238
@OptionGroup
@@ -287,12 +282,12 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
287282
"""
288283
}
289284

290-
func run(_ swiftTool: SwiftTool) throws {
291-
try with(swiftTool) { collections in
285+
func run(_ swiftTool: SwiftTool) async throws {
286+
try await with(swiftTool) { collections in
292287
let identity = PackageIdentity(urlString: self.packageURL)
293288

294289
do { // assume URL is for a package in an imported collection
295-
let result = try temp_await { collections.getPackageMetadata(identity: identity, location: self.packageURL, callback: $0) }
290+
let result = try await collections.getPackageMetadata(identity: identity, location: self.packageURL)
296291

297292
if let versionString = version {
298293
guard let version = TSCUtility.Version(versionString), let result = result.package.versions.first(where: { $0.version == version }), let printedResult = printVersion(result) else {
@@ -333,7 +328,7 @@ public struct SwiftPackageCollectionsTool: ParsableCommand {
333328

334329
do {
335330
let source = PackageCollectionsModel.CollectionSource(type: .json, url: collectionURL, skipSignatureCheck: self.skipSignatureCheck)
336-
let collection = try temp_await { collections.getCollection(source, callback: $0) }
331+
let collection = try await collections.getCollection(source)
337332

338333
let description = optionalRow("Description", collection.overview)
339334
let keywords = optionalRow("Keywords", collection.keywords?.joined(separator: ", "))
@@ -386,7 +381,7 @@ private extension JSONEncoder {
386381
}
387382

388383
private extension ParsableCommand {
389-
func with<T>(_ swiftTool: SwiftTool, handler: (_ collections: PackageCollectionsProtocol) throws -> T) throws -> T {
384+
func with<T>(_ swiftTool: SwiftTool, handler: (_ collections: PackageCollectionsProtocol) async throws -> T) async throws -> T {
390385
_ = try? swiftTool.getActiveWorkspace(emitDeprecatedConfigurationWarning: true)
391386
let collections = PackageCollections(
392387
configuration: .init(
@@ -404,7 +399,7 @@ private extension ParsableCommand {
404399
}
405400
}
406401

407-
return try handler(collections)
402+
return try await handler(collections)
408403
}
409404

410405
func url(_ urlString: String) throws -> URL {
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This source file is part of the Swift open source project
2+
#
3+
# Copyright (c) 2023 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_executable(swift-package-collection
10+
Entrypoint.swift)
11+
target_link_libraries(swift-package-collection PRIVATE
12+
PackageCollectionsTool)
13+
14+
target_compile_options(swift-package-collection PRIVATE
15+
-parse-as-library)
16+
17+
install(TARGETS swift-package-collection
18+
RUNTIME DESTINATION bin)

Sources/swift-package-collection/main.swift renamed to Sources/swift-package-collection/Entrypoint.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,10 @@
1313
import Commands
1414
import PackageCollectionsTool
1515

16-
SwiftPackageCollectionsTool.main()
16+
@main
17+
struct Entrypoint {
18+
static func main() async {
19+
await SwiftPackageCollectionsTool.main()
20+
}
21+
}
22+

Sources/swift-package-manager/SwiftPM.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct SwiftPM {
3535
case "swift-run":
3636
SwiftRunTool.main()
3737
case "swift-package-collection":
38-
SwiftPackageCollectionsTool.main()
38+
await SwiftPackageCollectionsTool.main()
3939
case "swift-package-registry":
4040
SwiftPackageRegistryTool.main()
4141
default:

0 commit comments

Comments
 (0)