Skip to content

Commit 76f3df7

Browse files
committed
Refactoring the code
1 parent dec1de1 commit 76f3df7

File tree

5 files changed

+134
-224
lines changed

5 files changed

+134
-224
lines changed

src/commands.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@ import { generateLaunchConfigurations } from "./commands/generateLaunchConfigura
5151
import { runTest } from "./commands/runTest";
5252
import { generateSourcekitConfiguration } from "./commands/generateSourcekitConfiguration";
5353
import { SwiftLogger } from "./logging/SwiftLogger";
54-
import { installSwiftlyToolchain } from "./commands/installSwiftlyToolchain";
55-
import { installSwiftlySnapshotToolchain } from "./commands/installSwiftlySnapshotToolchain";
54+
import {
55+
installSwiftlyToolchain,
56+
installSwiftlySnapshotToolchain,
57+
} from "./commands/installSwiftlyToolchain";
5658

5759
/**
5860
* References:

src/commands/installSwiftlySnapshotToolchain.ts

Lines changed: 0 additions & 154 deletions
This file was deleted.

src/commands/installSwiftlyToolchain.ts

Lines changed: 120 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,85 @@
1313
//===----------------------------------------------------------------------===//
1414

1515
import * as vscode from "vscode";
16+
import { QuickPickItem } from "vscode";
1617
import { WorkspaceContext } from "../WorkspaceContext";
1718
import {
18-
Swiftly,
19-
SwiftlyProgressData,
2019
AvailableToolchain,
21-
isStableVersion,
2220
isSnapshotVersion,
21+
isStableVersion,
22+
Swiftly,
23+
SwiftlyProgressData,
2324
} from "../toolchain/swiftly";
2425
import { showReloadExtensionNotification } from "../ui/ReloadExtension";
2526

27+
interface SwiftlyToolchainItem extends QuickPickItem {
28+
toolchain: AvailableToolchain;
29+
}
30+
31+
async function downloadAndInstallToolchain(selected: SwiftlyToolchainItem, ctx: WorkspaceContext) {
32+
try {
33+
await vscode.window.withProgress(
34+
{
35+
location: vscode.ProgressLocation.Notification,
36+
title: `Installing Swift ${selected.toolchain.version.name}`,
37+
cancellable: false,
38+
},
39+
async progress => {
40+
progress.report({ message: "Starting installation..." });
41+
42+
let lastProgress = 0;
43+
44+
await Swiftly.installToolchain(
45+
selected.toolchain.version.name,
46+
(progressData: SwiftlyProgressData) => {
47+
if (
48+
progressData.step?.percent !== undefined &&
49+
progressData.step.percent > lastProgress
50+
) {
51+
const increment = progressData.step.percent - lastProgress;
52+
progress.report({
53+
increment,
54+
message:
55+
progressData.step.text ??
56+
`${progressData.step.percent}% complete`,
57+
});
58+
lastProgress = progressData.step.percent;
59+
}
60+
},
61+
ctx.logger
62+
);
63+
64+
progress.report({
65+
increment: 100 - lastProgress,
66+
message: "Installation complete",
67+
});
68+
}
69+
);
70+
void showReloadExtensionNotification(
71+
`Swift ${selected.toolchain.version.name} has been installed and activated. Visual Studio Code needs to be reloaded.`
72+
);
73+
} catch (error) {
74+
ctx.logger?.error(`Failed to install Swift ${selected.toolchain.version.name}: ${error}`);
75+
void vscode.window.showErrorMessage(
76+
`Failed to install Swift ${selected.toolchain.version.name}: ${error}`
77+
);
78+
}
79+
}
80+
2681
/**
2782
* Shows a quick pick dialog to install available Swiftly toolchains
2883
*/
2984
export async function installSwiftlyToolchain(ctx: WorkspaceContext): Promise<void> {
3085
if (!Swiftly.isSupported()) {
86+
ctx.logger?.warn("Swiftly is not supported on this platform.");
3187
void vscode.window.showErrorMessage(
3288
"Swiftly is not supported on this platform. Only macOS and Linux are supported."
3389
);
3490
return;
3591
}
3692

3793
if (!(await Swiftly.isInstalled())) {
94+
ctx.logger?.warn("Swiftly is not installed.");
3895
void vscode.window.showErrorMessage(
3996
"Swiftly is not installed. Please install Swiftly first from https://www.swift.org/install/"
4097
);
@@ -50,7 +107,7 @@ export async function installSwiftlyToolchain(ctx: WorkspaceContext): Promise<vo
50107
return;
51108
}
52109

53-
const uninstalledToolchains = availableToolchains.filter(toolchain => !toolchain.isInstalled);
110+
const uninstalledToolchains = availableToolchains.filter(toolchain => !toolchain.installed);
54111

55112
if (uninstalledToolchains.length === 0) {
56113
void vscode.window.showInformationMessage(
@@ -64,6 +121,9 @@ export async function installSwiftlyToolchain(ctx: WorkspaceContext): Promise<vo
64121
uninstalledToolchains.filter(toolchain => toolchain.version.type === "stable")
65122
);
66123

124+
ctx.logger.debug(
125+
`Available toolchains for installation: ${sortedToolchains.map(t => t.version.name).join(", ")}`
126+
);
67127
const quickPickItems = sortedToolchains.map(toolchain => ({
68128
label: `$(cloud-download) ${toolchain.version.name}`,
69129
toolchain: toolchain,
@@ -79,54 +139,69 @@ export async function installSwiftlyToolchain(ctx: WorkspaceContext): Promise<vo
79139
return;
80140
}
81141

82-
try {
83-
await vscode.window.withProgress(
84-
{
85-
location: vscode.ProgressLocation.Notification,
86-
title: `Installing Swift ${selected.toolchain.version.name}`,
87-
cancellable: false,
88-
},
89-
async progress => {
90-
progress.report({ message: "Starting installation..." });
91-
92-
let lastProgress = 0;
142+
await downloadAndInstallToolchain(selected, ctx);
143+
}
93144

94-
await Swiftly.installToolchain(
95-
selected.toolchain.version.name,
96-
(progressData: SwiftlyProgressData) => {
97-
if (
98-
progressData.step?.percent !== undefined &&
99-
progressData.step.percent > lastProgress
100-
) {
101-
const increment = progressData.step.percent - lastProgress;
102-
progress.report({
103-
increment,
104-
message:
105-
progressData.step.text ||
106-
`${progressData.step.percent}% complete`,
107-
});
108-
lastProgress = progressData.step.percent;
109-
}
110-
},
111-
ctx.logger
112-
);
145+
/**
146+
* Shows a quick pick dialog to install available Swiftly snapshot toolchains
147+
*/
148+
export async function installSwiftlySnapshotToolchain(ctx: WorkspaceContext): Promise<void> {
149+
if (!Swiftly.isSupported()) {
150+
void vscode.window.showErrorMessage(
151+
"Swiftly is not supported on this platform. Only macOS and Linux are supported."
152+
);
153+
return;
154+
}
113155

114-
progress.report({
115-
increment: 100 - lastProgress,
116-
message: "Installation complete",
117-
});
118-
}
156+
if (!(await Swiftly.isInstalled())) {
157+
void vscode.window.showErrorMessage(
158+
"Swiftly is not installed. Please install Swiftly first from https://www.swift.org/install/"
119159
);
160+
return;
161+
}
120162

121-
void showReloadExtensionNotification(
122-
`Swift ${selected.toolchain.version} has been installed and selected as the active toolchain. Visual Studio Code needs to be reloaded.`
163+
const availableToolchains = await Swiftly.listAvailable(ctx.logger);
164+
165+
if (availableToolchains.length === 0) {
166+
void vscode.window.showInformationMessage(
167+
"No toolchains are available for installation via Swiftly."
123168
);
124-
} catch (error) {
125-
ctx.logger?.error(`Failed to install Swift ${selected.toolchain.version.name}: ${error}`);
126-
void vscode.window.showErrorMessage(
127-
`Failed to install Swift ${selected.toolchain.version.name}: ${error}`
169+
return;
170+
}
171+
172+
// Filter for only uninstalled snapshot toolchains
173+
const uninstalledSnapshotToolchains = availableToolchains.filter(
174+
toolchain => !toolchain.installed && toolchain.version.type === "snapshot"
175+
);
176+
177+
if (uninstalledSnapshotToolchains.length === 0) {
178+
void vscode.window.showInformationMessage(
179+
"All available snapshot toolchains are already installed."
128180
);
181+
return;
129182
}
183+
184+
// Sort toolchains with most recent versions first
185+
const sortedToolchains = sortToolchainsByVersion(uninstalledSnapshotToolchains);
186+
187+
const quickPickItems = sortedToolchains.map(toolchain => ({
188+
label: `$(cloud-download) ${toolchain.version.name}`,
189+
description: "snapshot",
190+
detail: `Install snapshot version • Date: ${toolchain.version.type === "snapshot" ? toolchain.version.date || "Unknown" : "Unknown"} • Branch: ${toolchain.version.type === "snapshot" ? toolchain.version.branch || "Unknown" : "Unknown"}`,
191+
toolchain: toolchain,
192+
}));
193+
194+
const selected = await vscode.window.showQuickPick(quickPickItems, {
195+
title: "Install Swift Snapshot Toolchain via Swiftly",
196+
placeHolder: "Pick a Swift snapshot toolchain to install",
197+
canPickMany: false,
198+
});
199+
200+
if (!selected) {
201+
return;
202+
}
203+
204+
await downloadAndInstallToolchain(selected, ctx);
130205
}
131206

132207
/**

0 commit comments

Comments
 (0)