@@ -22,6 +22,7 @@ import { z } from "zod/v4/mini";
22
22
23
23
// Import the reusable installation function
24
24
import { installSwiftlyToolchainVersion } from "../commands/installSwiftlyToolchain" ;
25
+ import { ContextKeys } from "../contextKeys" ;
25
26
import { SwiftLogger } from "../logging/SwiftLogger" ;
26
27
import { showMissingToolchainDialog } from "../ui/ToolchainSelection" ;
27
28
import { findBinaryPath } from "../utilities/shell" ;
@@ -220,11 +221,11 @@ export class Swiftly {
220
221
}
221
222
222
223
/**
223
- * Finds the list of toolchains managed by Swiftly.
224
+ * Finds the list of toolchains installed via Swiftly.
224
225
*
225
- * @returns an array of toolchain paths
226
+ * @returns an array of toolchain version names.
226
227
*/
227
- public static async listAvailableToolchains ( logger ?: SwiftLogger ) : Promise < string [ ] > {
228
+ public static async list ( logger ?: SwiftLogger ) : Promise < string [ ] > {
228
229
if ( ! this . isSupported ( ) ) {
229
230
return [ ] ;
230
231
}
@@ -235,13 +236,13 @@ export class Swiftly {
235
236
}
236
237
237
238
if ( ! ( await Swiftly . supportsJsonOutput ( logger ) ) ) {
238
- return await Swiftly . getToolchainInstallLegacy ( logger ) ;
239
+ return await Swiftly . listFromSwiftlyConfig ( logger ) ;
239
240
}
240
241
241
- return await Swiftly . getListAvailableToolchains ( logger ) ;
242
+ return await Swiftly . listUsingJSONFormat ( logger ) ;
242
243
}
243
244
244
- private static async getListAvailableToolchains ( logger ?: SwiftLogger ) : Promise < string [ ] > {
245
+ private static async listUsingJSONFormat ( logger ?: SwiftLogger ) : Promise < string [ ] > {
245
246
try {
246
247
const { stdout } = await execFile ( "swiftly" , [ "list" , "--format=json" ] ) ;
247
248
const response = ListResult . parse ( JSON . parse ( stdout ) ) ;
@@ -254,7 +255,7 @@ export class Swiftly {
254
255
}
255
256
}
256
257
257
- private static async getToolchainInstallLegacy ( logger ?: SwiftLogger ) {
258
+ private static async listFromSwiftlyConfig ( logger ?: SwiftLogger ) {
258
259
try {
259
260
const swiftlyHomeDir : string | undefined = process . env [ "SWIFTLY_HOME_DIR" ] ;
260
261
if ( ! swiftlyHomeDir ) {
@@ -279,17 +280,32 @@ export class Swiftly {
279
280
}
280
281
}
281
282
283
+ /**
284
+ * Checks whether or not the current operating system supports Swiftly.
285
+ */
282
286
public static isSupported ( ) {
283
287
return process . platform === "linux" || process . platform === "darwin" ;
284
288
}
285
289
290
+ /**
291
+ * Retrieves the location of the toolchain that is currently in use by Swiftly.
292
+ *
293
+ * @param swiftlyPath Optional path to the Swiftly binary.
294
+ * @param cwd Optional current working directory to check within.
295
+ */
286
296
public static async inUseLocation ( swiftlyPath : string = "swiftly" , cwd ?: vscode . Uri ) {
287
297
const { stdout : inUse } = await execFile ( swiftlyPath , [ "use" , "--print-location" ] , {
288
298
cwd : cwd ?. fsPath ,
289
299
} ) ;
290
300
return inUse . trimEnd ( ) ;
291
301
}
292
302
303
+ /**
304
+ * Retrieves the version name of the toolchain that is currently in use by Swiftly.
305
+ *
306
+ * @param swiftlyPath Optional path to the Swiftly binary.
307
+ * @param cwd Optional current working directory to check within.
308
+ */
293
309
public static async inUseVersion (
294
310
swiftlyPath : string = "swiftly" ,
295
311
cwd ?: vscode . Uri
@@ -309,6 +325,11 @@ export class Swiftly {
309
325
return result . version ;
310
326
}
311
327
328
+ /**
329
+ * Instructs Swiftly to use a specific version of the Swift toolchain.
330
+ *
331
+ * @param version The version name to use. Obtainable via {@link Swiftly.list}.
332
+ */
312
333
public static async use ( version : string ) : Promise < void > {
313
334
if ( ! this . isSupported ( ) ) {
314
335
throw new Error ( "Swiftly is not supported on this platform" ) ;
@@ -319,6 +340,7 @@ export class Swiftly {
319
340
/**
320
341
* Determine if Swiftly is being used to manage the active toolchain and if so, return
321
342
* the path to the active toolchain.
343
+ *
322
344
* @returns The location of the active toolchain if swiftly is being used to manage it.
323
345
*/
324
346
public static async toolchain (
@@ -383,15 +405,15 @@ export class Swiftly {
383
405
}
384
406
385
407
/**
386
- * Lists all toolchains available for installation from swiftly
408
+ * Lists all toolchains available for installation from swiftly.
387
409
*
388
- * @param branch Optional branch to filter available toolchains (e.g., "main" for snapshots)
389
- * @param logger Optional logger for error reporting
390
- * @returns Array of available toolchains
410
+ * @param branch Optional branch to filter available toolchains (e.g., "main" for snapshots).
411
+ * @param logger Optional logger for error reporting.
412
+ * @returns Array of available toolchains.
391
413
*/
392
414
public static async listAvailable (
393
- logger ?: SwiftLogger ,
394
- branch ?: string
415
+ branch ?: string ,
416
+ logger ?: SwiftLogger
395
417
) : Promise < SwiftlyToolchain [ ] > {
396
418
if ( ! this . isSupported ( ) ) {
397
419
return [ ] ;
@@ -425,11 +447,11 @@ export class Swiftly {
425
447
}
426
448
427
449
/**
428
- * Installs a toolchain via swiftly with optional progress tracking
450
+ * Installs a toolchain via swiftly with optional progress tracking.
429
451
*
430
- * @param version The toolchain version to install
431
- * @param progressCallback Optional callback that receives progress data as JSON objects
432
- * @param logger Optional logger for error reporting
452
+ * @param version The toolchain version to install.
453
+ * @param progressCallback Optional callback that receives progress data as JSON objects.
454
+ * @param logger Optional logger for error reporting.
433
455
*/
434
456
public static async installToolchain (
435
457
version : string ,
@@ -742,6 +764,9 @@ export class Swiftly {
742
764
return JSON . parse ( swiftlyConfigRaw ) ;
743
765
}
744
766
767
+ /**
768
+ * Checks whether or not Swiftly is installed on the current system.
769
+ */
745
770
public static async isInstalled ( ) : Promise < boolean > {
746
771
if ( ! this . isSupported ( ) ) {
747
772
return false ;
@@ -754,3 +779,32 @@ export class Swiftly {
754
779
}
755
780
}
756
781
}
782
+
783
+ /**
784
+ * Checks whether or not Swiftly is installed and updates context keys appropriately.
785
+ */
786
+ export function checkForSwiftlyInstallation ( contextKeys : ContextKeys , logger : SwiftLogger ) : void {
787
+ contextKeys . supportsSwiftlyInstall = false ;
788
+ if ( ! Swiftly . isSupported ( ) ) {
789
+ logger . debug ( `Swiftly is not available on ${ process . platform } ` ) ;
790
+ return ;
791
+ }
792
+ // Don't block while checking the Swiftly insallation.
793
+ void Swiftly . isInstalled ( ) . then ( async isInstalled => {
794
+ if ( ! isInstalled ) {
795
+ logger . debug ( "Swiftly is not installed on this system." ) ;
796
+ return ;
797
+ }
798
+ const version = await Swiftly . version ( logger ) ;
799
+ if ( ! version ) {
800
+ logger . warn ( "Unable to determine Swiftly version." ) ;
801
+ return ;
802
+ }
803
+ logger . debug ( `Detected Swiftly version ${ version } .` ) ;
804
+ contextKeys . supportsSwiftlyInstall = version . isGreaterThanOrEqual ( {
805
+ major : 1 ,
806
+ minor : 1 ,
807
+ patch : 0 ,
808
+ } ) ;
809
+ } ) ;
810
+ }
0 commit comments