microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.4.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/ios/iOSSimulatorManager.ts

134lines · 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
4import { IVirtualDevice, VirtualDeviceManager } from "../VirtualDeviceManager";
5import { ChildProcess } from "../../common/node/childProcess";
6import { QuickPickOptions, window } from "vscode";
7import * as nls from "vscode-nls";
8nls.config({
9 messageFormat: nls.MessageFormat.bundle,
10 bundleFormat: nls.BundleFormat.standalone,
11})();
12const localize = nls.loadMessageBundle();
13
14export interface IiOSSimulator extends IVirtualDevice {
15 system: string;
16 state?: string;
17}
18
19export class IOSSimulatorManager extends VirtualDeviceManager {
20 private static SIMULATORS_LIST_COMMAND = "xcrun simctl list --json devices available";
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(): Promise<IiOSSimulator[]> {
57 const simulators: IiOSSimulator[] = [];
58 const res = JSON.parse(
59 await this.childProcess.execToString(IOSSimulatorManager.SIMULATORS_LIST_COMMAND),
60 );
61
62 Object.keys(res.devices).forEach(rawSystem => {
63 let system = rawSystem.split(".").slice(-1)[0]; // "com.apple.CoreSimulator.SimRuntime.iOS-11-4" -> "iOS-11-4"
64 res.devices[rawSystem].forEach((device: any) => {
65 simulators.push({
66 name: device.name,
67 id: device.udid,
68 system: system,
69 state: device.state,
70 });
71 });
72 });
73
74 return simulators;
75 }
76
77 private async selectSystem(): Promise<string | undefined> {
78 const systemsList = this.getSystemsList();
79 const quickPickOptions: QuickPickOptions = {
80 ignoreFocusOut: true,
81 canPickMany: false,
82 placeHolder: localize(
83 "SelectIOSSystemVersion",
84 "Select system version of iOS virtual device",
85 ),
86 };
87 let result: string | undefined = systemsList[0];
88 if (systemsList.length > 1) {
89 result = await window.showQuickPick(systemsList, quickPickOptions);
90 }
91 return result?.toString();
92 }
93
94 public async startSelection(): Promise<string | undefined> {
95 this.simulators = await this.collectSimulators();
96 const runningSimulator = this.getRunningSimulator(this.simulators);
97 if (runningSimulator) {
98 return runningSimulator.name;
99 }
100 const system = await this.selectSystem();
101 if (system) {
102 this.lastSelectedSystem = system;
103 const filter = (el: IiOSSimulator) => el.system === this.lastSelectedSystem;
104 return this.selectVirtualDevice(filter);
105 }
106 return undefined;
107 }
108
109 protected async getVirtualDevicesNamesList(
110 filter?: (el: IiOSSimulator) => unknown,
111 ): Promise<string[]> {
112 const names: string[] = [];
113 this.simulators.forEach((el: IiOSSimulator) => {
114 if (el.name && (!filter || filter(el))) {
115 names.push(el.name);
116 }
117 });
118 return names;
119 }
120
121 protected getSystemsList(): string[] {
122 const names: Set<string> = new Set();
123 this.simulators.forEach((el: IiOSSimulator) => {
124 if (el.system.indexOf("iOS") >= 0) {
125 names.add(el.system);
126 }
127 });
128 return Array.from(names);
129 }
130
131 private getRunningSimulator(simulators: IiOSSimulator[]): IiOSSimulator | undefined {
132 return simulators.find(el => el.state === "Booted");
133 }
134}
135