microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/iOSDebugModeManager.ts
82lines · 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 | | |
0a68f8dbArtem Egorov8 years ago | 6 | import {OutputChannelLogger} from "../log/OutputChannelLogger"; |
a9d96b7cdigeff10 years ago | 7 | import {PromiseUtil} from "../../common/node/promise"; |
| 8 | import {PlistBuddy} from "./plistBuddy"; | |
| 9 | import {SimulatorPlist} from "./simulatorPlist"; | |
d7d405aeYuri Skorokhodov7 years ago | 10 | import * as nls from "vscode-nls"; |
| 11 | const localize = nls.loadMessageBundle(); | |
a9d96b7cdigeff10 years ago | 12 | |
1ed272e1digeff10 years ago | 13 | export class IOSDebugModeManager { |
dbe130e4Jimmy Thomson10 years ago | 14 | public static WEBSOCKET_EXECUTOR_NAME = "RCTWebSocketExecutor"; |
a9d96b7cdigeff10 years ago | 15 | private static EXECUTOR_CLASS_SETTING_NAME = ":RCTDevMenu:executorClass"; |
278d8e56Vladimir Kotikov8 years ago | 16 | private static REMOTE_DEBUGGING_SETTING_NAME = ":RCTDevMenu:isDebuggingRemotely"; |
de32cf1edigeff10 years ago | 17 | private static MAX_RETRIES = 5; |
| 18 | private static DELAY_UNTIL_RETRY = 2000; | |
0a68f8dbArtem Egorov8 years ago | 19 | private logger: OutputChannelLogger = OutputChannelLogger.getMainChannel(); |
de32cf1edigeff10 years ago | 20 | |
a9d96b7cdigeff10 years ago | 21 | private projectRoot: string; |
1ed272e1digeff10 years ago | 22 | private simulatorPlist: SimulatorPlist; |
a9d96b7cdigeff10 years ago | 23 | |
a949e43fRuslan Bikkinin7 years ago | 24 | constructor(projectRoot: string, scheme?: string) { |
a9d96b7cdigeff10 years ago | 25 | this.projectRoot = projectRoot; |
a949e43fRuslan Bikkinin7 years ago | 26 | this.simulatorPlist = new SimulatorPlist(this.projectRoot, scheme); |
a9d96b7cdigeff10 years ago | 27 | } |
| 28 | | |
db6fd42aRuslan Bikkinin7 years ago | 29 | public setSimulatorRemoteDebuggingSetting(enable: boolean, configuration?: string, productName?: string): Q.Promise<void> { |
de32cf1edigeff10 years ago | 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. | |
db6fd42aRuslan Bikkinin7 years ago | 34 | return this.findPListFile(configuration, productName) |
de32cf1edigeff10 years ago | 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 | |
278d8e56Vladimir Kotikov8 years ago | 38 | |
| 39 | return (enable | |
dbe130e4Jimmy Thomson10 years ago | 40 | ? plistBuddy.setPlistProperty(plistFile, IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME, IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME) |
278d8e56Vladimir Kotikov8 years ago | 41 | : plistBuddy.deletePlistProperty(plistFile, IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME)) |
| 42 | .then(() => plistBuddy.setPlistBooleanProperty(plistFile, IOSDebugModeManager.REMOTE_DEBUGGING_SETTING_NAME, enable)); | |
de32cf1edigeff10 years ago | 43 | }); |
| 44 | } | |
| 45 | | |
db6fd42aRuslan Bikkinin7 years ago | 46 | public getSimulatorRemoteDebuggingSetting(configuration?: string, productName?: string): Q.Promise<boolean> { |
| 47 | return this.findPListFile(configuration, productName) | |
278d8e56Vladimir Kotikov8 years ago | 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 | }); | |
1ed272e1digeff10 years ago | 60 | } |
| 61 | | |
db6fd42aRuslan Bikkinin7 years ago | 62 | public findPListFile(configuration?: string, productName?: string): Q.Promise<string> { |
1ed272e1digeff10 years ago | 63 | const pu = new PromiseUtil(); |
d7d405aeYuri Skorokhodov7 years ago | 64 | const failureString = localize("UnableToFindPlistFileToConfigureDebugging", "Unable to find plist file to configure debugging"); |
1ed272e1digeff10 years ago | 65 | |
1d00ededdigeff10 years ago | 66 | return pu.retryAsync( |
| 67 | () => | |
db6fd42aRuslan Bikkinin7 years ago | 68 | this.tryOneAttemptToFindPListFile(configuration, productName), // Operation to retry until successful |
1ed272e1digeff10 years ago | 69 | (file: string) => |
5c8365a6Artem Egorov8 years ago | 70 | file !== "", // Condition to check if the operation was successful, and this logic is done |
1ed272e1digeff10 years ago | 71 | IOSDebugModeManager.MAX_RETRIES, |
| 72 | IOSDebugModeManager.DELAY_UNTIL_RETRY, | |
de32cf1edigeff10 years ago | 73 | failureString); // Error to show in case all retries fail |
a9d96b7cdigeff10 years ago | 74 | } |
dbe130e4Jimmy Thomson10 years ago | 75 | |
db6fd42aRuslan Bikkinin7 years ago | 76 | private tryOneAttemptToFindPListFile(configuration?: string, productName?: string): Q.Promise<string> { |
| 77 | return this.simulatorPlist.findPlistFile(configuration, productName).catch(reason => { | |
0a68f8dbArtem Egorov8 years ago | 78 | this.logger.debug(`Failed one attempt to find plist file: ${reason}`); |
5c8365a6Artem Egorov8 years ago | 79 | return ""; |
5edb6e49Digeff10 years ago | 80 | }); |
dbe130e4Jimmy Thomson10 years ago | 81 | } |
a9d96b7cdigeff10 years ago | 82 | } |