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