forked from redhat-developer/vscode-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequirements.ts
More file actions
146 lines (135 loc) · 4.65 KB
/
requirements.ts
File metadata and controls
146 lines (135 loc) · 4.65 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
'use strict';
import { workspace, Uri } from 'vscode';
import * as cp from 'child_process';
import * as path from 'path';
import * as pathExists from 'path-exists';
import * as expandHomeDir from 'expand-home-dir';
import * as findJavaHome from 'find-java-home';
import { Commands } from './commands';
const isWindows = process.platform.indexOf('win') === 0;
const JAVAC_FILENAME = 'javac' + (isWindows ? '.exe' : '');
export interface RequirementsData {
java_home: string;
java_version: number;
}
interface ErrorData {
message: string;
label: string;
command: string;
commandParam: any;
}
/**
* 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(): Promise<RequirementsData> {
const javaHome = await checkJavaRuntime();
const javaVersion = await checkJavaVersion(javaHome);
return Promise.resolve({ java_home: javaHome, java_version: javaVersion });
}
function checkJavaRuntime(): Promise<string> {
return new Promise((resolve, reject) => {
let source: string;
let javaHome: string = readJavaConfig();
if (javaHome) {
source = 'java.home variable defined in VS Code settings';
} else {
javaHome = process.env['JDK_HOME'];
if (javaHome) {
source = 'JDK_HOME environment variable';
} else {
javaHome = process.env['JAVA_HOME'];
source = 'JAVA_HOME environment variable';
}
}
if (javaHome) {
javaHome = expandHomeDir(javaHome);
if (!pathExists.sync(javaHome)) {
invalidJavaHome(reject, `The ${source} points to a missing or inaccessible folder (${javaHome})`);
}
else if (!pathExists.sync(path.resolve(javaHome, 'bin', JAVAC_FILENAME))) {
let msg: string;
if (pathExists.sync(path.resolve(javaHome, JAVAC_FILENAME))) {
msg = `'bin' should be removed from the ${source} (${javaHome})`;
} else {
msg = `The ${source} (${javaHome}) does not point to a JDK.`;
}
invalidJavaHome(reject, msg);
}
return resolve(javaHome);
}
// No settings, let's try to detect as last resort.
findJavaHome((err, home) => {
if (err) {
openJDKDownload(reject, 'Java runtime (JDK, not JRE) could not be located');
}
else {
resolve(home);
}
});
});
}
function readJavaConfig(): string {
const config = workspace.getConfiguration();
return config.get<string>('java.home', null);
}
function checkJavaVersion(javaHome: string): Promise<number> {
return new Promise((resolve, reject) => {
cp.execFile(javaHome + '/bin/java', ['-version'], {}, (error, stdout, stderr) => {
const javaVersion = parseMajorVersion(stderr);
if (javaVersion < 8) {
openJDKDownload(reject, 'Java 8 or more recent is required to run. Please download and install a recent JDK');
} 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) {
let jdkUrl = 'https://developers.redhat.com/products/openjdk/download/?sc_cid=701f2000000RWTnAAO';
if (process.platform === 'darwin') {
jdkUrl = 'http://www.oracle.com/technetwork/java/javase/downloads/index.html';
}
reject({
message: cause,
label: 'Get the Java Development Kit',
command: Commands.OPEN_BROWSER,
commandParam: Uri.parse(jdkUrl),
});
}
function invalidJavaHome(reject, cause: string) {
if (cause.indexOf("java.home") > -1) {
reject({
message: cause,
label: 'Open settings',
command: Commands.OPEN_JSON_SETTINGS
});
} else {
reject({
message: cause,
});
}
}