microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.11.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSDeviceTracker.ts

172lines · 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
09f6024fHeniker4 years ago4import { ChildProcess, execFile } from "child_process";
4bb0956eRedMickey5 years ago5import { AbstractDeviceTracker } from "../abstractDeviceTracker";
6import { DeviceStorage } from "../networkInspector/devices/deviceStorage";
7import { ClientOS } from "../networkInspector/clientUtils";
8import { IOSClienDevice } from "../networkInspector/devices/iOSClienDevice";
9import { findFileInFolderHierarchy } from "../../common/extensionHelper";
09f6024fHeniker4 years ago10import iosUtil, { DeviceTarget, isXcodeDetected } from "./iOSContainerUtility";
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();
09f6024fHeniker4 years ago20this.portForwardingClientPath = `${
21findFileInFolderHierarchy(__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 {
09f6024fHeniker4 years ago49const currentDevicesIds = new Set(
4bb0956eRedMickey5 years ago50[...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(
09f6024fHeniker4 years ago122`Port forwarding app failed to start: ${String(
123err?.message,
124)}, ${stdout}, ${stderr}`,
4bb0956eRedMickey5 years ago125);
126}
127},
128);
129this.logger.debug(`Port forwarding app started for ${port} port`);
130childProcess.addListener("error", err => {
131this.logger.error("Port forwarding app error", err);
132});
133childProcess.addListener("exit", code => {
09f6024fHeniker4 years ago134this.logger.debug(`Port forwarding app exited with code ${String(code)}`);
4bb0956eRedMickey5 years ago135});
136return childProcess;
137}
138
139/**
140* @preserve
141* End region: https://github.com/facebook/flipper/blob/v0.79.1/desktop/app/src/dispatcher/iOSDevice.tsx#L63-L79
142*/
143
4cd25962JiglioNero4 years ago144private async getRunningSimulators(): Promise<IDebuggableIOSTarget[]> {
145return (await this.iOSTargetManager.getTargetList(
146target => target.isOnline,
147)) as IDebuggableIOSTarget[];
4bb0956eRedMickey5 years ago148}
149
150/**
151* @preserve
152* Start region: the code is borrowed from https://github.com/facebook/flipper/blob/v0.79.1/desktop/app/src/dispatcher/iOSDevice.tsx#L227-L232
153*
154* Copyright (c) Facebook, Inc. and its affiliates.
155*
156* This source code is licensed under the MIT license found in the
157* LICENSE file in the root directory of this source tree.
158*
159* @format
160*/
161private getActiveDevices(): Promise<Array<DeviceTarget>> {
162return iosUtil.targets().catch(e => {
163this.logger.error(e.message);
164return [];
165});
166}
167
168/**
169* @preserve
170* End region: https://github.com/facebook/flipper/blob/v0.79.1/desktop/app/src/dispatcher/iOSDevice.tsx#L227-L232
171*/
172}