microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/macos/macOSPlatform.ts
221lines · 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 semver from "semver"; |
| 5 | import * as path from "path"; |
| 6 | import { GeneralMobilePlatform, MobilePlatformDeps } from "../generalMobilePlatform"; |
| 7 | import { MacOSDebugModeManager } from "./macOSDebugModeManager"; |
| 8 | import { ImacOSRunOptions, PlatformType } from "../launchArgs"; |
| 9 | import { OutputVerifier, PatternToFailure } from "../../common/outputVerifier"; |
| 10 | import { TelemetryHelper } from "../../common/telemetryHelper"; |
| 11 | import { CommandExecutor } from "../../common/commandExecutor"; |
| 12 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 13 | import { PlistBuddy } from "../ios/plistBuddy"; |
| 14 | import { ChildProcess } from "../../common/node/childProcess"; |
| 15 | |
| 16 | /** |
| 17 | * macOS specific platform implementation for debugging RN applications. |
| 18 | */ |
| 19 | export class MacOSPlatform extends GeneralMobilePlatform { |
| 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 = GeneralMobilePlatform.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 === MacOSPlatform.simulatorString |
| 125 | ? "" |
| 126 | : this.runOptions.target; |
| 127 | if (target) { |
| 128 | runArguments.push(`--${target}`); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | return runArguments; |
| 133 | } |
| 134 | |
| 135 | public async enableJSDebuggingMode(): Promise<void> { |
| 136 | // Configure the app for debugging |
| 137 | // Wait until the configuration file exists, and check to see if debugging is enabled |
| 138 | const [debugModeEnabled, appName] = await Promise.all<boolean | string>([ |
| 139 | this.macOSDebugModeManager.getAppRemoteDebuggingSetting( |
| 140 | this.runOptions.configuration, |
| 141 | this.runOptions.productName, |
| 142 | ), |
| 143 | this.getApplicationName(), |
| 144 | ]); |
| 145 | if (debugModeEnabled) { |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | // Debugging must still be enabled |
| 150 | // We enable debugging by writing to a plist file that backs a NSUserDefaults object, |
| 151 | // but that file is written to by the app on occasion. To avoid races, we shut the app |
| 152 | // down before writing to the file. |
| 153 | await this.terminateMacOSapp(<string>appName); |
| 154 | // Write to the settings file while the app is not running to avoid races |
| 155 | await this.macOSDebugModeManager.setAppRemoteDebuggingSetting( |
| 156 | /*enable=*/ true, |
| 157 | this.runOptions.configuration, |
| 158 | this.runOptions.productName, |
| 159 | ); |
| 160 | // Relaunch the app |
| 161 | await this.runApp(); |
| 162 | } |
| 163 | |
| 164 | public disableJSDebuggingMode(): Promise<void> { |
| 165 | return this.macOSDebugModeManager.setAppRemoteDebuggingSetting( |
| 166 | /*enable=*/ false, |
| 167 | this.runOptions.configuration, |
| 168 | this.runOptions.productName, |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | private async getApplicationName(): Promise<string> { |
| 173 | const iOSBuildLocationData = await this.plistBuddy.getExecutableAndConfigurationFolder( |
| 174 | this.macosProjectRoot, |
| 175 | this.projectPath, |
| 176 | PlatformType.macOS, |
| 177 | false, |
| 178 | this.runOptions.configuration, |
| 179 | this.runOptions.productName, |
| 180 | this.getSchemeFromDebuggingParameters(), |
| 181 | ); |
| 182 | return iOSBuildLocationData.executable; |
| 183 | } |
| 184 | |
| 185 | private getSchemeFromDebuggingParameters(): string | undefined { |
| 186 | let scheme = this.runOptions.scheme; |
| 187 | if (!scheme) { |
| 188 | const schemeFromArgs = MacOSPlatform.getOptFromRunArgs( |
| 189 | this.runArguments, |
| 190 | "--scheme", |
| 191 | false, |
| 192 | ); |
| 193 | if (schemeFromArgs) { |
| 194 | scheme = schemeFromArgs; |
| 195 | } |
| 196 | } |
| 197 | return scheme; |
| 198 | } |
| 199 | |
| 200 | private async terminateMacOSapp(appName: string): Promise<void> { |
| 201 | let childProcess = new ChildProcess(); |
| 202 | // An example of the output from the command above: |
| 203 | // 40943 ?? 4:13.97 node /Users/user/Documents/rn_for_mac_proj/node_modules/.bin/react-native start --port 8081 |
| 204 | // 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 |
| 205 | // 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 |
| 206 | // 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 |
| 207 | const searchResults = await childProcess.execToString(`ps -ax | grep ${appName}`); |
| 208 | if (searchResults) { |
| 209 | const processIdRgx = /(^\d*)\s\?\?/g; |
| 210 | // We are looking for a process whose path contains the "appName.app" part |
| 211 | const processData = searchResults.split("\n").find(str => str.includes(appName)); |
| 212 | |
| 213 | if (processData) { |
| 214 | const match = processIdRgx.exec(processData.trim()); |
| 215 | if (match && match[1]) { |
| 216 | await childProcess.execToString(`kill ${match[1]}`); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |