From 4e48a73f9c342544e4eabd1c1cd31cdfb6a6e5e3 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 21 Feb 2020 10:04:03 +0800 Subject: Improve server version info --- crates/rust-analyzer/build.rs | 30 ++++++++++++++++++++++++++++- editors/code/package.json | 5 +++++ editors/code/src/commands/index.ts | 1 + editors/code/src/commands/server_version.ts | 9 +++++++++ editors/code/src/main.ts | 1 + 5 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 editors/code/src/commands/server_version.ts diff --git a/crates/rust-analyzer/build.rs b/crates/rust-analyzer/build.rs index 05f9772c0..cda8833d1 100644 --- a/crates/rust-analyzer/build.rs +++ b/crates/rust-analyzer/build.rs @@ -1,12 +1,40 @@ //! Just embed git-hash to `--version` -use std::process::Command; +use std::{env, path::PathBuf, process::Command}; fn main() { + let _ = set_rerun(); + let rev = rev().unwrap_or_else(|| "???????".to_string()); println!("cargo:rustc-env=REV={}", rev) } +fn set_rerun() { + let mut manifest_dir = PathBuf::from( + env::var("CARGO_MANIFEST_DIR").expect("`CARGO_MANIFEST_DIR` is always set by cargo."), + ); + + while manifest_dir.parent().is_some() { + if manifest_dir.join(".git/HEAD").exists() { + let git_dir = manifest_dir.join(".git"); + + println!("cargo:rerun-if-changed={}", git_dir.join("HEAD").display()); + // current branch ref + if let Ok(output) = + Command::new("git").args(&["rev-parse", "--symbolic-full-name", "HEAD"]).output() + { + if let Ok(ref_link) = String::from_utf8(output.stdout) { + println!("cargo:rerun-if-changed={}", git_dir.join(ref_link).display()); + } + } + return; + } + + manifest_dir.pop(); + } + println!("cargo:warning=Could not find `.git/HEAD` from manifest dir!"); +} + fn rev() -> Option { let output = Command::new("git").args(&["rev-parse", "HEAD"]).output().ok()?; let stdout = String::from_utf8(output.stdout).ok()?; diff --git a/editors/code/package.json b/editors/code/package.json index c498c14b4..72befe2b6 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -131,6 +131,11 @@ "command": "rust-analyzer.ssr", "title": "Structural Search Replace", "category": "Rust Analyzer" + }, + { + "command": "rust-analyzer.serverVersion", + "title": "Show RA Version", + "category": "Rust Analyzer" } ], "keybindings": [ diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index bebd99ca9..839245f48 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -13,6 +13,7 @@ export * from './syntax_tree'; export * from './expand_macro'; export * from './runnables'; export * from './ssr'; +export * from './server_version'; export function collectGarbage(ctx: Ctx): Cmd { return async () => { diff --git a/editors/code/src/commands/server_version.ts b/editors/code/src/commands/server_version.ts new file mode 100644 index 000000000..3a982a418 --- /dev/null +++ b/editors/code/src/commands/server_version.ts @@ -0,0 +1,9 @@ +import * as vscode from 'vscode'; +import { ServerVersion } from '../installation/server'; +import { Cmd } from '../ctx'; + +export function serverVersion(): Cmd { + return () => { + vscode.window.showInformationMessage('rust-analyzer version : ' + ServerVersion); + }; +} \ No newline at end of file diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index a22e0bc66..de19a44e5 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -55,6 +55,7 @@ export async function activate(context: vscode.ExtensionContext) { ctx.registerCommand('run', commands.run); ctx.registerCommand('onEnter', commands.onEnter); ctx.registerCommand('ssr', commands.ssr); + ctx.registerCommand('serverVersion', commands.serverVersion); // Internal commands which are invoked by the server. ctx.registerCommand('runSingle', commands.runSingle); -- cgit v1.2.3 From b21ead6edef8ecf67749150df2f79ab2263465c5 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Thu, 20 Feb 2020 15:25:59 +0800 Subject: Add document for `Show RA Version` --- docs/user/features.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/user/features.md b/docs/user/features.md index 309d2775d..a00fa35da 100644 --- a/docs/user/features.md +++ b/docs/user/features.md @@ -89,6 +89,10 @@ Shows the full macro expansion of the macro at current cursor. Shows internal statistic about memory usage of rust-analyzer +#### Show RA Version + +Show current rust-analyzer version + #### Run garbage collection Manually triggers GC -- cgit v1.2.3 From d70ddec74693e1269997e8feac5a209a43b7e897 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Thu, 20 Feb 2020 16:47:12 +0800 Subject: Remove unused placeholder --- crates/rust-analyzer/build.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/rust-analyzer/build.rs b/crates/rust-analyzer/build.rs index cda8833d1..d4b010c04 100644 --- a/crates/rust-analyzer/build.rs +++ b/crates/rust-analyzer/build.rs @@ -3,7 +3,7 @@ use std::{env, path::PathBuf, process::Command}; fn main() { - let _ = set_rerun(); + set_rerun(); let rev = rev().unwrap_or_else(|| "???????".to_string()); println!("cargo:rustc-env=REV={}", rev) -- cgit v1.2.3 From e7a0d8f8d0ab4b03e1d85fce2973bdc50d0391c0 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Thu, 20 Feb 2020 16:49:31 +0800 Subject: Add trailing newline --- editors/code/src/commands/server_version.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/editors/code/src/commands/server_version.ts b/editors/code/src/commands/server_version.ts index 3a982a418..307408a37 100644 --- a/editors/code/src/commands/server_version.ts +++ b/editors/code/src/commands/server_version.ts @@ -6,4 +6,5 @@ export function serverVersion(): Cmd { return () => { vscode.window.showInformationMessage('rust-analyzer version : ' + ServerVersion); }; -} \ No newline at end of file +} + -- cgit v1.2.3 From 489be40d3a6181b0eb0ee71e8b399692b639baf4 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 21 Feb 2020 09:57:24 +0800 Subject: Use ensureServerBinary instead --- editors/code/src/commands/server_version.ts | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/editors/code/src/commands/server_version.ts b/editors/code/src/commands/server_version.ts index 307408a37..34c18bf3b 100644 --- a/editors/code/src/commands/server_version.ts +++ b/editors/code/src/commands/server_version.ts @@ -1,10 +1,22 @@ import * as vscode from 'vscode'; -import { ServerVersion } from '../installation/server'; -import { Cmd } from '../ctx'; +import { ensureServerBinary } from '../installation/server'; +import { Ctx, Cmd } from '../ctx'; +import { spawnSync } from 'child_process'; -export function serverVersion(): Cmd { - return () => { - vscode.window.showInformationMessage('rust-analyzer version : ' + ServerVersion); +export function serverVersion(ctx: Ctx): Cmd { + return async () => { + const binaryPath = await ensureServerBinary(ctx.config.serverSource); + + if (binaryPath == null) { + throw new Error( + "Rust Analyzer Language Server is not available. " + + "Please, ensure its [proper installation](https://rust-analyzer.github.io/manual.html#installation)." + ); + } + + const res = spawnSync(binaryPath, ["--version"]); + const version = res.output?.filter(x => x !== null).map(String).join(" "); + vscode.window.showInformationMessage('rust-analyzer version : ' + version); }; } -- cgit v1.2.3 From 319a09847b47086b73ea7184ee39b3be0b924c60 Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 21 Feb 2020 15:51:55 +0800 Subject: Use stdout directly --- editors/code/src/commands/server_version.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/editors/code/src/commands/server_version.ts b/editors/code/src/commands/server_version.ts index 34c18bf3b..421301b42 100644 --- a/editors/code/src/commands/server_version.ts +++ b/editors/code/src/commands/server_version.ts @@ -14,8 +14,7 @@ export function serverVersion(ctx: Ctx): Cmd { ); } - const res = spawnSync(binaryPath, ["--version"]); - const version = res.output?.filter(x => x !== null).map(String).join(" "); + const version = spawnSync(binaryPath, ["--version"], { encoding: "utf8" }).stdout; vscode.window.showInformationMessage('rust-analyzer version : ' + version); }; } -- cgit v1.2.3