microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
4cd259621ddfbd348fade892a2f3ee87fd1924c5

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/extension/macos/macOSPlatform.ts

219lines · 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 semver from "semver";
5import * as path from "path";
6import { GeneralPlatform, MobilePlatformDeps, TargetType } from "../generalPlatform";
7import { MacOSDebugModeManager } from "./macOSDebugModeManager";
8import { ImacOSRunOptions, PlatformType } from "../launchArgs";
9import { OutputVerifier, PatternToFailure } from "../../common/outputVerifier";
10import { TelemetryHelper } from "../../common/telemetryHelper";
11import { CommandExecutor } from "../../common/commandExecutor";
12import { InternalErrorCode } from "../../common/error/internalErrorCode";
13import { PlistBuddy } from "../ios/plistBuddy";
14import { ChildProcess } from "../../common/node/childProcess";
15
16/**
17 * macOS specific platform implementation for debugging RN applications.
18 */
19export class MacOSPlatform extends GeneralPlatform {
20 private static SUCCESS_PATTERNS = ["Launching app"];
21 private static FAILURE_PATTERNS: PatternToFailure[] = [
22 {
23 pattern: "Unrecognized command 'run-macos'",
24 errorCode: InternalErrorCode.ReactNativemacOSIsNotInstalled,
25 },
26 ];
27
28 public static DEFAULT_MACOS_PROJECT_RELATIVE_PATH = "macos";
29
30 private macosProjectRoot: string;
31 private plistBuddy: PlistBuddy;
32 private macOSDebugModeManager: MacOSDebugModeManager;
33
34 constructor(protected runOptions: ImacOSRunOptions, platformDeps: MobilePlatformDeps = {}) {
35 super(runOptions, platformDeps);
36
37 const macosProjectFolderPath = MacOSPlatform.getOptFromRunArgs(
38 this.runArguments,
39 "--project-path",
40 false,
41 );
42 this.macosProjectRoot = path.join(
43 this.projectPath,
44 macosProjectFolderPath || MacOSPlatform.DEFAULT_MACOS_PROJECT_RELATIVE_PATH,
45 );
46 this.plistBuddy = new PlistBuddy();
47
48 const schemeFromArgs = MacOSPlatform.getOptFromRunArgs(
49 this.runArguments,
50 "--scheme",
51 false,
52 );
53 this.macOSDebugModeManager = new MacOSDebugModeManager(
54 this.macosProjectRoot,
55 this.projectPath,
56 schemeFromArgs ? schemeFromArgs : this.runOptions.scheme,
57 );
58 }
59
60 public async runApp(): Promise<void> {
61 let extProps: any = {
62 platform: {
63 value: PlatformType.macOS,
64 isPii: false,
65 },
66 };
67
68 if (this.runOptions.isDirect) {
69 extProps.isDirect = {
70 value: true,
71 isPii: false,
72 };
73 }
74
75 extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(
76 this.runOptions,
77 this.runOptions.reactNativeVersions,
78 extProps,
79 );
80
81 await TelemetryHelper.generate("MacOSPlatform.runApp", extProps, async () => {
82 const env = GeneralPlatform.getEnvArgument(
83 process.env,
84 this.runOptions.env,
85 this.runOptions.envFile,
86 );
87
88 if (
89 !semver.valid(
90 this.runOptions.reactNativeVersions.reactNativeVersion,
91 ) /*Custom RN implementations should support this flag*/ ||
92 semver.gte(
93 this.runOptions.reactNativeVersions.reactNativeVersion,
94 MacOSPlatform.NO_PACKAGER_VERSION,
95 )
96 ) {
97 this.runArguments.push("--no-packager");
98 }
99
100 const runmacOSSpawn = new CommandExecutor(
101 this.runOptions.nodeModulesRoot,
102 this.projectPath,
103 this.logger,
104 ).spawnReactCommand(`run-${this.platformName}`, this.runArguments, { env });
105 await new OutputVerifier(
106 () => Promise.resolve(MacOSPlatform.SUCCESS_PATTERNS),
107 () => Promise.resolve(MacOSPlatform.FAILURE_PATTERNS),
108 this.platformName,
109 ).process(runmacOSSpawn);
110 });
111 }
112
113 public async prewarmBundleCache(): Promise<void> {
114 return this.packager.prewarmBundleCache(PlatformType.macOS);
115 }
116
117 public getRunArguments(): string[] {
118 let runArguments: string[] = [];
119
120 if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
121 runArguments.push(...this.runOptions.runArguments);
122 } else {
123 let target =
124 this.runOptions.target === TargetType.Simulator ? "" : this.runOptions.target;
125 if (target) {
126 runArguments.push(`--${target}`);
127 }
128 }
129
130 return runArguments;
131 }
132
133 public async enableJSDebuggingMode(): Promise<void> {
134 // Configure the app for debugging
135 // Wait until the configuration file exists, and check to see if debugging is enabled
136 const [debugModeEnabled, appName] = await Promise.all<boolean | string>([
137 this.macOSDebugModeManager.getAppRemoteDebuggingSetting(
138 this.runOptions.configuration,
139 this.runOptions.productName,
140 ),
141 this.getApplicationName(),
142 ]);
143 if (debugModeEnabled) {
144 return;
145 }
146
147 // Debugging must still be enabled
148 // We enable debugging by writing to a plist file that backs a NSUserDefaults object,
149 // but that file is written to by the app on occasion. To avoid races, we shut the app
150 // down before writing to the file.
151 await this.terminateMacOSapp(<string>appName);
152 // Write to the settings file while the app is not running to avoid races
153 await this.macOSDebugModeManager.setAppRemoteDebuggingSetting(
154 /*enable=*/ true,
155 this.runOptions.configuration,
156 this.runOptions.productName,
157 );
158 // Relaunch the app
159 await this.runApp();
160 }
161
162 public disableJSDebuggingMode(): Promise<void> {
163 return this.macOSDebugModeManager.setAppRemoteDebuggingSetting(
164 /*enable=*/ false,
165 this.runOptions.configuration,
166 this.runOptions.productName,
167 );
168 }
169
170 private async getApplicationName(): Promise<string> {
171 const iOSBuildLocationData = await this.plistBuddy.getExecutableAndConfigurationFolder(
172 this.macosProjectRoot,
173 this.projectPath,
174 PlatformType.macOS,
175 false,
176 this.runOptions.configuration,
177 this.runOptions.productName,
178 this.getSchemeFromDebuggingParameters(),
179 );
180 return iOSBuildLocationData.executable;
181 }
182
183 private getSchemeFromDebuggingParameters(): string | undefined {
184 let scheme = this.runOptions.scheme;
185 if (!scheme) {
186 const schemeFromArgs = MacOSPlatform.getOptFromRunArgs(
187 this.runArguments,
188 "--scheme",
189 false,
190 );
191 if (schemeFromArgs) {
192 scheme = schemeFromArgs;
193 }
194 }
195 return scheme;
196 }
197
198 private async terminateMacOSapp(appName: string): Promise<void> {
199 let childProcess = new ChildProcess();
200 // An example of the output from the command above:
201 // 40943 ?? 4:13.97 node /Users/user/Documents/rn_for_mac_proj/node_modules/.bin/react-native start --port 8081
202 // 40959 ?? 0:10.36 /Users/user/.nvm/versions/node/v10.19.0/bin/node /Users/user/Documents/rn_for_mac_proj/node_modules/metro/node_modules/jest-worker/build/workers/processChild.js
203 // 41004 ?? 0:21.34 /Users/user/Library/Developer/Xcode/DerivedData/rn_for_mac_proj-ghuavabiztosiqfqkrityjoxqfmv/Build/Products/Debug/rn_for_mac_proj.app/Contents/MacOS/rn_for_mac_proj
204 // 75514 ttys007 0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn rn_for_mac_proj
205 const searchResults = await childProcess.execToString(`ps -ax | grep ${appName}`);
206 if (searchResults) {
207 const processIdRgx = /(^\d*)\s\?\?/g;
208 // We are looking for a process whose path contains the "appName.app" part
209 const processData = searchResults.split("\n").find(str => str.includes(appName));
210
211 if (processData) {
212 const match = processIdRgx.exec(processData.trim());
213 if (match && match[1]) {
214 await childProcess.execToString(`kill ${match[1]}`);
215 }
216 }
217 }
218 }
219}
220