From 0afde11063fc48ec9666f2d05e3b798f92b0c9c0 Mon Sep 17 00:00:00 2001 From: Szymon Rybczak Date: Fri, 19 May 2023 10:29:29 +0200 Subject: [PATCH] feat: add `--target` option (#1928) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat: add `--target` option * Update packages/cli-platform-ios/src/commands/runIOS/index.ts Co-authored-by: Michał Pierzchała * fix: delete ability to select target from interactive mode --------- Co-authored-by: Michał Pierzchała --- packages/cli-platform-ios/README.md | 8 +++ .../src/commands/buildIOS/buildProject.ts | 1 + .../src/commands/buildIOS/index.ts | 4 ++ .../src/commands/runIOS/index.ts | 62 ++++++++++++++----- 4 files changed, 60 insertions(+), 15 deletions(-) diff --git a/packages/cli-platform-ios/README.md b/packages/cli-platform-ios/README.md index 1629cbeab..46ac0434b 100644 --- a/packages/cli-platform-ios/README.md +++ b/packages/cli-platform-ios/README.md @@ -61,6 +61,10 @@ react-native run-ios --mode "Release" Explicitly set Xcode scheme to use. +#### `--target ` + +Explicitly set Xcode target to use. + #### `--device [string]` Explicitly set device to use by name. The value is not required if you have a single device connected. @@ -155,6 +159,10 @@ react-native build-ios --mode "Release" Explicitly set Xcode scheme to use. +#### `--target ` + +Explicitly set Xcode target to use. + #### `--device [string]` Explicitly set device to use by name. The value is not required if you have a single device connected. diff --git a/packages/cli-platform-ios/src/commands/buildIOS/buildProject.ts b/packages/cli-platform-ios/src/commands/buildIOS/buildProject.ts index 172a5088b..fb4de4a1c 100644 --- a/packages/cli-platform-ios/src/commands/buildIOS/buildProject.ts +++ b/packages/cli-platform-ios/src/commands/buildIOS/buildProject.ts @@ -13,6 +13,7 @@ import { export type BuildFlags = { mode: string; + target: string; packager: boolean; verbose: boolean; xcconfig?: string; diff --git a/packages/cli-platform-ios/src/commands/buildIOS/index.ts b/packages/cli-platform-ios/src/commands/buildIOS/index.ts index 81e0ba46f..8a04d858a 100644 --- a/packages/cli-platform-ios/src/commands/buildIOS/index.ts +++ b/packages/cli-platform-ios/src/commands/buildIOS/index.ts @@ -265,6 +265,10 @@ export const iosBuildOptions = [ description: 'Custom params that will be passed to xcodebuild command.', parse: (val: string) => val.split(' '), }, + { + name: '--target ', + description: 'Explicitly set Xcode target to use.', + }, ]; export default { diff --git a/packages/cli-platform-ios/src/commands/runIOS/index.ts b/packages/cli-platform-ios/src/commands/runIOS/index.ts index 24e341e2d..c8dfcb5c9 100644 --- a/packages/cli-platform-ios/src/commands/runIOS/index.ts +++ b/packages/cli-platform-ios/src/commands/runIOS/index.ts @@ -291,11 +291,12 @@ async function runOnSimulator( args, ); - appPath = getBuildPath( + appPath = await getBuildPath( xcodeProject, args.mode || args.configuration, buildOutput, scheme, + args.target, ); } else { appPath = args.binaryPath; @@ -372,11 +373,12 @@ async function runOnDevice( args, ); - const appPath = getBuildPath( + const appPath = await getBuildPath( xcodeProject, args.mode || args.configuration, buildOutput, scheme, + args.target, true, ); const appProcess = child_process.spawn(`${appPath}/${scheme}`, [], { @@ -394,11 +396,12 @@ async function runOnDevice( args, ); - appPath = getBuildPath( + appPath = await getBuildPath( xcodeProject, args.mode || args.configuration, buildOutput, scheme, + args.target, ); } else { appPath = args.binaryPath; @@ -437,29 +440,53 @@ function bootSimulator(selectedSimulator: Device) { child_process.spawnSync('xcrun', ['simctl', 'boot', selectedSimulator.udid]); } -function getTargetPaths(buildSettings: string) { +async function getTargetPaths( + buildSettings: string, + scheme: string, + target: string | undefined, +) { const settings = JSON.parse(buildSettings); - // Find app in all building settings - look for WRAPPER_EXTENSION: 'app', - for (const i in settings) { - const wrapperExtension = settings[i].buildSettings.WRAPPER_EXTENSION; - - if (wrapperExtension === 'app') { - return { - targetBuildDir: settings[i].buildSettings.TARGET_BUILD_DIR, - executableFolderPath: settings[i].buildSettings.EXECUTABLE_FOLDER_PATH, - }; + const targets = settings.map(({target}: any) => target); + + 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 wrapperExtension = + settings[targetIndex].buildSettings.WRAPPER_EXTENSION; + + if (wrapperExtension === 'app') { + return { + targetBuildDir: settings[targetIndex].buildSettings.TARGET_BUILD_DIR, + executableFolderPath: + settings[targetIndex].buildSettings.EXECUTABLE_FOLDER_PATH, + }; + } + return {}; } -function getBuildPath( +async function getBuildPath( xcodeProject: IOSProjectInfo, mode: BuildFlags['mode'], buildOutput: string, scheme: string, + target: string, isCatalyst: boolean = false, ) { const buildSettings = child_process.execFileSync( @@ -478,7 +505,12 @@ function getBuildPath( ], {encoding: 'utf8'}, ); - const {targetBuildDir, executableFolderPath} = getTargetPaths(buildSettings); + + const {targetBuildDir, executableFolderPath} = await getTargetPaths( + buildSettings, + scheme, + target, + ); if (!targetBuildDir) { throw new CLIError('Failed to get the target build directory.');