-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathrequirements.ts
More file actions
172 lines (151 loc) · 6.03 KB
/
requirements.ts
File metadata and controls
172 lines (151 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
* Copyright 2019 Red Hat, Inc. and others.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
'use strict';
import * as cp from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { Uri, workspace } from 'vscode';
const expandHomeDir = require('expand-home-dir');
import * as findJavaHome from 'find-java-home';
import { JavaExtensionAPI } from '../extension';
import { localize } from './i18nUtil';
const isWindows = process.platform.indexOf('win') === 0;
const JAVA_FILENAME = 'java' + (isWindows?'.exe': '');
const REQUIRED_JAVA_VERSION = 21;
export interface RequirementsData {
tooling_jre: string;
tooling_jre_version: number;
java_home: string;
java_version: number;
}
// Referenced:
// https://github.com/redhat-developer/vscode-microprofile/blob/master/src/languageServer/requirements.ts
/**
* Resolves the requirements needed to run the extension.
* Returns a promise that will resolve to a RequirementsData if
* all requirements are resolved, it will reject with ErrorData if
* if any of the requirements fail to resolve.
*
*/
export async function resolveRequirements(api: JavaExtensionAPI): Promise<RequirementsData> {
const requirementsData = api.javaRequirement;
// Java check for LSP4Jakarta and LCLS support
// Reuse the embedded JRE from 'redhat.java' if it exists and passes check
if (requirementsData && requirementsData.tooling_jre_version >= REQUIRED_JAVA_VERSION) {
return Promise.resolve(requirementsData);
}
const javaHome = await checkJavaRuntime('java.jdt.ls.java.home');
const javaVersion = await checkJavaVersion(javaHome, true);
return Promise.resolve({tooling_jre: javaHome, tooling_jre_version: javaVersion, java_home: javaHome, java_version: javaVersion});
}
export async function resolveLclsRequirements(api:JavaExtensionAPI) {
const javaHome = await checkJavaRuntime('xml.java.home');
return checkJavaVersion(javaHome, false);
}
function checkJavaRuntime(property: string): Promise<string> {
return new Promise((resolve, reject) => {
let source: string;
let javaHome: string|undefined = readJavaHomeConfig(property);
if (javaHome) {
source = localize("check.java.runtime.vscode.java.home");
} else {
javaHome = process.env['JDK_HOME'];
if (javaHome) {
source = localize("check.java.runtime.env.jdk.home");
} else {
javaHome = process.env['JAVA_HOME'];
source = localize("check.java.runtime.env.java.home");
}
}
if (javaHome) {
javaHome = expandHomeDir(javaHome);
if (!fs.existsSync(javaHome!)) {
openJDKDownload(reject, source + localize("open.jdk.download.part.missing.folder"));
} else if (!fs.existsSync(path.resolve(javaHome as string, 'bin', JAVA_FILENAME))) {
openJDKDownload(reject, source + localize("open.jdk.download.part.no.runtime"));
}
return resolve(javaHome!);
}
// No settings, let's try to detect as last resort.
findJavaHome({ allowJre: true }, (err: any, home: any) => {
if (err) {
openJDKDownload(reject, localize("check.java.runtime.failed.locate"));
}
else {
resolve(home);
}
});
});
}
function readJavaHomeConfig(property: string): string|undefined {
const config = workspace.getConfiguration();
let javaHome = config.get<string>(property);
return (javaHome != null) ? javaHome : config.get<string>('java.home');
}
// Provided javaHome, parse major version and reject sub Java21
function checkJavaVersion(javaHome: string, promptDownload: boolean): Promise<number> {
return new Promise((resolve, reject) => {
cp.execFile(javaHome + '/bin/java', ['-version'], {}, (error, stdout, stderr) => {
const javaVersion = parseMajorVersion(stderr);
if (javaVersion < REQUIRED_JAVA_VERSION) {
if (promptDownload) {
openJDKDownload(reject, localize("check.java.runtime.version.outdated", REQUIRED_JAVA_VERSION));
} else {
defineXmlJavaHome(reject, localize("define.xml.java.home.message", REQUIRED_JAVA_VERSION));
}
} else {
resolve(javaVersion);
}
});
});
}
export function parseMajorVersion(content: string): number {
let regexp = /version "(.*)"/g;
let match = regexp.exec(content);
if (!match) {
return 0;
}
let version = match[1];
// Ignore '1.' prefix for legacy Java versions
if (version.startsWith('1.')) {
version = version.substring(2);
}
// look into the interesting bits now
regexp = /\d+/g;
match = regexp.exec(version);
let javaVersion = 0;
if (match) {
javaVersion = parseInt(match[0]);
}
return javaVersion;
}
function defineXmlJavaHome(reject: any, cause: string) {
reject({
message: cause,
label: localize("check.java.runtime.dismiss.label"),
replaceClose: false
})
}
function openJDKDownload(reject: any, cause: string) {
let jdkUrl = 'https://developer.ibm.com/languages/java/semeru-runtimes/downloads/';
if (process.platform === 'darwin') {
jdkUrl = 'http://www.oracle.com/technetwork/java/javase/downloads/index.html';
}
reject({
message: cause,
label: localize("open.jdk.download.label"),
openUrl: Uri.parse(jdkUrl),
replaceClose: false
});
}