microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
8a67e140aee58da792b1e9c1c17358a98d5a3704

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/ios/iOSDebugModeManager.ts

80lines · 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 * as Q from "q";
5
6import {Log} from "../../common/log/log";
7import {LogLevel} from "../../common/log/logHelper";
8import {PromiseUtil} from "../../common/node/promise";
9import {PlistBuddy} from "./plistBuddy";
10import {SimulatorPlist} from "./simulatorPlist";
11
12export class IOSDebugModeManager {
13 public static WEBSOCKET_EXECUTOR_NAME = "RCTWebSocketExecutor";
14 private static EXECUTOR_CLASS_SETTING_NAME = ":RCTDevMenu:executorClass";
15 private static REMOTE_DEBUGGING_SETTING_NAME = ":RCTDevMenu:isDebuggingRemotely";
16 private static MAX_RETRIES = 5;
17 private static DELAY_UNTIL_RETRY = 2000;
18
19 private projectRoot: string;
20 private simulatorPlist: SimulatorPlist;
21
22 constructor(projectRoot: string) {
23 this.projectRoot = projectRoot;
24 this.simulatorPlist = new SimulatorPlist(this.projectRoot);
25 }
26
27 public setSimulatorRemoteDebuggingSetting(enable: boolean): Q.Promise<void> {
28 const plistBuddy = new PlistBuddy();
29
30 // Find the plistFile with the configuration setting
31 // There is a race here between us checking for the plist file, and the application starting up.
32 return this.findPListFile()
33 .then((plistFile: string) => {
34 // Set the executorClass to be RCTWebSocketExecutor so on the next startup it will default into debug mode
35 // This is approximately equivalent to clicking the "Debug in Chrome" button
36
37 return (enable
38 ? plistBuddy.setPlistProperty(plistFile, IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME, IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME)
39 : plistBuddy.deletePlistProperty(plistFile, IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME))
40 .then(() => plistBuddy.setPlistBooleanProperty(plistFile, IOSDebugModeManager.REMOTE_DEBUGGING_SETTING_NAME, enable));
41 });
42 }
43
44 public getSimulatorRemoteDebuggingSetting(): Q.Promise<boolean> {
45 return this.findPListFile()
46 .then((plistFile: string) => {
47 // Attempt to read from the file, but if the property is not defined then return the empty string
48 return Q.all([
49 new PlistBuddy().readPlistProperty(plistFile, IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME),
50 new PlistBuddy().readPlistProperty(plistFile, IOSDebugModeManager.REMOTE_DEBUGGING_SETTING_NAME),
51 ])
52 .spread((executorClassName: string, remoteDebugEnabled: string) => {
53 return executorClassName === IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME
54 && remoteDebugEnabled === "true";
55 })
56 .catch(() => false);
57 });
58 }
59
60 public findPListFile(): Q.Promise<string> {
61 const pu = new PromiseUtil();
62 const failureString = `Unable to find plist file to configure debugging`;
63
64 return pu.retryAsync(
65 () =>
66 this.tryOneAttemptToFindPListFile(), // Operation to retry until successful
67 (file: string) =>
68 file !== "", // Condition to check if the operation was successful, and this logic is done
69 IOSDebugModeManager.MAX_RETRIES,
70 IOSDebugModeManager.DELAY_UNTIL_RETRY,
71 failureString); // Error to show in case all retries fail
72 }
73
74 private tryOneAttemptToFindPListFile(): Q.Promise<string> {
75 return this.simulatorPlist.findPlistFile().catch(reason => {
76 Log.logInternalMessage(LogLevel.Info, `Failed one attempt to find plist file: ${reason}`);
77 return "";
78 });
79 }
80}
81