mirror of
https://github.com/apache/cordova-android.git
synced 2025-01-19 07:02:51 +08:00
refactor(check_reqs): cleanup default Java location detection on Windows (#1102)
* test(check_reqs): test default Java location detection on Windows * refactor(check_reqs): use glob for default Java location detection on Windows This changes the implementation to be closer to what it was before #842 with everything being in one place. * fix: remove always-taken if statement * feat: take both Program Files variants from env * refactor(check_reqs): cosmetic changes
This commit is contained in:
parent
d5b9029a23
commit
0e8234abfd
38
bin/templates/cordova/lib/check_reqs.js
vendored
38
bin/templates/cordova/lib/check_reqs.js
vendored
@ -22,6 +22,7 @@ var path = require('path');
|
||||
var fs = require('fs-extra');
|
||||
var os = require('os');
|
||||
var which = require('which');
|
||||
const glob = require('fast-glob');
|
||||
var REPO_ROOT = path.join(__dirname, '..', '..', '..', '..');
|
||||
var PROJECT_ROOT = path.join(__dirname, '..', '..');
|
||||
const { CordovaError, ConfigParser, events } = require('cordova-common');
|
||||
@ -36,20 +37,6 @@ function forgivingWhichSync (cmd) {
|
||||
return whichResult === null ? '' : fs.realpathSync(whichResult);
|
||||
}
|
||||
|
||||
function getJDKDirectory (directory) {
|
||||
const p = path.resolve(directory, 'java');
|
||||
if (fs.existsSync(p)) {
|
||||
const directories = fs.readdirSync(p);
|
||||
for (let i = 0; i < directories.length; i++) {
|
||||
const dir = directories[i];
|
||||
if (/^(jdk)+./.test(dir)) {
|
||||
return path.resolve(directory, 'java', dir);
|
||||
}
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
module.exports.isWindows = function () {
|
||||
return (os.platform() === 'win32');
|
||||
};
|
||||
@ -189,21 +176,18 @@ module.exports.check_java = function () {
|
||||
}
|
||||
}
|
||||
} else if (module.exports.isWindows()) {
|
||||
const programFilesEnv = path.resolve(process.env.ProgramFiles);
|
||||
const programFiles = 'C:\\Program Files\\';
|
||||
const programFilesx86 = 'C:\\Program Files (x86)\\';
|
||||
const { env } = process;
|
||||
const baseDirs = [env.ProgramFiles, env['ProgramFiles(x86)']];
|
||||
const globOpts = { absolute: true, onlyDirectories: true };
|
||||
const flatMap = (arr, f) => [].concat(...arr.map(f));
|
||||
|
||||
let firstJdkDir =
|
||||
getJDKDirectory(programFilesEnv) ||
|
||||
getJDKDirectory(programFiles) ||
|
||||
getJDKDirectory(programFilesx86);
|
||||
const jdkDir = flatMap(baseDirs, cwd =>
|
||||
glob.sync('java/jdk*', { cwd, ...globOpts })
|
||||
)[0];
|
||||
|
||||
if (firstJdkDir) {
|
||||
firstJdkDir = firstJdkDir.replace(/\//g, path.sep);
|
||||
if (!javacPath) {
|
||||
process.env.PATH += path.delimiter + path.join(firstJdkDir, 'bin');
|
||||
}
|
||||
process.env.JAVA_HOME = firstJdkDir;
|
||||
if (jdkDir) {
|
||||
env.PATH += path.delimiter + path.join(jdkDir, 'bin');
|
||||
env.JAVA_HOME = path.normalize(jdkDir);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -19,6 +19,7 @@
|
||||
|
||||
var rewire = require('rewire');
|
||||
var android_sdk = require('../../bin/templates/cordova/lib/android_sdk');
|
||||
var os = require('os');
|
||||
var fs = require('fs-extra');
|
||||
var path = require('path');
|
||||
var events = require('cordova-common').events;
|
||||
@ -45,6 +46,38 @@ describe('check_reqs', function () {
|
||||
});
|
||||
Object.assign(process.env, original_env);
|
||||
});
|
||||
|
||||
describe('check_java', () => {
|
||||
let tmpDir;
|
||||
beforeEach(() => {
|
||||
const tmpDirTemplate = path.join(os.tmpdir(), 'cordova-android-test-');
|
||||
tmpDir = fs.realpathSync(fs.mkdtempSync(tmpDirTemplate));
|
||||
});
|
||||
afterEach(() => {
|
||||
fs.removeSync(tmpDir);
|
||||
});
|
||||
|
||||
it('detects JDK in default location on windows', async () => {
|
||||
check_reqs.isWindows = () => true;
|
||||
check_reqs.__set__({
|
||||
execa: async () => ({}),
|
||||
forgivingWhichSync: () => ''
|
||||
});
|
||||
|
||||
delete process.env.JAVA_HOME;
|
||||
process.env.ProgramFiles = tmpDir;
|
||||
|
||||
const jdkDir = path.join(tmpDir, 'java/jdk1.6.0_02');
|
||||
fs.ensureDirSync(jdkDir);
|
||||
|
||||
await check_reqs.check_java();
|
||||
|
||||
expect(process.env.JAVA_HOME).toBe(jdkDir);
|
||||
expect(process.env.PATH.split(path.delimiter))
|
||||
.toContain(path.join(jdkDir, 'bin'));
|
||||
});
|
||||
});
|
||||
|
||||
describe('check_android', function () {
|
||||
describe('find and set ANDROID_HOME when ANDROID_HOME and ANDROID_SDK_ROOT is not set', function () {
|
||||
beforeEach(function () {
|
||||
|
Loading…
Reference in New Issue
Block a user