raphinesse commented on a change in pull request #1130:
URL: https://github.com/apache/cordova-android/pull/1130#discussion_r528329004



##########
File path: bin/templates/cordova/lib/Java.js
##########
@@ -0,0 +1,157 @@
+/*
+       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');
+
+let javaIsEnsured = false;
+
+module.exports = class Java {

Review comment:
       I would prefer to just export a plain object, as that class has only 
static members. Just keep it simple.

##########
File path: bin/templates/cordova/lib/check_reqs.js
##########
@@ -143,72 +134,23 @@ module.exports.check_gradle = function () {
                             'in your path, or install Android Studio'));
 };
 
-// Returns a promise.
-module.exports.check_java = function () {
-    var javacPath = forgivingWhichSync('javac');
-    var hasJavaHome = !!process.env.JAVA_HOME;
-    return Promise.resolve().then(function () {
-        if (hasJavaHome) {
-            // Windows java installer doesn't add javac to PATH, nor set 
JAVA_HOME (ugh).
-            if (!javacPath) {
-                process.env.PATH += path.delimiter + 
path.join(process.env.JAVA_HOME, 'bin');
-            }
-        } else {
-            if (javacPath) {
-                // OS X has a command for finding JAVA_HOME.
-                var find_java = '/usr/libexec/java_home';
-                var default_java_error_msg = 'Failed to find \'JAVA_HOME\' 
environment variable. Try setting it manually.';
-                if (fs.existsSync(find_java)) {
-                    return execa(find_java).then(({ stdout }) => {
-                        process.env.JAVA_HOME = stdout;
-                    }).catch(function (err) {
-                        if (err) {
-                            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'))) {
-                        process.env.JAVA_HOME = maybeJavaHome;
-                    } else {
-                        throw new CordovaError(default_java_error_msg);
-                    }
-                }
-            } else if (module.exports.isWindows()) {
-                const { env } = process;
-                const baseDirs = [env.ProgramFiles, env['ProgramFiles(x86)']];
-                const globOpts = { absolute: true, onlyDirectories: true };
-                const flatMap = (arr, f) => [].concat(...arr.map(f));
-
-                const jdkDir = flatMap(baseDirs, cwd =>
-                    glob.sync('java/jdk*', { cwd, ...globOpts })
-                )[0];
-
-                if (jdkDir) {
-                    env.PATH += path.delimiter + path.join(jdkDir, 'bin');
-                    env.JAVA_HOME = path.normalize(jdkDir);
-                }
-            }
-        }
-    }).then(function () {
-        return execa('javac', ['-version'], { all: true })
-            .then(({ all: output }) => {
-                // Java <= 8 writes version info to stderr, Java >= 9 to stdout
-                const match = /javac\s+([\d.]+)/i.exec(output);
-                return match && match[1];
-            }, () => {
-                var 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);
-            });
-    });
+/**
+ * Checks for the java installation and correct version
+ */
+module.exports.check_java = async function () {
+    const EXPECTED_JAVA_MAJOR = 1;
+    const EXPECTED_JAVA_MINOR = 8;
+
+    const javaVersion = await Java.getVersion();
+
+    if (javaVersion.major < EXPECTED_JAVA_MAJOR || javaVersion.minor < 
EXPECTED_JAVA_MINOR) {

Review comment:
       If we use `semver`, we can simplify this to 
   ```suggestion
       if (!semver.satisfies(javaVersion, '>= 1.8')) {
   ```

##########
File path: bin/templates/cordova/lib/Java.js
##########
@@ -0,0 +1,157 @@
+/*
+       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');
+
+let javaIsEnsured = false;
+
+module.exports = class Java {
+    /**
+     * Gets the version from the javac executable.
+     * @returns {Object}
+     * {
+     *      major: number
+     *      minor: number
+     *      patch: number
+     *      build: number
+     * }
+     */
+    static async getVersion () {
+        await Java._ensure();
+
+        // Java <= 8 writes version info to stderr, Java >= 9 to stdout
+        let version = null;
+        try {
+            version = (await execa('javac', ['-version'], { all: true })).all;
+        } 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 Java._parseVersion(version);

Review comment:
       I would greatly prefer not to version parsing ourselves here:
   ```suggestion
           return semver.coerce(version);
   ```
   We already depend on semver for parts of our tooling, so no harm in using it 
here as well.

##########
File path: spec/unit/check_reqs.spec.js
##########
@@ -58,11 +59,8 @@ describe('check_reqs', function () {
         });
 
         it('detects JDK in default location on windows', async () => {

Review comment:
       This test should be moved to Java.spec.js, since the tested 
functionality is now implemented in Java.js




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to