Skip to content

Commit b30c472

Browse files
feat(cli): Select a cap run target by target name (#8199)
Co-authored-by: Pedro Bilro <[email protected]>
1 parent eb51288 commit b30c472

File tree

5 files changed

+81
-7
lines changed

5 files changed

+81
-7
lines changed

cli/src/android/run.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,20 @@ const debug = Debug('capacitor:android:run');
1212

1313
export async function runAndroid(
1414
config: Config,
15-
{ target: selectedTarget, flavor: selectedFlavor, forwardPorts: selectedPorts }: RunCommandOptions,
15+
{
16+
target: selectedTarget,
17+
targetName: selectedTargetName,
18+
targetNameSdkVersion: selectedTargetSdkVersion,
19+
flavor: selectedFlavor,
20+
forwardPorts: selectedPorts,
21+
}: RunCommandOptions,
1622
): Promise<void> {
17-
const target = await promptForPlatformTarget(await getPlatformTargets('android'), selectedTarget);
23+
const target = await promptForPlatformTarget(
24+
await getPlatformTargets('android'),
25+
selectedTarget ?? selectedTargetName,
26+
selectedTargetSdkVersion,
27+
selectedTargetName !== undefined,
28+
);
1829

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

cli/src/common.ts

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ export interface PlatformTarget {
378378
export async function promptForPlatformTarget(
379379
targets: PlatformTarget[],
380380
selectedTarget?: string,
381+
selectedTargetSdkVersion?: string,
382+
selectByName?: boolean,
381383
): Promise<PlatformTarget> {
382384
const { prompt } = await import('prompts');
383385
const validTargets = targets.filter((t) => t.id !== undefined);
@@ -405,10 +407,37 @@ export async function promptForPlatformTarget(
405407
}
406408

407409
const targetID = selectedTarget.trim();
408-
const target = targets.find((t) => t.id === targetID);
410+
const target = targets.find((t) => {
411+
if (selectByName === true) {
412+
let name = t.name ?? t.model;
413+
if (name) {
414+
// Apple device names may have "smart quotes" in the name,
415+
// strip them and replace them with the "straight" versions
416+
name = name.replace(/[\u2018\u2019]/g, "'").replace(/[\u201C\u201D]/g, '"');
417+
}
418+
419+
if (selectedTargetSdkVersion) {
420+
return name === targetID && t.sdkVersion === selectedTargetSdkVersion;
421+
}
422+
423+
return name === targetID;
424+
}
425+
426+
return t.id === targetID;
427+
});
409428

410429
if (!target) {
411-
fatal(`Invalid target ID: ${c.input(targetID)}.\n` + `Valid targets are: ${targets.map((t) => t.id).join(', ')}`);
430+
if (selectByName) {
431+
let invalidTargetName = targetID;
432+
if (selectedTargetSdkVersion) {
433+
invalidTargetName += ` [${selectedTargetSdkVersion}]`;
434+
}
435+
fatal(
436+
`Invalid target name: ${c.input(invalidTargetName)}.\n` +
437+
`Valid targets are:\n ${targets.map((t) => `${t.name ?? t.model} [${t.sdkVersion}]`).join('\n')}`,
438+
);
439+
}
440+
fatal(`Invalid target ID: ${c.input(targetID)}.\n` + `Valid targets are:\n ${targets.map((t) => t.id).join('\n')}`);
412441
}
413442

414443
return target;

cli/src/index.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,11 @@ export function runProgram(config: Config): void {
234234
.option('--list', 'list targets, then quit')
235235
.addOption(new Option('--json').hideHelp())
236236
.option('--target <id>', 'use a specific target')
237+
.option('--target-name <name>', 'use a specific target by name')
238+
.option(
239+
'--target-name-sdk-version <version>',
240+
'use a specific sdk version when using --target-name, ex: 26.0 (for iOS 26) or 35 (for Android API 35)',
241+
)
237242
.option('--no-sync', `do not run ${c.input('sync')}`)
238243
.option('--forwardPorts <port:port>', 'Automatically run "adb reverse" for better live-reloading support')
239244
.option('-l, --live-reload', 'Enable Live Reload')
@@ -246,7 +251,21 @@ export function runProgram(config: Config): void {
246251
config,
247252
async (
248253
platform,
249-
{ scheme, flavor, list, json, target, sync, forwardPorts, liveReload, host, port, configuration },
254+
{
255+
scheme,
256+
flavor,
257+
list,
258+
json,
259+
target,
260+
targetName,
261+
targetNameSdkVersion,
262+
sync,
263+
forwardPorts,
264+
liveReload,
265+
host,
266+
port,
267+
configuration,
268+
},
250269
) => {
251270
const { runCommand } = await import('./tasks/run');
252271
await runCommand(config, platform, {
@@ -255,6 +274,8 @@ export function runProgram(config: Config): void {
255274
list,
256275
json,
257276
target,
277+
targetName,
278+
targetNameSdkVersion,
258279
sync,
259280
forwardPorts,
260281
liveReload,

cli/src/ios/run.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,20 @@ const debug = Debug('capacitor:ios:run');
1313

1414
export async function runIOS(
1515
config: Config,
16-
{ target: selectedTarget, scheme: selectedScheme, configuration: selectedConfiguration }: RunCommandOptions,
16+
{
17+
target: selectedTarget,
18+
targetName: selectedTargetName,
19+
targetNameSdkVersion: selectedTargetSdkVersion,
20+
scheme: selectedScheme,
21+
configuration: selectedConfiguration,
22+
}: RunCommandOptions,
1723
): Promise<void> {
18-
const target = await promptForPlatformTarget(await getPlatformTargets('ios'), selectedTarget);
24+
const target = await promptForPlatformTarget(
25+
await getPlatformTargets('ios'),
26+
selectedTarget ?? selectedTargetName,
27+
selectedTargetSdkVersion,
28+
selectedTargetName !== undefined,
29+
);
1930

2031
const runScheme = selectedScheme || config.ios.scheme;
2132
const configuration = selectedConfiguration || 'Debug';

cli/src/tasks/run.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ export interface RunCommandOptions {
2626
list?: boolean;
2727
json?: boolean;
2828
target?: string;
29+
targetName?: string;
30+
targetNameSdkVersion?: string;
2931
sync?: boolean;
3032
forwardPorts?: string;
3133
liveReload?: boolean;

0 commit comments

Comments
 (0)