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