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