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