Skip to content
Open
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
15 changes: 13 additions & 2 deletions cli/src/android/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,20 @@ const debug = Debug('capacitor:android:run');

export async function runAndroid(
config: Config,
{ target: selectedTarget, flavor: selectedFlavor, forwardPorts: selectedPorts }: RunCommandOptions,
{
target: selectedTarget,
targetName: selectedTargetName,
targetNameSdkVersion: selectedTargetSdkVersion,
flavor: selectedFlavor,
forwardPorts: selectedPorts,
}: RunCommandOptions,
): Promise<void> {
const target = await promptForPlatformTarget(await getPlatformTargets('android'), selectedTarget);
const target = await promptForPlatformTarget(
await getPlatformTargets('android'),
selectedTarget ?? selectedTargetName,
selectedTargetSdkVersion,
selectedTargetName !== undefined,
);

const runFlavor = selectedFlavor || config.android?.flavor || '';

Expand Down
20 changes: 19 additions & 1 deletion cli/src/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,8 @@ export interface PlatformTarget {
export async function promptForPlatformTarget(
targets: PlatformTarget[],
selectedTarget?: string,
selectedTargetSdkVersion?: string,
selectByName?: boolean,
): Promise<PlatformTarget> {
const { prompt } = await import('prompts');
const validTargets = targets.filter((t) => t.id !== undefined);
Expand Down Expand Up @@ -405,9 +407,25 @@ export async function promptForPlatformTarget(
}

const targetID = selectedTarget.trim();
const target = targets.find((t) => t.id === targetID);
const target = targets.find((t) => {
if (selectByName === true) {
if (selectedTargetSdkVersion) {
return t.name === targetID && t.sdkVersion === selectedTargetSdkVersion;
}

return t.name === targetID;
}

return t.id === targetID;
});

if (!target) {
if (selectByName) {
fatal(
`Invalid target name: ${c.input(targetID)}.\n` +
`Valid targets are: ${targets.map((t) => `${t.name} [${t.sdkVersion}]`).join(', ')}`,
);
}
fatal(`Invalid target ID: ${c.input(targetID)}.\n` + `Valid targets are: ${targets.map((t) => t.id).join(', ')}`);
}

Expand Down
20 changes: 19 additions & 1 deletion cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,8 @@ export function runProgram(config: Config): void {
.option('--list', 'list targets, then quit')
.addOption(new Option('--json').hideHelp())
.option('--target <id>', 'use a specific target')
.option('--target-name <name>', 'use a specific target by name')
.option('--target-name-sdk-version <version>', 'use a specific sdk version when using --target-name')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonder if it's relevant providing examples of the SDK version format to use (assume it's the SDK int for Android and regular iOS version for iOS, but maybe worth making explicit)?

.option('--no-sync', `do not run ${c.input('sync')}`)
.option('--forwardPorts <port:port>', 'Automatically run "adb reverse" for better live-reloading support')
.option('-l, --live-reload', 'Enable Live Reload')
Expand All @@ -246,7 +248,21 @@ export function runProgram(config: Config): void {
config,
async (
platform,
{ scheme, flavor, list, json, target, sync, forwardPorts, liveReload, host, port, configuration },
{
scheme,
flavor,
list,
json,
target,
targetName,
targetNameSdkVersion,
sync,
forwardPorts,
liveReload,
host,
port,
configuration,
},
) => {
const { runCommand } = await import('./tasks/run');
await runCommand(config, platform, {
Expand All @@ -255,6 +271,8 @@ export function runProgram(config: Config): void {
list,
json,
target,
targetName,
targetNameSdkVersion,
sync,
forwardPorts,
liveReload,
Expand Down
15 changes: 13 additions & 2 deletions cli/src/ios/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@ const debug = Debug('capacitor:ios:run');

export async function runIOS(
config: Config,
{ target: selectedTarget, scheme: selectedScheme, configuration: selectedConfiguration }: RunCommandOptions,
{
target: selectedTarget,
targetName: selectedTargetName,
targetNameSdkVersion: selectedTargetSdkVersion,
scheme: selectedScheme,
configuration: selectedConfiguration,
}: RunCommandOptions,
): Promise<void> {
const target = await promptForPlatformTarget(await getPlatformTargets('ios'), selectedTarget);
const target = await promptForPlatformTarget(
await getPlatformTargets('ios'),
selectedTarget ?? selectedTargetName,
selectedTargetSdkVersion,
selectedTargetName !== undefined,
);

const runScheme = selectedScheme || config.ios.scheme;
const configuration = selectedConfiguration || 'Debug';
Expand Down
2 changes: 2 additions & 0 deletions cli/src/tasks/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export interface RunCommandOptions {
list?: boolean;
json?: boolean;
target?: string;
targetName?: string;
targetNameSdkVersion?: string;
sync?: boolean;
forwardPorts?: string;
liveReload?: boolean;
Expand Down