Skip to content

Commit f09fe75

Browse files
committed
VSCode extension now sets executable permissions for the language server prior to launch
1 parent fbe96e5 commit f09fe75

File tree

4 files changed

+89
-12
lines changed

4 files changed

+89
-12
lines changed

BUILDING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Contains the commands require to build this project. Note that you might need to
2424

2525
## Running/Debugging the VSCode extension
2626
* Open the debug tab in VSCode
27-
* Run the `Extension (kotlin-language-server)` launch configuration
27+
* Run the `Extension` launch configuration
2828

2929
## Packaging the VSCode extension
3030
* `vsce package -o build.vsix`

vscode-extension-src/extension.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
import * as vscode from 'vscode';
55
import * as path from "path";
66
import * as fs from "fs";
7-
import {LanguageClient, LanguageClientOptions, ServerOptions} from "vscode-languageclient";
7+
import * as child_process from "child_process";
8+
import { LanguageClient, LanguageClientOptions, ServerOptions } from "vscode-languageclient";
9+
import { isOSUnixoid } from './osUtils';
10+
import { LOG } from './logger';
811

912
// this method is called when your extension is activated
1013
// your extension is activated the very first time the command is executed
1114
export function activate(context: vscode.ExtensionContext) {
12-
console.log('Activating Kotlin language server...');
15+
LOG.info('Activating Kotlin language server...');
1316

1417
let javaExecutablePath = findJavaExecutable('java');
1518

@@ -42,14 +45,20 @@ export function activate(context: vscode.ExtensionContext) {
4245
}
4346
let startScriptPath = path.resolve(context.extensionPath, "build", "install", "kotlin-language-server", "bin", correctScriptName("kotlin-language-server"))
4447
let args = [];
48+
49+
// Ensure that start script can be executed
50+
if (isOSUnixoid()) {
51+
child_process.exec("chmod +x " + startScriptPath)
52+
}
53+
4554
// Start the child java process
4655
let serverOptions: ServerOptions = {
4756
command: startScriptPath,
4857
args: args,
4958
options: { cwd: vscode.workspace.rootPath }
5059
}
5160

52-
console.log(startScriptPath + ' ' + args.join(' '));
61+
LOG.info("Launching {} with args {}", startScriptPath, args.join(' '));
5362

5463
// Create the language client and start the client.
5564
let languageClient = new LanguageClient('kotlin', 'Kotlin Language Server', serverOptions, clientOptions);
@@ -64,14 +73,14 @@ export function activate(context: vscode.ExtensionContext) {
6473
export function deactivate() {
6574
}
6675

67-
function findJavaExecutable(binname: string) {
68-
binname = correctBinname(binname);
76+
function findJavaExecutable(rawBinname: string) {
77+
let binname = correctBinname(rawBinname);
6978

7079
// First search java.home setting
7180
let userJavaHome = vscode.workspace.getConfiguration('java').get('home') as string;
7281

7382
if (userJavaHome != null) {
74-
console.log('Looking for java in settings java.home ' + userJavaHome + '...');
83+
LOG.debug("Looking for Java in java.home (settings): {}", userJavaHome);
7584

7685
let candidate = findJavaExecutableInJavaHome(userJavaHome, binname);
7786

@@ -83,7 +92,7 @@ function findJavaExecutable(binname: string) {
8392
let envJavaHome = process.env['JAVA_HOME'];
8493

8594
if (envJavaHome) {
86-
console.log('Looking for java in environment variable JAVA_HOME ' + envJavaHome + '...');
95+
LOG.debug("Looking for Java in JAVA_HOME (environment variable): {}", envJavaHome);
8796

8897
let candidate = findJavaExecutableInJavaHome(envJavaHome, binname);
8998

@@ -93,7 +102,7 @@ function findJavaExecutable(binname: string) {
93102

94103
// Then search PATH parts
95104
if (process.env['PATH']) {
96-
console.log('Looking for java in PATH');
105+
LOG.debug("Looking for Java in PATH");
97106

98107
let pathparts = process.env['PATH'].split(path.delimiter);
99108
for (let i = 0; i < pathparts.length; i++) {
@@ -104,8 +113,9 @@ function findJavaExecutable(binname: string) {
104113
}
105114
}
106115

107-
// Else return the binary name directly (this will likely always fail downstream)
108-
return null;
116+
// Else return the binary name directly (this will likely always fail downstream)
117+
LOG.debug("Could not find Java, will try using binary name directly");
118+
return binname;
109119
}
110120

111121
function correctBinname(binname: string) {
@@ -131,4 +141,4 @@ function findJavaExecutableInJavaHome(javaHome: string, binname: string) {
131141
if (fs.existsSync(binpath))
132142
return binpath;
133143
}
134-
}
144+
}

vscode-extension-src/logger.ts

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
export enum LogLevel {
2+
NONE = 100,
3+
ERROR = 2,
4+
WARN = 1,
5+
INFO = 0,
6+
DEBUG = -1,
7+
TRACE = -2,
8+
DEEP_TRACE = -3
9+
}
10+
11+
export class Logger {
12+
level: LogLevel;
13+
14+
public constructor(level: LogLevel) {
15+
this.level = level;
16+
}
17+
18+
private format(msg: String, placeholders: any[]): string {
19+
let result = "";
20+
let i = 0;
21+
let placeholderIndex = 0;
22+
while (i < msg.length) {
23+
let c = msg.charAt(i);
24+
let next = msg.charAt(i + 1);
25+
if (c === '{' && next === '}') {
26+
result += placeholders[placeholderIndex];
27+
placeholderIndex++;
28+
i += 2;
29+
} else {
30+
result += c;
31+
i++;
32+
}
33+
}
34+
return result;
35+
}
36+
37+
private log(prefix: String, level: LogLevel, msg: String, placeholders: any[]): void {
38+
if (level >= this.level) {
39+
console.log(prefix + this.format(msg, placeholders));
40+
}
41+
}
42+
43+
public error(msg: String, ...placeholders: any[]): void { this.log("Extension: [ERROR] ", LogLevel.ERROR, msg, placeholders); }
44+
45+
public warn(msg: String, ...placeholders: any[]): void { this.log("Extension: [WARN] ", LogLevel.WARN, msg, placeholders); }
46+
47+
public info(msg: String, ...placeholders: any[]): void { this.log("Extension: [INFO] ", LogLevel.INFO, msg, placeholders); }
48+
49+
public debug(msg: String, ...placeholders: any[]): void { this.log("Extension: [DEBUG] ", LogLevel.DEBUG, msg, placeholders); }
50+
51+
public trace(msg: String, ...placeholders: any[]): void { this.log("Extension: [TRACE] ", LogLevel.TRACE, msg, placeholders); }
52+
53+
public deepTrace(msg: String, ...placeholders: any[]): void { this.log("Extension: [D_TRACE]", LogLevel.DEEP_TRACE, msg, placeholders); }
54+
}
55+
56+
export const LOG = new Logger(LogLevel.INFO);

vscode-extension-src/osUtils.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export function isOSWindows(): boolean {
2+
return process.platform === "win32";
3+
}
4+
5+
export function isOSUnixoid(): boolean {
6+
let platform = process.platform;
7+
return platform === "linux"
8+
|| platform === "darwin"
9+
|| platform === "freebsd"
10+
|| platform === "openbsd";
11+
}

0 commit comments

Comments
 (0)