microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4f8669f9862e5f7db84d8d4f84b7c0d2006d7bb6

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/ios/iOSDebugModeManager.ts

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