Skip to content

Commit

Permalink
fix: grep issue in avd creation (#71)
Browse files Browse the repository at this point in the history
Co-authored-by: Priyansh Garg <[email protected]>
  • Loading branch information
itsspriyansh and garg3133 authored Dec 15, 2024
1 parent 97d94f8 commit 675cdc4
Showing 1 changed file with 32 additions and 16 deletions.
48 changes: 32 additions & 16 deletions src/commands/android/subcommands/install/avd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,14 @@ import {getBinaryLocation} from '../../utils/common';
import {execBinaryAsync, execBinarySync} from '../../utils/sdk';
import {getInstalledSystemImages, showMissingBinaryHelp} from '../common';

type DeviceType = 'Nexus' | 'Pixel' | 'Wear OS' | 'Android TV' | 'Desktop' | 'Others';

const deviceTypesToGrepCommand: Record<DeviceType, string> = {
'Nexus': 'Nexus',
'Pixel': 'pixel',
'Wear OS': 'wear',
'Android TV': 'tv',
'Desktop': 'desktop',
'Others': '-Ev "wear|Nexus|pixel|tv|desktop"'
};
const DEVICE_TYPES = [
{name: 'Nexus', value: 'Nexus'},
{name: 'Pixel', value: 'pixel'},
{name: 'Wear OS', value: 'wear'},
{name: 'Android TV', value: 'tv'},
{name: 'Desktop', value: 'desktop'},
{name: 'Others', value: 'Others'}
];

export async function createAvd(sdkRoot: string, platform: Platform): Promise<boolean> {
try {
Expand Down Expand Up @@ -64,24 +62,42 @@ export async function createAvd(sdkRoot: string, platform: Platform): Promise<bo
type: 'list',
name: 'deviceType',
message: 'Select the device type for AVD:',
choices: Object.keys(deviceTypesToGrepCommand)
choices: DEVICE_TYPES
});
const deviceType = deviceTypeAnswer.deviceType;

let cmd = `list devices -c | grep ${deviceTypesToGrepCommand[deviceType as DeviceType]}`;
let cmd = 'list devices -c';
const availableDeviceProfiles = execBinarySync(avdmanagerLocation, 'avdmanager', platform, cmd);

if (!availableDeviceProfiles) {
Logger.log(`${colors.red(`No potential device profile found for device type ${deviceType}.`)} Please try again.`);
Logger.log(colors.red('\nSomething went wrong while retrieving available device profiles.'), 'Please try again.');

return false;
}

const deviceTypeValues = DEVICE_TYPES.map(deviceType => deviceType.value);
const matchingDeviceProfiles = availableDeviceProfiles
.split('\n')
.filter(line => line !== '')
.filter(deviceProfile => {
if (deviceType === 'Others') {
return !deviceTypeValues.some(deviceTypeValue => deviceProfile.includes(deviceTypeValue));
} else {
return deviceProfile.includes(deviceType);
}
});


if (!matchingDeviceProfiles.length) {
Logger.log(colors.red(`\nNo potential device profile found for device type "${deviceType}".`), 'Please try again.');

return false;
}
const availableDeviceProfilesList = availableDeviceProfiles.split('\n').filter(deviceProfile => deviceProfile !== '');

const deviceAnswer = await inquirer.prompt({
type: 'list',
name: 'deviceProfile',
message: 'Select the device profile for AVD:',
choices: availableDeviceProfilesList
choices: matchingDeviceProfiles
});
const deviceProfile = deviceAnswer.deviceProfile;

Expand Down

0 comments on commit 675cdc4

Please sign in to comment.