@@ -29,43 +29,83 @@ struct Uninstall: SwiftlyCommand {
29
29
30
30
$ swiftly uninstall main-snapshot
31
31
$ swiftly uninstall 5.7-snapshot
32
+
33
+ The latest installed stable release can be uninstalled by specifying 'latest':
34
+
35
+ $ swiftly uninstall latest
32
36
"""
33
37
) )
34
38
var toolchain : String
35
39
40
+ @Flag (
41
+ name: [ . long, . customShort( " y " ) ] ,
42
+ help: " Uninstall all selected toolchains without prompting for confirmation. "
43
+ )
44
+ var assumeYes : Bool = false
45
+
36
46
mutating func run( ) async throws {
37
47
let selector = try ToolchainSelector ( parsing: self . toolchain)
38
48
let config = try Config . load ( )
39
49
let toolchains = config. listInstalledToolchains ( selector: selector)
40
50
41
51
guard !toolchains. isEmpty else {
42
- print ( " no toolchains matched \" \( self . toolchain) \" " )
52
+ SwiftlyCore . print ( " No toolchains matched \" \( self . toolchain) \" " )
43
53
return
44
54
}
45
55
46
- print ( " The following toolchains will be uninstalled: " )
56
+ if !self . assumeYes {
57
+ SwiftlyCore . print ( " The following toolchains will be uninstalled: " )
47
58
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 "
54
63
55
- guard proceed == " y " else {
56
- print ( " aborting uninstall " )
57
- return
64
+ guard proceed == " y " else {
65
+ SwiftlyCore . print ( " Aborting uninstall " )
66
+ return
67
+ }
58
68
}
59
69
60
- print ( )
70
+ SwiftlyCore . print ( )
61
71
62
72
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 " )
66
79
}
67
80
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
+ }
70
110
}
71
111
}
0 commit comments