Skip to content

Commit 8c6e2d7

Browse files
clean up naming of Swiftly methods
1 parent f35b386 commit 8c6e2d7

File tree

6 files changed

+87
-63
lines changed

6 files changed

+87
-63
lines changed

src/commands/installSwiftlyToolchain.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ export async function installSwiftlyToolchain(ctx: WorkspaceContext): Promise<vo
9898
return;
9999
}
100100

101-
const availableToolchains = await Swiftly.listAvailable(ctx.logger);
101+
const availableToolchains = await Swiftly.listAvailable(undefined, ctx.logger);
102102

103103
if (availableToolchains.length === 0) {
104104
ctx.logger?.debug("No toolchains available for installation via Swiftly.");
@@ -174,7 +174,7 @@ export async function installSwiftlySnapshotToolchain(ctx: WorkspaceContext): Pr
174174
return; // User cancelled input
175175
}
176176

177-
const availableToolchains = await Swiftly.listAvailable(ctx.logger, branch);
177+
const availableToolchains = await Swiftly.listAvailable(branch, ctx.logger);
178178

179179
if (availableToolchains.length === 0) {
180180
ctx.logger?.debug("No toolchains available for installation via Swiftly.");

src/toolchain/swiftly.ts

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,11 @@ export class Swiftly {
167167
}
168168

169169
/**
170-
* Finds the list of toolchains managed by Swiftly.
170+
* Finds the list of toolchains installed via Swiftly.
171171
*
172-
* @returns an array of toolchain paths
172+
* @returns an array of toolchain version names.
173173
*/
174-
public static async listAvailableToolchains(logger?: SwiftLogger): Promise<string[]> {
174+
public static async list(logger?: SwiftLogger): Promise<string[]> {
175175
if (!this.isSupported()) {
176176
return [];
177177
}
@@ -182,13 +182,13 @@ export class Swiftly {
182182
}
183183

184184
if (!(await Swiftly.supportsJsonOutput(logger))) {
185-
return await Swiftly.getToolchainInstallLegacy(logger);
185+
return await Swiftly.listFromSwiftlyConfig(logger);
186186
}
187187

188-
return await Swiftly.getListAvailableToolchains(logger);
188+
return await Swiftly.listUsingJSONFormat(logger);
189189
}
190190

191-
private static async getListAvailableToolchains(logger?: SwiftLogger): Promise<string[]> {
191+
private static async listUsingJSONFormat(logger?: SwiftLogger): Promise<string[]> {
192192
try {
193193
const { stdout } = await execFile("swiftly", ["list", "--format=json"]);
194194
const response = ListResult.parse(JSON.parse(stdout));
@@ -201,7 +201,7 @@ export class Swiftly {
201201
}
202202
}
203203

204-
private static async getToolchainInstallLegacy(logger?: SwiftLogger) {
204+
private static async listFromSwiftlyConfig(logger?: SwiftLogger) {
205205
try {
206206
const swiftlyHomeDir: string | undefined = process.env["SWIFTLY_HOME_DIR"];
207207
if (!swiftlyHomeDir) {
@@ -226,17 +226,32 @@ export class Swiftly {
226226
}
227227
}
228228

229+
/**
230+
* Checks whether or not the current operating system supports Swiftly.
231+
*/
229232
public static isSupported() {
230233
return process.platform === "linux" || process.platform === "darwin";
231234
}
232235

236+
/**
237+
* Retrieves the location of the toolchain that is currently in use by Swiftly.
238+
*
239+
* @param swiftlyPath Optional path to the Swiftly binary.
240+
* @param cwd Optional current working directory to check within.
241+
*/
233242
public static async inUseLocation(swiftlyPath: string = "swiftly", cwd?: vscode.Uri) {
234243
const { stdout: inUse } = await execFile(swiftlyPath, ["use", "--print-location"], {
235244
cwd: cwd?.fsPath,
236245
});
237246
return inUse.trimEnd();
238247
}
239248

249+
/**
250+
* Retrieves the version name of the toolchain that is currently in use by Swiftly.
251+
*
252+
* @param swiftlyPath Optional path to the Swiftly binary.
253+
* @param cwd Optional current working directory to check within.
254+
*/
240255
public static async inUseVersion(
241256
swiftlyPath: string = "swiftly",
242257
cwd?: vscode.Uri
@@ -256,6 +271,11 @@ export class Swiftly {
256271
return result.version;
257272
}
258273

274+
/**
275+
* Instructs Swiftly to use a specific version of the Swift toolchain.
276+
*
277+
* @param version The version name to use. Obtainable via {@link Swiftly.list}.
278+
*/
259279
public static async use(version: string): Promise<void> {
260280
if (!this.isSupported()) {
261281
throw new Error("Swiftly is not supported on this platform");
@@ -266,6 +286,7 @@ export class Swiftly {
266286
/**
267287
* Determine if Swiftly is being used to manage the active toolchain and if so, return
268288
* the path to the active toolchain.
289+
*
269290
* @returns The location of the active toolchain if swiftly is being used to manage it.
270291
*/
271292
public static async toolchain(
@@ -298,15 +319,15 @@ export class Swiftly {
298319
}
299320

300321
/**
301-
* Lists all toolchains available for installation from swiftly
322+
* Lists all toolchains available for installation from swiftly.
302323
*
303-
* @param branch Optional branch to filter available toolchains (e.g., "main" for snapshots)
304-
* @param logger Optional logger for error reporting
305-
* @returns Array of available toolchains
324+
* @param branch Optional branch to filter available toolchains (e.g., "main" for snapshots).
325+
* @param logger Optional logger for error reporting.
326+
* @returns Array of available toolchains.
306327
*/
307328
public static async listAvailable(
308-
logger?: SwiftLogger,
309-
branch?: string
329+
branch?: string,
330+
logger?: SwiftLogger
310331
): Promise<SwiftlyToolchain[]> {
311332
if (!this.isSupported()) {
312333
return [];
@@ -340,11 +361,11 @@ export class Swiftly {
340361
}
341362

342363
/**
343-
* Installs a toolchain via swiftly with optional progress tracking
364+
* Installs a toolchain via swiftly with optional progress tracking.
344365
*
345-
* @param version The toolchain version to install
346-
* @param progressCallback Optional callback that receives progress data as JSON objects
347-
* @param logger Optional logger for error reporting
366+
* @param version The toolchain version to install.
367+
* @param progressCallback Optional callback that receives progress data as JSON objects.
368+
* @param logger Optional logger for error reporting.
348369
*/
349370
public static async installToolchain(
350371
version: string,
@@ -657,6 +678,9 @@ export class Swiftly {
657678
return JSON.parse(swiftlyConfigRaw);
658679
}
659680

681+
/**
682+
* Checks whether or not Swiftly is installed on the current system.
683+
*/
660684
public static async isInstalled(): Promise<boolean> {
661685
if (!this.isSupported()) {
662686
return false;

src/ui/ToolchainSelection.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -213,24 +213,26 @@ async function getQuickPickItems(
213213
const sortedToolchains = toolchains.sort((a, b) => b.label.localeCompare(a.label));
214214

215215
// Find any Swift toolchains installed via Swiftly
216-
const swiftlyToolchains = (
217-
await Swiftly.listAvailableToolchains(logger)
218-
).map<SwiftlyToolchainItem>(toolchainPath => ({
219-
type: "toolchain",
220-
label: path.basename(toolchainPath),
221-
category: "swiftly",
222-
version: path.basename(toolchainPath),
223-
onDidSelect: async () => {
224-
try {
225-
await Swiftly.use(toolchainPath);
226-
void showReloadExtensionNotification(
227-
"Changing the Swift path requires Visual Studio Code be reloaded."
228-
);
229-
} catch (error) {
230-
void vscode.window.showErrorMessage(`Failed to switch Swiftly toolchain: ${error}`);
231-
}
232-
},
233-
}));
216+
const swiftlyToolchains = (await Swiftly.list(logger)).map<SwiftlyToolchainItem>(
217+
toolchainPath => ({
218+
type: "toolchain",
219+
label: path.basename(toolchainPath),
220+
category: "swiftly",
221+
version: path.basename(toolchainPath),
222+
onDidSelect: async () => {
223+
try {
224+
await Swiftly.use(toolchainPath);
225+
void showReloadExtensionNotification(
226+
"Changing the Swift path requires Visual Studio Code be reloaded."
227+
);
228+
} catch (error) {
229+
void vscode.window.showErrorMessage(
230+
`Failed to switch Swiftly toolchain: ${error}`
231+
);
232+
}
233+
},
234+
})
235+
);
234236

235237
if (activeToolchain) {
236238
const currentSwiftlyVersion = activeToolchain.isSwiftlyManaged

test/unit-tests/toolchain/swiftly.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ suite("Swiftly Unit Tests", () => {
6262
}
6363
});
6464

65-
suite("getSwiftlyToolchainInstalls", () => {
65+
suite("list()", () => {
6666
test("should return toolchain names from list-available command for version 1.1.0", async () => {
6767
// Mock version check to return 1.1.0
6868
mockUtilities.execFile.withArgs("swiftly", ["--version"]).resolves({
@@ -123,7 +123,7 @@ suite("Swiftly Unit Tests", () => {
123123
stderr: "",
124124
});
125125

126-
const result = await Swiftly.listAvailableToolchains();
126+
const result = await Swiftly.list();
127127

128128
expect(result).to.deep.equal([
129129
"xcode",
@@ -216,7 +216,7 @@ suite("Swiftly Unit Tests", () => {
216216
stderr: "",
217217
});
218218

219-
const result = await Swiftly.listAvailableToolchains();
219+
const result = await Swiftly.list();
220220

221221
expect(result).to.deep.equal([
222222
"xcode",
@@ -235,7 +235,7 @@ suite("Swiftly Unit Tests", () => {
235235
test("should return empty array when platform is not supported", async () => {
236236
mockedPlatform.setValue("win32");
237237

238-
const result = await Swiftly.listAvailableToolchains();
238+
const result = await Swiftly.list();
239239

240240
expect(result).to.deep.equal([]);
241241
expect(mockUtilities.execFile).not.have.been.called;
@@ -316,7 +316,7 @@ suite("Swiftly Unit Tests", () => {
316316
});
317317
});
318318

319-
suite("listAvailable", () => {
319+
suite("listAvailable()", () => {
320320
test("should return empty array on unsupported platform", async () => {
321321
mockedPlatform.setValue("win32");
322322

@@ -588,7 +588,7 @@ suite("Swiftly Unit Tests", () => {
588588
stderr: "",
589589
});
590590

591-
const result = await Swiftly.listAvailable(undefined, "main-snapshot");
591+
const result = await Swiftly.listAvailable("main-snapshot");
592592
expect(result).to.deep.equal([
593593
{
594594
inUse: false,

test/unit-tests/toolchain/toolchain.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ suite("SwiftToolchain Unit Test Suite", () => {
314314
}),
315315
});
316316

317-
const toolchains = await Swiftly.listAvailableToolchains();
317+
const toolchains = await Swiftly.list();
318318
expect(toolchains).to.deep.equal([
319319
path.join(mockHomeDir, "toolchains", "swift-5.9.0"),
320320
path.join(mockHomeDir, "toolchains", "swift-6.0.0"),
@@ -332,7 +332,7 @@ suite("SwiftToolchain Unit Test Suite", () => {
332332
}),
333333
});
334334

335-
const toolchains = await Swiftly.listAvailableToolchains();
335+
const toolchains = await Swiftly.list();
336336
expect(toolchains).to.deep.equal([
337337
path.join(mockHomeDir, "toolchains", "swift-5.9.0"),
338338
path.join(mockHomeDir, "toolchains", "swift-6.0.0"),
@@ -343,7 +343,7 @@ suite("SwiftToolchain Unit Test Suite", () => {
343343
mockedPlatform.setValue("linux");
344344
mockedEnv.setValue({});
345345

346-
const toolchains = await Swiftly.listAvailableToolchains();
346+
const toolchains = await Swiftly.list();
347347
expect(toolchains).to.be.empty;
348348
});
349349

@@ -354,7 +354,7 @@ suite("SwiftToolchain Unit Test Suite", () => {
354354

355355
mockFS({});
356356

357-
await expect(Swiftly.listAvailableToolchains()).to.be.rejected.then(error => {
357+
await expect(Swiftly.list()).to.be.rejected.then(error => {
358358
expect(error.message).to.include(
359359
"Failed to retrieve Swiftly installations from disk"
360360
);
@@ -372,13 +372,13 @@ suite("SwiftToolchain Unit Test Suite", () => {
372372
}),
373373
});
374374

375-
const toolchains = await Swiftly.listAvailableToolchains();
375+
const toolchains = await Swiftly.list();
376376
expect(toolchains).to.be.empty;
377377
});
378378

379379
test("returns empty array on Windows", async () => {
380380
mockedPlatform.setValue("win32");
381-
const toolchains = await Swiftly.listAvailableToolchains();
381+
const toolchains = await Swiftly.list();
382382
expect(toolchains).to.be.empty;
383383
});
384384

@@ -393,7 +393,7 @@ suite("SwiftToolchain Unit Test Suite", () => {
393393
}),
394394
});
395395

396-
const toolchains = await Swiftly.listAvailableToolchains();
396+
const toolchains = await Swiftly.list();
397397
expect(toolchains).to.deep.equal([
398398
path.join(mockHomeDir, "toolchains", "swift-5.9.0"),
399399
path.join(mockHomeDir, "toolchains", "swift-6.0.0"),

0 commit comments

Comments
 (0)