microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1.7.0

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/extension/macos/macOSDebugModeManager.ts

97lines · 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
4import * as path from "path";
5import { homedir } from "os";
6import { ApplePlatformDebugModeManager } from "../applePlatformDebugModeManager";
7import { PlistBuddy } from "../ios/plistBuddy";
8import { FileSystem } from "../../common/node/fileSystem";
9import { PlatformType } from "../launchArgs";
10import { DefaultsHelper } from "./defaultsHelper";
11
12export 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 async 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 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 );
48 }
49
50 public async getAppRemoteDebuggingSetting(
51 configuration?: string,
52 productName?: string,
53 ): Promise<boolean> {
54 const plistFile = await this.findPListFileWithRetry(configuration, productName);
55 try {
56 // Attempt to read from the file, but if the property is not defined then return the empty string
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 }
65 }
66
67 protected async tryOneAttemptToFindPListFile(
68 configuration?: string,
69 productName?: string,
70 ): Promise<string> {
71 try {
72 return await this.findPlistFile(configuration, productName);
73 } catch (reason) {
74 this.logger.debug(`Failed one attempt to find plist file: ${reason}`);
75 return "";
76 }
77 }
78
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 }
96 }
97}
98