-
Notifications
You must be signed in to change notification settings - Fork 99
Expand file tree
/
Copy pathrequirements.ts
More file actions
193 lines (181 loc) · 7.5 KB
/
requirements.ts
File metadata and controls
193 lines (181 loc) · 7.5 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict';
import { window, workspace, Uri, ExtensionContext, ConfigurationTarget, env } from 'vscode';
import * as cp from 'child_process';
import * as path from 'path';
import { IS_WORKSPACE_JDK_XML_ALLOWED, getKey, IS_WORKSPACE_VMARGS_XML_ALLOWED, getJavaagentFlag, IS_WORKSPACE_JDK_ALLOWED, getXMLConfiguration, getJavaConfiguration, xmlServerVmargs } from './settings';
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':'');
export interface RequirementsData {
java_home: string;
java_version: number;
}
interface ErrorData {
message: string;
label: string;
openUrl: Uri;
replaceClose: boolean;
}
/**
* 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 fails to resolve.
*
*/
export async function resolveRequirements(context: ExtensionContext): Promise<RequirementsData> {
const javaHome = await checkJavaRuntime(context);
const javaVersion = await checkJavaVersion(javaHome);
return Promise.resolve({ 'java_home': javaHome, 'java_version': javaVersion});
}
function checkJavaRuntime(context: ExtensionContext): Promise<string> {
return new Promise(async (resolve, reject) => {
let source : string;
let javaHome = await readXMLJavaHomeConfig(context);
if (javaHome) {
source = 'The xml.java.home variable defined in VS Code settings';
} else {
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';
}
}
}
if (javaHome) {
javaHome = expandHomeDir(javaHome);
if (!pathExists.sync(javaHome)) {
openJDKDownload(reject, source+' points to a missing folder');
} else if (!pathExists.sync(path.resolve(javaHome, 'bin', JAVA_FILENAME))){
openJDKDownload(reject, source+ ' does not point to a Java runtime.');
}
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);
}
});
});
}
export async function readXMLJavaHomeConfig(context: ExtensionContext) {
const xmlJavaHome = 'xml.java.home';
let javaHome = workspace.getConfiguration().inspect<string>(xmlJavaHome).workspaceValue;
let isVerified = javaHome === undefined || javaHome === null;
if (isVerified) {
javaHome = getXMLConfiguration().get("java.home");
}
const allow = 'Allow';
const disallow = 'Disallow';
const key = getKey(IS_WORKSPACE_JDK_XML_ALLOWED, context.storagePath, javaHome);
const globalState = context.globalState;
if (!isVerified) {
isVerified = globalState.get(key);
if (isVerified === undefined) {
await window.showErrorMessage(`Security Warning! Do you allow this workspace to set the ${xmlJavaHome} variable? \n ${xmlJavaHome}: ${javaHome}`, disallow, allow).then(async selection => {
if (selection === allow) {
globalState.update(key, true);
} else if (selection === disallow) {
globalState.update(key, false);
await workspace.getConfiguration().update(xmlJavaHome, undefined, ConfigurationTarget.Workspace);
}
});
isVerified = globalState.get(key);
}
}
const vmargs = workspace.getConfiguration().inspect(xmlServerVmargs).workspaceValue;
if (vmargs !== undefined) {
const agentFlag = getJavaagentFlag(vmargs);
if (agentFlag !== null) {
const keyVmargs = getKey(IS_WORKSPACE_VMARGS_XML_ALLOWED, context.storagePath, vmargs);
const vmargsVerified = globalState.get(keyVmargs);
if (vmargsVerified === undefined || vmargsVerified === null) {
await window.showErrorMessage(`Security Warning! The ${xmlServerVmargs} variable defined in ${env.appName} settings includes the (${agentFlag}) javagent preference. Do you allow it to be used?`, disallow, allow).then(async selection => {
if (selection === allow) {
globalState.update(keyVmargs, true);
} else if (selection === disallow) {
globalState.update(keyVmargs, false);
await workspace.getConfiguration().update(xmlServerVmargs, undefined, ConfigurationTarget.Workspace);
}
});
}
}
}
if (isVerified) {
return javaHome;
} else {
return workspace.getConfiguration().inspect<string>('xml.java.home').globalValue;
}
}
async function readJavaHomeConfig(context: ExtensionContext) {
let javaHome = workspace.getConfiguration().inspect<string>('java.home').workspaceValue;
let isVerified = javaHome === undefined || javaHome === null;
if (isVerified) {
javaHome = getJavaConfiguration().get('home');
}
const key = getKey(IS_WORKSPACE_JDK_ALLOWED, context.storagePath, javaHome);
const globalState = context.globalState;
isVerified = globalState.get(key);
if (isVerified) {
return javaHome;
} else {
return workspace.getConfiguration().inspect<string>('java.home').globalValue;
}
}
function checkJavaVersion(java_home: string): Promise<number> {
return new Promise((resolve, reject) => {
cp.execFile(java_home + '/bin/java', ['-version'], {}, (error, stdout, stderr) => {
let javaVersion = parseMajorVersion(stderr);
if (javaVersion < 8) {
openJDKDownload(reject, 'Java 8 or more recent is required to run. Please download and install a recent Java runtime.');
}
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 openJDKDownload(reject, cause : string) {
let jdkUrl = 'https://developers.redhat.com/products/openjdk/download/?sc_cid=701f2000000RWTnAAO';
if (process.platform === 'darwin') {
jdkUrl = 'https://adoptopenjdk.net/releases.html';
}
reject({
message: cause,
label: 'Get the Java runtime',
openUrl: Uri.parse(jdkUrl),
replaceClose: false
});
}