microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/macos/macOSDebugModeManager.ts
103lines · 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 * 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 | |
| 33 | public setAppRemoteDebuggingSetting( |
| 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. |
| 40 | return this.findPListFileWithRetry(configuration, productName).then((plistFile: string) => { |
| 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 this.defaultsHelper.setPlistBooleanProperty( |
| 44 | plistFile, |
| 45 | MacOSDebugModeManager.REMOTE_DEBUGGING_FLAG_NAME, |
| 46 | enable, |
| 47 | ); |
| 48 | }); |
| 49 | } |
| 50 | |
| 51 | public getAppRemoteDebuggingSetting( |
| 52 | configuration?: string, |
| 53 | productName?: string, |
| 54 | ): Promise<boolean> { |
| 55 | return this.findPListFileWithRetry(configuration, productName).then((plistFile: string) => { |
| 56 | // Attempt to read from the file, but if the property is not defined then return the empty string |
| 57 | return this.plistBuddy |
| 58 | .readPlistProperty(plistFile, MacOSDebugModeManager.REMOTE_DEBUGGING_SETTING_NAME) |
| 59 | .then(remoteDebugEnabled => { |
| 60 | return remoteDebugEnabled === "true"; |
| 61 | }) |
| 62 | .catch(() => false); |
| 63 | }); |
| 64 | } |
| 65 | |
| 66 | protected tryOneAttemptToFindPListFile( |
| 67 | configuration?: string, |
| 68 | productName?: string, |
| 69 | ): Promise<string> { |
| 70 | return this.findPlistFile(configuration, productName).catch(reason => { |
| 71 | this.logger.debug(`Failed one attempt to find plist file: ${reason}`); |
| 72 | return ""; |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | private findPlistFile(configuration?: string, productName?: string): Promise<string> { |
| 77 | return this.plistBuddy |
| 78 | .getBundleId( |
| 79 | this.platformProjectRoot, |
| 80 | this.projectRoot, |
| 81 | PlatformType.macOS, |
| 82 | false, |
| 83 | configuration, |
| 84 | productName, |
| 85 | this.scheme, |
| 86 | ) // Find the name of the application |
| 87 | .then(bundleId => { |
| 88 | const plistFilePath = path.join( |
| 89 | homedir(), |
| 90 | "Library", |
| 91 | "Preferences", |
| 92 | `${bundleId}.plist`, |
| 93 | ); |
| 94 | return this.nodeFileSystem.exists(plistFilePath).then((exist: boolean) => { |
| 95 | if (!exist) { |
| 96 | throw new Error(`Unable to find plist file for ${bundleId}`); |
| 97 | } else { |
| 98 | return plistFilePath; |
| 99 | } |
| 100 | }); |
| 101 | }); |
| 102 | } |
| 103 | } |
| 104 | |