microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/macos/macOSDebugModeManager.ts
97lines · modeblame
1c2424f4RedMickey5 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 path from "path"; | |
| 5 | import { homedir } from "os"; | |
| 6 | import { ApplePlatformDebugModeManager } from "../applePlatformDebugModeManager"; | |
| 7 | import { PlistBuddy } from "../ios/plistBuddy"; | |
| 8 | import { FileSystem } from "../../common/node/fileSystem"; | |
| 9 | import { PlatformType } from "../launchArgs"; | |
| 10 | import { DefaultsHelper } from "./defaultsHelper"; | |
| 11 | | |
| 12 | export class MacOSDebugModeManager extends ApplePlatformDebugModeManager { | |
| 13 | protected static REMOTE_DEBUGGING_FLAG_NAME = "isDebuggingRemotely"; | |
| 14 | | |
| 15 | private scheme?: string; | |
| 16 | private nodeFileSystem: FileSystem; | |
| 17 | private plistBuddy: PlistBuddy; | |
| 18 | private defaultsHelper: DefaultsHelper; | |
| 19 | | |
| 20 | constructor( | |
| 21 | macosProjectRoot: string, | |
| 22 | projectRoot: string, | |
| 23 | scheme?: string, | |
| 24 | { nodeFileSystem = new FileSystem(), plistBuddy = undefined } = {}, | |
| 25 | ) { | |
| 26 | super(macosProjectRoot, projectRoot); | |
| 27 | this.scheme = scheme; | |
| 28 | this.nodeFileSystem = nodeFileSystem; | |
| 29 | this.plistBuddy = plistBuddy || new PlistBuddy(); | |
| 30 | this.defaultsHelper = new DefaultsHelper(); | |
| 31 | } | |
| 32 | | |
0d77292aJiglioNero4 years ago | 33 | public async setAppRemoteDebuggingSetting( |
1c2424f4RedMickey5 years ago | 34 | enable: boolean, |
| 35 | configuration?: string, | |
| 36 | productName?: string, | |
| 37 | ): Promise<void> { | |
| 38 | // Find the plistFile with the configuration setting | |
| 39 | // There is a race here between us checking for the plist file, and the application starting up. | |
0d77292aJiglioNero4 years ago | 40 | const plistFile = await this.findPListFileWithRetry(configuration, productName); |
| 41 | // Set the "isDebuggingRemotely" flag to "true", so on the next startup the application will default into debug mode | |
| 42 | // This is approximately equivalent to clicking the "Debug in Chrome" button | |
| 43 | return await this.defaultsHelper.setPlistBooleanProperty( | |
| 44 | plistFile, | |
| 45 | MacOSDebugModeManager.REMOTE_DEBUGGING_FLAG_NAME, | |
| 46 | enable, | |
| 47 | ); | |
1c2424f4RedMickey5 years ago | 48 | } |
| 49 | | |
0d77292aJiglioNero4 years ago | 50 | public async getAppRemoteDebuggingSetting( |
1c2424f4RedMickey5 years ago | 51 | configuration?: string, |
| 52 | productName?: string, | |
| 53 | ): Promise<boolean> { | |
0d77292aJiglioNero4 years ago | 54 | const plistFile = await this.findPListFileWithRetry(configuration, productName); |
| 55 | try { | |
1c2424f4RedMickey5 years ago | 56 | // Attempt to read from the file, but if the property is not defined then return the empty string |
0d77292aJiglioNero4 years ago | 57 | const remoteDebugEnabled = await this.plistBuddy.readPlistProperty( |
| 58 | plistFile, | |
| 59 | MacOSDebugModeManager.REMOTE_DEBUGGING_SETTING_NAME, | |
| 60 | ); | |
| 61 | return remoteDebugEnabled === "true"; | |
| 62 | } catch (e) { | |
| 63 | return false; | |
| 64 | } | |
1c2424f4RedMickey5 years ago | 65 | } |
| 66 | | |
0d77292aJiglioNero4 years ago | 67 | protected async tryOneAttemptToFindPListFile( |
1c2424f4RedMickey5 years ago | 68 | configuration?: string, |
| 69 | productName?: string, | |
| 70 | ): Promise<string> { | |
0d77292aJiglioNero4 years ago | 71 | try { |
| 72 | return await this.findPlistFile(configuration, productName); | |
| 73 | } catch (reason) { | |
09f6024fHeniker4 years ago | 74 | this.logger.debug(`Failed one attempt to find plist file: ${String(reason)}`); |
1c2424f4RedMickey5 years ago | 75 | return ""; |
0d77292aJiglioNero4 years ago | 76 | } |
1c2424f4RedMickey5 years ago | 77 | } |
| 78 | | |
0d77292aJiglioNero4 years ago | 79 | private async findPlistFile(configuration?: string, productName?: string): Promise<string> { |
| 80 | const bundleId = await this.plistBuddy.getBundleId( | |
| 81 | this.platformProjectRoot, | |
| 82 | this.projectRoot, | |
| 83 | PlatformType.macOS, | |
| 84 | false, | |
| 85 | configuration, | |
| 86 | productName, | |
| 87 | this.scheme, | |
| 88 | ); // Find the name of the application | |
| 89 | const plistFilePath = path.join(homedir(), "Library", "Preferences", `${bundleId}.plist`); | |
| 90 | const exist = await this.nodeFileSystem.exists(plistFilePath); | |
| 91 | if (!exist) { | |
| 92 | throw new Error(`Unable to find plist file for ${bundleId}`); | |
| 93 | } else { | |
| 94 | return plistFilePath; | |
| 95 | } | |
1c2424f4RedMickey5 years ago | 96 | } |
| 97 | } |