Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: java checks / get java version #1130

Merged
merged 1 commit into from
Mar 27, 2021

Conversation

breautek
Copy link
Contributor

Platforms affected

Android

Motivation and Context

Prerequisite PR for #1060

This PR abstracts the Java checks and java version retrieval so that it can be reused.

Description

Created a new Java class with static methods that provides an API to get the version. It also contains the code that finds the java executable that used to live inside check_reqs.

Java.js is contains 100% line coverage, which is an upgrade from when the code lived in check_reqs.

Testing

npm test

Checklist

  • I've run the tests to see all new and existing tests pass
  • I added automated test coverage as appropriate for this change
  • Commit is prefixed with (platform) if this change only applies to one platform (e.g. (android))
  • If this Pull Request resolves an issue, I linked to the issue in the text above (and used the correct keyword to close issues using keywords)
  • I've updated the documentation if necessary

@breautek breautek changed the title refactor: java checks refactor: java checks / get java version Nov 22, 2020
bin/templates/cordova/lib/Java.js Outdated Show resolved Hide resolved
bin/templates/cordova/lib/Java.js Outdated Show resolved Hide resolved
Copy link
Member

@erisu erisu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really want the name of the file as Java.js and not java.js? I believe in almost every repo our filenames are lowercased.

Since this is introducing a new coding style, I think you should document when to use uppercase vs. lowercase and put a document somewhere like the cordova-contribute so we can understand these new cases.

spec/unit/Java.spec.js Outdated Show resolved Hide resolved
@raphinesse
Copy link
Contributor

raphinesse commented Nov 22, 2020

Do we really want the name of the file as Java.js and not java.js? I believe in almost every repo our filenames are lowercased.

We actually have quite a few modules with non-lowercase filenames in this repo that exactly match the exported class' name: AndroidManifest.js, AndroidProject.js, ...

That being said, if we change this module from a class to a plain object, java.js would be fine with me as well. In fact, maybe the name could be more descriptive yet. Something like env/java.js, for example... 🤷‍♂️

@codecov-io
Copy link

codecov-io commented Nov 22, 2020

Codecov Report

Merging #1130 (c645ca3) into master (3081e5e) will increase coverage by 1.46%.
The diff coverage is 100.00%.

❗ Current head c645ca3 differs from pull request most recent head a3d2f6c. Consider uploading reports for the commit a3d2f6c to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1130      +/-   ##
==========================================
+ Coverage   71.80%   73.27%   +1.46%     
==========================================
  Files          21       22       +1     
  Lines        1745     1766      +21     
==========================================
+ Hits         1253     1294      +41     
+ Misses        492      472      -20     
Impacted Files Coverage Δ
bin/templates/cordova/lib/check_reqs.js 75.73% <100.00%> (+5.49%) ⬆️
bin/templates/cordova/lib/env/java.js 100.00% <100.00%> (ø)
bin/templates/cordova/lib/utils.js 95.23% <100.00%> (+2.38%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 3081e5e...a3d2f6c. Read the comment docs.

Copy link
Contributor

@raphinesse raphinesse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great to see improvements to the check_reqs module! Especially the separation of environment checking and inference is something I thought should have been done for a long time.

I made a quick pass over the code, left a few comments and pushed a fix for one of the new tests. Overall, this looks quite good to me. I hope I can take a closer look at it soon.

bin/templates/cordova/lib/Java.js Outdated Show resolved Hide resolved
bin/templates/cordova/lib/Java.js Outdated Show resolved Hide resolved
bin/templates/cordova/lib/check_reqs.js Outdated Show resolved Hide resolved
spec/unit/check_reqs.spec.js Outdated Show resolved Hide resolved
Copy link
Contributor

@raphinesse raphinesse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some more comments. I hope I didn't go overboard 😅

bin/templates/cordova/lib/Java.js Outdated Show resolved Hide resolved
bin/templates/cordova/lib/Java.js Outdated Show resolved Hide resolved
bin/templates/cordova/lib/Java.js Outdated Show resolved Hide resolved
spec/unit/Java.spec.js Outdated Show resolved Hide resolved
spec/unit/Java.spec.js Outdated Show resolved Hide resolved
spec/unit/Java.spec.js Outdated Show resolved Hide resolved
spec/unit/Java.spec.js Outdated Show resolved Hide resolved
spec/unit/Java.spec.js Outdated Show resolved Hide resolved
@breautek
Copy link
Contributor Author

That being said, if we change this module from a class to a plain object, java.js would be fine with me as well. In fact, maybe the name could be more descriptive yet. Something like env/java.js, for example...

Coding habits from my own projects. I can convert this into a simple JS object and move the file to an env/ folder. We could follow a similar pattern going forward with android studio, adb, bundle tools, etc...

I would want to keep the module exporting an object containing methods though as this makes it easy to unit test/spy functions without the use of rewire, and I personally want to try to limit the use of rewire as much as possible since it's not a package that is actually maintained.

@breautek breautek changed the title refactor: java checks / get java version WIP: refactor: java checks / get java version Jan 16, 2021
@breautek breautek marked this pull request as draft January 16, 2021 21:41
@breautek breautek changed the title WIP: refactor: java checks / get java version refactor: java checks / get java version Jan 17, 2021
@breautek breautek marked this pull request as ready for review January 17, 2021 02:03
Copy link
Contributor

@raphinesse raphinesse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR looks pretty good to me 👍

My suggestions and comments are mainly cosmetic. Feel free to implement or discard those as you see fit.

The few non-cosmetic remarks are not a deal breaker either for me. So if there's no time to address them, that's fine with me too.

Sorry for taking so long with the review but I'm pretty swamped with work currently.

bin/templates/cordova/lib/check_reqs.js Outdated Show resolved Hide resolved
bin/templates/cordova/lib/check_reqs.js Outdated Show resolved Hide resolved
bin/templates/cordova/lib/check_reqs.js Outdated Show resolved Hide resolved
bin/templates/cordova/lib/env/java.js Show resolved Hide resolved
bin/templates/cordova/lib/utils.js Outdated Show resolved Hide resolved
spec/unit/check_reqs.spec.js Outdated Show resolved Hide resolved
spec/unit/check_reqs.spec.js Outdated Show resolved Hide resolved
spec/unit/java.spec.js Outdated Show resolved Hide resolved
spec/unit/java.spec.js Outdated Show resolved Hide resolved
spec/unit/java.spec.js Show resolved Hide resolved
Co-authored-by: エリス <erisu@users.noreply.github.com>
Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

Update spec/unit/java.spec.js

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

Update spec/unit/java.spec.js

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

Update bin/templates/cordova/lib/utils.js

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

Update bin/templates/cordova/lib/check_reqs.js

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

Update spec/unit/check_reqs.spec.js

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

Update spec/unit/check_reqs.spec.js

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>
@breautek
Copy link
Contributor Author

@erisu @raphinesse

Finally got around into finishing this up, I believe this could be incorporated into 9.1.x release, or do you think it should wait until 10.x?

Copy link
Member

@erisu erisu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO, I think this can be released with the 9.1.0 minor release.

@breautek breautek added this to the 9.1.0 milestone Mar 27, 2021
@breautek breautek merged commit 774de78 into apache:master Mar 27, 2021
@breautek breautek deleted the refactor-java-checks branch March 27, 2021 13:05
@DavidStrausz
Copy link
Contributor

Hi @breautek, I hope it's okay if I add this here, but I can also open a new issue if you need one.
This causes the requirements check to fail on macOS Big Sur 11.2.3 if custom java options (_JAVA_OPTIONS) are set.

For reference in my case I set them in .zshrc like this:

export _JAVA_OPTIONS="-Xms2048M -Xmx4096M"

And this is the exact error:

Checking Java JDK and Android SDK versions
ANDROID_SDK_ROOT=/Users/username/Library/Android/sdk (recommended setting)
ANDROID_HOME=undefined (DEPRECATED)
Requirements check failed for JDK 1.8.x! Detected version: 2048.0.0
Check your ANDROID_SDK_ROOT / JAVA_HOME / PATH environment variables.

The reason for this is happening is because in the new implementation you are using javac -version to get the version, this prints the java options before printing the version like this:

Picked up _JAVA_OPTIONS: -Xms2048M -Xmx4096M
javac 1.8.0_271

The result is directly checked via semver without sanitizing the output first. In the old implementation a regular expression was used to only check the relevant parts:

const match = /javac\s+([\d.]+)/i.exec(output);

https://github.com/apache/cordova-android/pull/1130/files#diff-0d1afa0d5c88bdb18fbc6ab9ef7fc4bbe10f7047d387bb2964932dbb6eb8c4b7L198

@raphinesse
Copy link
Contributor

@DavidStrausz thanks for letting us know. Would you care to try if this problem can be resolved by adding the options env: {}, extendEnv: false to the execa call in getVersion?

If so, we surely would appreciate a pull request with that change.

@DavidStrausz
Copy link
Contributor

@raphinesse Thanks for your response! Interesting, didn't think of that way of fixing it yet, I will take a look sometime next week and will open a PR if this indeed resolves the issue!
If it does not work, I will readd the regex in my PR.

wedgberto pushed a commit to wedgberto/cordova-android that referenced this pull request May 17, 2022
Co-authored-by: エリス <erisu@users.noreply.github.com>
Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

Update spec/unit/java.spec.js

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

Update spec/unit/java.spec.js

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

Update bin/templates/cordova/lib/utils.js

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

Update bin/templates/cordova/lib/check_reqs.js

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

Update spec/unit/check_reqs.spec.js

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

Update spec/unit/check_reqs.spec.js

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>

Co-authored-by: Raphael von der Grün <raphinesse@gmail.com>
wedgberto pushed a commit to wedgberto/cordova-android that referenced this pull request May 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants