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(lib/run): simplify code to determine install/run target #1082

Closed
wants to merge 7 commits into from

Conversation

raphinesse
Copy link
Contributor

Motivation and Context

This change simplifies the code that determines on which target (device or emulator) the app should be installed and run on.

This refactoring was triggered during a bigger refactoring (still WIP) that has the goal of doing something similar to apache/cordova-electron#142 here.

Description

The changes made here should not change the observable behavior of the code beyond changing what is logged to the console. The changes included are:

  • factor out resolveInstallTarget from lib/run.run
  • improve unit tests for the lib/run module
  • convert resolveInstallTarget to async/await
  • DRY logic in resolveInstallTarget

The logic for selecting a install/run target is unchanged. Targets are tried to resolve in this order:

  • A connected device matching the given ID (if target ID given)
  • Any connected device (if --emulator option was not given)
  • A started emulator matching the given ID (if target ID given)
  • An existing emulator matching the given ID (will be started) (if target ID given)
  • An existing emulator that best matches the app's target API (will be started) (if --device option was not given)

Testing

The unit tests for this module still pass.

@raphinesse raphinesse added this to the 9.0.1 milestone Oct 5, 2020
@raphinesse raphinesse requested review from erisu and breautek October 5, 2020 08:12
@breautek
Copy link
Contributor

breautek commented Oct 6, 2020

When on this PR, it doesn't appear that the CLI properly detects of the emulator is already running and fails to redeploy the app on the cordova run command on the running emulator.

BUILD SUCCESSFUL in 8s
41 actionable tasks: 5 executed, 36 up-to-date
Built the following apk(s): 
        /development/totalpave/tp-issue-tracker/platforms/android/app/build/outputs/apk/debug/app-debug.apk
Checking Java JDK and Android SDK versions
ANDROID_SDK_ROOT=/development/Android/Sdk (recommended setting)
ANDROID_HOME=/development/Android/Sdk (DEPRECATED)
Using Android SDK: /development/Android/Sdk
No emulator specified, defaulting to Pixel_API_29
Waiting for emulator to start...
emulator: ERROR: Running multiple emulators with the same AVD is an experimental feature.
Please use -read-only flag to enable this feature.

Prompt waits for the emulator to start when it's already running.

@raphinesse raphinesse marked this pull request as draft October 6, 2020 07:10
@raphinesse
Copy link
Contributor Author

Thanks for the thorough check @breautek. I'll look into this when I find the time. I converted this to a draft until then.

If the `lookHarder` option is true and the initial `adb devices` run
returned no devices, `device.list` will kill the adb server and try
again.

Unfortunately `adb devices` contains incorrect information when it is
run too soon after the adb server has been restarted. Consider below
commands being executed with one running emulator:

    adb devices && killall adb && adb devices && adb devices

While the first invocation of `adb devices` lists the emulator as
running, the second and third one will list it as offline. This is
especially problematic if code contains a sequence such as:

    devs = await device.list(true);
    emus = await emulator.list();

In that case the third invocation of `adb devices` occurs in
`emulator.list()` but because of the `killall` it lists all emulators as
offline, causing `emus` to be empty.

This commit tries to work around that issue by explicitly restarting the
adb server after it was killed, and waiting for 100 ms after that. Local
experimentation has shown this to be sufficient in all instances. YMMV
of course.
@codecov-io
Copy link

codecov-io commented Oct 17, 2020

Codecov Report

Merging #1082 into master will decrease coverage by 0.14%.
The diff coverage is 93.10%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1082      +/-   ##
==========================================
- Coverage   69.83%   69.69%   -0.15%     
==========================================
  Files          20       20              
  Lines        1810     1808       -2     
==========================================
- Hits         1264     1260       -4     
- Misses        546      548       +2     
Impacted Files Coverage Δ
bin/templates/cordova/lib/run.js 96.82% <92.00%> (-3.18%) ⬇️
bin/templates/cordova/lib/device.js 100.00% <100.00%> (ø)
bin/templates/cordova/lib/retry.js 100.00% <0.00%> (ø)

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 5d3591b...dd6ffd9. Read the comment docs.

@raphinesse
Copy link
Contributor Author

Closing in favor of #1101

@raphinesse raphinesse closed this Oct 19, 2020
@raphinesse raphinesse deleted the run-refactor branch October 19, 2020 21:36
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.

3 participants