-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
java.js
146 lines (129 loc) · 5.57 KB
/
java.js
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
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
*/
const execa = require('execa');
const fs = require('fs-extra');
const path = require('path');
const glob = require('fast-glob');
const { CordovaError, events } = require('cordova-common');
const utils = require('../utils');
const semver = require('semver');
/**
* Will be set to true on successful ensureness.
* If true, skips the expensive java checks.
*/
let javaIsEnsured = false;
const java = {
/**
* Gets the version from the javac executable.
*
* @returns {semver.SemVer}
*/
getVersion: async () => {
await java._ensure(process.env);
// Java <= 8 writes version info to stderr, Java >= 9 to stdout
let version = null;
try {
const javacOutput = (await execa('javac', ['-version'], { all: true })).all;
/*
A regex match for the java version looks like the following:
version: [
'javac 1.8.0',
'1.8.0',
index: 45,
input: 'Picked up _JAVA_OPTIONS: -Xms1024M -Xmx2048M\njavac 1.8.0_271',
groups: undefined
]
We have to use a regular expression to get the java version, because on some environments
(e.g. macOS Big Sur) javac prints _JAVA_OPTIONS before printing the version and semver.coerce()
will fail to get the correct version from the output.
*/
const match = /javac\s+([\d.]+)/i.exec(javacOutput);
if (match && match[1]) {
version = match[1];
}
} catch (ex) {
events.emit('verbose', ex.shortMessage);
let msg =
'Failed to run "javac -version", make sure that you have a JDK version 8 installed.\n' +
'You can get it from the following location:\n' +
'https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html';
if (process.env.JAVA_HOME) {
msg += '\n\n';
msg += 'Your JAVA_HOME is invalid: ' + process.env.JAVA_HOME;
}
throw new CordovaError(msg);
}
return semver.coerce(version);
},
/**
* Ensures that Java is installed. Will throw exception if not.
* Will set JAVA_HOME and PATH environment variables.
*
* This function becomes a no-op if already ran previously.
*/
_ensure: async (environment) => {
if (javaIsEnsured) {
return;
}
const javacPath = utils.forgivingWhichSync('javac');
const javaHome = environment.CORDOVA_JAVA_HOME || environment.JAVA_HOME;
if (javaHome) {
// Ensure that CORDOVA_JAVA_HOME overrides
environment.JAVA_HOME = javaHome;
// Windows java installer doesn't add javac to PATH, nor set JAVA_HOME (ugh).
if (!javacPath) {
environment.PATH += path.delimiter + path.join(environment.JAVA_HOME, 'bin');
}
} else {
if (javacPath) {
// OS X has a command for finding JAVA_HOME.
const find_java = '/usr/libexec/java_home';
const default_java_error_msg = 'Failed to find \'JAVA_HOME\' environment variable. Try setting it manually.';
if (fs.existsSync(find_java)) {
try {
environment.JAVA_HOME = (await execa(find_java)).stdout;
} catch (ex) {
events.emit('verbose', ex.shortMessage);
throw new CordovaError(default_java_error_msg);
}
} else {
// See if we can derive it from javac's location.
var maybeJavaHome = path.dirname(path.dirname(javacPath));
if (fs.existsSync(path.join(maybeJavaHome, 'lib', 'tools.jar'))) {
environment.JAVA_HOME = maybeJavaHome;
} else {
throw new CordovaError(default_java_error_msg);
}
}
} else if (utils.isWindows()) {
const baseDirs = [environment.ProgramFiles, environment['ProgramFiles(x86)']];
const globOpts = { absolute: true, onlyDirectories: true };
const flatMap = (arr, f) => [].concat(...arr.map(f));
const jdkDir = flatMap(baseDirs, cwd => {
return glob.sync('java/jdk*', { cwd, ...globOpts });
}
)[0];
if (jdkDir) {
environment.PATH += path.delimiter + path.join(jdkDir, 'bin');
environment.JAVA_HOME = path.normalize(jdkDir);
}
}
}
javaIsEnsured = true;
}
};
module.exports = java;