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