Skip to content

Commit 2531b9f

Browse files
authored
Add a global variable cpuArch (#216)
* Move arch to a global variable * Let compiler to infer the type * Format source code
1 parent 09e12af commit 2531b9f

File tree

6 files changed

+44
-62
lines changed

6 files changed

+44
-62
lines changed

Sources/LinuxPlatform/Linux.swift

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ var swiftGPGKeysRefreshed = false
77
/// This implementation can be reused for any supported Linux platform.
88
/// TODO: replace dummy implementations
99
public struct Linux: Platform {
10-
let linuxPlatforms = [
11-
PlatformDefinition.ubuntu2404,
12-
PlatformDefinition.ubuntu2204,
13-
PlatformDefinition.ubuntu2004,
14-
PlatformDefinition.ubuntu1804,
15-
PlatformDefinition.fedora39,
16-
PlatformDefinition.rhel9,
17-
PlatformDefinition.amazonlinux2,
18-
PlatformDefinition.debian12,
10+
let linuxPlatforms: [PlatformDefinition] = [
11+
.ubuntu2404,
12+
.ubuntu2204,
13+
.ubuntu2004,
14+
.ubuntu1804,
15+
.fedora39,
16+
.rhel9,
17+
.amazonlinux2,
18+
.debian12,
1919
]
2020

2121
public init() {}
@@ -382,13 +382,7 @@ public struct Linux: Platform {
382382
}
383383

384384
public func getExecutableName() -> String {
385-
#if arch(x86_64)
386-
let arch = "x86_64"
387-
#elseif arch(arm64)
388-
let arch = "aarch64"
389-
#else
390-
fatalError("Unsupported processor architecture")
391-
#endif
385+
let arch = cpuArch
392386

393387
return "swiftly-\(arch)-unknown-linux-gnu"
394388
}
@@ -516,7 +510,7 @@ public struct Linux: Platform {
516510
return await self.manualSelectPlatform(platformPretty)
517511
}
518512

519-
return PlatformDefinition.amazonlinux2
513+
return .amazonlinux2
520514
} else if (id + (idlike ?? "")).contains("rhel") {
521515
guard versionID.hasPrefix("9") else {
522516
let message = "Unsupported version of RHEL"
@@ -528,7 +522,7 @@ public struct Linux: Platform {
528522
return await self.manualSelectPlatform(platformPretty)
529523
}
530524

531-
return PlatformDefinition.rhel9
525+
return .rhel9
532526
} else if let pd = [PlatformDefinition.ubuntu1804, .ubuntu2004, .ubuntu2204, .ubuntu2404, .debian12, .fedora39].first(where: { $0.name == id + versionID }) {
533527
return pd
534528
}

Sources/MacOSPlatform/MacOS.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public struct MacOS: Platform {
156156

157157
public func detectPlatform(disableConfirmation _: Bool, platform _: String?) async -> PlatformDefinition {
158158
// No special detection required on macOS platform
159-
PlatformDefinition.macOS
159+
.macOS
160160
}
161161

162162
public func getShell() async throws -> String {

Sources/SwiftlyCore/HTTPClient.swift

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -295,17 +295,7 @@ public struct SwiftlyHTTPClient {
295295
limit: Int? = nil,
296296
filter: ((ToolchainVersion.StableRelease) -> Bool)? = nil
297297
) async throws -> [ToolchainVersion.StableRelease] {
298-
let arch = if a == nil {
299-
#if arch(x86_64)
300-
"x86_64"
301-
#elseif arch(arm64)
302-
"aarch64"
303-
#else
304-
#error("Unsupported processor architecture")
305-
#endif
306-
} else {
307-
a!
308-
}
298+
let arch = a ?? cpuArch
309299

310300
let url = "https://www.swift.org/api/v1/install/releases.json"
311301
let swiftOrgReleases: [SwiftOrgRelease] = try await self.getFromJSON(url: url, type: [SwiftOrgRelease].self)
@@ -359,17 +349,7 @@ public struct SwiftlyHTTPClient {
359349
limit: Int? = nil,
360350
filter: ((ToolchainVersion.Snapshot) -> Bool)? = nil
361351
) async throws -> [ToolchainVersion.Snapshot] {
362-
let arch = if a == nil {
363-
#if arch(x86_64)
364-
"x86_64"
365-
#elseif arch(arm64)
366-
"aarch64"
367-
#else
368-
#error("Unsupported processor architecture")
369-
#endif
370-
} else {
371-
a!
372-
}
352+
let arch = a ?? cpuArch
373353

374354
let platformName = if platform.name == PlatformDefinition.macOS.name {
375355
"macos"

Sources/SwiftlyCore/SwiftlyCore.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,11 @@ public func readLine(prompt: String) -> String? {
5151
}
5252
return provider.readLine()
5353
}
54+
55+
#if arch(x86_64)
56+
public let cpuArch = "x86_64"
57+
#elseif arch(arm64)
58+
public let cpuArch = "aarch64"
59+
#else
60+
#error("Unsupported processor architecture")
61+
#endif

Tests/SwiftlyTests/HTTPClientTests.swift

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,27 @@ final class HTTPClientTests: SwiftlyTests {
5151
}
5252

5353
func testGetToolchainMetdataFromSwiftOrg() async throws {
54-
let supportedPlatforms = [
55-
PlatformDefinition.macOS,
56-
PlatformDefinition.ubuntu2404,
57-
PlatformDefinition.ubuntu2204,
58-
PlatformDefinition.ubuntu2004,
59-
// PlatformDefinition.ubuntu1804, // There are no releases for Ubuntu 18.04 in the branches being tested below
60-
PlatformDefinition.rhel9,
61-
PlatformDefinition.fedora39,
62-
PlatformDefinition.amazonlinux2,
63-
PlatformDefinition.debian12,
54+
let supportedPlatforms: [PlatformDefinition] = [
55+
.macOS,
56+
.ubuntu2404,
57+
.ubuntu2204,
58+
.ubuntu2004,
59+
// .ubuntu1804, // There are no releases for Ubuntu 18.04 in the branches being tested below
60+
.rhel9,
61+
.fedora39,
62+
.amazonlinux2,
63+
.debian12,
6464
]
6565

66-
let newPlatforms = [
67-
PlatformDefinition.ubuntu2404,
68-
PlatformDefinition.fedora39,
69-
PlatformDefinition.debian12,
66+
let newPlatforms: [PlatformDefinition] = [
67+
.ubuntu2404,
68+
.fedora39,
69+
.debian12,
7070
]
7171

72-
let branches = [
73-
ToolchainVersion.Snapshot.Branch.main,
74-
ToolchainVersion.Snapshot.Branch.release(major: 6, minor: 0), // This is available in swift.org API
72+
let branches: [ToolchainVersion.Snapshot.Branch] = [
73+
.main,
74+
.release(major: 6, minor: 0), // This is available in swift.org API
7575
]
7676

7777
for arch in ["x86_64", "aarch64"] {

Tests/SwiftlyTests/SwiftlyTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,10 @@ class SwiftlyTests: XCTestCase {
331331

332332
// Snapshots are currently unavailable for these platforms on swift.org
333333
// TODO: remove these once snapshots are available for them
334-
let snapshotsUnavailable = [
335-
PlatformDefinition.ubuntu2404,
336-
PlatformDefinition.fedora39,
337-
PlatformDefinition.debian12,
334+
let snapshotsUnavailable: [PlatformDefinition] = [
335+
.ubuntu2404,
336+
.fedora39,
337+
.debian12,
338338
]
339339

340340
return !snapshotsUnavailable.contains(pd)

0 commit comments

Comments
 (0)