Skip to content

Commit fa042e3

Browse files
authored
SwiftSDKTool: convert previously blocking code to async (#6623)
We previously had to roll back these changes due to CI issues, this is another attempt to bring them back.
1 parent b8f2570 commit fa042e3

File tree

10 files changed

+51
-66
lines changed

10 files changed

+51
-66
lines changed

Sources/PackageModel/SwiftSDKBundle.swift

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ public struct SwiftSDKBundle {
135135
_ fileSystem: some FileSystem,
136136
_ archiver: some Archiver,
137137
_ observabilityScope: ObservabilityScope
138-
) throws {
139-
_ = try withTemporaryDirectory(
138+
) async throws {
139+
_ = try await withTemporaryDirectory(
140140
removeTreeOnDeinit: true
141141
) { temporaryDirectory in
142142
let bundlePath: AbsolutePath
@@ -149,21 +149,18 @@ public struct SwiftSDKBundle {
149149
let bundleName = bundleURL.lastPathComponent
150150
let downloadedBundlePath = temporaryDirectory.appending(component: bundleName)
151151

152-
let client = LegacyHTTPClient()
153-
var request = LegacyHTTPClientRequest.download(
152+
let client = HTTPClient()
153+
var request = HTTPClientRequest.download(
154154
url: bundleURL,
155155
fileSystem: fileSystem,
156156
destination: downloadedBundlePath
157157
)
158158
request.options.validResponseCodes = [200]
159-
_ = try temp_await {
160-
client.execute(
161-
request,
162-
observabilityScope: observabilityScope,
163-
progress: nil,
164-
completion: $0
165-
)
166-
}
159+
_ = try await client.execute(
160+
request,
161+
observabilityScope: observabilityScope,
162+
progress: nil
163+
)
167164

168165
bundlePath = downloadedBundlePath
169166

@@ -177,7 +174,7 @@ public struct SwiftSDKBundle {
177174
throw DestinationError.invalidPathOrURL(bundlePathOrURL)
178175
}
179176

180-
try installIfValid(
177+
try await installIfValid(
181178
bundlePath: bundlePath,
182179
destinationsDirectory: swiftSDKsDirectory,
183180
temporaryDirectory: temporaryDirectory,
@@ -204,7 +201,7 @@ public struct SwiftSDKBundle {
204201
temporaryDirectory: AbsolutePath,
205202
_ fileSystem: some FileSystem,
206203
_ archiver: some Archiver
207-
) throws -> AbsolutePath {
204+
) async throws -> AbsolutePath {
208205
let regex = try RegEx(pattern: "(.+\\.artifactbundle).*")
209206

210207
guard let bundleName = bundlePath.components.last else {
@@ -227,7 +224,7 @@ public struct SwiftSDKBundle {
227224

228225
print("\(bundleName) is assumed to be an archive, unpacking...")
229226

230-
try temp_await { archiver.extract(from: bundlePath, to: temporaryDirectory, completion: $0) }
227+
try await archiver.extract(from: bundlePath, to: temporaryDirectory)
231228

232229
return temporaryDirectory.appending(component: unpackedBundleName)
233230
}
@@ -245,15 +242,15 @@ public struct SwiftSDKBundle {
245242
_ fileSystem: some FileSystem,
246243
_ archiver: some Archiver,
247244
_ observabilityScope: ObservabilityScope
248-
) throws {
245+
) async throws {
249246
#if os(macOS)
250247
// Check the quarantine attribute on bundles downloaded manually in the browser.
251248
guard !fileSystem.hasAttribute(.quarantine, bundlePath) else {
252249
throw DestinationError.quarantineAttributePresent(bundlePath: bundlePath)
253250
}
254251
#endif
255252

256-
let unpackedBundlePath = try unpackIfNeeded(
253+
let unpackedBundlePath = try await unpackIfNeeded(
257254
bundlePath: bundlePath,
258255
destinationsDirectory: destinationsDirectory,
259256
temporaryDirectory: temporaryDirectory,

Sources/SwiftSDKTool/InstallSwiftSDK.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public struct InstallSwiftSDK: SwiftSDKSubcommand {
3939
buildTimeTriple: Triple,
4040
_ destinationsDirectory: AbsolutePath,
4141
_ observabilityScope: ObservabilityScope
42-
) throws {
42+
) async throws {
4343
let cancellator = Cancellator(observabilityScope: observabilityScope)
4444
cancellator.installSignalHandlers()
45-
try SwiftSDKBundle.install(
45+
try await SwiftSDKBundle.install(
4646
bundlePathOrURL: bundlePathOrURL,
4747
swiftSDKsDirectory: destinationsDirectory,
4848
self.fileSystem,

Sources/SwiftSDKTool/RemoveSwiftSDK.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public struct RemoveSwiftSDK: SwiftSDKSubcommand {
3535
buildTimeTriple: Triple,
3636
_ destinationsDirectory: AbsolutePath,
3737
_ observabilityScope: ObservabilityScope
38-
) throws {
38+
) async throws {
3939
let destinationsDirectory = try self.getOrCreateDestinationsDirectory()
4040
let artifactBundleDirectory = destinationsDirectory.appending(component: self.sdkIDOrBundleName)
4141

Sources/SwiftSDKTool/SwiftSDKSubcommand.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import PackageModel
1919
import var TSCBasic.stdoutStream
2020

2121
/// A protocol for functions and properties common to all destination subcommands.
22-
protocol SwiftSDKSubcommand: ParsableCommand {
22+
protocol SwiftSDKSubcommand: AsyncParsableCommand {
2323
/// Common locations options provided by ArgumentParser.
2424
var locations: LocationOptions { get }
2525

@@ -32,7 +32,7 @@ protocol SwiftSDKSubcommand: ParsableCommand {
3232
buildTimeTriple: Triple,
3333
_ destinationsDirectory: AbsolutePath,
3434
_ observabilityScope: ObservabilityScope
35-
) throws
35+
) async throws
3636
}
3737

3838
extension SwiftSDKSubcommand {
@@ -59,7 +59,7 @@ extension SwiftSDKSubcommand {
5959
return destinationsDirectory
6060
}
6161

62-
public func run() throws {
62+
public func run() async throws {
6363
let observabilityHandler = SwiftToolObservabilityHandler(outputStream: stdoutStream, logLevel: .info)
6464
let observabilitySystem = ObservabilitySystem(observabilityHandler)
6565
let observabilityScope = observabilitySystem.topScope
@@ -70,7 +70,7 @@ extension SwiftSDKSubcommand {
7070

7171
var commandError: Error? = nil
7272
do {
73-
try self.run(buildTimeTriple: triple, destinationsDirectory, observabilityScope)
73+
try await self.run(buildTimeTriple: triple, destinationsDirectory, observabilityScope)
7474
if observabilityScope.errorsReported {
7575
throw ExitCode.failure
7676
}

Sources/SwiftSDKTool/SwiftSDKTool.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
import ArgumentParser
1414
import Basics
15-
16-
public struct SwiftSDKTool: ParsableCommand {
15+
16+
public struct SwiftSDKTool: AsyncParsableCommand {
1717
public static let configuration = CommandConfiguration(
1818
commandName: "experimental-sdk",
1919
_superCommandName: "swift",

Sources/swift-experimental-sdk/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
add_executable(swift-experimental-sdk
10-
SwiftSDKTool.swift)
10+
Entrypoint.swift)
1111
target_link_libraries(swift-experimental-sdk PRIVATE
1212
SwiftSDKTool)
1313

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift open source project
4+
//
5+
// Copyright (c) 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSDKTool
14+
15+
@main
16+
struct Entrypoint {
17+
static func main() async {
18+
await SwiftSDKTool.main()
19+
}
20+
}

Sources/swift-experimental-sdk/SwiftSDKTool.swift

Lines changed: 0 additions & 32 deletions
This file was deleted.

Sources/swift-package-manager/SwiftPM.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ let execName = (try? AbsolutePath(validating: firstArg).basenameWithoutExt) ??
2222

2323
@main
2424
struct SwiftPM {
25-
static func main() {
25+
static func main() async {
2626
switch execName {
2727
case "swift-package":
2828
SwiftPackageTool.main()
2929
case "swift-build":
3030
SwiftBuildTool.main()
3131
case "swift-experimental-sdk":
32-
SwiftSDKTool.main()
32+
await SwiftSDKTool.main()
3333
case "swift-test":
3434
SwiftTestTool.main()
3535
case "swift-run":

Tests/PackageModelTests/SwiftSDKBundleTests.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ final class SwiftSDKBundleTests: XCTestCase {
9797

9898
let archiver = MockArchiver()
9999

100-
try SwiftSDKBundle.install(
100+
try await SwiftSDKBundle.install(
101101
bundlePathOrURL: bundles[0].path,
102102
swiftSDKsDirectory: swiftSDKsDirectory,
103103
fileSystem,
@@ -107,7 +107,7 @@ final class SwiftSDKBundleTests: XCTestCase {
107107

108108
let invalidPath = "foobar"
109109
do {
110-
try SwiftSDKBundle.install(
110+
try await SwiftSDKBundle.install(
111111
bundlePathOrURL: "foobar",
112112
swiftSDKsDirectory: swiftSDKsDirectory,
113113
fileSystem,
@@ -131,7 +131,7 @@ final class SwiftSDKBundleTests: XCTestCase {
131131
}
132132

133133
do {
134-
try SwiftSDKBundle.install(
134+
try await SwiftSDKBundle.install(
135135
bundlePathOrURL: bundles[0].path,
136136
swiftSDKsDirectory: swiftSDKsDirectory,
137137
fileSystem,
@@ -155,7 +155,7 @@ final class SwiftSDKBundleTests: XCTestCase {
155155
}
156156

157157
do {
158-
try SwiftSDKBundle.install(
158+
try await SwiftSDKBundle.install(
159159
bundlePathOrURL: bundles[1].path,
160160
swiftSDKsDirectory: swiftSDKsDirectory,
161161
fileSystem,
@@ -191,7 +191,7 @@ final class SwiftSDKBundleTests: XCTestCase {
191191
let system = ObservabilitySystem.makeForTesting()
192192

193193
for bundle in bundles {
194-
try SwiftSDKBundle.install(
194+
try await SwiftSDKBundle.install(
195195
bundlePathOrURL: bundle.path,
196196
swiftSDKsDirectory: swiftSDKsDirectory,
197197
fileSystem,

0 commit comments

Comments
 (0)