Skip to content
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

feat: add --target option #1928

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -151,6 +155,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 @@ -261,6 +261,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