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