Skip to content

Commit c5ef849

Browse files
committed
Issue #32: Compute best path for node and VSCode language servers
1 parent 5f826e6 commit c5ef849

File tree

1 file changed

+44
-5
lines changed

1 file changed

+44
-5
lines changed

org.eclipse.languageserver/src/org/eclipse/languageserver/languages/InitializeLaunchConfigurations.java

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,14 @@
1010
*******************************************************************************/
1111
package org.eclipse.languageserver.languages;
1212

13+
import java.io.BufferedReader;
14+
import java.io.File;
15+
import java.io.IOException;
16+
import java.io.InputStreamReader;
1317
import java.util.HashMap;
1418
import java.util.Map;
1519

20+
import org.apache.commons.io.IOUtils;
1621
import org.eclipse.core.externaltools.internal.IExternalToolConstants;
1722
import org.eclipse.core.runtime.CoreException;
1823
import org.eclipse.core.runtime.Platform;
@@ -61,7 +66,7 @@ public void earlyStartup() {
6166
workingCopy.setAttribute(IExternalToolConstants.ATTR_SHOW_CONSOLE, false);
6267
workingCopy.setAttribute(IExternalToolConstants.ATTR_BUILD_SCOPE, "${none}");
6368
workingCopy.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, true);
64-
workingCopy.setAttribute(IExternalToolConstants.ATTR_LOCATION, "/usr/bin/node");
69+
workingCopy.setAttribute(IExternalToolConstants.ATTR_LOCATION, getNodeJsLocation());
6570
workingCopy.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, "/home/mistria/git/omnisharp-node-client/languageserver/server.js");
6671
workingCopy.setAttribute(ILaunchManager.ATTR_APPEND_ENVIRONMENT_VARIABLES, true);
6772
Map<String, String> environment = new HashMap<>(1);
@@ -90,10 +95,10 @@ public void earlyStartup() {
9095
workingCopy.setAttribute(IExternalToolConstants.ATTR_SHOW_CONSOLE, false);
9196
workingCopy.setAttribute(IExternalToolConstants.ATTR_BUILD_SCOPE, "${none}");
9297
workingCopy.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, true);
93-
workingCopy.setAttribute(IExternalToolConstants.ATTR_LOCATION, "/usr/bin/node");
98+
workingCopy.setAttribute(IExternalToolConstants.ATTR_LOCATION, getNodeJsLocation());
9499
// Assume node is already installed on machine and uses it
95100
// TODO: implement smarter and multi-platform discovery
96-
workingCopy.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, "/usr/share/code/resources/app/extensions/css/server/out/cssServerMain.js --stdio");
101+
workingCopy.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, getVSCodeLocation() + "/resources/app/extensions/css/server/out/cssServerMain.js --stdio");
97102
omniSharpLauch = workingCopy.doSave();
98103
registry.registerAssociation(contentTypeManager.getContentType("org.eclipse.wst.css.core.csssource"), LaunchConfigurationStreamProvider.findLaunchConfiguration(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE, InitializeLaunchConfigurations.VSCODE_CSS_NAME));
99104
}
@@ -117,10 +122,10 @@ public void earlyStartup() {
117122
workingCopy.setAttribute(IExternalToolConstants.ATTR_SHOW_CONSOLE, false);
118123
workingCopy.setAttribute(IExternalToolConstants.ATTR_BUILD_SCOPE, "${none}");
119124
workingCopy.setAttribute(DebugPlugin.ATTR_CAPTURE_OUTPUT, true);
120-
workingCopy.setAttribute(IExternalToolConstants.ATTR_LOCATION, "/usr/bin/node");
125+
workingCopy.setAttribute(IExternalToolConstants.ATTR_LOCATION, getNodeJsLocation());
121126
// Assume node is already installed on machine and uses it
122127
// TODO: implement smarter and multi-platform discovery
123-
workingCopy.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, "/usr/share/code/resources/app/extensions/json/server/out/jsonServerMain.js --stdio");
128+
workingCopy.setAttribute(IExternalToolConstants.ATTR_TOOL_ARGUMENTS, getVSCodeLocation() + "/resources/app/extensions/json/server/out/jsonServerMain.js --stdio");
124129
omniSharpLauch = workingCopy.doSave();
125130
registry.registerAssociation(contentTypeManager.getContentType("org.eclipse.wst.jsdt.core.jsonSource"), LaunchConfigurationStreamProvider.findLaunchConfiguration(IExternalToolConstants.ID_PROGRAM_LAUNCH_CONFIGURATION_TYPE, InitializeLaunchConfigurations.VSCODE_JSON_NAME));
126131
}
@@ -129,4 +134,38 @@ public void earlyStartup() {
129134
}
130135
}
131136

137+
private String getVSCodeLocation() {
138+
String res = null;
139+
if (Platform.getOS().equals(Platform.OS_LINUX)) {
140+
res = "/usr/share/code";
141+
} else if (Platform.getOS().equals(Platform.OS_WIN32)) {
142+
res = "C:/Program Files (x86)/Microsoft VS Code";
143+
} else if (Platform.getOS().equals(Platform.OS_MACOSX)) {
144+
res = "/usr/share/code";
145+
}
146+
if (new File(res).isDirectory()) {
147+
return res;
148+
}
149+
return "/unknown/path/to/VSCode";
150+
}
151+
152+
private String getNodeJsLocation() {
153+
String res = "/path/to/node";
154+
String[] command = new String[] {"/bin/bash", "-c", "which node"};
155+
if (Platform.getOS().equals(Platform.OS_WIN32)) {
156+
command = new String[] {"cmd", "/c", "where node"};
157+
}
158+
BufferedReader reader = null;
159+
try {
160+
Process p = Runtime.getRuntime().exec(command);
161+
reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
162+
res = reader.readLine();
163+
} catch (IOException e) {
164+
e.printStackTrace();
165+
} finally {
166+
IOUtils.closeQuietly(reader);
167+
}
168+
return res;
169+
}
170+
132171
}

0 commit comments

Comments
 (0)