Skip to content

Commit fa61d31

Browse files
Allow setting the Swiftly toolchain at the workspace level (#1860)
1 parent 165429c commit fa61d31

File tree

5 files changed

+480
-445
lines changed

5 files changed

+480
-445
lines changed

src/toolchain/swiftly.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
// SPDX-License-Identifier: Apache-2.0
1212
//
1313
//===----------------------------------------------------------------------===//
14+
import { ExecFileOptions } from "child_process";
1415
import * as fsSync from "fs";
1516
import * as fs from "fs/promises";
1617
import * as os from "os";
@@ -273,9 +274,9 @@ export class Swiftly {
273274
if (!Array.isArray(installedToolchains)) {
274275
return [];
275276
}
276-
return installedToolchains
277-
.filter((toolchain): toolchain is string => typeof toolchain === "string")
278-
.map(toolchain => path.join(swiftlyHomeDir, "toolchains", toolchain));
277+
return installedToolchains.filter(
278+
(toolchain): toolchain is string => typeof toolchain === "string"
279+
);
279280
} catch (error) {
280281
logger?.error(`Failed to retrieve Swiftly installations: ${error}`);
281282
throw new Error(
@@ -333,12 +334,21 @@ export class Swiftly {
333334
* Instructs Swiftly to use a specific version of the Swift toolchain.
334335
*
335336
* @param version The version name to use. Obtainable via {@link Swiftly.list}.
337+
* @param [cwd] Optional working directory to set the toolchain within.
336338
*/
337-
public static async use(version: string): Promise<void> {
339+
public static async use(version: string, cwd?: string): Promise<void> {
338340
if (!this.isSupported()) {
339341
throw new Error("Swiftly is not supported on this platform");
340342
}
341-
await execFile("swiftly", ["use", version]);
343+
const useArgs = ["use", "-y"];
344+
const options: ExecFileOptions = {};
345+
if (cwd) {
346+
options.cwd = cwd;
347+
} else {
348+
useArgs.push("--global-default");
349+
}
350+
useArgs.push(version);
351+
await execFile("swiftly", useArgs, options);
342352
}
343353

344354
/**

0 commit comments

Comments
 (0)