Skip to content

Commit 43f8193

Browse files
authored
swiftly uninstall all command to remove all installed toolchains (#87)
1 parent d14ced0 commit 43f8193

File tree

2 files changed

+29
-2
lines changed

2 files changed

+29
-2
lines changed

Sources/Swiftly/Uninstall.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ struct Uninstall: SwiftlyCommand {
3333
The latest installed stable release can be uninstalled by specifying 'latest':
3434
3535
$ swiftly uninstall latest
36+
37+
Finally, all installed toolchains can be uninstalled by specifying 'all':
38+
39+
$ swiftly uninstall all
3640
"""
3741
))
3842
var toolchain: String
@@ -44,9 +48,19 @@ struct Uninstall: SwiftlyCommand {
4448
var assumeYes: Bool = false
4549

4650
mutating func run() async throws {
47-
let selector = try ToolchainSelector(parsing: self.toolchain)
4851
let startingConfig = try Config.load()
49-
let toolchains = startingConfig.listInstalledToolchains(selector: selector)
52+
53+
let toolchains: [ToolchainVersion]
54+
if self.toolchain == "all" {
55+
// Sort the uninstalled toolchains such that the in-use toolchain will be uninstalled last.
56+
// This avoids printing any unnecessary output from using new toolchains while the uninstall is in progress.
57+
toolchains = startingConfig.listInstalledToolchains(selector: nil).sorted { a, b in
58+
a != startingConfig.inUse && (b == startingConfig.inUse || a < b)
59+
}
60+
} else {
61+
let selector = try ToolchainSelector(parsing: self.toolchain)
62+
toolchains = startingConfig.listInstalledToolchains(selector: selector)
63+
}
5064

5165
guard !toolchains.isEmpty else {
5266
SwiftlyCore.print("No toolchains matched \"\(self.toolchain)\"")

Tests/SwiftlyTests/UninstallTests.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,4 +283,17 @@ final class UninstallTests: SwiftlyTests {
283283
)
284284
}
285285
}
286+
287+
/// Tests that providing "all" as an argument to uninstall will uninstall all toolchains.
288+
func testUninstallAll() async throws {
289+
let toolchains = Set([Self.oldStable, Self.newStable, Self.newMainSnapshot, Self.oldReleaseSnapshot])
290+
try await self.withMockedHome(homeName: Self.homeName, toolchains: toolchains, inUse: Self.newMainSnapshot) {
291+
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", "-y", "all"])
292+
_ = try await uninstall.run()
293+
try await self.validateInstalledToolchains(
294+
[],
295+
description: "uninstall did not uninstall all toolchains"
296+
)
297+
}
298+
}
286299
}

0 commit comments

Comments
 (0)