microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/ios/iOSDebugModeManager.ts
64lines · 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 {PromiseUtil} from "../../common/node/promise"; |
| 7 | import {PlistBuddy} from "./plistBuddy"; |
| 8 | import {SimulatorPlist} from "./simulatorPlist"; |
| 9 | |
| 10 | export class IOSDebugModeManager { |
| 11 | public static WEBSOCKET_EXECUTOR_NAME = "RCTWebSocketExecutor"; |
| 12 | private static EXECUTOR_CLASS_SETTING_NAME = ":RCTDevMenu:executorClass"; |
| 13 | private static MAX_RETRIES = 5; |
| 14 | private static DELAY_UNTIL_RETRY = 2000; |
| 15 | |
| 16 | private projectRoot: string; |
| 17 | private simulatorPlist: SimulatorPlist; |
| 18 | |
| 19 | constructor(projectRoot: string) { |
| 20 | this.projectRoot = projectRoot; |
| 21 | this.simulatorPlist = new SimulatorPlist(this.projectRoot); |
| 22 | } |
| 23 | |
| 24 | public setSimulatorJSDebuggingModeSetting(enable: boolean): Q.Promise<void> { |
| 25 | const plistBuddy = new PlistBuddy(); |
| 26 | |
| 27 | // Find the plistFile with the configuration setting |
| 28 | // There is a race here between us checking for the plist file, and the application starting up. |
| 29 | return this.findPListFile() |
| 30 | .then((plistFile: string) => { |
| 31 | // Set the executorClass to be RCTWebSocketExecutor so on the next startup it will default into debug mode |
| 32 | // This is approximately equivalent to clicking the "Debug in Chrome" button |
| 33 | return enable |
| 34 | ? plistBuddy.setPlistProperty(plistFile, IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME, IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME) |
| 35 | : plistBuddy.deletePlistProperty(plistFile, IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME); |
| 36 | }); |
| 37 | } |
| 38 | |
| 39 | public getSimulatorJSDebuggingModeSetting(): Q.Promise<string> { |
| 40 | return this.findPListFile().then((plistFile: string) => { |
| 41 | // Attempt to read from the file, but if the property is not defined then return the empty string |
| 42 | return new PlistBuddy().readPlistProperty(plistFile, IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME) |
| 43 | .catch(() => ""); |
| 44 | }); |
| 45 | } |
| 46 | |
| 47 | public findPListFile(): Q.Promise<string> { |
| 48 | const pu = new PromiseUtil(); |
| 49 | const failureString = `Unable to find plist file to configure debugging`; |
| 50 | |
| 51 | return pu.retryAsync( |
| 52 | () => |
| 53 | this.tryOneAttemptToFindPListFile(), // Operation to retry until successful |
| 54 | (file: string) => |
| 55 | file !== null, // Condition to check if the operation was successful, and this logic is done |
| 56 | IOSDebugModeManager.MAX_RETRIES, |
| 57 | IOSDebugModeManager.DELAY_UNTIL_RETRY, |
| 58 | failureString); // Error to show in case all retries fail |
| 59 | } |
| 60 | |
| 61 | private tryOneAttemptToFindPListFile() { |
| 62 | return this.simulatorPlist.findPlistFile().catch((): string => null); |
| 63 | } |
| 64 | } |
| 65 | |