Skip to content

"swiftly uninstall all" command to remove all installed toolchains #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions Sources/Swiftly/Uninstall.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ struct Uninstall: SwiftlyCommand {
The latest installed stable release can be uninstalled by specifying 'latest':

$ swiftly uninstall latest

Finally, all installed toolchains can be uninstalled by specifying 'all':

$ swiftly uninstall all
"""
))
var toolchain: String
Expand All @@ -44,9 +48,19 @@ struct Uninstall: SwiftlyCommand {
var assumeYes: Bool = false

mutating func run() async throws {
let selector = try ToolchainSelector(parsing: self.toolchain)
let startingConfig = try Config.load()
let toolchains = startingConfig.listInstalledToolchains(selector: selector)

let toolchains: [ToolchainVersion]
if self.toolchain == "all" {
// Sort the uninstalled toolchains such that the in-use toolchain will be uninstalled last.
// This avoids printing any unnecessary output from using new toolchains while the uninstall is in progress.
toolchains = startingConfig.listInstalledToolchains(selector: nil).sorted { a, b in
a != startingConfig.inUse && (b == startingConfig.inUse || a < b)
}
} else {
let selector = try ToolchainSelector(parsing: self.toolchain)
toolchains = startingConfig.listInstalledToolchains(selector: selector)
}

guard !toolchains.isEmpty else {
SwiftlyCore.print("No toolchains matched \"\(self.toolchain)\"")
Expand Down
13 changes: 13 additions & 0 deletions Tests/SwiftlyTests/UninstallTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -283,4 +283,17 @@ final class UninstallTests: SwiftlyTests {
)
}
}

/// Tests that providing "all" as an argument to uninstall will uninstall all toolchains.
func testUninstallAll() async throws {
let toolchains = Set([Self.oldStable, Self.newStable, Self.newMainSnapshot, Self.oldReleaseSnapshot])
try await self.withMockedHome(homeName: Self.homeName, toolchains: toolchains, inUse: Self.newMainSnapshot) {
var uninstall = try self.parseCommand(Uninstall.self, ["uninstall", "-y", "all"])
_ = try await uninstall.run()
try await self.validateInstalledToolchains(
[],
description: "uninstall did not uninstall all toolchains"
)
}
}
}