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
This commit is contained in:
Yan Zhang 2019-08-02 12:23:11 +08:00 committed by GitHub
parent 50c981186a
commit fa49caa25a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 108 additions and 43 deletions

View File

@ -1,27 +1,28 @@
language: node_js
node_js:
- '--lts'
os:
- linux
- osx
before_install:
- |
if [ $TRAVIS_OS_NAME == "linux" ]; then
export CXX="g++-4.9" CC="gcc-4.9"
export DISPLAY=':99.0'
/usr/bin/Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
sleep 3
fi
install:
- npm install -g vsce
- npm install -g typescript
- npm install -g gulp
- npm install
script:
- gulp tslint
- vsce package
- npm test
language: node_js
node_js:
- '--lts'
os:
- linux
- osx
before_install:
- |
if [ $TRAVIS_OS_NAME == "linux" ]; then
export CXX="g++-4.9" CC="gcc-4.9"
export DISPLAY=':99.0'
/usr/bin/Xvfb :99 -screen 0 1024x768x24 > /dev/null 2>&1 &
sleep 3
fi
install:
- npm install -g vsce
- npm install -g typescript
- npm install -g gulp
- npm install
script:
- gulp tslint
- vsce package
- npm test

View File

@ -2,6 +2,7 @@
// Licensed under the MIT license.
import * as vscode from "vscode";
import * as utility from "./utility";
export const VSCODE_STARTDEBUG = "vscode.startDebug";
@ -31,5 +32,16 @@ export const JAVA_CHECK_PROJECT_SETTINGS = "vscode.java.checkProjectSettings";
export function executeJavaLanguageServerCommand(...rest) {
// TODO: need to handle error and trace telemetry
if (!utility.isJavaExtEnabled()) {
throw new utility.JavaExtensionNotActivatedError(
`Cannot execute command ${JAVA_EXECUTE_WORKSPACE_COMMAND}, VS Code Java Extension is not enabled.`);
}
return vscode.commands.executeCommand(JAVA_EXECUTE_WORKSPACE_COMMAND, ...rest);
}
export function executeJavaExtensionCommand(commandName: string, ...rest) {
if (!utility.isJavaExtEnabled()) {
throw new utility.JavaExtensionNotActivatedError(`Cannot execute command ${commandName}, VS Code Java Extension is not enabled.`);
}
return vscode.commands.executeCommand(commandName, ...rest);
}

View File

@ -59,19 +59,20 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
private provideDebugConfigurationsAsync(folder: vscode.WorkspaceFolder | undefined, token?: vscode.CancellationToken) {
return vscode.window.withProgress({ location: vscode.ProgressLocation.Window }, (p) => {
return new Promise((resolve, reject) => {
return new Promise(async (resolve, reject) => {
p.report({ message: "Auto generating configuration..." });
lsPlugin.resolveMainClass(folder ? folder.uri : undefined).then((res: lsPlugin.IMainClassOption[]) => {
const defaultLaunchConfig = {
type: "java",
name: "Debug (Launch) - Current File",
request: "launch",
// tslint:disable-next-line
mainClass: "${file}",
};
try {
const mainClasses = await lsPlugin.resolveMainClass(folder ? folder.uri : undefined);
let cache;
cache = {};
const defaultLaunchConfig = {
type: "java",
name: "Debug (Launch) - Current File",
request: "launch",
// tslint:disable-next-line
mainClass: "${file}",
};
const launchConfigs = res.map((item) => {
const launchConfigs = mainClasses.map((item) => {
return {
...defaultLaunchConfig,
name: this.constructLaunchConfigName(item.mainClass, item.projectName, cache),
@ -80,10 +81,13 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
};
});
resolve([defaultLaunchConfig, ...launchConfigs]);
}, (ex) => {
} catch (ex) {
if (ex instanceof utility.JavaExtensionNotActivatedError) {
utility.guideToInstallJavaExtension();
}
p.report({ message: `failed to generate configuration. ${ex}` });
reject(ex);
});
resolve(defaultLaunchConfig);
}
});
});
}
@ -160,8 +164,13 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
if (needsBuildWorkspace()) {
try {
const buildResult = await vscode.commands.executeCommand(commands.JAVA_BUILD_WORKSPACE, false);
const buildResult = await commands.executeJavaExtensionCommand(commands.JAVA_BUILD_WORKSPACE, false);
} catch (err) {
if (err instanceof utility.JavaExtensionNotActivatedError) {
utility.guideToInstallJavaExtension();
return undefined;
}
const ans = await utility.showErrorMessageWithTroubleshooting({
message: "Build failed, do you want to continue?",
type: Type.USAGEERROR,
@ -238,6 +247,10 @@ export class JavaDebugConfigurationProvider implements vscode.DebugConfiguration
throw new Error("Failed to start debug server.");
}
} catch (ex) {
if (ex instanceof utility.JavaExtensionNotActivatedError) {
utility.guideToInstallJavaExtension();
return undefined;
}
if (ex instanceof utility.UserError) {
utility.showErrorMessageWithTroubleshooting(ex.context);
return undefined;

View File

@ -61,7 +61,7 @@ function initializeExtension(operationId: string, context: vscode.ExtensionConte
await autobuildConfig.update("enabled", true);
// Force an incremental build to avoid auto build is not finishing during HCR.
try {
await vscode.commands.executeCommand(commands.JAVA_BUILD_WORKSPACE, false)
await commands.executeJavaExtensionCommand(commands.JAVA_BUILD_WORKSPACE, false)
} catch (err) {
// do nothing.
}

View File

@ -2,10 +2,12 @@
// Licensed under the MIT license.
import * as vscode from "vscode";
import { setUserError } from "vscode-extension-telemetry-wrapper";
import { logger, Type } from "./logger";
const TROUBLESHOOTING_LINK = "https://github.com/Microsoft/vscode-java-debug/blob/master/Troubleshooting.md";
const LEARN_MORE = "Learn More";
const JAVA_EXTENSION_ID = "redhat.java";
export class UserError extends Error {
public context: ITroubleshootingMessage;
@ -13,6 +15,14 @@ export class UserError extends Error {
constructor(context: ITroubleshootingMessage) {
super(context.message);
this.context = context;
setUserError(this);
}
}
export class JavaExtensionNotActivatedError extends Error {
constructor(message) {
super(message);
setUserError(this);
}
}
@ -85,6 +95,27 @@ function handleTroubleshooting(choice: string, message: string, anchor: string):
return choice;
}
export async function guideToInstallJavaExtension() {
const MESSAGE = "Language Support for Java is required. Please install and enable it.";
const INSTALL = "Install";
const choice = await vscode.window.showWarningMessage(MESSAGE, INSTALL);
if (choice === INSTALL) {
await installJavaExtension();
}
}
async function installJavaExtension() {
await vscode.window.withProgress({ location: vscode.ProgressLocation.Notification }, async (p) => {
p.report({ message: "Installing Language Support for Java ..." });
await vscode.commands.executeCommand("workbench.extensions.installExtension", JAVA_EXTENSION_ID);
});
const RELOAD = "Reload Window";
const choice = await vscode.window.showInformationMessage("Please reload window to activate Language Support for Java.", RELOAD);
if (choice === RELOAD) {
await vscode.commands.executeCommand("workbench.action.reloadWindow");
}
}
export function formatErrorProperties(ex: any): IProperties {
const exception = (ex && ex.data && ex.data.cause)
|| { stackTrace: (ex && ex.stack), detailMessage: String((ex && ex.message) || ex || "Unknown exception") };
@ -106,7 +137,10 @@ export function formatErrorProperties(ex: any): IProperties {
}
export async function getJavaHome(): Promise<string> {
const extension = vscode.extensions.getExtension("redhat.java");
const extension = vscode.extensions.getExtension(JAVA_EXTENSION_ID);
if (!extension) {
throw new JavaExtensionNotActivatedError("VS Code Java Extension is not enabled.");
}
try {
const extensionApi = await extension.activate();
if (extensionApi && extensionApi.javaRequirement) {
@ -117,3 +151,8 @@ export async function getJavaHome(): Promise<string> {
return "";
}
export function isJavaExtEnabled() {
const javaExt = vscode.extensions.getExtension(JAVA_EXTENSION_ID);
return !!javaExt;
}