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