Skip to content

Commit

Permalink
feat: add --target option
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonrybczak committed May 4, 2023
1 parent 064ad76 commit 6ce8038
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 16 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 @@ -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
11 changes: 10 additions & 1 deletion packages/cli-platform-ios/src/commands/buildIOS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ async function buildIOS(_: Array<string>, ctx: Config, args: FlagsT) {

let scheme = args.scheme || inferredSchemeName;
let mode = args.mode;
let target = args.target;

if (args.interactive) {
const selection = await selectFromInteractiveMode({scheme, mode});
const selection = await selectFromInteractiveMode({scheme, mode, target});

if (selection.scheme) {
scheme = selection.scheme;
Expand All @@ -80,6 +81,10 @@ async function buildIOS(_: Array<string>, ctx: Config, args: FlagsT) {
if (selection.mode) {
mode = selection.mode;
}

if (selection.target) {
target = selection.target;
}
}

const modifiedArgs = {...args, scheme, mode};
Expand Down Expand Up @@ -261,6 +266,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
68 changes: 53 additions & 15 deletions packages/cli-platform-ios/src/commands/runIOS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {

let scheme = args.scheme || inferredSchemeName;
let mode = args.mode;
let target = args.target;

if (args.interactive) {
const selection = await selectFromInteractiveMode({scheme, mode});
const selection = await selectFromInteractiveMode({scheme, mode, target});

if (selection.scheme) {
scheme = selection.scheme;
Expand All @@ -102,9 +103,13 @@ async function runIOS(_: Array<string>, ctx: Config, args: FlagsT) {
if (selection.mode) {
mode = selection.mode;
}

if (selection.target) {
target = selection.target;
}
}

const modifiedArgs = {...args, scheme, mode};
const modifiedArgs = {...args, scheme, mode, target};

modifiedArgs.mode = getConfigurationScheme(
{scheme: modifiedArgs.scheme, mode: modifiedArgs.mode},
Expand Down Expand Up @@ -291,11 +296,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 @@ -377,6 +383,7 @@ async function runOnDevice(
args.mode || args.configuration,
buildOutput,
scheme,
args.target,
true,
);
const appProcess = child_process.spawn(`${appPath}/${scheme}`, [], {
Expand All @@ -399,6 +406,7 @@ async function runOnDevice(
args.mode || args.configuration,
buildOutput,
scheme,
args.target,
);
} else {
appPath = args.binaryPath;
Expand Down Expand Up @@ -437,29 +445,54 @@ 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;

if (target) {
if (!targets.includes(target)) {
selectedTarget = targets[0];

logger.info(
`Target ${chalk.bold(target)} not found for scheme ${chalk.bold(
scheme,
)}, automatically selected target ${chalk.bold(targets[0])}`,
);
} 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 +511,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
17 changes: 17 additions & 0 deletions packages/cli-platform-ios/src/tools/prompts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,23 @@ export async function promptForConfigurationSelection(
return configuration;
}

export async function promptForTargetSelection(
targets: string[],
scheme: string,
): Promise<string> {
const {target} = await prompts({
name: 'target',
type: 'select',
message: `Select the target you want to use for scheme ${scheme}`,
choices: targets.map((value) => ({
title: value,
value: value,
})),
});

return target;
}

export async function promptForDeviceSelection(
availableDevices: Device[],
): Promise<Device | undefined> {
Expand Down
11 changes: 11 additions & 0 deletions packages/cli-platform-ios/src/tools/selectFromInteractiveMode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,23 @@ import {getProjectInfo} from './getProjectInfo';
import {
promptForConfigurationSelection,
promptForSchemeSelection,
promptForTargetSelection,
} from './prompts';

interface Args {
scheme: string;
mode: string;
target: string;
}

export async function selectFromInteractiveMode({
scheme,
mode,
target,
}: Args): Promise<Args> {
let newScheme = scheme;
let newMode = mode;
let newTarget = target;

const project = getProjectInfo();

Expand All @@ -32,8 +36,15 @@ export async function selectFromInteractiveMode({
logger.info(`Automatically selected ${chalk.bold(mode)} configuration.`);
}

if (project.targets.length > 2) {
newTarget = await promptForTargetSelection(project.targets, newScheme);
} else {
logger.info(`Automatically selected ${chalk.bold(newTarget)} target.`);
}

return {
scheme: newScheme,
mode: newMode,
target: newTarget,
};
}

0 comments on commit 6ce8038

Please sign in to comment.