@@ -19,13 +19,13 @@ import * as plist from "plist";
19
19
import * as vscode from "vscode" ;
20
20
import configuration from "../configuration" ;
21
21
import { SwiftOutputChannel } from "../ui/SwiftOutputChannel" ;
22
- import { execFile , ExecFileError , execSwift } from "../utilities/utilities" ;
22
+ import { execFile , execSwift } from "../utilities/utilities" ;
23
23
import { expandFilePathTilde , fileExists , pathExists } from "../utilities/filesystem" ;
24
24
import { Version } from "../utilities/version" ;
25
25
import { BuildFlags } from "./BuildFlags" ;
26
26
import { Sanitizer } from "./Sanitizer" ;
27
- import { SwiftlyConfig } from "./ToolchainVersion" ;
28
27
import { lineBreakRegex } from "../utilities/tasks" ;
28
+ import { swiftly } from "./swiftly" ;
29
29
30
30
/**
31
31
* Contents of **Info.plist** on Windows.
@@ -248,55 +248,6 @@ export class SwiftToolchain {
248
248
return result ;
249
249
}
250
250
251
- /**
252
- * Finds the list of toolchains managed by Swiftly.
253
- *
254
- * @returns an array of toolchain paths
255
- */
256
- public static async getSwiftlyToolchainInstalls ( ) : Promise < string [ ] > {
257
- // Swiftly is only available on Linux right now
258
- // TODO: Add support for macOS
259
- if ( process . platform !== "linux" ) {
260
- return [ ] ;
261
- }
262
- try {
263
- const swiftlyHomeDir : string | undefined = process . env [ "SWIFTLY_HOME_DIR" ] ;
264
- if ( ! swiftlyHomeDir ) {
265
- return [ ] ;
266
- }
267
- const swiftlyConfig = await SwiftToolchain . getSwiftlyConfig ( ) ;
268
- if ( ! swiftlyConfig || ! ( "installedToolchains" in swiftlyConfig ) ) {
269
- return [ ] ;
270
- }
271
- const installedToolchains = swiftlyConfig . installedToolchains ;
272
- if ( ! Array . isArray ( installedToolchains ) ) {
273
- return [ ] ;
274
- }
275
- return installedToolchains
276
- . filter ( ( toolchain ) : toolchain is string => typeof toolchain === "string" )
277
- . map ( toolchain => path . join ( swiftlyHomeDir , "toolchains" , toolchain ) ) ;
278
- } catch ( error ) {
279
- throw new Error ( "Failed to retrieve Swiftly installations from disk." ) ;
280
- }
281
- }
282
-
283
- /**
284
- * Reads the Swiftly configuration file, if it exists.
285
- *
286
- * @returns A parsed Swiftly configuration.
287
- */
288
- private static async getSwiftlyConfig ( ) : Promise < SwiftlyConfig | undefined > {
289
- const swiftlyHomeDir : string | undefined = process . env [ "SWIFTLY_HOME_DIR" ] ;
290
- if ( ! swiftlyHomeDir ) {
291
- return ;
292
- }
293
- const swiftlyConfigRaw = await fs . readFile (
294
- path . join ( swiftlyHomeDir , "config.json" ) ,
295
- "utf-8"
296
- ) ;
297
- return JSON . parse ( swiftlyConfigRaw ) ;
298
- }
299
-
300
251
/**
301
252
* Checks common directories for available swift toolchain installations.
302
253
*
@@ -608,7 +559,7 @@ export class SwiftToolchain {
608
559
let realSwift = await fs . realpath ( swift ) ;
609
560
if ( path . basename ( realSwift ) === "swiftly" ) {
610
561
try {
611
- const inUse = await this . swiftlyInUseLocation ( realSwift , cwd ) ;
562
+ const inUse = await swiftly . swiftlyInUseLocation ( realSwift , cwd ) ;
612
563
if ( inUse ) {
613
564
realSwift = path . join ( inUse , "usr" , "bin" , "swift" ) ;
614
565
}
@@ -660,7 +611,7 @@ export class SwiftToolchain {
660
611
const swiftlyPath = path . join ( configPath , "swiftly" ) ;
661
612
if ( await fileExists ( swiftlyPath ) ) {
662
613
try {
663
- const inUse = await this . swiftlyInUseLocation ( swiftlyPath , cwd ) ;
614
+ const inUse = await swiftly . swiftlyInUseLocation ( swiftlyPath , cwd ) ;
664
615
if ( inUse ) {
665
616
return path . join ( inUse , "usr" ) ;
666
617
}
@@ -671,7 +622,7 @@ export class SwiftToolchain {
671
622
return path . dirname ( configuration . path ) ;
672
623
}
673
624
674
- const swiftlyToolchainLocation = await this . swiftlyToolchain ( cwd ) ;
625
+ const swiftlyToolchainLocation = await swiftly . swiftlyToolchain ( cwd ) ;
675
626
if ( swiftlyToolchainLocation ) {
676
627
return swiftlyToolchainLocation ;
677
628
}
@@ -691,41 +642,6 @@ export class SwiftToolchain {
691
642
}
692
643
}
693
644
694
- private static async swiftlyInUseLocation ( swiftlyPath : string , cwd ?: vscode . Uri ) {
695
- const { stdout : inUse } = await execFile ( swiftlyPath , [ "use" , "--print-location" ] , {
696
- cwd : cwd ?. fsPath ,
697
- } ) ;
698
- return inUse . trimEnd ( ) ;
699
- }
700
-
701
- /**
702
- * Determine if Swiftly is being used to manage the active toolchain and if so, return
703
- * the path to the active toolchain.
704
- * @returns The location of the active toolchain if swiftly is being used to manage it.
705
- */
706
- private static async swiftlyToolchain ( cwd ?: vscode . Uri ) : Promise < string | undefined > {
707
- const swiftlyHomeDir : string | undefined = process . env [ "SWIFTLY_HOME_DIR" ] ;
708
- if ( swiftlyHomeDir ) {
709
- const { stdout : swiftLocation } = await execFile ( "which" , [ "swift" ] ) ;
710
- if ( swiftLocation . indexOf ( swiftlyHomeDir ) === 0 ) {
711
- // Print the location of the toolchain that swiftly is using. If there
712
- // is no cwd specified then it returns the global "inUse" toolchain otherwise
713
- // it respects the .swift-version file in the cwd and resolves using that.
714
- try {
715
- const inUse = await this . swiftlyInUseLocation ( "swiftly" , cwd ) ;
716
- if ( inUse . length > 0 ) {
717
- return path . join ( inUse , "usr" ) ;
718
- }
719
- } catch ( err : unknown ) {
720
- const error = err as ExecFileError ;
721
- // Its possible the toolchain in .swift-version is misconfigured or doesn't exist.
722
- void vscode . window . showErrorMessage ( `${ error . stderr } ` ) ;
723
- }
724
- }
725
- }
726
- return undefined ;
727
- }
728
-
729
645
/**
730
646
* @param targetInfo swift target info
731
647
* @returns path to Swift runtime
0 commit comments