Skip to content

Commit 88ec130

Browse files
committed
Include toolchain location in swiftly list --format json
Include the location in disk of each downloaded toolchain when running `swiftly list --format json`. This will help fix an open issue in vscode-swfit where toolchains from swiftly show up twice in the toolchain selection quick pick (one list found when looking on disk, and the other produced by `swiftly list`. (swiftlang/vscode-swift#1850)
1 parent 2fc7916 commit 88ec130

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

Sources/Swiftly/List.swift

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,16 @@ struct List: SwiftlyCommand {
5555
let toolchains = config.listInstalledToolchains(selector: selector).sorted { $0 > $1 }
5656
let (inUse, _) = try await selectToolchain(ctx, config: &config)
5757

58-
let installedToolchainInfos = toolchains.compactMap { toolchain -> InstallToolchainInfo? in
59-
InstallToolchainInfo(
60-
version: toolchain,
61-
inUse: inUse == toolchain,
62-
isDefault: toolchain == config.inUse
58+
var installedToolchainInfos: [InstallToolchainInfo] = []
59+
for toolchain in toolchains {
60+
let location = "\(try await Swiftly.currentPlatform.findToolchainLocation(ctx, toolchain))"
61+
installedToolchainInfos.append(
62+
InstallToolchainInfo(
63+
version: toolchain,
64+
inUse: inUse == toolchain,
65+
isDefault: toolchain == config.inUse,
66+
location: location
67+
)
6368
)
6469
}
6570

Sources/Swiftly/OutputSchema.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,11 +182,13 @@ struct InstallToolchainInfo: OutputData {
182182
let version: ToolchainVersion
183183
let inUse: Bool
184184
let isDefault: Bool
185+
let location: String
185186

186-
init(version: ToolchainVersion, inUse: Bool, isDefault: Bool) {
187+
init(version: ToolchainVersion, inUse: Bool, isDefault: Bool, location: String) {
187188
self.version = version
188189
self.inUse = inUse
189190
self.isDefault = isDefault
191+
self.location = location
190192
}
191193

192194
var description: String {
@@ -205,12 +207,14 @@ struct InstallToolchainInfo: OutputData {
205207
case version
206208
case inUse
207209
case isDefault
210+
case location
208211
}
209212

210213
public func encode(to encoder: Encoder) throws {
211214
var container = encoder.container(keyedBy: CodingKeys.self)
212215
try container.encode(self.inUse, forKey: .inUse)
213216
try container.encode(self.isDefault, forKey: .isDefault)
217+
try container.encode(self.location, forKey: .location)
214218

215219
// Encode the version as a object
216220
var versionContainer = container.nestedContainer(
@@ -244,6 +248,7 @@ struct InstallToolchainInfo: OutputData {
244248
let container = try decoder.container(keyedBy: CodingKeys.self)
245249
self.inUse = try container.decode(Bool.self, forKey: .inUse)
246250
self.isDefault = try container.decode(Bool.self, forKey: .isDefault)
251+
self.location = try container.decode(String.self, forKey: .location)
247252

248253
// Decode the version as a object
249254
let versionContainer = try container.nestedContainer(

Tests/SwiftlyTests/ListTests.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,21 @@ import Testing
285285
#expect(inUseToolchain.isDefault == true)
286286
}
287287
}
288+
289+
@Test func listJsonFormatIncludesLocation() async throws {
290+
try await self.runListTest {
291+
let output = try await SwiftlyTests.runWithMockedIO(
292+
List.self, ["list", "--format", "json"], format: .json
293+
)
294+
295+
let listInfo = try JSONDecoder().decode(
296+
InstalledToolchainsListInfo.self,
297+
from: output[0].data(using: .utf8)!
298+
)
299+
300+
for toolchain in listInfo.toolchains {
301+
#expect(toolchain.location.isEmpty == false)
302+
}
303+
}
304+
}
288305
}

0 commit comments

Comments
 (0)