microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/iOSSimulatorManager.ts
117lines · 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 { IVirtualDevice, VirtualDeviceManager } from "../VirtualDeviceManager"; |
| 5 | import { ChildProcess } from "../../common/node/childProcess"; |
| 6 | import { QuickPickOptions, window } from "vscode"; |
| 7 | import * as nls from "vscode-nls"; |
| 8 | nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); |
| 9 | const localize = nls.loadMessageBundle(); |
| 10 | |
| 11 | export interface IiOSSimulator extends IVirtualDevice { |
| 12 | system: string; |
| 13 | state?: string; |
| 14 | } |
| 15 | |
| 16 | export class IOSSimulatorManager extends VirtualDeviceManager { |
| 17 | private static SIMULATORS_LIST_COMMAND = "xcrun simctl list --json devices available"; |
| 18 | |
| 19 | private childProcess: ChildProcess; |
| 20 | private simulators: IiOSSimulator[]; |
| 21 | private lastSelectedSystem: string; |
| 22 | |
| 23 | constructor() { |
| 24 | super(); |
| 25 | this.childProcess = new ChildProcess(); |
| 26 | this.simulators = []; |
| 27 | } |
| 28 | |
| 29 | public findSimulator(name: string, system: string | null = this.lastSelectedSystem, simulators?: IiOSSimulator[]): IiOSSimulator | null { |
| 30 | const sims = simulators ? simulators : this.simulators; |
| 31 | const foundSimulator = sims.find((value) => value.name === name && (!system || value.system === system)); |
| 32 | if (!foundSimulator) { |
| 33 | return null; |
| 34 | } |
| 35 | return foundSimulator; |
| 36 | } |
| 37 | |
| 38 | public getSimulatorById(udid: string, simulators?: IiOSSimulator[]): IiOSSimulator | null { |
| 39 | const sims = simulators ? simulators : this.simulators; |
| 40 | const foundSimulator = sims.find((value) => value.id === udid); |
| 41 | if (!foundSimulator) { |
| 42 | return null; |
| 43 | } |
| 44 | return foundSimulator; |
| 45 | } |
| 46 | |
| 47 | public async collectSimulators(): Promise<IiOSSimulator[]> { |
| 48 | const simulators: IiOSSimulator[] = []; |
| 49 | const res = JSON.parse(await this.childProcess.execToString(IOSSimulatorManager.SIMULATORS_LIST_COMMAND)); |
| 50 | |
| 51 | Object.keys(res.devices).forEach((system) => { |
| 52 | res.devices[system].forEach((device: any) => { |
| 53 | simulators.push({ |
| 54 | name: device.name, |
| 55 | id: device.udid, |
| 56 | system: system.split(".").slice(-1)[0], // "com.apple.CoreSimulator.SimRuntime.iOS-11-4" -> "iOS-11-4" |
| 57 | state: device.state, |
| 58 | }); |
| 59 | }); |
| 60 | }); |
| 61 | |
| 62 | return simulators; |
| 63 | } |
| 64 | |
| 65 | private async selectSystem(): Promise<string | undefined> { |
| 66 | const systemsList = this.getSystemsList(); |
| 67 | const quickPickOptions: QuickPickOptions = { |
| 68 | ignoreFocusOut: true, |
| 69 | canPickMany: false, |
| 70 | placeHolder: localize("SelectIOSSystemVersion", "Select system version of iOS virtual device"), |
| 71 | }; |
| 72 | let result: string | undefined = systemsList[0]; |
| 73 | if (systemsList.length > 1) { |
| 74 | result = await window.showQuickPick(systemsList, quickPickOptions); |
| 75 | } |
| 76 | return result?.toString(); |
| 77 | } |
| 78 | |
| 79 | public async startSelection(): Promise<string | undefined> { |
| 80 | this.simulators = await this.collectSimulators(); |
| 81 | const runningSimulator = this.getRunningSimulator(this.simulators); |
| 82 | if (runningSimulator) { |
| 83 | return runningSimulator.name; |
| 84 | } |
| 85 | const system = await this.selectSystem(); |
| 86 | if (system) { |
| 87 | this.lastSelectedSystem = system; |
| 88 | const filter = (el: IiOSSimulator) => el.system === this.lastSelectedSystem; |
| 89 | return this.selectVirtualDevice(filter); |
| 90 | } |
| 91 | return undefined; |
| 92 | } |
| 93 | |
| 94 | protected async getVirtualDevicesNamesList(filter?: (el: IiOSSimulator) => unknown): Promise<string[]> { |
| 95 | const names: string[] = []; |
| 96 | this.simulators.forEach((el: IiOSSimulator) => { |
| 97 | if (el.name && (!filter || filter(el))) { |
| 98 | names.push(el.name); |
| 99 | } |
| 100 | }); |
| 101 | return names; |
| 102 | } |
| 103 | |
| 104 | protected getSystemsList(): string[] { |
| 105 | const names: Set<string> = new Set(); |
| 106 | this.simulators.forEach((el: IiOSSimulator) => { |
| 107 | if (el.system.indexOf("iOS") >= 0) { |
| 108 | names.add(el.system); |
| 109 | } |
| 110 | }); |
| 111 | return Array.from(names); |
| 112 | } |
| 113 | |
| 114 | private getRunningSimulator(simulators: IiOSSimulator[]): IiOSSimulator | undefined { |
| 115 | return simulators.find(el => el.state === "Booted"); |
| 116 | } |
| 117 | } |
| 118 | |