Skip to content

feat: add fallback behavior when running a default simulator on iOS #758

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

Merged
merged 7 commits into from
Oct 5, 2019

Conversation

Naturalclar
Copy link
Member

Summary:

This is an attempt to solve problem on #739, based on idea provided at #739 (comment)

This changes the behavior of runIOS when no simulator argument is provided.

When no simulator argument is provided, the cli will look for simulators in the following order

  • iPhone 11
  • iPhone X
  • iPhone 8

Test Plan:

yarn
yarn test
yarn lint

Verified

This commit was signed with the committer’s verified signature.
nbbeeken Neal Beeken

Verified

This commit was signed with the committer’s verified signature.
nbbeeken Neal Beeken
@Naturalclar Naturalclar changed the title Fix/issue739 feature: try for different iOS simulators when no simulator argument is provided Sep 29, 2019
Copy link
Member

@thymikee thymikee left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this! Looks good, left some feedback

@@ -492,7 +507,7 @@ export default {
description:
'Explicitly set simulator to use. Optionally include iOS version between' +
'parenthesis at the end to match an exact version: "iPhone 6 (10.0)"',
default: 'iPhone X',
default: '',
Copy link
Member

Choose a reason for hiding this comment

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

I think we can default to undefined (or just don't provide the default) – can we change that and adjust docs as well?

: 'iPhone 8';
}

const selectedSimulator = findMatchingSimulator(simulators, simulator);
Copy link
Member

Choose a reason for hiding this comment

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

this is calling findMatchingSimulator twice for iPhone 11 and iPhone X. Can we move this call to line 143 (iPhone 8) and get rid of selectedSimulator variable?

Verified

This commit was signed with the committer’s verified signature.
nbbeeken Neal Beeken

Verified

This commit was signed with the committer’s verified signature.
nbbeeken Neal Beeken
@Naturalclar
Copy link
Member Author

Thanks for the feedback! I've made some modification to reduce the number of times findMatchingSimulator run

Copy link
Member

@grabbou grabbou left a comment

Choose a reason for hiding this comment

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

Thanks for sending this over! I think this is a really good idea. Left some feedback as well, on top of what @thymikee already wrote.

* - iPhone X
* - iPhone 8
*/
if (!simulator) {
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if this could be replaced with a simple while loop or a for loop and break statement. Thoughts?

Copy link
Member Author

Choose a reason for hiding this comment

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

thanks for the suggestion!
I modified it to use array.some so that loop would break when simulator is found.

Copy link
Member

Choose a reason for hiding this comment

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

That is a good improvement, but still, I find doing side effects inside some a bit of a code smell.

How about:

for (const defaultSimulator of defaultSimulators) {
  selectedSimulator = findMatchingSimulator(simulators, defaultSimulator);
  if (selectedSimulator) {
     break;
  }
}

or

const selectedSimulator = args.simulator 
   ? findMatchingSimulator(simulators, args.simulator)
   : defaultSimulators.reduce(
       (selectedSimulator, simulator) => selectedSimulator || findMatchingSimulator(simulators, simulator),
       null
     );

The below (in my opinion) would be the most concise:

const selectedSimulator = defaultSimulators.reduce(
   (selectedSimulator, simulator) => selectedSimulator || findMatchingSimulator(simulators, simulator),
   findMatchingSimulator(simulators, args.simulator)
);

but it would require we bring: args.simulator default value to be iPhone 11 and rename defaultSimulators to be fallbackSimulators (in other words, try these ones in case user provided/default value does not exist on the machine).

Leaving that up to further discussion.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for the feedback and suggestions!
I find doing side effects inside some feels iffy as well, but was not able to come up with a solution at the moment.

I like the last suggestion of providing fallback when args.simulator is not available. It let me remove multiple if s.

I've modified the default simulator to be iPhone 11 and modified the document to match the code.

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@Naturalclar Naturalclar requested a review from grabbou October 2, 2019 06:55
grabbou
grabbou previously requested changes Oct 3, 2019
}
} else {
selectedSimulator = findMatchingSimulator(simulators, simulator);
if (!selectedSimulator) {
Copy link
Member

Choose a reason for hiding this comment

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

I believe we can move this and another if statement at line 152 outside of this block. If we would place it below line 163, we would be able to keep only one if statement, instead of the two.

* - iPhone X
* - iPhone 8
*/
if (!simulator) {
Copy link
Member

Choose a reason for hiding this comment

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

That is a good improvement, but still, I find doing side effects inside some a bit of a code smell.

How about:

for (const defaultSimulator of defaultSimulators) {
  selectedSimulator = findMatchingSimulator(simulators, defaultSimulator);
  if (selectedSimulator) {
     break;
  }
}

or

const selectedSimulator = args.simulator 
   ? findMatchingSimulator(simulators, args.simulator)
   : defaultSimulators.reduce(
       (selectedSimulator, simulator) => selectedSimulator || findMatchingSimulator(simulators, simulator),
       null
     );

The below (in my opinion) would be the most concise:

const selectedSimulator = defaultSimulators.reduce(
   (selectedSimulator, simulator) => selectedSimulator || findMatchingSimulator(simulators, simulator),
   findMatchingSimulator(simulators, args.simulator)
);

but it would require we bring: args.simulator default value to be iPhone 11 and rename defaultSimulators to be fallbackSimulators (in other words, try these ones in case user provided/default value does not exist on the machine).

Leaving that up to further discussion.

@Naturalclar Naturalclar requested a review from grabbou October 4, 2019 02:46
Copy link
Member

@thymikee thymikee left a comment

Choose a reason for hiding this comment

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

Let's ship it

const fallbackSimulators = ['iPhone X', 'iPhone 8'];
const selectedSimulator = fallbackSimulators.reduce((simulator, fallback) => {
return simulator || findMatchingSimulator(simulators, fallback);
}, findMatchingSimulator(simulators, args.simulator));
Copy link
Member

Choose a reason for hiding this comment

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

This loop always run through all simulators from fallbackSimulators, but since the data set is small and we avoid running findMatchingSimulator if necessary, I'm fine with that.

@thymikee thymikee dismissed grabbou’s stale review October 5, 2019 20:15

feedback addressed

@thymikee thymikee changed the title feature: try for different iOS simulators when no simulator argument is provided feat: add fallback behavior when running a default simulator on iOS Oct 5, 2019
@thymikee thymikee merged commit 77af493 into react-native-community:master Oct 5, 2019
@sonicdoe sonicdoe mentioned this pull request Oct 9, 2019
thymikee pushed a commit that referenced this pull request Oct 11, 2019
Co-authored-by: Jakob Krigovsky <jakob@krigovsky.com>
@sonicdoe sonicdoe mentioned this pull request Oct 11, 2019
thymikee pushed a commit that referenced this pull request Oct 11, 2019
… (#782)

Co-authored-by: Jakob Krigovsky <jakob@krigovsky.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants