microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/android/androidEmulatorManager.ts
107lines · modecode
| 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT license. See LICENSE file in the project root for details. |
| 3 | |
| 4 | import { AdbHelper } from "./adb"; |
| 5 | import { ChildProcess } from "../../common/node/childProcess"; |
| 6 | import { IVirtualDevice, VirtualDeviceManager } from "../VirtualDeviceManager"; |
| 7 | import { OutputChannelLogger } from "../log/OutputChannelLogger"; |
| 8 | import * as nls from "vscode-nls"; |
| 9 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 10 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 11 | nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); |
| 12 | const localize = nls.loadMessageBundle(); |
| 13 | export interface IAndroidEmulator extends IVirtualDevice { |
| 14 | } |
| 15 | |
| 16 | export class AndroidEmulatorManager extends VirtualDeviceManager { |
| 17 | private static readonly EMULATOR_COMMAND = "emulator"; |
| 18 | private static readonly EMULATOR_LIST_AVDS_COMMAND = `-list-avds`; |
| 19 | private static readonly EMULATOR_AVD_START_COMMAND = `-avd`; |
| 20 | |
| 21 | private static readonly EMULATOR_START_TIMEOUT = 120; |
| 22 | |
| 23 | private logger: OutputChannelLogger = OutputChannelLogger.getChannel(OutputChannelLogger.MAIN_CHANNEL_NAME, true); |
| 24 | |
| 25 | private adbHelper: AdbHelper; |
| 26 | private childProcess: ChildProcess; |
| 27 | |
| 28 | constructor(adbHelper: AdbHelper) { |
| 29 | super(); |
| 30 | this.adbHelper = adbHelper; |
| 31 | this.childProcess = new ChildProcess(); |
| 32 | } |
| 33 | |
| 34 | public async startEmulator(target: string): Promise<IAndroidEmulator | null> { |
| 35 | const onlineDevices = await this.adbHelper.getOnlineDevices(); |
| 36 | for (let i = 0; i < onlineDevices.length; i++){ |
| 37 | if (onlineDevices[i].id === target) { |
| 38 | return {id: onlineDevices[i].id}; |
| 39 | } |
| 40 | } |
| 41 | if (target && (await this.adbHelper.getOnlineDevices()).length === 0) { |
| 42 | if (target === "simulator") { |
| 43 | const newEmulator = await this.startSelection(); |
| 44 | if (newEmulator) { |
| 45 | const emulatorId = await this.tryLaunchEmulatorByName(newEmulator); |
| 46 | return {name: newEmulator, id: emulatorId}; |
| 47 | } |
| 48 | } |
| 49 | else if (!target.includes("device")) { |
| 50 | const emulatorId = await this.tryLaunchEmulatorByName(target); |
| 51 | return {name: target, id: emulatorId}; |
| 52 | } |
| 53 | } |
| 54 | return null; |
| 55 | } |
| 56 | |
| 57 | public async tryLaunchEmulatorByName(emulatorName: string): Promise<string> { |
| 58 | return new Promise((resolve, reject) => { |
| 59 | const emulatorProcess = this.childProcess.spawn(AndroidEmulatorManager.EMULATOR_COMMAND, [AndroidEmulatorManager.EMULATOR_AVD_START_COMMAND, emulatorName], { |
| 60 | detached: true, |
| 61 | }, true); |
| 62 | emulatorProcess.outcome.catch((error) => { |
| 63 | if (process.platform == "win32" && process.env.SESSIONNAME && process.env.SESSIONNAME.toLowerCase().includes("rdp-tcp")) { |
| 64 | this.logger.warning(localize("RDPEmulatorWarning", "Android emulator was launched from the Windows RDP session, this might lead to failures.")); |
| 65 | } |
| 66 | reject(ErrorHelper.getInternalError(InternalErrorCode.VirtualDeviceSelectionError, error)); |
| 67 | }); |
| 68 | emulatorProcess.spawnedProcess.unref(); |
| 69 | |
| 70 | const rejectTimeout = setTimeout(() => { |
| 71 | cleanup(); |
| 72 | reject(ErrorHelper.getInternalError(InternalErrorCode.VirtualDeviceSelectionError, localize("EmulatorStartWarning", "Could not start the emulator {0} within {1} seconds.", emulatorName, AndroidEmulatorManager.EMULATOR_START_TIMEOUT))); |
| 73 | }, AndroidEmulatorManager.EMULATOR_START_TIMEOUT * 1000); |
| 74 | |
| 75 | const bootCheckInterval = setInterval(async () => { |
| 76 | const connectedDevices = await this.adbHelper.getOnlineDevices(); |
| 77 | if (connectedDevices.length > 0) { |
| 78 | this.logger.info(localize("EmulatorLaunched", "Launched emulator {0}", emulatorName)); |
| 79 | cleanup(); |
| 80 | resolve(connectedDevices[0].id); |
| 81 | } |
| 82 | }, 1000); |
| 83 | |
| 84 | const cleanup = () => { |
| 85 | clearTimeout(rejectTimeout); |
| 86 | clearInterval(bootCheckInterval); |
| 87 | }; |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | public startSelection(): Promise<string | undefined> { |
| 92 | return this.selectVirtualDevice(); |
| 93 | } |
| 94 | |
| 95 | protected async getVirtualDevicesNamesList(): Promise<string[]> { |
| 96 | const res = await this.childProcess.execToString(`${AndroidEmulatorManager.EMULATOR_COMMAND} ${AndroidEmulatorManager.EMULATOR_LIST_AVDS_COMMAND}`); |
| 97 | let emulatorsList: string[] = []; |
| 98 | if (res) { |
| 99 | emulatorsList = res.split(/\r?\n|\r/g); |
| 100 | const indexOfBlank = emulatorsList.indexOf(""); |
| 101 | if (emulatorsList.indexOf("") >= 0) { |
| 102 | emulatorsList.splice(indexOfBlank, 1); |
| 103 | } |
| 104 | } |
| 105 | return emulatorsList; |
| 106 | } |
| 107 | } |
| 108 | |