microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/iOSPlatform.ts
307lines · 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 * as semver from "semver"; |
| 6 | |
| 7 | import {ChildProcess} from "../../common/node/childProcess"; |
| 8 | import {CommandExecutor} from "../../common/commandExecutor"; |
| 9 | import {GeneralMobilePlatform, MobilePlatformDeps, TargetType} from "../generalMobilePlatform"; |
| 10 | import {IIOSRunOptions, PlatformType} from "../launchArgs"; |
| 11 | import {PlistBuddy} from "./plistBuddy"; |
| 12 | import {IOSDebugModeManager} from "./iOSDebugModeManager"; |
| 13 | import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier"; |
| 14 | import {TelemetryHelper} from "../../common/telemetryHelper"; |
| 15 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 16 | import * as nls from "vscode-nls"; |
| 17 | import { AppLauncher } from "../appLauncher"; |
| 18 | import { IiOSSimulator, IOSSimulatorManager } from "./iOSSimulatorManager"; |
| 19 | nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); |
| 20 | const localize = nls.loadMessageBundle(); |
| 21 | |
| 22 | export class IOSPlatform extends GeneralMobilePlatform { |
| 23 | public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios"; |
| 24 | |
| 25 | private plistBuddy = new PlistBuddy(); |
| 26 | private targetType: TargetType = "simulator"; |
| 27 | private iosProjectRoot: string; |
| 28 | private iosDebugModeManager: IOSDebugModeManager; |
| 29 | private simulatorManager: IOSSimulatorManager; |
| 30 | |
| 31 | private defaultConfiguration: string = "Debug"; |
| 32 | private configurationArgumentName: string = "--configuration"; |
| 33 | |
| 34 | // We should add the common iOS build/run errors we find to this list |
| 35 | private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{ |
| 36 | pattern: "No devices are booted", |
| 37 | errorCode: InternalErrorCode.IOSSimulatorNotLaunchable, |
| 38 | }, { |
| 39 | pattern: "FBSOpenApplicationErrorDomain", |
| 40 | errorCode: InternalErrorCode.IOSSimulatorNotLaunchable, |
| 41 | }, { |
| 42 | pattern: "ios-deploy", |
| 43 | errorCode: InternalErrorCode.IOSDeployNotFound, |
| 44 | }]; |
| 45 | |
| 46 | private static readonly RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"]; |
| 47 | |
| 48 | public showDevMenu(appLauncher: AppLauncher): Promise<void> { |
| 49 | const worker = appLauncher.getAppWorker(); |
| 50 | if (worker) { |
| 51 | worker.showDevMenuCommand(); |
| 52 | } |
| 53 | |
| 54 | return Promise.resolve(); |
| 55 | } |
| 56 | |
| 57 | public reloadApp(appLauncher: AppLauncher): Promise<void> { |
| 58 | const worker = appLauncher.getAppWorker(); |
| 59 | if (worker) { |
| 60 | worker.reloadAppCommand(); |
| 61 | } |
| 62 | return Promise.resolve(); |
| 63 | } |
| 64 | |
| 65 | constructor(protected runOptions: IIOSRunOptions, platformDeps: MobilePlatformDeps = {}) { |
| 66 | super(runOptions, platformDeps); |
| 67 | |
| 68 | this.simulatorManager = new IOSSimulatorManager(); |
| 69 | this.runOptions.configuration = this.getConfiguration(); |
| 70 | |
| 71 | if (this.runOptions.iosRelativeProjectPath) { // Deprecated option |
| 72 | this.logger.warning(localize("iosRelativeProjectPathOptionIsDeprecatedUseRunArgumentsInstead", "'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead.")); |
| 73 | } |
| 74 | |
| 75 | const iosProjectFolderPath = IOSPlatform.getOptFromRunArgs(this.runArguments, "--project-path", false); |
| 76 | this.iosProjectRoot = path.join(this.projectPath, iosProjectFolderPath || this.runOptions.iosRelativeProjectPath || IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH); |
| 77 | const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false); |
| 78 | this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot, this.projectPath, schemeFromArgs ? schemeFromArgs : this.runOptions.scheme); |
| 79 | |
| 80 | if (this.runArguments && this.runArguments.length > 0) { |
| 81 | this.targetType = (this.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ? |
| 82 | IOSPlatform.deviceString : IOSPlatform.simulatorString; |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString && |
| 87 | this.runOptions.target !== IOSPlatform.deviceString)) { |
| 88 | |
| 89 | this.targetType = IOSPlatform.simulatorString; |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | this.targetType = this.runOptions.target || IOSPlatform.simulatorString; |
| 94 | } |
| 95 | |
| 96 | public resolveVirtualDevice(target: string): Promise<IiOSSimulator | null> { |
| 97 | if (target === "simulator") { |
| 98 | return this.simulatorManager.startSelection() |
| 99 | .then((simulatorName: string | undefined) => { |
| 100 | if (simulatorName) { |
| 101 | const simulator = this.simulatorManager.findSimulator(simulatorName); |
| 102 | if (simulator) { |
| 103 | GeneralMobilePlatform.removeRunArgument(this.runArguments, "--simulator", true); |
| 104 | GeneralMobilePlatform.setRunArgument(this.runArguments, "--udid", simulator.id); |
| 105 | } |
| 106 | return simulator; |
| 107 | } |
| 108 | else { |
| 109 | return null; |
| 110 | } |
| 111 | }); |
| 112 | } |
| 113 | else if (!target.includes("device")) { |
| 114 | return this.simulatorManager.collectSimulators() |
| 115 | .then((simulators) => { |
| 116 | let simulator = this.simulatorManager.getSimulatorById(target, simulators); |
| 117 | if (simulator) { |
| 118 | GeneralMobilePlatform.removeRunArgument(this.runArguments, "--simulator", false); |
| 119 | GeneralMobilePlatform.setRunArgument(this.runArguments, "--udid", simulator.id); |
| 120 | } |
| 121 | else { |
| 122 | simulator = this.simulatorManager.findSimulator(target, null, simulators); |
| 123 | } |
| 124 | return simulator; |
| 125 | }); |
| 126 | } |
| 127 | else { |
| 128 | return Promise.resolve(null); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | public runApp(): Promise<void> { |
| 133 | let extProps = { |
| 134 | platform: { |
| 135 | value: PlatformType.iOS, |
| 136 | isPii: false, |
| 137 | }, |
| 138 | }; |
| 139 | |
| 140 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(this.runOptions.reactNativeVersions.reactNativeVersion, "reactNativeVersion", extProps); |
| 141 | |
| 142 | return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => { |
| 143 | // Compile, deploy, and launch the app on either a simulator or a device |
| 144 | const env = GeneralMobilePlatform.getEnvArgument(process.env, this.runOptions.env, this.runOptions.envFile); |
| 145 | |
| 146 | if (!semver.valid(this.runOptions.reactNativeVersions.reactNativeVersion) /*Custom RN implementations should support this flag*/ || semver.gte(this.runOptions.reactNativeVersions.reactNativeVersion, IOSPlatform.NO_PACKAGER_VERSION)) { |
| 147 | this.runArguments.push("--no-packager"); |
| 148 | } |
| 149 | // Since @react-native-community/cli@2.1.0 build output are hidden by default |
| 150 | // we are using `--verbose` to show it as it contains `BUILD SUCCESSFUL` and other patterns |
| 151 | if (semver.gte(this.runOptions.reactNativeVersions.reactNativeVersion, "0.60.0")) { |
| 152 | this.runArguments.push("--verbose"); |
| 153 | } |
| 154 | const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env}); |
| 155 | return new OutputVerifier( |
| 156 | () => |
| 157 | this.generateSuccessPatterns(this.runOptions.reactNativeVersions.reactNativeVersion), |
| 158 | () => |
| 159 | Promise.resolve(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), PlatformType.iOS) |
| 160 | .process(runIosSpawn); |
| 161 | }); |
| 162 | } |
| 163 | |
| 164 | public enableJSDebuggingMode(): Promise<void> { |
| 165 | // Configure the app for debugging |
| 166 | if (this.targetType === IOSPlatform.deviceString) { |
| 167 | // Note that currently we cannot automatically switch the device into debug mode. |
| 168 | this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging."); |
| 169 | return Promise.resolve(); |
| 170 | } |
| 171 | |
| 172 | // Wait until the configuration file exists, and check to see if debugging is enabled |
| 173 | return Promise.all<boolean | string>([ |
| 174 | this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName), |
| 175 | this.getBundleId(), |
| 176 | ]) |
| 177 | .then(([debugModeEnabled, bundleId]) => { |
| 178 | if (debugModeEnabled) { |
| 179 | return Promise.resolve(); |
| 180 | } |
| 181 | |
| 182 | // Debugging must still be enabled |
| 183 | // We enable debugging by writing to a plist file that backs a NSUserDefaults object, |
| 184 | // but that file is written to by the app on occasion. To avoid races, we shut the app |
| 185 | // down before writing to the file. |
| 186 | const childProcess = new ChildProcess(); |
| 187 | |
| 188 | return childProcess.execToString("xcrun simctl spawn booted launchctl list") |
| 189 | .then((output: string) => { |
| 190 | // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37] |
| 191 | const regex = new RegExp(`(\\S+${bundleId}\\S+)`); |
| 192 | const match = regex.exec(output); |
| 193 | |
| 194 | // If we don't find a match, the app must not be running and so we do not need to close it |
| 195 | return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null; |
| 196 | }) |
| 197 | .then(() => { |
| 198 | // Write to the settings file while the app is not running to avoid races |
| 199 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName); |
| 200 | }) |
| 201 | .then(() => { |
| 202 | // Relaunch the app |
| 203 | return this.runApp(); |
| 204 | }); |
| 205 | }); |
| 206 | } |
| 207 | |
| 208 | public disableJSDebuggingMode(): Promise<void> { |
| 209 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName); |
| 210 | } |
| 211 | |
| 212 | public prewarmBundleCache(): Promise<void> { |
| 213 | return this.packager.prewarmBundleCache(PlatformType.iOS); |
| 214 | } |
| 215 | |
| 216 | public getRunArguments(): string[] { |
| 217 | let runArguments: string[] = []; |
| 218 | |
| 219 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { |
| 220 | runArguments = this.runOptions.runArguments; |
| 221 | if (this.runOptions.scheme) { |
| 222 | const schemeFromArgs = IOSPlatform.getOptFromRunArgs(runArguments, "--scheme", false); |
| 223 | if (!schemeFromArgs) { |
| 224 | runArguments.push("--scheme", this.runOptions.scheme); |
| 225 | } else { |
| 226 | this.logger.warning(localize("iosSchemeParameterAlreadySetInRunArguments", "'--scheme' is set as 'runArguments' configuration parameter value, 'scheme' configuration parameter value will be omitted")); |
| 227 | } |
| 228 | } |
| 229 | } else { |
| 230 | if (this.runOptions.target) { |
| 231 | runArguments.push(...this.handleTargetArg(this.runOptions.target)); |
| 232 | } |
| 233 | |
| 234 | if (this.runOptions.iosRelativeProjectPath) { |
| 235 | runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath); |
| 236 | } |
| 237 | |
| 238 | // provide any defined scheme |
| 239 | if (this.runOptions.scheme) { |
| 240 | runArguments.push("--scheme", this.runOptions.scheme); |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | return runArguments; |
| 245 | } |
| 246 | |
| 247 | private handleTargetArg(target: string): string[] { |
| 248 | if (target === IOSPlatform.deviceString || |
| 249 | target === IOSPlatform.simulatorString) { |
| 250 | return [`--${this.runOptions.target}`]; |
| 251 | } else { |
| 252 | if (target.indexOf(IOSPlatform.deviceString) !== -1) { |
| 253 | const deviceArgs = target.split("="); |
| 254 | return deviceArgs[1] ? [`--${IOSPlatform.deviceString}`, deviceArgs[1]] : [`--${IOSPlatform.deviceString}`]; |
| 255 | } else { |
| 256 | return [`--${IOSPlatform.simulatorString}`, `${this.runOptions.target}`]; |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | private generateSuccessPatterns(version: string): Promise<string[]> { |
| 262 | // Clone RUN_IOS_SUCCESS_PATTERNS to avoid its runtime mutation |
| 263 | let successPatterns = [...IOSPlatform.RUN_IOS_SUCCESS_PATTERNS]; |
| 264 | if (this.targetType === IOSPlatform.deviceString) { |
| 265 | if (semver.gte(version, "0.60.0")) { |
| 266 | successPatterns.push("success Installed the app on the device"); |
| 267 | } else { |
| 268 | successPatterns.push("INSTALLATION SUCCEEDED"); |
| 269 | } |
| 270 | return Promise.resolve(successPatterns); |
| 271 | } else { |
| 272 | return this.getBundleId() |
| 273 | .then(bundleId => { |
| 274 | if (semver.gte(version, "0.60.0")) { |
| 275 | successPatterns.push(`Launching "${bundleId}"\nsuccess Successfully launched the app `); |
| 276 | } else { |
| 277 | successPatterns.push(`Launching ${bundleId}\n${bundleId}: `); |
| 278 | } |
| 279 | return successPatterns; |
| 280 | }); |
| 281 | } |
| 282 | |
| 283 | } |
| 284 | |
| 285 | private getConfiguration(): string { |
| 286 | return IOSPlatform.getOptFromRunArgs(this.runArguments, this.configurationArgumentName) || this.defaultConfiguration; |
| 287 | } |
| 288 | |
| 289 | private getBundleId(): Promise<string> { |
| 290 | let scheme = this.runOptions.scheme; |
| 291 | if (!scheme) { |
| 292 | const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false); |
| 293 | if (schemeFromArgs) { |
| 294 | scheme = schemeFromArgs; |
| 295 | } |
| 296 | } |
| 297 | return this.plistBuddy.getBundleId(this.iosProjectRoot, this.projectPath, true, this.runOptions.configuration, this.runOptions.productName, scheme); |
| 298 | } |
| 299 | |
| 300 | /*private static remote(fsPath: string): RemoteExtension { // TODO replace with a new implementation from appLauncher |
| 301 | if (this.remoteExtension) { |
| 302 | return this.remoteExtension; |
| 303 | } else { |
| 304 | return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath)); |
| 305 | } |
| 306 | }*/ |
| 307 | } |
| 308 | |