-
Notifications
You must be signed in to change notification settings - Fork 431
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Automatically register VMInstalls for the JDKs installed on the local…
… machine (#3301) * Automatically register VMInstalls for the JDKs installed on the local machine * Bump to [email protected] to fix wrong jdk detection issue on macOS * Validate if the detected jdk is a real java home * Add a setting 'java.configuration.detectJdksAtStart' to control whether to detect jdks on start
- Loading branch information
1 parent
a9b87bd
commit 17edf20
Showing
11 changed files
with
169 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
'use strict'; | ||
|
||
import { existsSync } from 'fs'; | ||
import { IJavaRuntime, findRuntimes, getSources } from 'jdk-utils'; | ||
import { join } from 'path'; | ||
import { ExtensionContext, Uri, workspace } from 'vscode'; | ||
|
||
let cachedJdks: IJavaRuntime[]; | ||
let cachedJreNames: string[]; | ||
|
||
export async function loadSupportedJreNames(context: ExtensionContext): Promise<void> { | ||
const buffer = await workspace.fs.readFile(Uri.file(context.asAbsolutePath("package.json"))); | ||
const packageJson = JSON.parse(buffer.toString()); | ||
cachedJreNames = packageJson?.contributes?.configuration?.properties?.["java.configuration.runtimes"]?.items?.properties?.name?.enum; | ||
} | ||
|
||
export function getSupportedJreNames(): string[] { | ||
return cachedJreNames; | ||
} | ||
|
||
export async function listJdks(force?: boolean): Promise<IJavaRuntime[]> { | ||
if (force || !cachedJdks) { | ||
cachedJdks = await findRuntimes({ checkJavac: true, withVersion: true, withTags: true }) | ||
.then(jdks => jdks.filter(jdk => { | ||
// Validate if it's a real Java Home. | ||
return existsSync(join(jdk.homedir, "lib", "rt.jar")) | ||
|| existsSync(join(jdk.homedir, "jre", "lib", "rt.jar")) // Java 8 | ||
|| existsSync(join(jdk.homedir, "lib", "jrt-fs.jar")); // Java 9+ | ||
})); | ||
} | ||
|
||
return [].concat(cachedJdks); | ||
} | ||
|
||
/** | ||
* Sort by source where JDk is located. | ||
* The order is: | ||
* 1. JDK_HOME, JAVA_HOME, PATH | ||
* 2. JDK manager such as SDKMAN, jEnv, jabba, asdf | ||
* 3. Common places such as /usr/lib/jvm | ||
* 4. Others | ||
*/ | ||
export function sortJdksBySource(jdks: IJavaRuntime[]) { | ||
const rankedJdks = jdks as Array<IJavaRuntime & { rank: number }>; | ||
const env: string[] = ["JDK_HOME", "JAVA_HOME", "PATH"]; | ||
const jdkManagers: string[] = ["SDKMAN", "jEnv", "jabba", "asdf"]; | ||
for (const jdk of rankedJdks) { | ||
const detectedSources: string[] = getSources(jdk); | ||
for (const [index, source] of env.entries()) { | ||
if (detectedSources.includes(source)) { | ||
jdk.rank = index; // jdk from environment variables | ||
break; | ||
} | ||
} | ||
|
||
if (jdk.rank) { | ||
continue; | ||
} | ||
|
||
const fromManager: boolean = detectedSources.some(source => jdkManagers.includes(source)); | ||
if (fromManager) { | ||
jdk.rank = env.length + 1; // jdk from the jdk managers such as SDKMAN | ||
} else if (!detectedSources.length){ | ||
jdk.rank = env.length + 2; // jdk from common places | ||
} else { | ||
jdk.rank = env.length + 3; // jdk from other source such as ~/.gradle/jdks | ||
} | ||
} | ||
rankedJdks.sort((a, b) => a.rank - b.rank); | ||
} | ||
|
||
/** | ||
* Sort by major version in descend order. | ||
*/ | ||
export function sortJdksByVersion(jdks: IJavaRuntime[]) { | ||
jdks.sort((a, b) => (b.version?.major ?? 0) - (a.version?.major ?? 0)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.