Skip to content

Commit 32c5c86

Browse files
committed
Detect Swiftly Installation
1 parent d4561f3 commit 32c5c86

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

src/toolchain/swiftly.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export class Swiftly {
148148
}
149149
}
150150

151-
private static isSupported() {
151+
public static isSupported() {
152152
return process.platform === "linux" || process.platform === "darwin";
153153
}
154154

@@ -235,4 +235,16 @@ export class Swiftly {
235235
);
236236
return JSON.parse(swiftlyConfigRaw);
237237
}
238+
239+
public static async isInstalled(): Promise<boolean> {
240+
if (!this.isSupported()) {
241+
return false;
242+
}
243+
try {
244+
const { stdout } = await execFile("which", ["swiftly"]);
245+
return stdout.trim().length > 0;
246+
} catch (error) {
247+
return false;
248+
}
249+
}
238250
}

src/ui/ToolchainSelection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ async function getQuickPickItems(
259259
}
260260
// Various actions that the user can perform (e.g. to install new toolchains)
261261
const actionItems: ActionItem[] = [];
262-
if (process.platform === "linux" || process.platform === "darwin") {
262+
if (Swiftly.isSupported() && !(await Swiftly.isInstalled())) {
263263
const platformName = process.platform === "linux" ? "Linux" : "macOS";
264264
actionItems.push({
265265
type: "action",

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

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,48 @@ suite("Swiftly Unit Tests", () => {
102102
expect(mockUtilities.execFile).not.have.been.called;
103103
});
104104
});
105+
106+
suite("isInstalled", () => {
107+
test("should return true when swiftly is found", async () => {
108+
mockUtilities.execFile.withArgs("which", ["swiftly"]).resolves({
109+
stdout: "/usr/local/bin/swiftly\n",
110+
stderr: "",
111+
});
112+
113+
const result = await Swiftly.isInstalled();
114+
115+
expect(result).to.be.true;
116+
expect(mockUtilities.execFile).to.have.been.calledWith("which", ["swiftly"]);
117+
});
118+
119+
test("should return false when swiftly is not found", async () => {
120+
mockUtilities.execFile.withArgs("which", ["swiftly"]).rejects(new Error("not found"));
121+
122+
const result = await Swiftly.isInstalled();
123+
124+
expect(result).to.be.false;
125+
expect(mockUtilities.execFile).to.have.been.calledWith("which", ["swiftly"]);
126+
});
127+
128+
test("should return false when which returns empty output", async () => {
129+
mockUtilities.execFile.withArgs("which", ["swiftly"]).resolves({
130+
stdout: "",
131+
stderr: "",
132+
});
133+
134+
const result = await Swiftly.isInstalled();
135+
136+
expect(result).to.be.false;
137+
expect(mockUtilities.execFile).to.have.been.calledWith("which", ["swiftly"]);
138+
});
139+
140+
test("should return false when platform is not supported", async () => {
141+
mockedPlatform.setValue("win32");
142+
143+
const result = await Swiftly.isInstalled();
144+
145+
expect(result).to.be.false;
146+
expect(mockUtilities.execFile).not.to.have.been.called;
147+
});
148+
});
105149
});

0 commit comments

Comments
 (0)