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
125 lines (115 loc) · 3.95 KB
/
requirements.ts
File metadata and controls
125 lines (115 loc) · 3.95 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
'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';
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;
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(): Promise<RequirementsData> {
let java_home = await checkJavaRuntime();
let javaVersion = await checkJavaVersion(java_home);
return Promise.resolve({ 'java_home': java_home, 'java_version': javaVersion});
}
function checkJavaRuntime(): Promise<string> {
return new Promise((resolve, reject) => {
let source : string;
let javaHome : string = readJavaConfig();
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');
}
if(!pathExists.sync(path.resolve(javaHome, 'bin', JAVAC_FILENAME))){
openJDKDownload(reject, source+ ' does not point to a JDK.');
}
return resolve(javaHome);
}
//No settings, let's try to detect as last resort.
findJavaHome(function (err, home) {
if (err){
openJDKDownload(reject,'Java runtime could not be located');
}
else {
resolve(home);
}
});
});
}
function readJavaConfig() : string {
const config = workspace.getConfiguration();
return config.get<string>('java.home',null);
}
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 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 = 'http://developers.redhat.com/products/openjdk/overview/';
if (process.platform === 'darwin') {
jdkUrl = 'http://www.oracle.com/technetwork/java/javase/downloads/index.html';
}
reject({
message: cause,
label: 'Get Java Development Kit',
openUrl: Uri.parse(jdkUrl),
replaceClose: false
});
}