microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.5.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSDebugModeManager.ts

117lines · 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 { OutputChannelLogger } from "../log/OutputChannelLogger";
5import { PromiseUtil } from "../../common/node/promise";
6import { PlistBuddy } from "./plistBuddy";
7import { SimulatorPlist } from "./simulatorPlist";
8import * as nls from "vscode-nls";
9nls.config({
10 messageFormat: nls.MessageFormat.bundle,
11 bundleFormat: nls.BundleFormat.standalone,
12})();
13const localize = nls.loadMessageBundle();
14
15export class IOSDebugModeManager {
16 public static WEBSOCKET_EXECUTOR_NAME = "RCTWebSocketExecutor";
17 private static EXECUTOR_CLASS_SETTING_NAME = ":RCTDevMenu:executorClass";
18 private static REMOTE_DEBUGGING_SETTING_NAME = ":RCTDevMenu:isDebuggingRemotely";
19 private static MAX_RETRIES = 5;
20 private static DELAY_UNTIL_RETRY = 2000;
21 private logger: OutputChannelLogger = OutputChannelLogger.getMainChannel();
22
23 private projectRoot: string;
24 private iosProjectRoot: string;
25 private simulatorPlist: SimulatorPlist;
26
27 constructor(iosProjectRoot: string, projectRoot: string, scheme?: string) {
28 this.projectRoot = projectRoot;
29 this.iosProjectRoot = iosProjectRoot;
30 this.simulatorPlist = new SimulatorPlist(this.iosProjectRoot, this.projectRoot, scheme);
31 }
32
33 public setSimulatorRemoteDebuggingSetting(
34 enable: boolean,
35 configuration?: string,
36 productName?: string,
37 ): Promise<void> {
38 const plistBuddy = new PlistBuddy();
39
40 // Find the plistFile with the configuration setting
41 // There is a race here between us checking for the plist file, and the application starting up.
42 return this.findPListFile(configuration, productName).then((plistFile: string) => {
43 // Set the executorClass to be RCTWebSocketExecutor so on the next startup it will default into debug mode
44 // This is approximately equivalent to clicking the "Debug in Chrome" button
45
46 return (enable
47 ? plistBuddy.setPlistProperty(
48 plistFile,
49 IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME,
50 IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME,
51 )
52 : plistBuddy.deletePlistProperty(
53 plistFile,
54 IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME,
55 )
56 ).then(() =>
57 plistBuddy.setPlistBooleanProperty(
58 plistFile,
59 IOSDebugModeManager.REMOTE_DEBUGGING_SETTING_NAME,
60 enable,
61 ),
62 );
63 });
64 }
65
66 public getSimulatorRemoteDebuggingSetting(
67 configuration?: string,
68 productName?: string,
69 ): Promise<boolean> {
70 return this.findPListFile(configuration, productName).then((plistFile: string) => {
71 // Attempt to read from the file, but if the property is not defined then return the empty string
72 return Promise.all([
73 new PlistBuddy().readPlistProperty(
74 plistFile,
75 IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME,
76 ),
77 new PlistBuddy().readPlistProperty(
78 plistFile,
79 IOSDebugModeManager.REMOTE_DEBUGGING_SETTING_NAME,
80 ),
81 ])
82 .then(([executorClassName, remoteDebugEnabled]) => {
83 return (
84 executorClassName === IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME &&
85 remoteDebugEnabled === "true"
86 );
87 })
88 .catch(() => false);
89 });
90 }
91
92 public findPListFile(configuration?: string, productName?: string): Promise<string> {
93 const pu = new PromiseUtil();
94 const failureString = localize(
95 "UnableToFindPlistFileToConfigureDebugging",
96 "Unable to find plist file to configure debugging",
97 );
98
99 return pu.retryAsync(
100 () => this.tryOneAttemptToFindPListFile(configuration, productName), // Operation to retry until successful
101 (file: string) => file !== "", // Condition to check if the operation was successful, and this logic is done
102 IOSDebugModeManager.MAX_RETRIES,
103 IOSDebugModeManager.DELAY_UNTIL_RETRY,
104 failureString,
105 ); // Error to show in case all retries fail
106 }
107
108 private tryOneAttemptToFindPListFile(
109 configuration?: string,
110 productName?: string,
111 ): Promise<string> {
112 return this.simulatorPlist.findPlistFile(configuration, productName).catch(reason => {
113 this.logger.debug(`Failed one attempt to find plist file: ${reason}`);
114 return "";
115 });
116 }
117}
118