Skip to content

Commit

Permalink
Refactors.
Browse files Browse the repository at this point in the history
  • Loading branch information
garg3133 committed Sep 4, 2024
1 parent 6a9d746 commit 50c63be
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
7 changes: 6 additions & 1 deletion src/commands/android/dotcommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {ANDROID_DOTCOMMANDS} from '../../constants';
import Logger from '../../logger';
import {getPlatformName} from '../../utils';
import {Platform, SdkBinary} from './interfaces';
import {showMissingBinaryHelp} from './subcommands/common';
import {checkJavaInstallation, getBinaryLocation, getSdkRootFromEnv} from './utils/common';
import {spawnCommandSync} from './utils/sdk';

Expand Down Expand Up @@ -57,6 +58,11 @@ export class AndroidDotCommand {

const binaryName = this.dotcmd.split('.')[1] as SdkBinary;
const binaryLocation = getBinaryLocation(this.sdkRoot, this.platform, binaryName, true);
if (!binaryLocation) {
showMissingBinaryHelp(binaryName);

return false;
}

return spawnCommandSync(binaryLocation, binaryName, this.platform, this.args);
}
Expand All @@ -66,4 +72,3 @@ export class AndroidDotCommand {
dotenv.config({path: path.join(this.rootDir, '.env')});
}
}

16 changes: 6 additions & 10 deletions src/commands/android/subcommands/install/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ async function promptForFlag(): Promise<string> {
type: 'list',
name: 'flag',
message: 'Select what do you want to install:',
choices: ['APK', 'AVD', 'System image']
choices: [
{name: 'Android app (APK)', value: 'app'},
{name: 'Android Virtual Device (AVD)', value: 'avd'},
{name: 'System image', value: 'system-image'}
]
});
Logger.log();

const flag = flagAnswer.flag;
if (flag === 'APK') {
return 'app';
} else if (flag === 'System image') {
return 'system-image';
}

return 'avd';
return flagAnswer.flag;
}

54 changes: 28 additions & 26 deletions src/commands/android/subcommands/install/system-image.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,42 +20,43 @@ export async function installSystemImage(sdkRoot: string, platform: Platform): P

const stdout = execBinarySync(sdkmanagerLocation, 'sdkmanager', platform, '--list');
if (!stdout) {
Logger.log(`${colors.red('Failed to fetch available system images!')} Please try again.`);
Logger.log(`${colors.red('\nFailed to fetch available system images!')} Please try again.\n`);

return false;
}

// sdkmanager output has repetitive system image names in different sections (Installed
// packages, Available packages, Available updates).
// Parse the output and store the system images in a Set to avoid duplicates.
const images = new Set<string>();
// Before removing duplicates, sort the system images to get them in increasing order of API level.
// `sdkmanager --list` output have repetitive system image names in different sections
// (Installed packages, Available packages, Available updates, etc.)
//
// Parse the output and store the system image names in a Set to avoid duplicates.
const availableImageNames = new Set<string>();

// Before parsing and removing duplicates, sort the system images
// to get them in increasing order of API level.
const lines = stdout.split('\n').sort();

lines.forEach(line => {
if (!line.includes('system-images;')) {
return;
}
const image = line.split('|')[0].trim();
images.add(image);

const imageName = line.split('|')[0].trim();
availableImageNames.add(imageName);
});

// System images are represented in the format: system-images;android-<api-level>;<type>;<arch>
// Group all the system image types by API level. Group all the architectures by system image type.
const availableSystemImages: AvailableSystemImages = {};

images.forEach(image => {
if (!image.includes('system-image')) {
availableImageNames.forEach(imageName => {
if (!imageName.includes('system-image')) {
return;
}
const imageSplit = image.split(';');
const imageSplit = imageName.split(';');
const apiLevel = imageSplit[1];
const type = imageSplit[2];
const arch = imageSplit[3];

if (!availableSystemImages[apiLevel]) {
availableSystemImages[apiLevel] = [];
}
availableSystemImages[apiLevel] ||= [];

const imageType = availableSystemImages[apiLevel].find(image => image.type === type);
if (!imageType) {
Expand All @@ -68,7 +69,9 @@ export async function installSystemImage(sdkRoot: string, platform: Platform): P
}
});

const apiLevelsWithNames = Object.keys(availableSystemImages).map(apiLevel => {
// We've got the available system images grouped by API level.
// Now, prompt the user to select the API level, system image type, and architecture.
const apiLevelChoices = Object.keys(availableSystemImages).map(apiLevel => {
let name = apiLevel;
if ((apiLevelNames as ApiLevelNames)[apiLevel]) {
name = `${apiLevel}: ${(apiLevelNames as ApiLevelNames)[apiLevel]}`;
Expand All @@ -77,13 +80,13 @@ export async function installSystemImage(sdkRoot: string, platform: Platform): P
return {name, value: apiLevel};
});

const androidVersionAnswer = await inquirer.prompt({
const apiLevelAnswer = await inquirer.prompt({
type: 'list',
name: 'androidVersion',
name: 'apiLevel',
message: 'Select the API level for system image:',
choices: apiLevelsWithNames
choices: apiLevelChoices
});
const apiLevel = androidVersionAnswer.androidVersion;
const apiLevel = apiLevelAnswer.apiLevel;

const systemImageTypeAnswer = await inquirer.prompt({
type: 'list',
Expand All @@ -108,21 +111,20 @@ export async function installSystemImage(sdkRoot: string, platform: Platform): P

const installationStatus = spawnCommandSync(sdkmanagerLocation, 'sdkmanager', platform, [systemImageName]);
if (installationStatus) {
Logger.log(colors.green('System image installed successfully!\n'));
Logger.log(colors.green('\nSystem image installed successfully!\n'));

return true;
}

Logger.log(colors.red('Something went wrong while installing system image.'));
Logger.log(`Please run ${colors.cyan('npx @nightwatch/mobile-helper android.sdkmanager --list_installed')} to verify if the system image was installed.`);
Logger.log('If the system image was not installed, please try installing again.\n');
Logger.log(colors.red('\nSomething went wrong while installing system image.\n'));
Logger.log(`To verify if the system image was installed, run: ${colors.cyan('npx @nightwatch/mobile-helper android.sdkmanager --list_installed')}`);
Logger.log('If the system image is not found listed, please try installing again.\n');

return false;
} catch (error) {
Logger.log(colors.red('Error occured while installing system image.'));
Logger.log(colors.red('\nError occurred while installing system image.'));
console.error(error);

return false;
}
}

2 changes: 1 addition & 1 deletion src/commands/android/subcommands/uninstall/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ export async function uninstallApp(options: Options, sdkRoot: string, platform:

return false;
} catch (error) {
Logger.log(colors.red('Error occurred while uninstalling app.'));
Logger.log(colors.red('\nError occurred while uninstalling app.'));
console.error(error);

return false;
Expand Down
4 changes: 3 additions & 1 deletion src/commands/android/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ export const getBinaryNameForOS = (platform: Platform, binaryName: string) => {
return binaryName;
};

export const getBinaryLocation = (sdkRoot: string, platform: Platform, binaryName: SdkBinary, suppressOutput = false) => {
export const getBinaryLocation = (
sdkRoot: string, platform: Platform, binaryName: SdkBinary, suppressOutput = false
): string => {
const failLocations: string[] = [];

const binaryFullName = getBinaryNameForOS(platform, binaryName);
Expand Down

0 comments on commit 50c63be

Please sign in to comment.