Browse Source

Show error message if vhdl-tool executable cannot be found

VHDL Tool 1 month ago
parent
commit
dd4e452f6b
1 changed files with 38 additions and 19 deletions
  1. 38 19
      src/extension.ts

+ 38 - 19
src/extension.ts

@@ -1,29 +1,48 @@
1 1
 'use strict';
2 2
 
3 3
 import * as path from 'path';
4
-import { workspace, Disposable, ExtensionContext } from 'vscode';
4
+import { workspace, Disposable, ExtensionContext, window } from 'vscode';
5 5
 import { LanguageClient, LanguageClientOptions, SettingMonitor, ServerOptions, TransportKind } from 'vscode-languageclient';
6
+import * as child_process from 'child_process';
7
+import * as fs from 'fs';
8
+
9
+/*
10
+ * Checks if the executable is on the PATH
11
+ */
12
+export function executableExists(exe: string): boolean {
13
+  const isWindows = process.platform === 'win32';
14
+  const cmd: string = isWindows ? 'where' : 'which';
15
+  const out = child_process.spawnSync(cmd, [exe]);
16
+  return out.status === 0 || (isWindows && fs.existsSync(exe));
17
+}
6 18
 
7 19
 export function activate(context: ExtensionContext) {
8 20
 
9
-	let args: string[] = ['lsp'];
21
+	if (executableExists('vhdl-tool')) {
22
+
23
+		let args: string[] = ['lsp'];
10 24
 
11
-	// If the extension is launched in debug mode then the debug server options are used
12
-	// Otherwise the run options are used
13
-	let serverOptions: ServerOptions = {
14
-		run:   { command: 'vhdl-tool', args},
15
-		debug: { command: 'vhdl-tool', args}
16
-	} 
17
-	
18
-	// Options to control the language client
19
-	let clientOptions: LanguageClientOptions = {
20
-		// Register the server for VHDL files
21
-		documentSelector: ['vhdl'],
25
+		// If the extension is launched in debug mode then the debug server options are used
26
+		// Otherwise the run options are used
27
+		let serverOptions: ServerOptions = {
28
+			run:   { command: 'vhdl-tool', args},
29
+			debug: { command: 'vhdl-tool', args}
30
+		} 
31
+		
32
+		// Options to control the language client
33
+		let clientOptions: LanguageClientOptions = {
34
+			// Register the server for VHDL files
35
+			documentSelector: ['vhdl'],
36
+		}
37
+		
38
+		// Create the language client and start the client.
39
+		let disposable = new LanguageClient('vhdltool', 'Language Server VHDL', serverOptions, clientOptions).start();
40
+		
41
+		// Push the disposable to the context's subscriptions so that the client can be deactivated on extension deactivation
42
+		context.subscriptions.push(disposable);
43
+	} else {
44
+		const notInstalledMsg: string =
45
+			'Please download VHDL-Tool from https://www.vhdltool.com/download and place it somewhere in your $PATH';
46
+		window.showErrorMessage(notInstalledMsg);
22 47
 	}
23
-	
24
-	// Create the language client and start the client.
25
-	let disposable = new LanguageClient('vhdltool', 'Language Server VHDL', serverOptions, clientOptions).start();
26
-	
27
-	// Push the disposable to the context's subscriptions so that the client can be deactivated on extension deactivation
28
-	context.subscriptions.push(disposable);
29 48
 }