13
13
//===----------------------------------------------------------------------===//
14
14
15
15
import * as vscode from "vscode" ;
16
+ import { QuickPickItem } from "vscode" ;
16
17
import { WorkspaceContext } from "../WorkspaceContext" ;
17
18
import {
18
- Swiftly ,
19
- SwiftlyProgressData ,
20
19
AvailableToolchain ,
21
- isStableVersion ,
22
20
isSnapshotVersion ,
21
+ isStableVersion ,
22
+ Swiftly ,
23
+ SwiftlyProgressData ,
23
24
} from "../toolchain/swiftly" ;
24
25
import { showReloadExtensionNotification } from "../ui/ReloadExtension" ;
25
26
27
+ interface SwiftlyToolchainItem extends QuickPickItem {
28
+ toolchain : AvailableToolchain ;
29
+ }
30
+
31
+ async function downloadAndInstallToolchain ( selected : SwiftlyToolchainItem , ctx : WorkspaceContext ) {
32
+ try {
33
+ await vscode . window . withProgress (
34
+ {
35
+ location : vscode . ProgressLocation . Notification ,
36
+ title : `Installing Swift ${ selected . toolchain . version . name } ` ,
37
+ cancellable : false ,
38
+ } ,
39
+ async progress => {
40
+ progress . report ( { message : "Starting installation..." } ) ;
41
+
42
+ let lastProgress = 0 ;
43
+
44
+ await Swiftly . installToolchain (
45
+ selected . toolchain . version . name ,
46
+ ( progressData : SwiftlyProgressData ) => {
47
+ if (
48
+ progressData . step ?. percent !== undefined &&
49
+ progressData . step . percent > lastProgress
50
+ ) {
51
+ const increment = progressData . step . percent - lastProgress ;
52
+ progress . report ( {
53
+ increment,
54
+ message :
55
+ progressData . step . text ??
56
+ `${ progressData . step . percent } % complete` ,
57
+ } ) ;
58
+ lastProgress = progressData . step . percent ;
59
+ }
60
+ } ,
61
+ ctx . logger
62
+ ) ;
63
+
64
+ progress . report ( {
65
+ increment : 100 - lastProgress ,
66
+ message : "Installation complete" ,
67
+ } ) ;
68
+ }
69
+ ) ;
70
+ void showReloadExtensionNotification (
71
+ `Swift ${ selected . toolchain . version . name } has been installed and activated. Visual Studio Code needs to be reloaded.`
72
+ ) ;
73
+ } catch ( error ) {
74
+ ctx . logger ?. error ( `Failed to install Swift ${ selected . toolchain . version . name } : ${ error } ` ) ;
75
+ void vscode . window . showErrorMessage (
76
+ `Failed to install Swift ${ selected . toolchain . version . name } : ${ error } `
77
+ ) ;
78
+ }
79
+ }
80
+
26
81
/**
27
82
* Shows a quick pick dialog to install available Swiftly toolchains
28
83
*/
29
84
export async function installSwiftlyToolchain ( ctx : WorkspaceContext ) : Promise < void > {
30
85
if ( ! Swiftly . isSupported ( ) ) {
86
+ ctx . logger ?. warn ( "Swiftly is not supported on this platform." ) ;
31
87
void vscode . window . showErrorMessage (
32
88
"Swiftly is not supported on this platform. Only macOS and Linux are supported."
33
89
) ;
34
90
return ;
35
91
}
36
92
37
93
if ( ! ( await Swiftly . isInstalled ( ) ) ) {
94
+ ctx . logger ?. warn ( "Swiftly is not installed." ) ;
38
95
void vscode . window . showErrorMessage (
39
96
"Swiftly is not installed. Please install Swiftly first from https://www.swift.org/install/"
40
97
) ;
@@ -50,7 +107,7 @@ export async function installSwiftlyToolchain(ctx: WorkspaceContext): Promise<vo
50
107
return ;
51
108
}
52
109
53
- const uninstalledToolchains = availableToolchains . filter ( toolchain => ! toolchain . isInstalled ) ;
110
+ const uninstalledToolchains = availableToolchains . filter ( toolchain => ! toolchain . installed ) ;
54
111
55
112
if ( uninstalledToolchains . length === 0 ) {
56
113
void vscode . window . showInformationMessage (
@@ -64,6 +121,9 @@ export async function installSwiftlyToolchain(ctx: WorkspaceContext): Promise<vo
64
121
uninstalledToolchains . filter ( toolchain => toolchain . version . type === "stable" )
65
122
) ;
66
123
124
+ ctx . logger . debug (
125
+ `Available toolchains for installation: ${ sortedToolchains . map ( t => t . version . name ) . join ( ", " ) } `
126
+ ) ;
67
127
const quickPickItems = sortedToolchains . map ( toolchain => ( {
68
128
label : `$(cloud-download) ${ toolchain . version . name } ` ,
69
129
toolchain : toolchain ,
@@ -79,54 +139,69 @@ export async function installSwiftlyToolchain(ctx: WorkspaceContext): Promise<vo
79
139
return ;
80
140
}
81
141
82
- try {
83
- await vscode . window . withProgress (
84
- {
85
- location : vscode . ProgressLocation . Notification ,
86
- title : `Installing Swift ${ selected . toolchain . version . name } ` ,
87
- cancellable : false ,
88
- } ,
89
- async progress => {
90
- progress . report ( { message : "Starting installation..." } ) ;
91
-
92
- let lastProgress = 0 ;
142
+ await downloadAndInstallToolchain ( selected , ctx ) ;
143
+ }
93
144
94
- await Swiftly . installToolchain (
95
- selected . toolchain . version . name ,
96
- ( progressData : SwiftlyProgressData ) => {
97
- if (
98
- progressData . step ?. percent !== undefined &&
99
- progressData . step . percent > lastProgress
100
- ) {
101
- const increment = progressData . step . percent - lastProgress ;
102
- progress . report ( {
103
- increment,
104
- message :
105
- progressData . step . text ||
106
- `${ progressData . step . percent } % complete` ,
107
- } ) ;
108
- lastProgress = progressData . step . percent ;
109
- }
110
- } ,
111
- ctx . logger
112
- ) ;
145
+ /**
146
+ * Shows a quick pick dialog to install available Swiftly snapshot toolchains
147
+ */
148
+ export async function installSwiftlySnapshotToolchain ( ctx : WorkspaceContext ) : Promise < void > {
149
+ if ( ! Swiftly . isSupported ( ) ) {
150
+ void vscode . window . showErrorMessage (
151
+ "Swiftly is not supported on this platform. Only macOS and Linux are supported."
152
+ ) ;
153
+ return ;
154
+ }
113
155
114
- progress . report ( {
115
- increment : 100 - lastProgress ,
116
- message : "Installation complete" ,
117
- } ) ;
118
- }
156
+ if ( ! ( await Swiftly . isInstalled ( ) ) ) {
157
+ void vscode . window . showErrorMessage (
158
+ "Swiftly is not installed. Please install Swiftly first from https://www.swift.org/install/"
119
159
) ;
160
+ return ;
161
+ }
120
162
121
- void showReloadExtensionNotification (
122
- `Swift ${ selected . toolchain . version } has been installed and selected as the active toolchain. Visual Studio Code needs to be reloaded.`
163
+ const availableToolchains = await Swiftly . listAvailable ( ctx . logger ) ;
164
+
165
+ if ( availableToolchains . length === 0 ) {
166
+ void vscode . window . showInformationMessage (
167
+ "No toolchains are available for installation via Swiftly."
123
168
) ;
124
- } catch ( error ) {
125
- ctx . logger ?. error ( `Failed to install Swift ${ selected . toolchain . version . name } : ${ error } ` ) ;
126
- void vscode . window . showErrorMessage (
127
- `Failed to install Swift ${ selected . toolchain . version . name } : ${ error } `
169
+ return ;
170
+ }
171
+
172
+ // Filter for only uninstalled snapshot toolchains
173
+ const uninstalledSnapshotToolchains = availableToolchains . filter (
174
+ toolchain => ! toolchain . installed && toolchain . version . type === "snapshot"
175
+ ) ;
176
+
177
+ if ( uninstalledSnapshotToolchains . length === 0 ) {
178
+ void vscode . window . showInformationMessage (
179
+ "All available snapshot toolchains are already installed."
128
180
) ;
181
+ return ;
129
182
}
183
+
184
+ // Sort toolchains with most recent versions first
185
+ const sortedToolchains = sortToolchainsByVersion ( uninstalledSnapshotToolchains ) ;
186
+
187
+ const quickPickItems = sortedToolchains . map ( toolchain => ( {
188
+ label : `$(cloud-download) ${ toolchain . version . name } ` ,
189
+ description : "snapshot" ,
190
+ detail : `Install snapshot version • Date: ${ toolchain . version . type === "snapshot" ? toolchain . version . date || "Unknown" : "Unknown" } • Branch: ${ toolchain . version . type === "snapshot" ? toolchain . version . branch || "Unknown" : "Unknown" } ` ,
191
+ toolchain : toolchain ,
192
+ } ) ) ;
193
+
194
+ const selected = await vscode . window . showQuickPick ( quickPickItems , {
195
+ title : "Install Swift Snapshot Toolchain via Swiftly" ,
196
+ placeHolder : "Pick a Swift snapshot toolchain to install" ,
197
+ canPickMany : false ,
198
+ } ) ;
199
+
200
+ if ( ! selected ) {
201
+ return ;
202
+ }
203
+
204
+ await downloadAndInstallToolchain ( selected , ctx ) ;
130
205
}
131
206
132
207
/**
0 commit comments