Skip to content

Commit

Permalink
Migrate from find-java-home to jdk-utils
Browse files Browse the repository at this point in the history
Signed-off-by: Jessica He <[email protected]>
  • Loading branch information
JessicaJHee authored and datho7561 committed Aug 4, 2022
1 parent 42ef9d2 commit 2c828dd
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
14 changes: 5 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@
"dependencies": {
"@redhat-developer/vscode-redhat-telemetry": "0.4.2",
"expand-home-dir": "^0.0.3",
"find-java-home": "1.2.2",
"fs-extra": "^8.1.0",
"glob": "^7.1.4",
"jdk-utils": "^0.4.3",
"path-exists": "^4.0.0",
"vscode-languageclient": "^7.0.0",
"yauzl": "^2.10.0"
Expand Down
40 changes: 23 additions & 17 deletions src/server/requirements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import * as cp from 'child_process';
import * as path from 'path';
import { ConfigurationTarget, env, ExtensionContext, Uri, window, workspace } from 'vscode';
import { getJavaagentFlag, getJavaConfiguration, getKey, getXMLConfiguration, IS_WORKSPACE_JDK_ALLOWED, IS_WORKSPACE_JDK_XML_ALLOWED, IS_WORKSPACE_VMARGS_XML_ALLOWED, xmlServerVmargs } from '../settings/settings';
import { findRuntimes, IJavaRuntime, getSources } from 'jdk-utils';

const pathExists = require('path-exists');
const expandHomeDir = require('expand-home-dir');
const findJavaHome = require('find-java-home');
const isWindows = process.platform.indexOf('win') === 0;
const JAVA_FILENAME = 'java' + (isWindows ? '.exe' : '');

Expand Down Expand Up @@ -45,14 +45,6 @@ function checkJavaRuntime(context: ExtensionContext): Promise<string> {
javaHome = await readJavaHomeConfig(context);
if (javaHome) {
source = 'The java.home variable defined in VS Code settings';
} else {
javaHome = process.env['JDK_HOME'];
if (javaHome) {
source = 'The JDK_HOME environment variable';
} else {
javaHome = process.env['JAVA_HOME'];
source = 'The JAVA_HOME environment variable';
}
}
}

Expand All @@ -66,14 +58,14 @@ function checkJavaRuntime(context: ExtensionContext): Promise<string> {
return resolve(javaHome);
}
//No settings, let's try to detect as last resort.
findJavaHome({ allowJre: true }, function (err, home) {
if (err) {
openJDKDownload(reject, 'Java runtime could not be located.');
}
else {
resolve(home);
}
});
const javaRuntimes = await findRuntimes({ withVersion: true, withTags: true });
if (javaRuntimes.length) {
sortJdksBySource(javaRuntimes);
javaHome = javaRuntimes[0].homedir;
} else {
openJDKDownload(reject, "Java runtime could not be located. Please download and install Java or use the binary server.");
}
return resolve(javaHome);
});
}

Expand Down Expand Up @@ -127,6 +119,20 @@ export async function readXMLJavaHomeConfig(context: ExtensionContext) {
}
}

function sortJdksBySource(jdks: IJavaRuntime[]) {
const rankedJdks = jdks as Array<IJavaRuntime & { rank: number }>;
const sources = ["JDK_HOME", "JAVA_HOME", "PATH"];
for (const [index, source] of sources.entries()) {
for (const jdk of rankedJdks) {
if (jdk.rank === undefined && getSources(jdk).includes(source)) {
jdk.rank = index;
}
}
}
rankedJdks.filter(jdk => jdk.rank === undefined).forEach(jdk => jdk.rank = sources.length);
rankedJdks.sort((a, b) => a.rank - b.rank);
}

async function readJavaHomeConfig(context: ExtensionContext) {
let javaHome = workspace.getConfiguration().inspect<string>('java.home').workspaceValue;
let isVerified = javaHome === undefined || javaHome === null;
Expand Down

0 comments on commit 2c828dd

Please sign in to comment.