-
Notifications
You must be signed in to change notification settings - Fork 932
refactor(cli-platform-apple): move installing and launching app logic to separate function #2234
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
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/cli-platform-apple/src/commands/runCommand/getBuildSettings.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import {CLIError, logger} from '@react-native-community/cli-tools'; | ||
| import {IOSProjectInfo} from '@react-native-community/cli-types'; | ||
| import chalk from 'chalk'; | ||
| import child_process from 'child_process'; | ||
|
|
||
| export async function getBuildSettings( | ||
| xcodeProject: IOSProjectInfo, | ||
| mode: string, | ||
| buildOutput: string, | ||
| scheme: string, | ||
| target?: string, | ||
| ) { | ||
| const buildSettings = child_process.execFileSync( | ||
| 'xcodebuild', | ||
| [ | ||
| xcodeProject.isWorkspace ? '-workspace' : '-project', | ||
| xcodeProject.name, | ||
| '-scheme', | ||
| scheme, | ||
| '-sdk', | ||
| getPlatformName(buildOutput), | ||
| '-configuration', | ||
| mode, | ||
| '-showBuildSettings', | ||
| '-json', | ||
| ], | ||
| {encoding: 'utf8'}, | ||
| ); | ||
|
|
||
| const settings = JSON.parse(buildSettings); | ||
szymonrybczak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| const targets = settings.map( | ||
| ({target: settingsTarget}: any) => settingsTarget, | ||
szymonrybczak marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| let selectedTarget = targets[0]; | ||
|
|
||
| if (target) { | ||
| if (!targets.includes(target)) { | ||
| logger.info( | ||
| `Target ${chalk.bold(target)} not found for scheme ${chalk.bold( | ||
| scheme, | ||
| )}, automatically selected target ${chalk.bold(selectedTarget)}`, | ||
| ); | ||
| } else { | ||
| selectedTarget = target; | ||
| } | ||
| } | ||
|
|
||
| // Find app in all building settings - look for WRAPPER_EXTENSION: 'app', | ||
| const targetIndex = targets.indexOf(selectedTarget); | ||
| const targetSettings = settings[targetIndex].buildSettings; | ||
|
|
||
| const wrapperExtension = targetSettings.WRAPPER_EXTENSION; | ||
|
|
||
| if (wrapperExtension === 'app') { | ||
| return settings[targetIndex].buildSettings; | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function getPlatformName(buildOutput: string) { | ||
| // Xcode can sometimes escape `=` with a backslash or put the value in quotes | ||
| const platformNameMatch = /export PLATFORM_NAME\\?="?(\w+)"?$/m.exec( | ||
| buildOutput, | ||
| ); | ||
| if (!platformNameMatch) { | ||
| throw new CLIError( | ||
| 'Couldn\'t find "PLATFORM_NAME" variable in xcodebuild output. Please report this issue and run your project with Xcode instead.', | ||
| ); | ||
| } | ||
| return platformNameMatch[1]; | ||
| } | ||
101 changes: 101 additions & 0 deletions
101
packages/cli-platform-apple/src/commands/runCommand/installApp.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,101 @@ | ||
| import child_process from 'child_process'; | ||
| import {CLIError, logger} from '@react-native-community/cli-tools'; | ||
| import {IOSProjectInfo} from '@react-native-community/cli-types'; | ||
| import chalk from 'chalk'; | ||
| import {getBuildPath} from './getBuildPath'; | ||
| import {getBuildSettings} from './getBuildSettings'; | ||
| import path from 'path'; | ||
|
|
||
| function handleLaunchResult( | ||
| success: boolean, | ||
| errorMessage: string, | ||
| errorDetails = '', | ||
| ) { | ||
| if (success) { | ||
| logger.success('Successfully launched the app'); | ||
| } else { | ||
| logger.error(errorMessage, errorDetails); | ||
| } | ||
| } | ||
|
|
||
| type Options = { | ||
| buildOutput: any; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add some basic type. According to data flow this can be |
||
| xcodeProject: IOSProjectInfo; | ||
| mode: string; | ||
| scheme: string; | ||
| target?: string; | ||
| udid: string; | ||
| binaryPath?: string; | ||
| }; | ||
|
|
||
| export default async function installApp({ | ||
| buildOutput, | ||
| xcodeProject, | ||
| mode, | ||
| scheme, | ||
| target, | ||
| udid, | ||
| binaryPath, | ||
| }: Options) { | ||
| let appPath = binaryPath; | ||
|
|
||
| const buildSettings = await getBuildSettings( | ||
| xcodeProject, | ||
| mode, | ||
| buildOutput, | ||
| scheme, | ||
| target, | ||
| ); | ||
|
|
||
| if (!appPath) { | ||
| appPath = await getBuildPath(buildSettings); | ||
| } | ||
|
|
||
| const targetBuildDir = buildSettings.TARGET_BUILD_DIR; | ||
| const infoPlistPath = buildSettings.INFOPLIST_PATH; | ||
|
|
||
| if (!infoPlistPath) { | ||
| throw new CLIError('Failed to find Info.plist'); | ||
| } | ||
|
|
||
| if (!targetBuildDir) { | ||
| throw new CLIError('Failed to get target build directory.'); | ||
| } | ||
|
|
||
| logger.info(`Installing "${chalk.bold(appPath)}`); | ||
|
|
||
| if (udid && appPath) { | ||
| child_process.spawnSync('xcrun', ['simctl', 'install', udid, appPath], { | ||
| stdio: 'inherit', | ||
| }); | ||
| } | ||
|
|
||
| const bundleID = child_process | ||
| .execFileSync( | ||
| '/usr/libexec/PlistBuddy', | ||
| [ | ||
| '-c', | ||
| 'Print:CFBundleIdentifier', | ||
| path.join(targetBuildDir, infoPlistPath), | ||
| ], | ||
| {encoding: 'utf8'}, | ||
| ) | ||
| .trim(); | ||
|
|
||
| console.log(bundleID); | ||
szymonrybczak marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| logger.info(`Launching "${chalk.bold(bundleID)}"`); | ||
|
|
||
| let result = child_process.spawnSync('xcrun', [ | ||
| 'simctl', | ||
| 'launch', | ||
| udid, | ||
| bundleID, | ||
| ]); | ||
|
|
||
| handleLaunchResult( | ||
| result.status === 0, | ||
| 'Failed to launch the app on simulator', | ||
| result.stderr.toString(), | ||
| ); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we refine that type?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be of type
BuildSettingsfrom here