microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/iOSSimulatorManager.ts
118lines · modeblame
119d7878JiglioNero5 years ago | 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; | |
4dfc9ffdRedMickey5 years ago | 13 | state?: string; |
119d7878JiglioNero5 years ago | 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 | | |
3b728847JiglioNero5 years ago | 51 | Object.keys(res.devices).forEach((rawSystem) => { |
| 52 | let system = rawSystem.split(".").slice(-1)[0]; // "com.apple.CoreSimulator.SimRuntime.iOS-11-4" -> "iOS-11-4" | |
| 53 | res.devices[rawSystem].forEach((device: any) => { | |
119d7878JiglioNero5 years ago | 54 | simulators.push({ |
| 55 | name: device.name, | |
| 56 | id: device.udid, | |
3b728847JiglioNero5 years ago | 57 | system: system, |
4dfc9ffdRedMickey5 years ago | 58 | state: device.state, |
119d7878JiglioNero5 years ago | 59 | }); |
| 60 | }); | |
| 61 | }); | |
| 62 | | |
| 63 | return simulators; | |
| 64 | } | |
| 65 | | |
| 66 | private async selectSystem(): Promise<string | undefined> { | |
| 67 | const systemsList = this.getSystemsList(); | |
| 68 | const quickPickOptions: QuickPickOptions = { | |
| 69 | ignoreFocusOut: true, | |
| 70 | canPickMany: false, | |
| 71 | placeHolder: localize("SelectIOSSystemVersion", "Select system version of iOS virtual device"), | |
| 72 | }; | |
| 73 | let result: string | undefined = systemsList[0]; | |
| 74 | if (systemsList.length > 1) { | |
| 75 | result = await window.showQuickPick(systemsList, quickPickOptions); | |
| 76 | } | |
| 77 | return result?.toString(); | |
| 78 | } | |
| 79 | | |
| 80 | public async startSelection(): Promise<string | undefined> { | |
| 81 | this.simulators = await this.collectSimulators(); | |
4dfc9ffdRedMickey5 years ago | 82 | const runningSimulator = this.getRunningSimulator(this.simulators); |
| 83 | if (runningSimulator) { | |
| 84 | return runningSimulator.name; | |
| 85 | } | |
119d7878JiglioNero5 years ago | 86 | const system = await this.selectSystem(); |
| 87 | if (system) { | |
| 88 | this.lastSelectedSystem = system; | |
| 89 | const filter = (el: IiOSSimulator) => el.system === this.lastSelectedSystem; | |
| 90 | return this.selectVirtualDevice(filter); | |
| 91 | } | |
| 92 | return undefined; | |
| 93 | } | |
| 94 | | |
d55f3c22Yuri Skorokhodov5 years ago | 95 | protected async getVirtualDevicesNamesList(filter?: (el: IiOSSimulator) => unknown): Promise<string[]> { |
119d7878JiglioNero5 years ago | 96 | const names: string[] = []; |
| 97 | this.simulators.forEach((el: IiOSSimulator) => { | |
| 98 | if (el.name && (!filter || filter(el))) { | |
| 99 | names.push(el.name); | |
| 100 | } | |
| 101 | }); | |
| 102 | return names; | |
| 103 | } | |
| 104 | | |
| 105 | protected getSystemsList(): string[] { | |
| 106 | const names: Set<string> = new Set(); | |
| 107 | this.simulators.forEach((el: IiOSSimulator) => { | |
| 108 | if (el.system.indexOf("iOS") >= 0) { | |
| 109 | names.add(el.system); | |
| 110 | } | |
| 111 | }); | |
| 112 | return Array.from(names); | |
| 113 | } | |
| 114 | | |
4dfc9ffdRedMickey5 years ago | 115 | private getRunningSimulator(simulators: IiOSSimulator[]): IiOSSimulator | undefined { |
| 116 | return simulators.find(el => el.state === "Booted"); | |
| 117 | } | |
119d7878JiglioNero5 years ago | 118 | } |