microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/iOSDebugModeManager.ts
92lines · 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 { ApplePlatformDebugModeManager } from "../applePlatformDebugModeManager"; |
| 5 | import { PlistBuddy } from "./plistBuddy"; |
| 6 | import { SimulatorPlist } from "./simulatorPlist"; |
| 7 | |
| 8 | export class IOSDebugModeManager extends ApplePlatformDebugModeManager { |
| 9 | public static WEBSOCKET_EXECUTOR_NAME = "RCTWebSocketExecutor"; |
| 10 | private static EXECUTOR_CLASS_SETTING_NAME = ":RCTDevMenu:executorClass"; |
| 11 | |
| 12 | private simulatorPlist: SimulatorPlist; |
| 13 | |
| 14 | constructor(iosProjectRoot: string, projectRoot: string, scheme?: string) { |
| 15 | super(iosProjectRoot, projectRoot); |
| 16 | this.projectRoot = projectRoot; |
| 17 | this.simulatorPlist = new SimulatorPlist( |
| 18 | this.platformProjectRoot, |
| 19 | this.projectRoot, |
| 20 | scheme, |
| 21 | ); |
| 22 | } |
| 23 | |
| 24 | public setAppRemoteDebuggingSetting( |
| 25 | enable: boolean, |
| 26 | configuration?: string, |
| 27 | productName?: string, |
| 28 | ): Promise<void> { |
| 29 | const plistBuddy = new PlistBuddy(); |
| 30 | |
| 31 | // Find the plistFile with the configuration setting |
| 32 | // There is a race here between us checking for the plist file, and the application starting up. |
| 33 | return this.findPListFileWithRetry(configuration, productName).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 |
| 36 | |
| 37 | return (enable |
| 38 | ? plistBuddy.setPlistProperty( |
| 39 | plistFile, |
| 40 | IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME, |
| 41 | IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME, |
| 42 | ) |
| 43 | : plistBuddy.deletePlistProperty( |
| 44 | plistFile, |
| 45 | IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME, |
| 46 | ) |
| 47 | ).then(() => |
| 48 | plistBuddy.setPlistBooleanProperty( |
| 49 | plistFile, |
| 50 | IOSDebugModeManager.REMOTE_DEBUGGING_SETTING_NAME, |
| 51 | enable, |
| 52 | ), |
| 53 | ); |
| 54 | }); |
| 55 | } |
| 56 | |
| 57 | public getAppRemoteDebuggingSetting( |
| 58 | configuration?: string, |
| 59 | productName?: string, |
| 60 | ): Promise<boolean> { |
| 61 | return this.findPListFileWithRetry(configuration, productName).then((plistFile: string) => { |
| 62 | // Attempt to read from the file, but if the property is not defined then return the empty string |
| 63 | return Promise.all([ |
| 64 | new PlistBuddy().readPlistProperty( |
| 65 | plistFile, |
| 66 | IOSDebugModeManager.EXECUTOR_CLASS_SETTING_NAME, |
| 67 | ), |
| 68 | new PlistBuddy().readPlistProperty( |
| 69 | plistFile, |
| 70 | IOSDebugModeManager.REMOTE_DEBUGGING_SETTING_NAME, |
| 71 | ), |
| 72 | ]) |
| 73 | .then(([executorClassName, remoteDebugEnabled]) => { |
| 74 | return ( |
| 75 | executorClassName === IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME && |
| 76 | remoteDebugEnabled === "true" |
| 77 | ); |
| 78 | }) |
| 79 | .catch(() => false); |
| 80 | }); |
| 81 | } |
| 82 | |
| 83 | protected tryOneAttemptToFindPListFile( |
| 84 | configuration?: string, |
| 85 | productName?: string, |
| 86 | ): Promise<string> { |
| 87 | return this.simulatorPlist.findPlistFile(configuration, productName).catch(reason => { |
| 88 | this.logger.debug(`Failed one attempt to find plist file: ${reason}`); |
| 89 | return ""; |
| 90 | }); |
| 91 | } |
| 92 | } |
| 93 | |