microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/iOSDebugModeManager.ts
117lines · 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 | | |
34472878RedMickey5 years ago | 4 | import { OutputChannelLogger } from "../log/OutputChannelLogger"; |
| 5 | import { PromiseUtil } from "../../common/node/promise"; | |
| 6 | import { PlistBuddy } from "./plistBuddy"; | |
| 7 | import { SimulatorPlist } from "./simulatorPlist"; | |
d7d405aeYuri Skorokhodov7 years ago | 8 | import * as nls from "vscode-nls"; |
34472878RedMickey5 years ago | 9 | nls.config({ |
| 10 | messageFormat: nls.MessageFormat.bundle, | |
| 11 | bundleFormat: nls.BundleFormat.standalone, | |
| 12 | })(); | |
d7d405aeYuri Skorokhodov7 years ago | 13 | const localize = nls.loadMessageBundle(); |
a9d96b7cdigeff10 years ago | 14 | |
1ed272e1digeff10 years ago | 15 | export class IOSDebugModeManager { |
dbe130e4Jimmy Thomson10 years ago | 16 | public static WEBSOCKET_EXECUTOR_NAME = "RCTWebSocketExecutor"; |
a9d96b7cdigeff10 years ago | 17 | private static EXECUTOR_CLASS_SETTING_NAME = ":RCTDevMenu:executorClass"; |
278d8e56Vladimir Kotikov8 years ago | 18 | private static REMOTE_DEBUGGING_SETTING_NAME = ":RCTDevMenu:isDebuggingRemotely"; |
de32cf1edigeff10 years ago | 19 | private static MAX_RETRIES = 5; |
| 20 | private static DELAY_UNTIL_RETRY = 2000; | |
0a68f8dbArtem Egorov8 years ago | 21 | private logger: OutputChannelLogger = OutputChannelLogger.getMainChannel(); |
de32cf1edigeff10 years ago | 22 | |
a9d96b7cdigeff10 years ago | 23 | private projectRoot: string; |
764655c9Yuri Skorokhodov6 years ago | 24 | private iosProjectRoot: string; |
1ed272e1digeff10 years ago | 25 | private simulatorPlist: SimulatorPlist; |
a9d96b7cdigeff10 years ago | 26 | |
764655c9Yuri Skorokhodov6 years ago | 27 | constructor(iosProjectRoot: string, projectRoot: string, scheme?: string) { |
a9d96b7cdigeff10 years ago | 28 | this.projectRoot = projectRoot; |
764655c9Yuri Skorokhodov6 years ago | 29 | this.iosProjectRoot = iosProjectRoot; |
| 30 | this.simulatorPlist = new SimulatorPlist(this.iosProjectRoot, this.projectRoot, scheme); | |
a9d96b7cdigeff10 years ago | 31 | } |
| 32 | | |
34472878RedMickey5 years ago | 33 | public setSimulatorRemoteDebuggingSetting( |
| 34 | enable: boolean, | |
| 35 | configuration?: string, | |
| 36 | productName?: string, | |
| 37 | ): Promise<void> { | |
de32cf1edigeff10 years ago | 38 | const plistBuddy = new PlistBuddy(); |
| 39 | | |
| 40 | // Find the plistFile with the configuration setting | |
| 41 | // There is a race here between us checking for the plist file, and the application starting up. | |
34472878RedMickey5 years ago | 42 | return this.findPListFile(configuration, productName).then((plistFile: string) => { |
| 43 | // Set the executorClass to be RCTWebSocketExecutor so on the next startup it will default into debug mode | |
| 44 | // This is approximately equivalent to clicking the "Debug in Chrome" button | |
278d8e56Vladimir Kotikov8 years ago | 45 | |
34472878RedMickey5 years ago | 46 | return (enable |
| 47 | ? plistBuddy.setPlistProperty( | |
| 48 | plistFile, | |
| 49 | IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME, | |
| 50 | IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME, | |
| 51 | ) | |
| 52 | : plistBuddy.deletePlistProperty( | |
| 53 | plistFile, | |
| 54 | IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME, | |
| 55 | ) | |
| 56 | ).then(() => | |
| 57 | plistBuddy.setPlistBooleanProperty( | |
| 58 | plistFile, | |
| 59 | IOSDebugModeManager.REMOTE_DEBUGGING_SETTING_NAME, | |
| 60 | enable, | |
| 61 | ), | |
| 62 | ); | |
| 63 | }); | |
de32cf1edigeff10 years ago | 64 | } |
| 65 | | |
34472878RedMickey5 years ago | 66 | public getSimulatorRemoteDebuggingSetting( |
| 67 | configuration?: string, | |
| 68 | productName?: string, | |
| 69 | ): Promise<boolean> { | |
| 70 | return this.findPListFile(configuration, productName).then((plistFile: string) => { | |
| 71 | // Attempt to read from the file, but if the property is not defined then return the empty string | |
| 72 | return Promise.all([ | |
| 73 | new PlistBuddy().readPlistProperty( | |
| 74 | plistFile, | |
| 75 | IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME, | |
| 76 | ), | |
| 77 | new PlistBuddy().readPlistProperty( | |
| 78 | plistFile, | |
| 79 | IOSDebugModeManager.REMOTE_DEBUGGING_SETTING_NAME, | |
| 80 | ), | |
| 81 | ]) | |
| 82 | .then(([executorClassName, remoteDebugEnabled]) => { | |
| 83 | return ( | |
| 84 | executorClassName === IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME && | |
| 85 | remoteDebugEnabled === "true" | |
| 86 | ); | |
| 87 | }) | |
| 88 | .catch(() => false); | |
| 89 | }); | |
1ed272e1digeff10 years ago | 90 | } |
| 91 | | |
ce5e88eeYuri Skorokhodov5 years ago | 92 | public findPListFile(configuration?: string, productName?: string): Promise<string> { |
1ed272e1digeff10 years ago | 93 | const pu = new PromiseUtil(); |
34472878RedMickey5 years ago | 94 | const failureString = localize( |
| 95 | "UnableToFindPlistFileToConfigureDebugging", | |
| 96 | "Unable to find plist file to configure debugging", | |
| 97 | ); | |
1ed272e1digeff10 years ago | 98 | |
1d00ededdigeff10 years ago | 99 | return pu.retryAsync( |
34472878RedMickey5 years ago | 100 | () => this.tryOneAttemptToFindPListFile(configuration, productName), // Operation to retry until successful |
| 101 | (file: string) => file !== "", // Condition to check if the operation was successful, and this logic is done | |
1ed272e1digeff10 years ago | 102 | IOSDebugModeManager.MAX_RETRIES, |
| 103 | IOSDebugModeManager.DELAY_UNTIL_RETRY, | |
34472878RedMickey5 years ago | 104 | failureString, |
| 105 | ); // Error to show in case all retries fail | |
a9d96b7cdigeff10 years ago | 106 | } |
dbe130e4Jimmy Thomson10 years ago | 107 | |
34472878RedMickey5 years ago | 108 | private tryOneAttemptToFindPListFile( |
| 109 | configuration?: string, | |
| 110 | productName?: string, | |
| 111 | ): Promise<string> { | |
db6fd42aRuslan Bikkinin7 years ago | 112 | return this.simulatorPlist.findPlistFile(configuration, productName).catch(reason => { |
0a68f8dbArtem Egorov8 years ago | 113 | this.logger.debug(`Failed one attempt to find plist file: ${reason}`); |
5c8365a6Artem Egorov8 years ago | 114 | return ""; |
5edb6e49Digeff10 years ago | 115 | }); |
dbe130e4Jimmy Thomson10 years ago | 116 | } |
a9d96b7cdigeff10 years ago | 117 | } |