Skip to content

Commit

Permalink
try use Promise<ChildPRomise>
Browse files Browse the repository at this point in the history
  • Loading branch information
tgodzik committed Apr 12, 2023
1 parent 022a78c commit 906323d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ describe("fetchMetals", () => {
});
});

it("should find coursier in PATH", () => {
it("should find coursier in PATH", async () => {
const pathEnv = process.env["PATH"];
if (pathEnv) {
expect(validateCoursier(pathEnv)).toBeDefined();
expect(await validateCoursier(pathEnv)).toBeDefined();
} else {
fail("PATH environment variable is not defined");
}
});

it("should not find coursier if not present in PATH", () => {
expect(validateCoursier("path/fake")).toBeUndefined();
it("should not find coursier if not present in PATH", async () => {
expect(await validateCoursier("path/fake")).toBeUndefined();
});
});
15 changes: 8 additions & 7 deletions packages/metals-languageclient/src/fetchMetals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,21 @@ import fs from "fs";
import { ChildProcessPromise, spawn } from "promisify-child-process";
import { JavaConfig } from "./getJavaConfig";
import { OutputChannel } from "./interfaces/OutputChannel";
import { spawnSync } from "child_process";

interface FetchMetalsOptions {
serverVersion: string;
serverProperties: string[];
javaConfig: JavaConfig;
}

export function fetchMetals(
export async function fetchMetals(
{
serverVersion,
serverProperties,
javaConfig: { javaPath, javaOptions, extraEnv, coursierPath },
}: FetchMetalsOptions,
output: OutputChannel
): ChildProcessPromise {
): Promise<ChildProcessPromise> {
const fetchProperties = serverProperties.filter(
(p) => !p.startsWith("-agentlib")
);
Expand All @@ -46,7 +45,7 @@ export function fetchMetals(
const path = process.env["PATH"];
let possibleCoursier: string | undefined;
if (path) {
possibleCoursier = validateCoursier(path);
possibleCoursier = await validateCoursier(path);
}

function spawnDefault(): ChildProcessPromise {
Expand Down Expand Up @@ -78,7 +77,9 @@ export function fetchMetals(
}
}

export function validateCoursier(pathEnv: string): string | undefined {
export async function validateCoursier(
pathEnv: string
): Promise<string | undefined> {
const isWindows = process.platform === "win32";
const possibleCoursier = pathEnv
.split(path.delimiter)
Expand All @@ -99,8 +100,8 @@ export function validateCoursier(pathEnv: string): string | undefined {
(isWindows && p.endsWith(path.sep + "cs.exe"))
);
if (possibleCoursier) {
const coursierVersion = spawnSync(possibleCoursier, ["version"]);
if (coursierVersion.status !== 0) {
const coursierVersion = await spawn(possibleCoursier, ["version"]);
if (coursierVersion.code !== 0) {
return undefined;
} else {
return possibleCoursier;
Expand Down
94 changes: 49 additions & 45 deletions packages/metals-vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,55 +193,59 @@ function fetchAndLaunchMetals(
);

const title = `Downloading Metals v${serverVersion}`;
return trackDownloadProgress(title, outputChannel, fetchProcess).then(
(classpath) => {
return launchMetals(
outputChannel,
context,
classpath,
serverProperties,
javaConfig,
serverVersion
);
},
(reason) => {
if (reason instanceof Error) {
outputChannel.appendLine(
"Downloading Metals failed with the following:"
return fetchProcess
.then((childProcess: ChildProcessPromise) => {
return trackDownloadProgress(title, outputChannel, childProcess);
})
.then(
(classpath) => {
return launchMetals(
outputChannel,
context,
classpath,
serverProperties,
javaConfig,
serverVersion
);
outputChannel.appendLine(reason.message);
}
const msg = (() => {
const proxy =
`See https://scalameta.org/metals/docs/editors/vscode/#http-proxy for instructions ` +
`if you are using an HTTP proxy.`;
if (process.env.FLATPAK_SANDBOX_DIR) {
return (
`Failed to download Metals. It seems you are running Visual Studio Code inside the ` +
`Flatpak sandbox, which is known to interfere with the download of Metals. ` +
`Please, try running Visual Studio Code without Flatpak.`
);
} else {
return (
`Failed to download Metals, make sure you have an internet connection, ` +
`the Metals version '${serverVersion}' is correct and the Java Home '${javaHome}' is valid. ` +
`You can configure the Metals version and Java Home in the settings.` +
proxy
},
(reason) => {
if (reason instanceof Error) {
outputChannel.appendLine(
"Downloading Metals failed with the following:"
);
outputChannel.appendLine(reason.message);
}
})();
outputChannel.show();
window
.showErrorMessage(msg, openSettingsAction, downloadJava)
.then((choice) => {
if (choice === openSettingsAction) {
commands.executeCommand(workbenchCommands.openSettings);
} else if (choice === downloadJava) {
showInstallJavaAction(outputChannel);
const msg = (() => {
const proxy =
`See https://scalameta.org/metals/docs/editors/vscode/#http-proxy for instructions ` +
`if you are using an HTTP proxy.`;
if (process.env.FLATPAK_SANDBOX_DIR) {
return (
`Failed to download Metals. It seems you are running Visual Studio Code inside the ` +
`Flatpak sandbox, which is known to interfere with the download of Metals. ` +
`Please, try running Visual Studio Code without Flatpak.`
);
} else {
return (
`Failed to download Metals, make sure you have an internet connection, ` +
`the Metals version '${serverVersion}' is correct and the Java Home '${javaHome}' is valid. ` +
`You can configure the Metals version and Java Home in the settings.` +
proxy
);
}
});
}
);
})();
outputChannel.show();
window
.showErrorMessage(msg, openSettingsAction, downloadJava)
.then((choice) => {
if (choice === openSettingsAction) {
commands.executeCommand(workbenchCommands.openSettings);
} else if (choice === downloadJava) {
showInstallJavaAction(outputChannel);
}
});
}
);
}

function launchMetals(
Expand Down

0 comments on commit 906323d

Please sign in to comment.