|
1 | | -import { getRuntime } from "jdk-utils"; |
| 1 | +import { getRuntime, IJavaRuntime } from "jdk-utils"; |
2 | 2 | import * as vscode from "vscode"; |
3 | 3 | import { getSupportedJreNames } from "./jdkUtils"; |
| 4 | +import { Commands } from "./commands"; |
| 5 | +import * as path from "path"; |
4 | 6 |
|
5 | 7 |
|
6 | 8 | export namespace JavaRuntimes { |
| 9 | + function compatible(runtime: IJavaRuntime, jreName: string): boolean { |
| 10 | + if (!runtime.version) { |
| 11 | + return true; |
| 12 | + } |
| 13 | + const majorVersion = runtime.version.major; |
| 14 | + if (majorVersion === 8) { |
| 15 | + return jreName === 'JavaSE-1.8'; |
| 16 | + } |
| 17 | + const versionStrings = /[0-9]+/g.exec(jreName); |
| 18 | + if (versionStrings && versionStrings.length > 0) { |
| 19 | + return majorVersion >= parseInt(versionStrings[0]); |
| 20 | + } |
| 21 | + return false; |
| 22 | + } |
| 23 | + |
7 | 24 | export async function initialize(context: vscode.ExtensionContext): Promise<void> { |
8 | | - context.subscriptions.push(vscode.commands.registerCommand('java.runtimes.add', async () => { |
| 25 | + context.subscriptions.push(vscode.commands.registerCommand(Commands.ADD_JAVA_RUNTIME, async () => { |
| 26 | + const lastSelectedDirectory: vscode.Uri | undefined = context.workspaceState.get('java.runtimes.lastSelectedDirectory'); |
9 | 27 | const directory = await vscode.window.showOpenDialog({ |
10 | 28 | canSelectFiles: false, |
11 | 29 | canSelectFolders: true, |
12 | 30 | canSelectMany: false, |
13 | 31 | title: 'Select JDK Directory', |
| 32 | + defaultUri: lastSelectedDirectory, |
14 | 33 | }); |
15 | 34 | if (directory) { |
16 | | - const runtime = await getRuntime(directory[0].fsPath); |
| 35 | + context.workspaceState.update('java.runtimes.lastSelectedDirectory', vscode.Uri.file(path.dirname(directory[0].fsPath))); |
| 36 | + const runtime = await getRuntime(directory[0].fsPath, {withVersion: true}); |
17 | 37 | if (runtime) { |
18 | 38 | const config = vscode.workspace.getConfiguration('java.configuration').get('runtimes'); |
19 | 39 | if (Array.isArray(config)) { |
20 | | - if (config.some(r => r.path === directory[0].fsPath)) { |
21 | | - vscode.window.showErrorMessage(`JDK Directory ${directory[0].fsPath} already configured`); |
22 | | - } else { |
23 | | - const name = await vscode.window.showQuickPick(getSupportedJreNames(), { |
| 40 | + const candidates = getSupportedJreNames().filter(name => !config.some(r => r.name === name) && compatible(runtime, name)); |
| 41 | + if (candidates.length > 0) { |
| 42 | + const name = await vscode.window.showQuickPick(candidates, { |
24 | 43 | title: 'Select Java Runtime', |
25 | 44 | }); |
26 | 45 | if (name) { |
27 | 46 | config.push({ |
28 | 47 | name: name, |
29 | 48 | path: directory[0].fsPath, |
30 | 49 | }); |
| 50 | + vscode.workspace.getConfiguration('java.configuration').update('runtimes', config, vscode.ConfigurationTarget.Global); |
| 51 | + vscode.window.showInformationMessage(`JDK Directory ${directory[0].fsPath} added`); |
31 | 52 | } |
32 | | - vscode.workspace.getConfiguration('java.configuration').update('runtimes', config, vscode.ConfigurationTarget.Global); |
33 | | - vscode.window.showInformationMessage(`JDK Directory ${directory[0].fsPath} added`); |
| 53 | + } else { |
| 54 | + vscode.window.showErrorMessage('No compatible environment available'); |
34 | 55 | } |
35 | 56 | } |
36 | 57 | } else { |
|
0 commit comments