@@ -29,43 +29,83 @@ struct Uninstall: SwiftlyCommand {
2929
3030 $ swiftly uninstall main-snapshot
3131 $ swiftly uninstall 5.7-snapshot
32+
33+ The latest installed stable release can be uninstalled by specifying 'latest':
34+
35+ $ swiftly uninstall latest
3236 """
3337 ) )
3438 var toolchain : String
3539
40+ @Flag (
41+ name: [ . long, . customShort( " y " ) ] ,
42+ help: " Uninstall all selected toolchains without prompting for confirmation. "
43+ )
44+ var assumeYes : Bool = false
45+
3646 mutating func run( ) async throws {
3747 let selector = try ToolchainSelector ( parsing: self . toolchain)
3848 let config = try Config . load ( )
3949 let toolchains = config. listInstalledToolchains ( selector: selector)
4050
4151 guard !toolchains. isEmpty else {
42- print ( " no toolchains matched \" \( self . toolchain) \" " )
52+ SwiftlyCore . print ( " No toolchains matched \" \( self . toolchain) \" " )
4353 return
4454 }
4555
46- print ( " The following toolchains will be uninstalled: " )
56+ if !self . assumeYes {
57+ SwiftlyCore . print ( " The following toolchains will be uninstalled: " )
4758
48- for toolchain in toolchains {
49- print ( " \( toolchain) " )
50- }
51-
52- print ( " Proceed? (y/n) " , terminator: " : " )
53- let proceed = readLine ( strippingNewline: true ) ?? " n "
59+ for toolchain in toolchains {
60+ SwiftlyCore . print ( " \( toolchain) " )
61+ }
62+ let proceed = SwiftlyCore . readLine ( prompt: " Proceed? (y/n) " ) ?? " n "
5463
55- guard proceed == " y " else {
56- print ( " aborting uninstall " )
57- return
64+ guard proceed == " y " else {
65+ SwiftlyCore . print ( " Aborting uninstall " )
66+ return
67+ }
5868 }
5969
60- print ( )
70+ SwiftlyCore . print ( )
6171
6272 for toolchain in toolchains {
63- print ( " Uninstalling \( toolchain) ... " , terminator: " " )
64- try Swiftly . currentPlatform. uninstall ( version: toolchain)
65- print ( " done " )
73+ SwiftlyCore . print ( " Uninstalling \( toolchain) ... " , terminator: " " )
74+ try Swiftly . currentPlatform. uninstall ( toolchain)
75+ try Config . update { config in
76+ config. installedToolchains. remove ( toolchain)
77+ }
78+ SwiftlyCore . print ( " done " )
6679 }
6780
68- print ( )
69- print ( " \( toolchains. count) toolchain(s) successfully uninstalled " )
81+ SwiftlyCore . print ( )
82+ SwiftlyCore . print ( " \( toolchains. count) toolchain(s) successfully uninstalled " )
83+
84+ var latestConfig = try Config . load ( )
85+
86+ // If the in-use toolchain was one of the uninstalled toolchains, use the latest installed
87+ // toolchain.
88+ if let previouslyInUse = latestConfig. inUse, toolchains. contains ( previouslyInUse) {
89+ let selector : ToolchainSelector
90+ switch previouslyInUse {
91+ case let . stable( sr) :
92+ // If a.b.c was previously in use, switch to the latest a.b toolchain.
93+ selector = . stable( major: sr. major, minor: sr. minor, patch: nil )
94+ case let . snapshot( s) :
95+ // If a snapshot was previously in use, switch to the latest snapshot associated with that branch.
96+ selector = . snapshot( branch: s. branch, date: nil )
97+ }
98+
99+ if let toUse = latestConfig. listInstalledToolchains ( selector: selector) . max ( )
100+ ?? latestConfig. listInstalledToolchains ( selector: . latest) . max ( )
101+ ?? latestConfig. installedToolchains. max ( )
102+ {
103+ try await Use . execute ( toUse)
104+ } else {
105+ // If there are no more toolchains installed, clear the inUse config entry.
106+ latestConfig. inUse = nil
107+ try latestConfig. save ( )
108+ }
109+ }
70110 }
71111}
0 commit comments