From 982ad0f4d134550d50043b546cfc7b0c31a92fb1 Mon Sep 17 00:00:00 2001 From: He1pa <56333845+He1pa@users.noreply.github.com> Date: Tue, 11 Jun 2024 19:41:06 +0800 Subject: [PATCH] feat: prompt to install lsp binary manually (#49) Signed-off-by: he1pa <18012015693@163.com> --- README.md | 4 ---- package.json | 2 +- src/extension.ts | 8 ++++---- src/install.ts | 43 ++++++++++++++++++++++++++++++++++++++--- src/test/suite/index.ts | 4 ++-- tsconfig.json | 3 ++- 6 files changed, 49 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 001bbaa..dbd17bd 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,6 @@ This extension provides some coding assistance, including the following features - **Syntax Highlight:** ![Highlight](https://kcl-lang.io/assets/images/Highlight-eb0516cd26555785169222292bede364.png) -[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fkcl-lang%2Fvscode-kcl.svg?type=shield)](https://app.fossa.com/projects/git%2Bgithub.com%2Fkcl-lang%2Fvscode-kcl?ref=badge_shield) - **Goto Definition:** Goto definition of schema, variable, schema attribute, and import pkg. ![Goto Definition](https://kcl-lang.io/assets/images/GotoDef-0875243eacd6b76e49b7a5b95cb9ce55.gif) - **Completion:** Keywords completions and dot(`.`) completion. @@ -51,6 +50,3 @@ We are working actively on improving the KCL development on VS Code. All kinds o ## License Apache License Version 2.0 - - -[![FOSSA Status](https://app.fossa.com/api/projects/git%2Bgithub.com%2Fkcl-lang%2Fvscode-kcl.svg?type=large)](https://app.fossa.com/projects/git%2Bgithub.com%2Fkcl-lang%2Fvscode-kcl?ref=badge_large) \ No newline at end of file diff --git a/package.json b/package.json index 36f2135..1aa22ca 100644 --- a/package.json +++ b/package.json @@ -3,7 +3,7 @@ "publisher": "kcl", "displayName": "KCL", "description": "Language extension for KCL", - "version": "0.1.6", + "version": "0.1.7", "engines": { "vscode": "^1.63.0" }, diff --git a/src/extension.ts b/src/extension.ts index 73c3c19..1a34386 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -35,10 +35,10 @@ export async function activate(context: vscode.ExtensionContext) { context.subscriptions.push(autoCompletionProvider); context.subscriptions.push(restartLanguageServerCommand); - const language_server_path: string | undefined = install.kcl_rust_lsp_location(); - // if (!language_server_path) { - // language_server_path = await install.promptInstallLanguageServer(client); - // } + let language_server_path: string | undefined = install.kcl_lsp_location(); + if (!language_server_path) { + language_server_path = await install.promptInstallLanguageServer(client); + } if (language_server_path) { startLanguageServerWith(language_server_path); diff --git a/src/install.ts b/src/install.ts index 339a7cb..108b120 100644 --- a/src/install.ts +++ b/src/install.ts @@ -6,12 +6,14 @@ import axios, { AxiosResponse } from 'axios'; import * as fs from 'fs'; import * as shelljs from 'shelljs'; import { LanguageClient } from 'vscode-languageclient/node'; +import { exec } from 'child_process'; const KCL_PATH = path.join(os.homedir(), '.kcl'); const KPM_PATH = path.join(KCL_PATH, 'kpm'); const KPM_BIN_PATH = path.join(KPM_PATH, 'bin'); const GIT_ORG = 'kcl-lang'; const KCL_REPO = 'kcl'; +const LSP_INSTALL_URL = 'https://www.kcl-lang.io/docs/user_docs/getting-started/install#install-language-server'; export const RELEASE_BASE_URL = `https://github.com/${GIT_ORG}/${KCL_REPO}/releases/download`; const RELEASE_API = `https://api.github.com/repos/${GIT_ORG}/${KCL_REPO}/releases/latest`; export const KCL_LANGUAGE_SERVER = 'kcl-language-server'; @@ -34,22 +36,55 @@ export function kcl_rust_lsp_location(): string | undefined { return fs.existsSync(getInstallPath(KCL_LANGUAGE_SERVER)) ? getInstallPath(KCL_LANGUAGE_SERVER) : shelljs.which("kcl-language-server")?.toString(); } +export function kcl_lsp_location(): string | undefined { + return shelljs.which("kcl-language-server")?.toString(); +} + export async function promptInstallLanguageServer(client: LanguageClient | undefined): Promise { const installOptions = ['Install', 'Cancel']; const selected = await vscode.window.showErrorMessage( - `The kcl-language-server is required for KCL code intelliSense. Install now?`, + `The kcl-language-server is required for KCL extension. Install manually from: ${LSP_INSTALL_URL}?`, ...installOptions ); switch (selected) { case 'Install': - return installLanguageServer(client); + vscode.env.openExternal(vscode.Uri.parse(LSP_INSTALL_URL)); + return; + default: + return; + } +} + +// todo: Fix the issue where sudo permissions require a password +export async function installLanguageServerWithScript(client: LanguageClient | undefined) { + outputChannel.show(); + outputChannel.clear(); + const platform = os.type() === 'Windows_NT' ? 'windows' : os.type().toLowerCase(); + switch (platform) { + case 'darwin': + return downloadAndRunScript(`curl -fsSL https://kcl-lang.io/script/install-kcl-lsp.sh | /bin/bash`); + case 'linux': + return downloadAndRunScript(`wget -q https://kcl-lang.io/script/install-kcl-lsp.sh -O - | /bin/bash`); + case 'windows': + return downloadAndRunScript(`powershell -Command "iwr -useb https://kcl-lang.io/script/install-kcl-lsp.ps1 | iex"`); default: return; } } +function downloadAndRunScript(scriptCommand: string) { + return new Promise((resolve, reject) => { + exec(scriptCommand, (error, stdout, stderr) => { + if (error) { + outputMsg(`Failed to install language server: ${error.message}`); + } else { + outputMsg(`Language server installation completed successfully`); + } + }); + }); +} -// todo: refactor with srcipt 'wget -q https://kcl-lang.io/script/install-kcl-lsp.sh -O - | /bin/bash' +// todo: deprecated and replaced by installLanguageServerWithScript export async function installLanguageServer(client: LanguageClient | undefined): Promise { outputChannel.show(); outputChannel.clear(); @@ -136,6 +171,8 @@ export async function downloadToLocal(releaseURL: string, installPath: string): fs.unlinkSync(installPath); resolve(false); }); + }).catch ((error) => { + outputMsg(`Failed to download binary: ${error.message}`); }); } catch (error) { downloadEnd = true; diff --git a/src/test/suite/index.ts b/src/test/suite/index.ts index 7029e38..136f3ac 100644 --- a/src/test/suite/index.ts +++ b/src/test/suite/index.ts @@ -1,6 +1,6 @@ import * as path from 'path'; -import * as Mocha from 'mocha'; -import * as glob from 'glob'; +import Mocha from 'mocha'; +import glob from 'glob'; export function run(): Promise { // Create the mocha test diff --git a/tsconfig.json b/tsconfig.json index 8e55fda..f6cdf09 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,12 @@ { "compilerOptions": { - "module": "commonjs", + "module": "Node16", "target": "es2019", "lib": ["ES2019"], "outDir": "out", "sourceMap": true, "strict": true, + "noImplicitAny": false, "rootDir": "src" }, "exclude": [