Skip to content

Commit 90457aa

Browse files
committed
Create helpful extensions to the generated types.
Add swiftly user agent middleware Restrict the generator to only the swiftly portion of the API
1 parent ffdc305 commit 90457aa

File tree

3 files changed

+75
-21
lines changed

3 files changed

+75
-21
lines changed

Sources/Swiftly/SelfUpdate.swift

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -31,45 +31,38 @@ internal struct SelfUpdate: SwiftlyCommand {
3131
SwiftlyCore.print("Checking for swiftly updates...")
3232

3333
let swiftlyRelease = try await SwiftlyCore.httpClient.getCurrentSwiftlyRelease()
34-
guard let releaseVersion = try? SwiftlyVersion(parsing: swiftlyRelease.version) else {
35-
throw SwiftlyError(message: "Invalid swiftly version reported: \(swiftlyRelease.version)")
36-
}
3734

38-
guard releaseVersion > SwiftlyCore.version else {
35+
guard try swiftlyRelease.swiftlyVersion > SwiftlyCore.version else {
3936
SwiftlyCore.print("Already up to date.")
4037
return SwiftlyCore.version
4138
}
4239

4340
var downloadURL: Foundation.URL?
4441
for platform in swiftlyRelease.platforms {
4542
#if os(macOS)
46-
guard platform.platform.value1 == .darwin else {
43+
guard platform.isDarwin else {
4744
continue
4845
}
4946
#elseif os(Linux)
50-
guard platform.platform.value1 == .linux else {
47+
guard platform.isLinux else {
5148
continue
5249
}
5350
#endif
5451

5552
#if arch(x86_64)
56-
guard let url = URL(string: platform.x8664) else {
57-
throw SwiftlyError(message: "The swiftly release URL is not valid: \(platform.x8664)")
58-
}
59-
downloadURL = url
53+
downloadURL = try platform.x86_64URL
6054
#elseif arch(arm64)
61-
guard let url = URL(string: platform.arm64) else {
62-
throw SwiftlyError(message: "The swiftly release URL is not valid: \(platform.arm64)")
63-
}
64-
downloadURL = url
55+
downloadURL = try platform.arm64URL
6556
#endif
6657
}
6758

6859
guard let downloadURL else {
69-
throw SwiftlyError(message: "No matching platform was found in swiftly release.")
60+
throw SwiftlyError(message: "The newest release of swiftly is incompatible with your current OS and/or processor architecture.")
7061
}
7162

72-
SwiftlyCore.print("A new version is available: \(releaseVersion)")
63+
let version = try swiftlyRelease.swiftlyVersion
64+
65+
SwiftlyCore.print("A new version is available: \(version)")
7366

7467
let tmpFile = Swiftly.currentPlatform.getTempFilePath()
7568
FileManager.default.createFile(atPath: tmpFile.path, contents: nil)
@@ -79,7 +72,7 @@ internal struct SelfUpdate: SwiftlyCommand {
7972

8073
let animation = PercentProgressAnimation(
8174
stream: stdoutStream,
82-
header: "Downloading swiftly \(releaseVersion)"
75+
header: "Downloading swiftly \(version)"
8376
)
8477
do {
8578
try await SwiftlyCore.httpClient.downloadFile(
@@ -105,7 +98,7 @@ internal struct SelfUpdate: SwiftlyCommand {
10598
try await Swiftly.currentPlatform.verifySignature(httpClient: SwiftlyCore.httpClient, archiveDownloadURL: downloadURL, archive: tmpFile, verbose: verbose)
10699
try Swiftly.currentPlatform.extractSwiftlyAndInstall(from: tmpFile)
107100

108-
SwiftlyCore.print("Successfully updated swiftly to \(releaseVersion) (was \(SwiftlyCore.version))")
109-
return releaseVersion
101+
SwiftlyCore.print("Successfully updated swiftly to \(version) (was \(SwiftlyCore.version))")
102+
return version
110103
}
111104
}

Sources/SwiftlyCore/HTTPClient.swift

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,75 @@
11
import _StringProcessing
22
import AsyncHTTPClient
33
import Foundation
4+
import HTTPTypes
45
import NIO
56
import NIOFoundationCompat
67
import NIOHTTP1
78
import OpenAPIAsyncHTTPClient
89
import OpenAPIRuntime
910

11+
extension Components.Schemas.SwiftlyRelease {
12+
public var swiftlyVersion: SwiftlyVersion {
13+
get throws {
14+
guard let releaseVersion = try? SwiftlyVersion(parsing: self.version) else {
15+
throw SwiftlyError(message: "Invalid swiftly version reported: \(self.version)")
16+
}
17+
18+
return releaseVersion
19+
}
20+
}
21+
}
22+
23+
extension Components.Schemas.SwiftlyReleasePlatformArtifacts {
24+
public var isDarwin: Bool {
25+
self.platform.value1 == .darwin
26+
}
27+
28+
public var isLinux: Bool {
29+
self.platform.value1 == .linux
30+
}
31+
32+
public var x86_64URL: URL {
33+
get throws {
34+
guard let url = URL(string: self.x8664) else {
35+
throw SwiftlyError(message: "The swiftly x86_64 URL is invalid: \(self.x8664)")
36+
}
37+
38+
return url
39+
}
40+
}
41+
42+
public var arm64URL: URL {
43+
get throws {
44+
guard let url = URL(string: self.arm64) else {
45+
throw SwiftlyError(message: "The swiftly arm64 URL is invalid: \(self.arm64)")
46+
}
47+
48+
return url
49+
}
50+
}
51+
}
52+
1053
public protocol HTTPRequestExecutor {
1154
func execute(_ request: HTTPClientRequest, timeout: TimeAmount) async throws -> HTTPClientResponse
1255
func getCurrentSwiftlyRelease() async throws -> Components.Schemas.SwiftlyRelease
1356
}
1457

58+
internal struct SwiftlyUserAgentMiddleware: ClientMiddleware {
59+
package func intercept(
60+
_ request: HTTPRequest,
61+
body: HTTPBody?,
62+
baseURL: URL,
63+
operationID _: String,
64+
next: (HTTPRequest, HTTPBody?, URL) async throws -> (HTTPResponse, HTTPBody?)
65+
) async throws -> (HTTPResponse, HTTPBody?) {
66+
var request = request
67+
// Adds the `Authorization` header field with the provided value.
68+
request.headerFields[.userAgent] = "swiftly/\(SwiftlyCore.version)"
69+
return try await next(request, body, baseURL)
70+
}
71+
}
72+
1573
/// An `HTTPRequestExecutor` backed by the shared `HTTPClient`.
1674
internal class HTTPRequestExecutorImpl: HTTPRequestExecutor {
1775
let httpClient: HTTPClient
@@ -59,11 +117,12 @@ internal class HTTPRequestExecutorImpl: HTTPRequestExecutor {
59117

60118
public func getCurrentSwiftlyRelease() async throws -> Components.Schemas.SwiftlyRelease {
61119
let config = AsyncHTTPClientTransport.Configuration(client: self.httpClient, timeout: .seconds(30))
120+
let swiftlyUserAgent = SwiftlyUserAgentMiddleware()
62121

63-
// FIXME: use a middleware that adds the swiftly user agent header (problem is how to do this with async http client)
64122
let client = Client(
65123
serverURL: URL(string: "https://swift.org/api")!,
66-
transport: AsyncHTTPClientTransport(configuration: config)
124+
transport: AsyncHTTPClientTransport(configuration: config),
125+
middlewares: [swiftlyUserAgent]
67126
)
68127

69128
let response = try await client.getCurrentSwiftlyRelease()

Sources/SwiftlyCore/openapi-generator-config.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@ generate:
33
- client
44
namingStrategy: idiomatic
55
accessModifier: public
6+
filter:
7+
tags: ["Swiftly"]

0 commit comments

Comments
 (0)