12
12
//
13
13
//===----------------------------------------------------------------------===//
14
14
import * as vscode from "vscode" ;
15
- import { QuickPickItem } from "vscode" ;
16
15
17
16
import { WorkspaceContext } from "../WorkspaceContext" ;
18
17
import { SwiftLogger } from "../logging/SwiftLogger" ;
19
- import { AvailableToolchain , Swiftly , SwiftlyProgressData } from "../toolchain/swiftly" ;
20
- import { showReloadExtensionNotification } from "../ui/ReloadExtension" ;
21
-
22
- interface SwiftlyToolchainItem extends QuickPickItem {
23
- toolchain : AvailableToolchain ;
24
- }
25
-
26
- async function downloadAndInstallToolchain ( selected : SwiftlyToolchainItem , ctx : WorkspaceContext ) {
27
- return await installSwiftlyToolchainVersion ( selected . toolchain . version . name , ctx . logger , true ) ;
28
- }
18
+ import { Swiftly , SwiftlyProgressData } from "../toolchain/swiftly" ;
19
+ import { askWhereToSetToolchain } from "../ui/ToolchainSelection" ;
29
20
30
21
/**
31
- * Installs a Swiftly toolchain by version string
22
+ * Installs a Swiftly toolchain and shows a progress notification to the user.
23
+ *
32
24
* @param version The toolchain version to install
33
25
* @param logger Optional logger for error reporting
34
- * @param showReloadNotification Whether to show reload notification after installation
35
26
* @returns Promise<boolean> true if installation succeeded, false otherwise
36
27
*/
37
- export async function installSwiftlyToolchainVersion (
28
+ export async function installSwiftlyToolchainWithProgress (
38
29
version : string ,
39
- logger ?: SwiftLogger ,
40
- showReloadNotification : boolean = true ,
41
- token ?: vscode . CancellationToken
30
+ logger ?: SwiftLogger
42
31
) : Promise < boolean > {
43
32
try {
44
33
await vscode . window . withProgress (
@@ -47,8 +36,7 @@ export async function installSwiftlyToolchainVersion(
47
36
title : `Installing Swift ${ version } ` ,
48
37
cancellable : true ,
49
38
} ,
50
- async ( progress , progressToken ) => {
51
- const effectiveToken = token || progressToken ;
39
+ async ( progress , token ) => {
52
40
progress . report ( { message : "Starting installation..." } ) ;
53
41
54
42
let lastProgress = 0 ;
@@ -71,7 +59,7 @@ export async function installSwiftlyToolchainVersion(
71
59
}
72
60
} ,
73
61
logger ,
74
- effectiveToken
62
+ token
75
63
) ;
76
64
77
65
progress . report ( {
@@ -81,11 +69,6 @@ export async function installSwiftlyToolchainVersion(
81
69
}
82
70
) ;
83
71
84
- if ( showReloadNotification ) {
85
- void showReloadExtensionNotification (
86
- `Swift ${ version } has been installed and activated. Visual Studio Code needs to be reloaded.`
87
- ) ;
88
- }
89
72
return true ;
90
73
} catch ( error ) {
91
74
const errorMessage = ( error as Error ) . message ;
@@ -104,7 +87,10 @@ export async function installSwiftlyToolchainVersion(
104
87
/**
105
88
* Shows a quick pick dialog to install available Swiftly toolchains
106
89
*/
107
- export async function installSwiftlyToolchain ( ctx : WorkspaceContext ) : Promise < void > {
90
+ export async function promptToInstallSwiftlyToolchain (
91
+ ctx : WorkspaceContext ,
92
+ type : "stable" | "snapshot"
93
+ ) : Promise < void > {
108
94
if ( ! Swiftly . isSupported ( ) ) {
109
95
ctx . logger ?. warn ( "Swiftly is not supported on this platform." ) ;
110
96
void vscode . window . showErrorMessage (
@@ -121,7 +107,21 @@ export async function installSwiftlyToolchain(ctx: WorkspaceContext): Promise<vo
121
107
return ;
122
108
}
123
109
124
- const availableToolchains = await Swiftly . listAvailable ( undefined , ctx . logger ) ;
110
+ let branch : string | undefined = undefined ;
111
+ if ( type === "snapshot" ) {
112
+ // Prompt user to enter the branch for snapshot toolchains
113
+ branch = await vscode . window . showInputBox ( {
114
+ title : "Enter Swift Snapshot Branch" ,
115
+ prompt : "Enter the branch name to list snapshot toolchains (e.g., 'main-snapshot', '6.1-snapshot')" ,
116
+ placeHolder : "main-snapshot" ,
117
+ value : "main-snapshot" ,
118
+ } ) ;
119
+ if ( ! branch ) {
120
+ return ; // User cancelled input
121
+ }
122
+ }
123
+
124
+ const availableToolchains = await Swiftly . listAvailable ( branch , ctx . logger ) ;
125
125
126
126
if ( availableToolchains . length === 0 ) {
127
127
ctx . logger ?. debug ( "No toolchains available for installation via Swiftly." ) ;
@@ -131,7 +131,9 @@ export async function installSwiftlyToolchain(ctx: WorkspaceContext): Promise<vo
131
131
return ;
132
132
}
133
133
134
- const uninstalledToolchains = availableToolchains . filter ( toolchain => ! toolchain . installed ) ;
134
+ const uninstalledToolchains = availableToolchains
135
+ . filter ( toolchain => ! toolchain . installed )
136
+ . filter ( toolchain => toolchain . version . type === type ) ;
135
137
136
138
if ( uninstalledToolchains . length === 0 ) {
137
139
ctx . logger ?. debug ( "All available toolchains are already installed." ) ;
@@ -154,85 +156,27 @@ export async function installSwiftlyToolchain(ctx: WorkspaceContext): Promise<vo
154
156
placeHolder : "Pick a Swift toolchain to install" ,
155
157
canPickMany : false ,
156
158
} ) ;
157
-
158
159
if ( ! selected ) {
159
160
return ;
160
161
}
161
162
162
- await downloadAndInstallToolchain ( selected , ctx ) ;
163
- }
164
-
165
- /**
166
- * Shows a quick pick dialog to install available Swiftly snapshot toolchains
167
- */
168
- export async function installSwiftlySnapshotToolchain ( ctx : WorkspaceContext ) : Promise < void > {
169
- if ( ! Swiftly . isSupported ( ) ) {
170
- void vscode . window . showErrorMessage (
171
- "Swiftly is not supported on this platform. Only macOS and Linux are supported."
172
- ) ;
163
+ const target = await askWhereToSetToolchain ( ) ;
164
+ if ( ! target ) {
173
165
return ;
174
166
}
175
167
176
- if ( ! ( await Swiftly . isInstalled ( ) ) ) {
177
- void vscode . window . showErrorMessage (
178
- "Swiftly is not installed. Please install Swiftly first from https://www.swift.org/install/"
179
- ) ;
168
+ // Install the toolchain via Swiftly
169
+ if ( ! ( await installSwiftlyToolchainWithProgress ( selected . toolchain . version . name , ctx . logger ) ) ) {
180
170
return ;
181
171
}
182
-
183
- // Prompt user to enter the branch for snapshot toolchains
184
- const branch = await vscode . window . showInputBox ( {
185
- title : "Enter Swift Snapshot Branch" ,
186
- prompt : "Enter the branch name to list snapshot toolchains (e.g., 'main-snapshot', '6.1-snapshot')" ,
187
- placeHolder : "main-snapshot" ,
188
- value : "main-snapshot" ,
189
- } ) ;
190
-
191
- if ( ! branch ) {
192
- return ; // User cancelled input
193
- }
194
-
195
- const availableToolchains = await Swiftly . listAvailable ( branch , ctx . logger ) ;
196
-
197
- if ( availableToolchains . length === 0 ) {
198
- ctx . logger ?. debug ( "No toolchains available for installation via Swiftly." ) ;
199
- void vscode . window . showInformationMessage (
200
- "No toolchains are available for installation via Swiftly."
201
- ) ;
202
- return ;
203
- }
204
-
205
- // Filter for only uninstalled snapshot toolchains
206
- const uninstalledSnapshotToolchains = availableToolchains . filter (
207
- toolchain => ! toolchain . installed && toolchain . version . type === "snapshot"
208
- ) ;
209
-
210
- if ( uninstalledSnapshotToolchains . length === 0 ) {
211
- ctx . logger ?. debug ( "All available snapshot toolchains are already installed." ) ;
212
- void vscode . window . showInformationMessage (
213
- "All available snapshot toolchains are already installed."
172
+ // Tell Swiftly to use the newly installed toolchain
173
+ if ( target === vscode . ConfigurationTarget . Workspace ) {
174
+ await Promise . all (
175
+ vscode . workspace . workspaceFolders ?. map ( folder =>
176
+ Swiftly . use ( selected . toolchain . version . name , folder . uri . fsPath )
177
+ ) ?? [ ]
214
178
) ;
215
179
return ;
216
180
}
217
-
218
- const quickPickItems = uninstalledSnapshotToolchains . map ( toolchain => ( {
219
- label : `$(cloud-download) ${ toolchain . version . name } ` ,
220
- description : "snapshot" ,
221
- detail : `Date: ${
222
- toolchain . version . type === "snapshot" ? toolchain . version . date || "Unknown" : "Unknown"
223
- } • Branch: ${ toolchain . version . type === "snapshot" ? toolchain . version . branch || "Unknown" : "Unknown" } `,
224
- toolchain : toolchain ,
225
- } ) ) ;
226
-
227
- const selected = await vscode . window . showQuickPick ( quickPickItems , {
228
- title : "Install Swift Snapshot Toolchain via Swiftly" ,
229
- placeHolder : "Pick a Swift snapshot toolchain to install" ,
230
- canPickMany : false ,
231
- } ) ;
232
-
233
- if ( ! selected ) {
234
- return ;
235
- }
236
-
237
- await downloadAndInstallToolchain ( selected , ctx ) ;
181
+ await Swiftly . use ( selected . toolchain . version . name ) ;
238
182
}
0 commit comments