Skip to content

Commit 7aa6735

Browse files
Rename swift-latest to Latest Installed Toolchain in the toolchain selection dialog (#877)
* filter out toolchain directories that don't have a Swift binary * add a notification to clarify what Latest Installed Toolchain means
1 parent f484b48 commit 7aa6735

File tree

2 files changed

+45
-17
lines changed

2 files changed

+45
-17
lines changed

src/toolchain/toolchain.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -261,13 +261,21 @@ export class SwiftToolchain {
261261
*/
262262
public static async findToolchainsIn(directory: string): Promise<string[]> {
263263
try {
264-
return (await fs.readdir(directory, { withFileTypes: true }))
265-
.filter(
266-
dirent =>
267-
dirent.name.startsWith("swift-") &&
268-
(dirent.isDirectory() || dirent.isSymbolicLink())
269-
)
270-
.map(dirent => path.join(dirent.path, dirent.name));
264+
const toolchains = await Promise.all(
265+
(await fs.readdir(directory, { withFileTypes: true }))
266+
.filter(dirent => dirent.name.startsWith("swift-"))
267+
.map(async dirent => {
268+
const toolchainPath = path.join(dirent.path, dirent.name);
269+
const toolchainSwiftPath = path.join(toolchainPath, "usr", "bin", "swift");
270+
if (!(await pathExists(toolchainSwiftPath))) {
271+
return null;
272+
}
273+
return toolchainPath;
274+
})
275+
);
276+
return toolchains.filter(
277+
(toolchain): toolchain is string => typeof toolchain === "string"
278+
);
271279
} catch {
272280
// Assume that there are no installations here
273281
return [];

src/ui/ToolchainSelection.ts

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ interface SwiftToolchainItem extends vscode.QuickPickItem {
9898
type: "toolchain";
9999
toolchainPath: string;
100100
swiftFolderPath: string;
101+
onDidSelect?(): Promise<void>;
101102
}
102103

103104
/** A {@link vscode.QuickPickItem} that performs an action for the user */
@@ -150,13 +151,24 @@ async function getQuickPickItems(
150151
});
151152
const toolchains = (await SwiftToolchain.getToolchainInstalls())
152153
.reverse()
153-
.map<SwiftToolchainItem>(toolchainPath => ({
154-
type: "toolchain",
155-
label: path.basename(toolchainPath, ".xctoolchain"),
156-
detail: toolchainPath,
157-
toolchainPath: path.join(toolchainPath, "usr"),
158-
swiftFolderPath: path.join(toolchainPath, "usr", "bin"),
159-
}));
154+
.map<SwiftToolchainItem>(toolchainPath => {
155+
const result: SwiftToolchainItem = {
156+
type: "toolchain",
157+
label: path.basename(toolchainPath, ".xctoolchain"),
158+
detail: toolchainPath,
159+
toolchainPath: path.join(toolchainPath, "usr"),
160+
swiftFolderPath: path.join(toolchainPath, "usr", "bin"),
161+
};
162+
if (result.label === "swift-latest") {
163+
result.label = "Latest Installed Toolchain";
164+
result.onDidSelect = async () => {
165+
vscode.window.showInformationMessage(
166+
`The Swift extension is now configured to always use the most recently installed toolchain pointed at by the symbolic link "${toolchainPath}".`
167+
);
168+
};
169+
}
170+
return result;
171+
});
160172
const swiftlyToolchains = (await SwiftToolchain.getSwiftlyToolchainInstalls())
161173
.reverse()
162174
.map<SwiftToolchainItem>(toolchainPath => ({
@@ -233,7 +245,10 @@ export async function showToolchainSelectionQuickPick(activeToolchain: SwiftTool
233245
if (selected?.type === "action") {
234246
await selected.run();
235247
} else if (selected?.type === "toolchain") {
236-
await setToolchainPath(selected.swiftFolderPath, "prompt");
248+
const isUpdated = await setToolchainPath(selected.swiftFolderPath, "prompt");
249+
if (isUpdated && selected.onDidSelect) {
250+
await selected.onDidSelect();
251+
}
237252
}
238253
}
239254

@@ -249,7 +264,7 @@ async function removeToolchainPath() {
249264
async function setToolchainPath(
250265
value: string | undefined,
251266
target?: vscode.ConfigurationTarget | "prompt"
252-
): Promise<void> {
267+
): Promise<boolean> {
253268
if (target === "prompt") {
254269
const items: (vscode.QuickPickItem & {
255270
target?: vscode.ConfigurationTarget;
@@ -275,14 +290,19 @@ async function setToolchainPath(
275290
canPickMany: false,
276291
});
277292
if (!selected) {
278-
return;
293+
return false;
279294
}
280295
target = selected.target;
281296
} else {
282297
target = vscode.ConfigurationTarget.Global; // Global scope by default
283298
}
284299
}
285300
await vscode.workspace.getConfiguration("swift").update("path", value, target);
301+
await checkAndRemoveWorkspaceSetting(target);
302+
return true;
303+
}
304+
305+
async function checkAndRemoveWorkspaceSetting(target: vscode.ConfigurationTarget | undefined) {
286306
// Check to see if the configuration would be overridden by workspace settings
287307
if (target !== vscode.ConfigurationTarget.Global) {
288308
return;

0 commit comments

Comments
 (0)