microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.8.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSDeviceTracker.ts

170lines · modeblame

4bb0956eRedMickey5 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT license. See LICENSE file in the project root for details.
3
4import { AbstractDeviceTracker } from "../abstractDeviceTracker";
4cd25962JiglioNero4 years ago5import iosUtil, { DeviceTarget, isXcodeDetected } from "./iOSContainerUtility";
4bb0956eRedMickey5 years ago6import { DeviceStorage } from "../networkInspector/devices/deviceStorage";
7import { ClientOS } from "../networkInspector/clientUtils";
8import { IOSClienDevice } from "../networkInspector/devices/iOSClienDevice";
9import { findFileInFolderHierarchy } from "../../common/extensionHelper";
10import { ChildProcess, execFile } from "child_process";
4cd25962JiglioNero4 years ago11import { IDebuggableIOSTarget, IOSTargetManager } from "./iOSTargetManager";
4bb0956eRedMickey5 years ago12
13export class IOSDeviceTracker extends AbstractDeviceTracker {
14private readonly portForwardingClientPath: string;
4cd25962JiglioNero4 years ago15private iOSTargetManager: IOSTargetManager;
4bb0956eRedMickey5 years ago16private portForwarders: Array<ChildProcess>;
17
18constructor() {
19super();
20this.portForwardingClientPath =
21(findFileInFolderHierarchy(__dirname, "static/PortForwardingMacApp.app") || __dirname) +
22"/Contents/MacOS/PortForwardingMacApp";
4cd25962JiglioNero4 years ago23this.iOSTargetManager = new IOSTargetManager();
4bb0956eRedMickey5 years ago24this.portForwarders = [];
25}
26
27public async start(): Promise<void> {
28this.logger.debug("Start iOS device tracker");
4cd25962JiglioNero4 years ago29if (await isXcodeDetected()) {
4bb0956eRedMickey5 years ago30this.startDevicePortForwarders();
31}
32await this.queryDevicesLoop();
33}
34
35public stop(): void {
36this.logger.debug("Stop iOS device tracker");
37this.isStop = true;
38this.portForwarders.forEach(process => process.kill());
39}
40
41protected async queryDevices(): Promise<void> {
42const simulators = await this.getRunningSimulators();
4cd25962JiglioNero4 years ago43this.processDevices(simulators, true);
4bb0956eRedMickey5 years ago44const devices = await this.getActiveDevices();
4cd25962JiglioNero4 years ago45this.processDevices(devices, false);
4bb0956eRedMickey5 years ago46}
47
4cd25962JiglioNero4 years ago48private processDevices(activeDevices: Array<DeviceTarget>, isVirtualTarget: boolean): void {
4bb0956eRedMickey5 years ago49let currentDevicesIds = new Set(
50[...DeviceStorage.devices.entries()]
4cd25962JiglioNero4 years ago51.filter(
52entry =>
53entry[1] instanceof IOSClienDevice &&
54entry[1].isVirtualTarget === isVirtualTarget,
55)
4bb0956eRedMickey5 years ago56.map(entry => entry[0]),
57);
58
59for (const activeDevice of activeDevices) {
60if (currentDevicesIds.has(activeDevice.id)) {
61currentDevicesIds.delete(activeDevice.id);
62} else {
4cd25962JiglioNero4 years ago63const iosDevice = new IOSClienDevice(
4bb0956eRedMickey5 years ago64activeDevice.id,
4cd25962JiglioNero4 years ago65isVirtualTarget,
4bb0956eRedMickey5 years ago66ClientOS.iOS,
4cd25962JiglioNero4 years ago67activeDevice.isOnline,
4bb0956eRedMickey5 years ago68activeDevice.name,
69);
4cd25962JiglioNero4 years ago70DeviceStorage.devices.set(iosDevice.id, iosDevice);
4bb0956eRedMickey5 years ago71}
72}
73
74currentDevicesIds.forEach(oldDeviceId => {
75DeviceStorage.devices.delete(oldDeviceId);
76});
77}
78
79/**
80* @preserve
81* Start region: The code is borrowed from https://github.com/facebook/flipper/blob/v0.79.1/desktop/app/src/dispatcher/iOSDevice.tsx#L81-L88
82*
83* Copyright (c) Facebook, Inc. and its affiliates.
84*
85* This source code is licensed under the MIT license found in the
86* LICENSE file in the root directory of this source tree.
87*
88* @format
89*/
90private startDevicePortForwarders(): void {
91if (this.portForwarders.length > 0) {
92// Only ever start them once.
93return;
94}
95// start port forwarding server for real device connections
96this.portForwarders = [this.forwardPort(8089, 8079), this.forwardPort(8088, 8078)];
97}
98
99/**
100* @preserve
101* End region: https://github.com/facebook/flipper/blob/v0.79.1/desktop/app/src/dispatcher/iOSDevice.tsx#L81-L88
102*/
103
104/**
105* @preserve
106* Start region: the code is borrowed from https://github.com/facebook/flipper/blob/v0.79.1/desktop/app/src/dispatcher/iOSDevice.tsx#L63-L79
107*
108* Copyright (c) Facebook, Inc. and its affiliates.
109*
110* This source code is licensed under the MIT license found in the
111* LICENSE file in the root directory of this source tree.
112*
113* @format
114*/
115private forwardPort(port: number, multiplexChannelPort: number): ChildProcess {
116const childProcess = execFile(
117this.portForwardingClientPath,
118[`-portForward=${port}`, `-multiplexChannelPort=${multiplexChannelPort}`],
119(err, stdout, stderr) => {
120if (!err?.killed) {
121this.logger.error(
122`Port forwarding app failed to start: ${err?.message}, ${stdout}, ${stderr}`,
123);
124}
125},
126);
127this.logger.debug(`Port forwarding app started for ${port} port`);
128childProcess.addListener("error", err => {
129this.logger.error("Port forwarding app error", err);
130});
131childProcess.addListener("exit", code => {
132this.logger.debug(`Port forwarding app exited with code ${code}`);
133});
134return childProcess;
135}
136
137/**
138* @preserve
139* End region: https://github.com/facebook/flipper/blob/v0.79.1/desktop/app/src/dispatcher/iOSDevice.tsx#L63-L79
140*/
141
4cd25962JiglioNero4 years ago142private async getRunningSimulators(): Promise<IDebuggableIOSTarget[]> {
143return (await this.iOSTargetManager.getTargetList(
144target => target.isOnline,
145)) as IDebuggableIOSTarget[];
4bb0956eRedMickey5 years ago146}
147
148/**
149* @preserve
150* Start region: the code is borrowed from https://github.com/facebook/flipper/blob/v0.79.1/desktop/app/src/dispatcher/iOSDevice.tsx#L227-L232
151*
152* Copyright (c) Facebook, Inc. and its affiliates.
153*
154* This source code is licensed under the MIT license found in the
155* LICENSE file in the root directory of this source tree.
156*
157* @format
158*/
159private getActiveDevices(): Promise<Array<DeviceTarget>> {
160return iosUtil.targets().catch(e => {
161this.logger.error(e.message);
162return [];
163});
164}
165
166/**
167* @preserve
168* End region: https://github.com/facebook/flipper/blob/v0.79.1/desktop/app/src/dispatcher/iOSDevice.tsx#L227-L232
169*/
170}