-
Notifications
You must be signed in to change notification settings - Fork 928
feat: add platform-cli-apple with reusable utilities for OOT platforms #2208
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
thymikee
merged 15 commits into
react-native-community:main
from
okwasniewski:feat/split-runIOS
Dec 20, 2023
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
2e85bcd
feat: refactor run-ios to separate files, export more utilities
okwasniewski 53ef539
[wip] feat: use builder pattern to easily reuse commands for OOT plat…
okwasniewski 80fd32f
feat: add package
okwasniewski c7834c7
docs: document cli-platform-apple
okwasniewski 6436654
fix: move generated files
okwasniewski edbf273
fix: indentation
okwasniewski 081320a
fix: building packages
okwasniewski ef4ce02
fix: tests
okwasniewski 7bb95f0
fix: account for macOS in simulatorDest
okwasniewski 3a0e470
fix: recheck pods for build command
okwasniewski e242f66
feat: add getProjectConfig for OOT platforms
okwasniewski 91f2604
feat: fallback to first available device
okwasniewski 1d4d80e
refactor: use platformInfo utility
okwasniewski 529b79b
fix: bring back podspecs
okwasniewski 6143e29
fix: apply reviewers comments
okwasniewski 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
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
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
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
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
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
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,42 @@ | ||
# @react-native-community/cli-platform-apple | ||
|
||
This package is part of the [React Native CLI](../../README.md). It contains utilities for building reusable commands targetting Apple platforms. | ||
|
||
## Installation | ||
|
||
```sh | ||
yarn add @react-native-community/cli-platform-apple | ||
``` | ||
|
||
## Usage | ||
|
||
This package is intended to be used internally in [React Native CLI](../../README.md) and by out of tree platforms. | ||
|
||
It exports builder commands that can be used to create custom `run-`, `log-` and `build-` commands for example: `yarn run-<oot-platform>`. | ||
|
||
Inside of `<oot-platform>/packages/react-native/react-native.config.js`: | ||
|
||
```js | ||
const { | ||
buildOptions, | ||
createBuild, | ||
} = require('@react-native-community/cli-platform-apple'); | ||
|
||
const buildVisionOS = { | ||
name: 'build-visionos', | ||
description: 'builds your app for visionOS platform', | ||
func: createBuild({platformName: 'visionos'}), | ||
examples: [ | ||
{ | ||
desc: 'Build the app for visionOS in Release mode', | ||
cmd: 'npx react-native build-visionos --mode "Release"', | ||
}, | ||
], | ||
options: buildOptions, | ||
}; | ||
|
||
module.exports = { | ||
commands: [buildVisionOS], // <- Add command here | ||
//.. | ||
}; | ||
``` |
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,35 @@ | ||
{ | ||
"name": "@react-native-community/cli-platform-apple", | ||
"version": "13.1.0", | ||
"license": "MIT", | ||
"main": "build/index.js", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"dependencies": { | ||
"@react-native-community/cli-tools": "13.1.0", | ||
"chalk": "^4.1.2", | ||
"execa": "^5.0.0", | ||
"fast-xml-parser": "^4.0.12", | ||
"glob": "^7.1.3", | ||
"ora": "^5.4.1" | ||
}, | ||
"devDependencies": { | ||
"@react-native-community/cli-types": "13.1.0", | ||
"@types/glob": "^7.1.1", | ||
"@types/lodash": "^4.14.149", | ||
"hasbin": "^1.2.3" | ||
}, | ||
"files": [ | ||
"build", | ||
"!*.d.ts", | ||
"!*.map" | ||
], | ||
"homepage": "https://github.com/react-native-community/cli/tree/main/packages/cli-platform-apple", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/react-native-community/cli.git", | ||
"directory": "packages/cli-platform-apple" | ||
} | ||
} | ||
|
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
File renamed without changes.
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
64 changes: 64 additions & 0 deletions
64
packages/cli-platform-apple/src/commands/buildCommand/createBuild.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,64 @@ | ||
import fs from 'fs'; | ||
import {CLIError} from '@react-native-community/cli-tools'; | ||
import {Config, IOSProjectConfig} from '@react-native-community/cli-types'; | ||
import getArchitecture from '../../tools/getArchitecture'; | ||
import resolvePods from '../../tools/pods'; | ||
import {BuildFlags} from './buildOptions'; | ||
import {buildProject} from './buildProject'; | ||
import {getConfiguration} from './getConfiguration'; | ||
import {getXcodeProjectAndDir} from './getXcodeProjectAndDir'; | ||
import {BuilderCommand} from '../../types'; | ||
import findXcodeProject from '../../config/findXcodeProject'; | ||
|
||
const createBuild = | ||
({platformName}: BuilderCommand) => | ||
async (_: Array<string>, ctx: Config, args: BuildFlags) => { | ||
const platform = ctx.project[platformName] as IOSProjectConfig; | ||
if (platform === undefined) { | ||
throw new CLIError(`Unable to find ${platform} platform config`); | ||
} | ||
|
||
let {xcodeProject, sourceDir} = getXcodeProjectAndDir(platform); | ||
|
||
let installedPods = false; | ||
if (platform?.automaticPodsInstallation || args.forcePods) { | ||
const isAppRunningNewArchitecture = platform?.sourceDir | ||
? await getArchitecture(platform?.sourceDir) | ||
: undefined; | ||
|
||
await resolvePods(ctx.root, ctx.dependencies, platformName, { | ||
forceInstall: args.forcePods, | ||
newArchEnabled: isAppRunningNewArchitecture, | ||
}); | ||
|
||
installedPods = true; | ||
} | ||
|
||
// if project is freshly created, revisit Xcode project to verify Pods are installed correctly. | ||
// This is needed because ctx project is created before Pods are installed, so it might have outdated information. | ||
if (installedPods) { | ||
const recheckXcodeProject = findXcodeProject(fs.readdirSync(sourceDir)); | ||
if (recheckXcodeProject) { | ||
xcodeProject = recheckXcodeProject; | ||
} | ||
} | ||
|
||
process.chdir(sourceDir); | ||
|
||
const {scheme, mode} = await getConfiguration( | ||
xcodeProject, | ||
sourceDir, | ||
args, | ||
); | ||
|
||
return buildProject( | ||
xcodeProject, | ||
platformName, | ||
undefined, | ||
mode, | ||
scheme, | ||
args, | ||
); | ||
}; | ||
|
||
export default createBuild; |
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions
6
packages/cli-platform-apple/src/commands/buildCommand/simulatorDestinationMap.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,6 @@ | ||
export const simulatorDestinationMap: Record<string, string> = { | ||
ios: 'iOS Simulator', | ||
macos: 'macOS', | ||
visionos: 'visionOS Simulator', | ||
tvos: 'tvOS Simulator', | ||
}; |
98 changes: 98 additions & 0 deletions
98
packages/cli-platform-apple/src/commands/logCommand/createLog.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,98 @@ | ||
import {CLIError, logger, prompt} from '@react-native-community/cli-tools'; | ||
import {Config, IOSProjectConfig} from '@react-native-community/cli-types'; | ||
import {spawnSync} from 'child_process'; | ||
import os from 'os'; | ||
import path from 'path'; | ||
import getSimulators from '../../tools/getSimulators'; | ||
import listDevices from '../../tools/listDevices'; | ||
import {getPlatformInfo} from '../runCommand/getPlatformInfo'; | ||
import {BuilderCommand} from '../../types'; | ||
|
||
/** | ||
* Starts Apple device syslog tail | ||
*/ | ||
|
||
type Args = { | ||
interactive: boolean; | ||
}; | ||
|
||
const createLog = | ||
({platformName}: BuilderCommand) => | ||
async (_: Array<string>, ctx: Config, args: Args) => { | ||
const platform = ctx.project[platformName] as IOSProjectConfig; | ||
const {readableName: platformReadableName} = getPlatformInfo(platformName); | ||
|
||
if (platform === undefined) { | ||
throw new CLIError(`Unable to find ${platform} platform config`); | ||
} | ||
|
||
// Here we're using two command because first command `xcrun simctl list --json devices` outputs `state` but doesn't return `available`. But second command `xcrun xcdevice list` outputs `available` but doesn't output `state`. So we need to connect outputs of both commands. | ||
const simulators = getSimulators(); | ||
const bootedSimulators = Object.keys(simulators.devices) | ||
.map((key) => simulators.devices[key]) | ||
.reduce((acc, val) => acc.concat(val), []) | ||
.filter(({state}) => state === 'Booted'); | ||
|
||
const {sdkNames} = getPlatformInfo(platformName); | ||
const devices = await listDevices(sdkNames); | ||
|
||
const availableSimulators = devices.filter( | ||
({type, isAvailable}) => type === 'simulator' && isAvailable, | ||
); | ||
|
||
if (availableSimulators.length === 0) { | ||
logger.error('No simulators detected. Install simulators via Xcode.'); | ||
return; | ||
} | ||
|
||
const bootedAndAvailableSimulators = bootedSimulators.map((booted) => { | ||
const available = availableSimulators.find( | ||
({udid}) => udid === booted.udid, | ||
); | ||
return {...available, ...booted}; | ||
}); | ||
|
||
if (bootedAndAvailableSimulators.length === 0) { | ||
logger.error( | ||
`No booted and available ${platformReadableName} simulators found.`, | ||
); | ||
return; | ||
} | ||
|
||
if (args.interactive && bootedAndAvailableSimulators.length > 1) { | ||
const {udid} = await prompt({ | ||
type: 'select', | ||
name: 'udid', | ||
message: `Select ${platformReadableName} simulators to tail logs from`, | ||
choices: bootedAndAvailableSimulators.map((simulator) => ({ | ||
title: simulator.name, | ||
value: simulator.udid, | ||
})), | ||
}); | ||
okwasniewski marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
tailDeviceLogs(udid); | ||
} else { | ||
tailDeviceLogs(bootedAndAvailableSimulators[0].udid); | ||
} | ||
}; | ||
|
||
function tailDeviceLogs(udid: string) { | ||
const logDir = path.join( | ||
os.homedir(), | ||
'Library', | ||
'Logs', | ||
'CoreSimulator', | ||
udid, | ||
'asl', | ||
); | ||
|
||
const log = spawnSync('syslog', ['-w', '-F', 'std', '-d', logDir], { | ||
stdio: 'inherit', | ||
}); | ||
|
||
if (log.error !== null) { | ||
throw log.error; | ||
} | ||
} | ||
|
||
export default createLog; |
7 changes: 7 additions & 0 deletions
7
packages/cli-platform-apple/src/commands/logCommand/logOptions.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,7 @@ | ||
export const logOptions = [ | ||
{ | ||
name: '--interactive', | ||
description: | ||
'Explicitly select simulator to tail logs from. By default it will tail logs from the first booted and available simulator.', | ||
}, | ||
]; |
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.
Uh oh!
There was an error while loading. Please reload this page.