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:
Raphael von der Grün 2020-11-19 21:08:43 +01:00 committed by GitHub
parent d5b9029a23
commit 0e8234abfd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 27 deletions

View File

@ -22,6 +22,7 @@ var path = require('path');
var fs = require('fs-extra'); var fs = require('fs-extra');
var os = require('os'); var os = require('os');
var which = require('which'); var which = require('which');
const glob = require('fast-glob');
var REPO_ROOT = path.join(__dirname, '..', '..', '..', '..'); var REPO_ROOT = path.join(__dirname, '..', '..', '..', '..');
var PROJECT_ROOT = path.join(__dirname, '..', '..'); var PROJECT_ROOT = path.join(__dirname, '..', '..');
const { CordovaError, ConfigParser, events } = require('cordova-common'); const { CordovaError, ConfigParser, events } = require('cordova-common');
@ -36,20 +37,6 @@ function forgivingWhichSync (cmd) {
return whichResult === null ? '' : fs.realpathSync(whichResult); 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 () { module.exports.isWindows = function () {
return (os.platform() === 'win32'); return (os.platform() === 'win32');
}; };
@ -189,21 +176,18 @@ module.exports.check_java = function () {
} }
} }
} else if (module.exports.isWindows()) { } else if (module.exports.isWindows()) {
const programFilesEnv = path.resolve(process.env.ProgramFiles); const { env } = process;
const programFiles = 'C:\\Program Files\\'; const baseDirs = [env.ProgramFiles, env['ProgramFiles(x86)']];
const programFilesx86 = 'C:\\Program Files (x86)\\'; const globOpts = { absolute: true, onlyDirectories: true };
const flatMap = (arr, f) => [].concat(...arr.map(f));
let firstJdkDir = const jdkDir = flatMap(baseDirs, cwd =>
getJDKDirectory(programFilesEnv) || glob.sync('java/jdk*', { cwd, ...globOpts })
getJDKDirectory(programFiles) || )[0];
getJDKDirectory(programFilesx86);
if (firstJdkDir) { if (jdkDir) {
firstJdkDir = firstJdkDir.replace(/\//g, path.sep); env.PATH += path.delimiter + path.join(jdkDir, 'bin');
if (!javacPath) { env.JAVA_HOME = path.normalize(jdkDir);
process.env.PATH += path.delimiter + path.join(firstJdkDir, 'bin');
}
process.env.JAVA_HOME = firstJdkDir;
} }
} }
} }

View File

@ -19,6 +19,7 @@
var rewire = require('rewire'); var rewire = require('rewire');
var android_sdk = require('../../bin/templates/cordova/lib/android_sdk'); var android_sdk = require('../../bin/templates/cordova/lib/android_sdk');
var os = require('os');
var fs = require('fs-extra'); var fs = require('fs-extra');
var path = require('path'); var path = require('path');
var events = require('cordova-common').events; var events = require('cordova-common').events;
@ -45,6 +46,38 @@ describe('check_reqs', function () {
}); });
Object.assign(process.env, original_env); 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('check_android', function () {
describe('find and set ANDROID_HOME when ANDROID_HOME and ANDROID_SDK_ROOT is not set', function () { describe('find and set ANDROID_HOME when ANDROID_HOME and ANDROID_SDK_ROOT is not set', function () {
beforeEach(function () { beforeEach(function () {