@@ -22,6 +22,7 @@ import { z } from "zod/v4/mini";
2222
2323// Import the reusable installation function
2424import { installSwiftlyToolchainVersion } from "../commands/installSwiftlyToolchain" ;
25+ import { ContextKeys } from "../contextKeys" ;
2526import { SwiftLogger } from "../logging/SwiftLogger" ;
2627import { showMissingToolchainDialog } from "../ui/ToolchainSelection" ;
2728import { findBinaryPath } from "../utilities/shell" ;
@@ -220,11 +221,11 @@ export class Swiftly {
220221 }
221222
222223 /**
223- * Finds the list of toolchains managed by Swiftly.
224+ * Finds the list of toolchains installed via Swiftly.
224225 *
225- * @returns an array of toolchain paths
226+ * @returns an array of toolchain version names.
226227 */
227- public static async listAvailableToolchains ( logger ?: SwiftLogger ) : Promise < string [ ] > {
228+ public static async list ( logger ?: SwiftLogger ) : Promise < string [ ] > {
228229 if ( ! this . isSupported ( ) ) {
229230 return [ ] ;
230231 }
@@ -235,13 +236,13 @@ export class Swiftly {
235236 }
236237
237238 if ( ! ( await Swiftly . supportsJsonOutput ( logger ) ) ) {
238- return await Swiftly . getToolchainInstallLegacy ( logger ) ;
239+ return await Swiftly . listFromSwiftlyConfig ( logger ) ;
239240 }
240241
241- return await Swiftly . getListAvailableToolchains ( logger ) ;
242+ return await Swiftly . listUsingJSONFormat ( logger ) ;
242243 }
243244
244- private static async getListAvailableToolchains ( logger ?: SwiftLogger ) : Promise < string [ ] > {
245+ private static async listUsingJSONFormat ( logger ?: SwiftLogger ) : Promise < string [ ] > {
245246 try {
246247 const { stdout } = await execFile ( "swiftly" , [ "list" , "--format=json" ] ) ;
247248 const response = ListResult . parse ( JSON . parse ( stdout ) ) ;
@@ -254,7 +255,7 @@ export class Swiftly {
254255 }
255256 }
256257
257- private static async getToolchainInstallLegacy ( logger ?: SwiftLogger ) {
258+ private static async listFromSwiftlyConfig ( logger ?: SwiftLogger ) {
258259 try {
259260 const swiftlyHomeDir : string | undefined = process . env [ "SWIFTLY_HOME_DIR" ] ;
260261 if ( ! swiftlyHomeDir ) {
@@ -279,17 +280,32 @@ export class Swiftly {
279280 }
280281 }
281282
283+ /**
284+ * Checks whether or not the current operating system supports Swiftly.
285+ */
282286 public static isSupported ( ) {
283287 return process . platform === "linux" || process . platform === "darwin" ;
284288 }
285289
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+ */
286296 public static async inUseLocation ( swiftlyPath : string = "swiftly" , cwd ?: vscode . Uri ) {
287297 const { stdout : inUse } = await execFile ( swiftlyPath , [ "use" , "--print-location" ] , {
288298 cwd : cwd ?. fsPath ,
289299 } ) ;
290300 return inUse . trimEnd ( ) ;
291301 }
292302
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+ */
293309 public static async inUseVersion (
294310 swiftlyPath : string = "swiftly" ,
295311 cwd ?: vscode . Uri
@@ -309,6 +325,11 @@ export class Swiftly {
309325 return result . version ;
310326 }
311327
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+ */
312333 public static async use ( version : string ) : Promise < void > {
313334 if ( ! this . isSupported ( ) ) {
314335 throw new Error ( "Swiftly is not supported on this platform" ) ;
@@ -319,6 +340,7 @@ export class Swiftly {
319340 /**
320341 * Determine if Swiftly is being used to manage the active toolchain and if so, return
321342 * the path to the active toolchain.
343+ *
322344 * @returns The location of the active toolchain if swiftly is being used to manage it.
323345 */
324346 public static async toolchain (
@@ -383,15 +405,15 @@ export class Swiftly {
383405 }
384406
385407 /**
386- * Lists all toolchains available for installation from swiftly
408+ * Lists all toolchains available for installation from swiftly.
387409 *
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.
391413 */
392414 public static async listAvailable (
393- logger ?: SwiftLogger ,
394- branch ?: string
415+ branch ?: string ,
416+ logger ?: SwiftLogger
395417 ) : Promise < SwiftlyToolchain [ ] > {
396418 if ( ! this . isSupported ( ) ) {
397419 return [ ] ;
@@ -425,11 +447,11 @@ export class Swiftly {
425447 }
426448
427449 /**
428- * Installs a toolchain via swiftly with optional progress tracking
450+ * Installs a toolchain via swiftly with optional progress tracking.
429451 *
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.
433455 */
434456 public static async installToolchain (
435457 version : string ,
@@ -742,6 +764,9 @@ export class Swiftly {
742764 return JSON . parse ( swiftlyConfigRaw ) ;
743765 }
744766
767+ /**
768+ * Checks whether or not Swiftly is installed on the current system.
769+ */
745770 public static async isInstalled ( ) : Promise < boolean > {
746771 if ( ! this . isSupported ( ) ) {
747772 return false ;
@@ -754,3 +779,32 @@ export class Swiftly {
754779 }
755780 }
756781}
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