Skip to content

Commit

Permalink
feat: prompt to install lsp binary manually (#49)
Browse files Browse the repository at this point in the history
Signed-off-by: he1pa <18012015693@163.com>
  • Loading branch information
He1pa committed Jun 11, 2024
1 parent b4a31ad commit 982ad0f
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 15 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
8 changes: 4 additions & 4 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
43 changes: 40 additions & 3 deletions src/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<string | undefined> {
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<string>((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<string | undefined> {
outputChannel.show();
outputChannel.clear();
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/test/suite/index.ts
Original file line number Diff line number Diff line change
@@ -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<void> {
// Create the mocha test
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"compilerOptions": {
"module": "commonjs",
"module": "Node16",
"target": "es2019",
"lib": ["ES2019"],
"outDir": "out",
"sourceMap": true,
"strict": true,
"noImplicitAny": false,
"rootDir": "src"
},
"exclude": [
Expand Down

0 comments on commit 982ad0f

Please sign in to comment.