Skip to content

Commit

Permalink
Merge pull request #2155 from sconwayaus/vscode_is_binary_found
Browse files Browse the repository at this point in the history
Vscode is binary found
  • Loading branch information
hzeller authored Apr 16, 2024
2 parents 9469b2e + 49e7ce6 commit e3ef2a3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
35 changes: 26 additions & 9 deletions verilog/tools/ls/vscode/src/download-ls.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,30 @@
import * as vscode from "vscode";
import { platform, arch } from "os";
import { homedir, platform, arch } from "os";
import * as fs from "fs";
import * as path from "path";
import { IncomingMessage } from "http";
import { https as httpsFr } from "follow-redirects";
import { execSync } from "child_process";
import * as decompress from "decompress";
const decompressTargz = require("decompress-targz");
const decompressUnzip = require("decompress-unzip");
const TAG = require("../package.json").repository.tag;


function checkIfBinaryExists(binaryPath: string) {
let whichCommand: string, binaryExists: boolean;
if (platform() == "win32") whichCommand = "where";
else whichCommand = "command -v";
let whichCommand: string;
let binaryExists: boolean;

if (platform() == "win32") {
let parsedBinPath = path.parse(binaryPath);
whichCommand = `where "${parsedBinPath.dir}:${parsedBinPath.base}"`;
} else {
whichCommand = `command -v ${binaryPath}`;
}

binaryExists = true;
try {
execSync(`${whichCommand} ${binaryPath}`, { windowsHide: true });
execSync(whichCommand, { windowsHide: true });
} catch {
binaryExists = false;
}
Expand All @@ -29,6 +36,16 @@ export async function checkAndDownloadBinaries(
binaryPath: string,
output: vscode.OutputChannel
): Promise<string> {

output.appendLine("Platform: '" + platform() + "'");

// Update home paths to an absolute path
if (platform() != "win32" && binaryPath.startsWith("~/")) {
binaryPath = binaryPath.replace("~", "");
binaryPath = path.join(homedir(), binaryPath);
output.appendLine(`Adjusted server path: ${binaryPath}`);
}

if (checkIfBinaryExists(binaryPath)) {
// Language server binary exists -- nothing to do
return binaryPath;
Expand All @@ -49,8 +66,7 @@ export async function checkAndDownloadBinaries(
"verible-verilog-ls" + (platform() === "win32" ? ".exe" : "")
);
if (checkIfBinaryExists(pluginBinaryPath)) {
// Language server binary already downloaded
output.appendLine(`Using executable from path: ${pluginBinaryPath}`);
output.appendLine("Language server binary already downloaded");
return pluginBinaryPath;
}

Expand Down Expand Up @@ -85,6 +101,7 @@ export async function checkAndDownloadBinaries(
await new Promise<void>((resolve, reject) =>
httpsFr.get(releaseUrl, (response: IncomingMessage) => {
if (response.statusCode !== 200){
output.appendLine("Download failed with status code " + response.statusCode);
reject("Status code " + response.statusCode);
}
response.pipe(archive);
Expand All @@ -94,6 +111,7 @@ export async function checkAndDownloadBinaries(
resolve();
});
}).on("error", (_err) =>{
output.appendLine("Failed to start download");
return binaryPath;
})
);
Expand All @@ -105,14 +123,13 @@ export async function checkAndDownloadBinaries(
file.path = path.basename(file.path);
return file;
},
plugins: platform() === "win32" ? [] : [decompressTargz()],
plugins: platform() === "win32" ? [decompressUnzip()] : [decompressTargz()],
}).catch((_err) => {
return binaryPath;
})
.finally(() => {
fs.rm(archivePath, () => null);
});

output.appendLine(`Using executable from path: ${pluginBinaryPath}`);
return pluginBinaryPath;
}
3 changes: 3 additions & 0 deletions verilog/tools/ls/vscode/src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ async function initLanguageClient() {
const config = vscode.workspace.getConfiguration('verible');
const binary_path: string = await checkAndDownloadBinaries(config.get('path') as string, output);

output.appendLine(`Using executable from path: ${binary_path}`);

const verible_ls: vscodelc.Executable = {
command: binary_path,
args: await config.get<string[]>('arguments')
Expand All @@ -26,6 +28,7 @@ async function initLanguageClient() {
};

// Create the language client and start the client.
output.appendLine("Starting Language Server");
client = new vscodelc.LanguageClient(
'verible',
'Verible Language Server',
Expand Down

0 comments on commit e3ef2a3

Please sign in to comment.