From 0e8234abfd6157896a6af3badb59b80fe4121c82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Raphael=20von=20der=20Gr=C3=BCn?= Date: Thu, 19 Nov 2020 21:08:43 +0100 Subject: [PATCH] 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 --- bin/templates/cordova/lib/check_reqs.js | 38 +++++++------------------ spec/unit/check_reqs.spec.js | 33 +++++++++++++++++++++ 2 files changed, 44 insertions(+), 27 deletions(-) diff --git a/bin/templates/cordova/lib/check_reqs.js b/bin/templates/cordova/lib/check_reqs.js index d16b8740..10d660e9 100644 --- a/bin/templates/cordova/lib/check_reqs.js +++ b/bin/templates/cordova/lib/check_reqs.js @@ -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); } } } diff --git a/spec/unit/check_reqs.spec.js b/spec/unit/check_reqs.spec.js index e5531cc1..491bd430 100644 --- a/spec/unit/check_reqs.spec.js +++ b/spec/unit/check_reqs.spec.js @@ -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 () {