Skip to content

Commit

Permalink
feat: add --target option (#1928)
Browse files Browse the repository at this point in the history
* feat: add `--target` option

* Update packages/cli-platform-ios/src/commands/runIOS/index.ts

Co-authored-by: Michał Pierzchała <thymikee@gmail.com>

* fix: delete ability to select target from interactive mode

---------

Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
  • Loading branch information
szymonrybczak and thymikee authored May 19, 2023
1 parent 29c7ea0 commit 0afde11
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 15 deletions.
8 changes: 8 additions & 0 deletions packages/cli-platform-ios/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ react-native run-ios --mode "Release"

Explicitly set Xcode scheme to use.

#### `--target <string>`

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.
Expand Down Expand Up @@ -155,6 +159,10 @@ react-native build-ios --mode "Release"

Explicitly set Xcode scheme to use.

#### `--target <string>`

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {

export type BuildFlags = {
mode: string;
target: string;
packager: boolean;
verbose: boolean;
xcconfig?: string;
Expand Down
4 changes: 4 additions & 0 deletions packages/cli-platform-ios/src/commands/buildIOS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ export const iosBuildOptions = [
description: 'Custom params that will be passed to xcodebuild command.',
parse: (val: string) => val.split(' '),
},
{
name: '--target <string>',
description: 'Explicitly set Xcode target to use.',
},
];

export default {
Expand Down
62 changes: 47 additions & 15 deletions packages/cli-platform-ios/src/commands/runIOS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}`, [], {
Expand All @@ -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;
Expand Down Expand Up @@ -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(
Expand All @@ -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.');
Expand Down

0 comments on commit 0afde11

Please sign in to comment.