Skip to content

Commit fa49caa

Browse files
authored
Remove hard dependency of redhat.java (#617)
* Remove hard dependency of redhat.java Signed-off-by: Yan Zhang <yanzh@microsoft.com> * move helper method to utility Signed-off-by: Yan Zhang <yanzh@microsoft.com> * guide to install redhat.java directly Signed-off-by: Yan Zhang <yanzh@microsoft.com> * Prompt to reload window after installing redhat.java Signed-off-by: Yan Zhang <yanzh@microsoft.com> * address comments: check redhat.java enabled status instead of activated Signed-off-by: Yan Zhang <yanzh@microsoft.com> * Fix potential NPE. Signed-off-by: Yan Zhang <yanzh@microsoft.com> * fix travis linux build
1 parent 50c9811 commit fa49caa

File tree

5 files changed

+108
-43
lines changed

5 files changed

+108
-43
lines changed

.travis.yml

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,28 @@
1-
language: node_js
2-
3-
node_js:
4-
- '--lts'
5-
6-
os:
7-
- linux
8-
- osx
9-
10-
before_install:
11-
- |
12-
if [ $TRAVIS_OS_NAME == "linux" ]; then
13-
export CXX="g++-4.9" CC="gcc-4.9"
14-
export DISPLAY=':99.0'
15-
/usr/bin/Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
16-
sleep 3
17-
fi
18-
install:
19-
- npm install -g vsce
20-
- npm install -g typescript
21-
- npm install -g gulp
22-
- npm install
23-
24-
script:
25-
- gulp tslint
26-
- vsce package
27-
- npm test
1+
language: node_js
2+
3+
node_js:
4+
- '--lts'
5+
6+
os:
7+
- linux
8+
- osx
9+
10+
before_install:
11+
- |
12+
if [ $TRAVIS_OS_NAME == "linux" ]; then
13+
export CXX="g++-4.9" CC="gcc-4.9"
14+
export DISPLAY=':99.0'
15+
/usr/bin/Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
16+
sleep 3
17+
fi
18+
19+
install:
20+
- npm install -g vsce
21+
- npm install -g typescript
22+
- npm install -g gulp
23+
- npm install
24+
25+
script:
26+
- gulp tslint
27+
- vsce package
28+
- npm test

src/commands.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the MIT license.
33

44
import * as vscode from "vscode";
5+
import * as utility from "./utility";
56

67
export const VSCODE_STARTDEBUG = "vscode.startDebug";
78

@@ -31,5 +32,16 @@ export const JAVA_CHECK_PROJECT_SETTINGS = "vscode.java.checkProjectSettings";
3132

3233
export function executeJavaLanguageServerCommand(...rest) {
3334
// TODO: need to handle error and trace telemetry
35+
if (!utility.isJavaExtEnabled()) {
36+
throw new utility.JavaExtensionNotActivatedError(
37+
`Cannot execute command ${JAVA_EXECUTE_WORKSPACE_COMMAND}, VS Code Java Extension is not enabled.`);
38+
}
3439
return vscode.commands.executeCommand(JAVA_EXECUTE_WORKSPACE_COMMAND, ...rest);
3540
}
41+
42+
export function executeJavaExtensionCommand(commandName: string, ...rest) {
43+
if (!utility.isJavaExtEnabled()) {
44+
throw new utility.JavaExtensionNotActivatedError(`Cannot execute command ${commandName}, VS Code Java Extension is not enabled.`);
45+
}
46+
return vscode.commands.executeCommand(commandName, ...rest);
47+
}

src/configurationProvider.ts

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,20 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
5959

6060
private provideDebugConfigurationsAsync(folder: vscode.WorkspaceFolder | undefined, token?: vscode.CancellationToken) {
6161
return vscode.window.withProgress({ location: vscode.ProgressLocation.Window }, (p) => {
62-
return new Promise((resolve, reject) => {
62+
return new Promise(async (resolve, reject) => {
6363
p.report({ message: "Auto generating configuration..." });
64-
lsPlugin.resolveMainClass(folder ? folder.uri : undefined).then((res: lsPlugin.IMainClassOption[]) => {
64+
const defaultLaunchConfig = {
65+
type: "java",
66+
name: "Debug (Launch) - Current File",
67+
request: "launch",
68+
// tslint:disable-next-line
69+
mainClass: "${file}",
70+
};
71+
try {
72+
const mainClasses = await lsPlugin.resolveMainClass(folder ? folder.uri : undefined);
6573
let cache;
6674
cache = {};
67-
const defaultLaunchConfig = {
68-
type: "java",
69-
name: "Debug (Launch) - Current File",
70-
request: "launch",
71-
// tslint:disable-next-line
72-
mainClass: "${file}",
73-
};
74-
const launchConfigs = res.map((item) => {
75+
const launchConfigs = mainClasses.map((item) => {
7576
return {
7677
...defaultLaunchConfig,
7778
name: this.constructLaunchConfigName(item.mainClass, item.projectName, cache),
@@ -80,10 +81,13 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
8081
};
8182
});
8283
resolve([defaultLaunchConfig, ...launchConfigs]);
83-
}, (ex) => {
84+
} catch (ex) {
85+
if (ex instanceof utility.JavaExtensionNotActivatedError) {
86+
utility.guideToInstallJavaExtension();
87+
}
8488
p.report({ message: `failed to generate configuration. ${ex}` });
85-
reject(ex);
86-
});
89+
resolve(defaultLaunchConfig);
90+
}
8791
});
8892
});
8993
}
@@ -160,8 +164,13 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
160164

161165
if (needsBuildWorkspace()) {
162166
try {
163-
const buildResult = await vscode.commands.executeCommand(commands.JAVA_BUILD_WORKSPACE, false);
167+
const buildResult = await commands.executeJavaExtensionCommand(commands.JAVA_BUILD_WORKSPACE, false);
164168
} catch (err) {
169+
if (err instanceof utility.JavaExtensionNotActivatedError) {
170+
utility.guideToInstallJavaExtension();
171+
return undefined;
172+
}
173+
165174
const ans = await utility.showErrorMessageWithTroubleshooting({
166175
message: "Build failed, do you want to continue?",
167176
type: Type.USAGEERROR,
@@ -238,6 +247,10 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
238247
throw new Error("Failed to start debug server.");
239248
}
240249
} catch (ex) {
250+
if (ex instanceof utility.JavaExtensionNotActivatedError) {
251+
utility.guideToInstallJavaExtension();
252+
return undefined;
253+
}
241254
if (ex instanceof utility.UserError) {
242255
utility.showErrorMessageWithTroubleshooting(ex.context);
243256
return undefined;

src/extension.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ function initializeExtension(operationId: string, context: vscode.ExtensionConte
6161
await autobuildConfig.update("enabled", true);
6262
// Force an incremental build to avoid auto build is not finishing during HCR.
6363
try {
64-
await vscode.commands.executeCommand(commands.JAVA_BUILD_WORKSPACE, false)
64+
await commands.executeJavaExtensionCommand(commands.JAVA_BUILD_WORKSPACE, false)
6565
} catch (err) {
6666
// do nothing.
6767
}

src/utility.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@
22
// Licensed under the MIT license.
33

44
import * as vscode from "vscode";
5+
import { setUserError } from "vscode-extension-telemetry-wrapper";
56
import { logger, Type } from "./logger";
67

78
const TROUBLESHOOTING_LINK = "https://github.com/Microsoft/vscode-java-debug/blob/master/Troubleshooting.md";
89
const LEARN_MORE = "Learn More";
10+
const JAVA_EXTENSION_ID = "redhat.java";
911

1012
export class UserError extends Error {
1113
public context: ITroubleshootingMessage;
1214

1315
constructor(context: ITroubleshootingMessage) {
1416
super(context.message);
1517
this.context = context;
18+
setUserError(this);
19+
}
20+
}
21+
22+
export class JavaExtensionNotActivatedError extends Error {
23+
constructor(message) {
24+
super(message);
25+
setUserError(this);
1626
}
1727
}
1828

@@ -85,6 +95,27 @@ function handleTroubleshooting(choice: string, message: string, anchor: string):
8595
return choice;
8696
}
8797

98+
export async function guideToInstallJavaExtension() {
99+
const MESSAGE = "Language Support for Java is required. Please install and enable it.";
100+
const INSTALL = "Install";
101+
const choice = await vscode.window.showWarningMessage(MESSAGE, INSTALL);
102+
if (choice === INSTALL) {
103+
await installJavaExtension();
104+
}
105+
}
106+
107+
async function installJavaExtension() {
108+
await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification }, async (p) => {
109+
p.report({ message: "Installing Language Support for Java ..." });
110+
await vscode.commands.executeCommand("workbench.extensions.installExtension", JAVA_EXTENSION_ID);
111+
});
112+
const RELOAD = "Reload Window";
113+
const choice = await vscode.window.showInformationMessage("Please reload window to activate Language Support for Java.", RELOAD);
114+
if (choice === RELOAD) {
115+
await vscode.commands.executeCommand("workbench.action.reloadWindow");
116+
}
117+
}
118+
88119
export function formatErrorProperties(ex: any): IProperties {
89120
const exception = (ex && ex.data && ex.data.cause)
90121
|| { stackTrace: (ex && ex.stack), detailMessage: String((ex && ex.message) || ex || "Unknown exception") };
@@ -106,7 +137,10 @@ export function formatErrorProperties(ex: any): IProperties {
106137
}
107138

108139
export async function getJavaHome(): Promise<string> {
109-
const extension = vscode.extensions.getExtension("redhat.java");
140+
const extension = vscode.extensions.getExtension(JAVA_EXTENSION_ID);
141+
if (!extension) {
142+
throw new JavaExtensionNotActivatedError("VS Code Java Extension is not enabled.");
143+
}
110144
try {
111145
const extensionApi = await extension.activate();
112146
if (extensionApi && extensionApi.javaRequirement) {
@@ -117,3 +151,8 @@ export async function getJavaHome(): Promise<string> {
117151

118152
return "";
119153
}
154+
155+
export function isJavaExtEnabled() {
156+
const javaExt = vscode.extensions.getExtension(JAVA_EXTENSION_ID);
157+
return !!javaExt;
158+
}

0 commit comments

Comments
 (0)