-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add platform-cli-apple with reusable utilities for OOT platforms (
#2208) * feat: refactor run-ios to separate files, export more utilities * [wip] feat: use builder pattern to easily reuse commands for OOT platforms * feat: add package * docs: document cli-platform-apple * fix: move generated files * fix: indentation * fix: building packages * fix: tests * fix: account for macOS in simulatorDest * fix: recheck pods for build command * feat: add getProjectConfig for OOT platforms * feat: fallback to first available device * refactor: use platformInfo utility * fix: bring back podspecs * fix: apply reviewers comments
- Loading branch information
1 parent
3906b90
commit a4417a1
Showing
65 changed files
with
1,358 additions
and
936 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains 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 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 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 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 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 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 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 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 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 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 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 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 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, | ||
})), | ||
}); | ||
|
||
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 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.