microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2c19da7f131d11b4265a94fe25139194a565116e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSDebugModeManager.ts

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