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(ADB): improve internal API #757

Draft
wants to merge 33 commits into
base: main
Choose a base branch
from
Draft

refactor(ADB): improve internal API #757

wants to merge 33 commits into from

Conversation

Rudxain
Copy link
Member

@Rudxain Rudxain commented Dec 14, 2024

The goal is to make ADB commands:

I am sorry to whoever is going to review this 😅. I didn't want the diff to be so big 💀

@Rudxain Rudxain added documentation Improvements or additions to documentation refactor Rework of code — behaviour doesn't change labels Dec 14, 2024
@Rudxain Rudxain self-assigned this Dec 14, 2024
@Rudxain Rudxain changed the title Refactor/adb api refactor(ADB): improve internal API Dec 14, 2024
Copy link
Contributor

deepsource-io bot commented Dec 14, 2024

Here's the code health analysis summary for commits 1ed216a..fc72efc. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource Rust LogoRust❌ Failure
❗ 13 occurences introduced
🎯 15 occurences resolved
View Check ↗
DeepSource Test coverage LogoTest coverage⚠️ Artifact not reportedTimed out: Artifact was never reportedView Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

Copy link
Contributor

@adhirajsinghchauhan adhirajsinghchauhan left a comment

Choose a reason for hiding this comment

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

Overall, there's only one or two good changes here (e.g. explicit serial, deduping android_sdk >= 21). Most of the other ones are subjective, so they may not be beneficial to others. It is my opinion that changes for the sake of changes should not be done.

I am not a fan of shortening variable names, as none of them were long enough that they needed abbreviation in the first place. For example, command => cmd, packages => packs, shell => sh is wholly unnecessary and adds noise to this PR. It's also not done consistently, as sometimes we see the full word being used (packages), and in other places two different short-forms. (e.g. packs and pack)

Even the renaming of Phone to Device was not necessary, especially when considering it's unrelated to this PR, and that some variables are named device, some dev, and some remain as phone.

Forgive the bluntness, but please revert any such subjective changes. We can discuss the specifics on Discord.

Typing in natural language form is more efficient for the developer. When we think of typing "command", we expect to start typing it and have autocomplete suggest it as-is. It will not surface "cmd", so we'd need to remember to type abbreviations everywhere, only to realise that abbreviations are not consistently used. This extra step is at odds with the basic methodology of good, clean code for everyone.

@@ -137,7 +138,7 @@ impl Settings {
None => {
self.device = DeviceSettings {
device_id: phone.adb_id.clone(),
multi_user_mode: phone.android_sdk > 21,
multi_user_mode: supports_multi_user(phone),
Copy link
Contributor

Choose a reason for hiding this comment

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

Potential bug: the function checks >=; while the previous code was simply >. The official Android API to check for this was added in API 24; however, before this it was manufacturer-dependent. IIRC multiple users were possible even before API 21, via ADB (pm create-user). Not sure if it was exposed in a user-facing way.

See https://source.android.com/docs/devices/admin/multi-user#applying_the_overlay:

The multi-user feature is disabled by default. To enable the feature, device manufacturers must define […]

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep, I noticed some inconsistencies, so I tried to find docs about which API level should be checked, and I defaulted to Android 5.0

src/core/sync.rs Outdated
pub async fn perform_adb_commands(
/// Runs a shell command on the device.
/// See [`adb_cmd`] for details.
pub async fn adb_sh_cmd<S: AsRef<str>>(
Copy link
Contributor

Choose a reason for hiding this comment

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

Not a fan of shortening this, and other definitions. It does not help readability; in fact, it makes it more ambiguous at times.

For example, while we obviously know sh => shell and cmd => command, others may assume /bin/sh or Windows CMD, leading to confusion: does this function only work over sh and cmd, or is it a generic platform-agnostic abstraction?

src/core/sync.rs Outdated

/// `pm list packages` flag/state/type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PmLsPackFlag {
Copy link
Contributor

Choose a reason for hiding this comment

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

Enums are for our benefit, so we should use full, descriptive names here. Please change to:

  • IncludeUninstalled
  • OnlyEnabled
  • OnlyDisabled

For completeness, we could also add entries for all other possible flags (f, d, s, 3, i) (docs). Unused code should be stripped out of the final release binary anyway.

Copy link
Member Author

Choose a reason for hiding this comment

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

we should use full, descriptive names

I agree. Single-letter is bad. And it even looks ugly because of the relative length asymmetry with the enum name.

I tried to mirror the ADB CLI, but I've already deviated from it when I renamed list_users to ls_users, so I guess it doesn't hurt to improve the flag names.

For completeness, we could also add entries for all other possible flags (f, d, s, 3, i) (docs).

3 and s should probably be on a distinct enum, as they are a different kind of filter. Same for i and f.

About completeness, what about #[non_exhaustive] 👀 ? That way, we can easily add the other flags if we need them

@Rudxain
Copy link
Member Author

Rudxain commented Dec 16, 2024

Most of the other ones are subjective, so they may not be beneficial to others. It is my opinion that changes for the sake of changes should not be done.

I agree with the sentiment, as major refactors could introduce bugs. I tried my best to document all possible places where I could've created one.

I am not a fan of shortening variable names, as none of them were long enough that they needed abbreviation in the first place. For example, command => cmd, packages => packs, shell => sh is wholly unnecessary and adds noise to this PR

yeah 😅, I'll revert that.

Even the renaming of Phone to Device was not necessary, especially when considering it's unrelated to this PR, and that some variables are named device, some dev, and some remain as phone.

I'll revert the rename and defer that to its own PR. I did too much "opportunistic refactoring" (mostly just "opportunistic renaming", which is not very valuable)

Forgive the bluntness, but please revert any such subjective changes. We can discuss the specifics on Discord.

Ok 😅 🙁, I'll try to revert all out-of-scope changes. I can't do it today. I'll have more time on the weekend.

at odds with the basic methodology of good, clean code for everyone.

Agreed. One of the motivations I had in mind for this PR is to make the code-base more accessible to newcomers (#731), but the strongly context-bound names might go against that goal

@Rudxain
Copy link
Member Author

Rudxain commented Dec 26, 2024

This week, I've been busier than I expected, so I couldn't work on this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation refactor Rework of code — behaviour doesn't change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants