microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/iOSPlatform.ts
213lines · 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 Q from "q"; |
| 5 | import * as path from "path"; |
| 6 | import * as semver from "semver"; |
| 7 | |
| 8 | import {ChildProcess} from "../../common/node/childProcess"; |
| 9 | import {CommandExecutor} from "../../common/commandExecutor"; |
| 10 | import {GeneralMobilePlatform, MobilePlatformDeps, TargetType} from "../generalMobilePlatform"; |
| 11 | import {IIOSRunOptions} from "../launchArgs"; |
| 12 | import {PlistBuddy} from "./plistBuddy"; |
| 13 | import {IOSDebugModeManager} from "./iOSDebugModeManager"; |
| 14 | import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier"; |
| 15 | import {SettingsHelper} from "../settingsHelper"; |
| 16 | import {RemoteExtension} from "../../common/remoteExtension"; |
| 17 | import {ReactNativeProjectHelper} from "../../common/reactNativeProjectHelper"; |
| 18 | import {TelemetryHelper} from "../../common/telemetryHelper"; |
| 19 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 20 | import * as nls from "vscode-nls"; |
| 21 | const localize = nls.loadMessageBundle(); |
| 22 | |
| 23 | export class IOSPlatform extends GeneralMobilePlatform { |
| 24 | public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios"; |
| 25 | private static remoteExtension: RemoteExtension; |
| 26 | |
| 27 | private plistBuddy = new PlistBuddy(); |
| 28 | private targetType: TargetType = "simulator"; |
| 29 | private iosProjectRoot: string; |
| 30 | private iosDebugModeManager: IOSDebugModeManager; |
| 31 | |
| 32 | private defaultConfiguration: string = "Debug"; |
| 33 | private configurationArgumentName: string = "--configuration"; |
| 34 | |
| 35 | // We should add the common iOS build/run errors we find to this list |
| 36 | private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{ |
| 37 | pattern: "No devices are booted", |
| 38 | errorCode: InternalErrorCode.IOSSimulatorNotLaunchable, |
| 39 | }, { |
| 40 | pattern: "FBSOpenApplicationErrorDomain", |
| 41 | errorCode: InternalErrorCode.IOSSimulatorNotLaunchable, |
| 42 | }, { |
| 43 | pattern: "ios-deploy", |
| 44 | errorCode: InternalErrorCode.IOSDeployNotFound, |
| 45 | }]; |
| 46 | |
| 47 | private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"]; |
| 48 | |
| 49 | public showDevMenu(deviceId?: string): Q.Promise<void> { |
| 50 | return IOSPlatform.remote(this.runOptions.projectRoot).showDevMenu(deviceId); |
| 51 | } |
| 52 | |
| 53 | public reloadApp(deviceId?: string): Q.Promise<void> { |
| 54 | return IOSPlatform.remote(this.runOptions.projectRoot).reloadApp(deviceId); |
| 55 | } |
| 56 | |
| 57 | constructor(protected runOptions: IIOSRunOptions, platformDeps: MobilePlatformDeps = {}) { |
| 58 | super(runOptions, platformDeps); |
| 59 | |
| 60 | this.runOptions.configuration = this.getConfiguration(); |
| 61 | |
| 62 | if (this.runOptions.iosRelativeProjectPath) { // Deprecated option |
| 63 | this.logger.warning(localize("iosRelativeProjectPathOptionIsDeprecatedUseRunArgumentsInstead", "'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead.")); |
| 64 | } |
| 65 | |
| 66 | this.iosProjectRoot = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath || IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH); |
| 67 | this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot, this.runOptions.scheme); |
| 68 | |
| 69 | if (this.runArguments && this.runArguments.length > 0) { |
| 70 | this.targetType = (this.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ? |
| 71 | IOSPlatform.deviceString : IOSPlatform.simulatorString; |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString && |
| 76 | this.runOptions.target !== IOSPlatform.deviceString)) { |
| 77 | |
| 78 | this.targetType = IOSPlatform.simulatorString; |
| 79 | return; |
| 80 | } |
| 81 | |
| 82 | this.targetType = this.runOptions.target || IOSPlatform.simulatorString; |
| 83 | } |
| 84 | |
| 85 | public runApp(): Q.Promise<void> { |
| 86 | const extProps = { |
| 87 | platform: { |
| 88 | value: "ios", |
| 89 | isPii: false, |
| 90 | }, |
| 91 | }; |
| 92 | |
| 93 | return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => { |
| 94 | // Compile, deploy, and launch the app on either a simulator or a device |
| 95 | const env = this.getEnvArgument(); |
| 96 | |
| 97 | return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot) |
| 98 | .then(version => { |
| 99 | if (!semver.valid(version) /*Custom RN implementations should support this flag*/ || semver.gte(version, IOSPlatform.NO_PACKAGER_VERSION)) { |
| 100 | this.runArguments.push("--no-packager"); |
| 101 | } |
| 102 | const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env}); |
| 103 | return new OutputVerifier(() => this.generateSuccessPatterns(), () => Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), "ios") |
| 104 | .process(runIosSpawn); |
| 105 | }); |
| 106 | }); |
| 107 | } |
| 108 | |
| 109 | public enableJSDebuggingMode(): Q.Promise<void> { |
| 110 | // Configure the app for debugging |
| 111 | if (this.targetType === IOSPlatform.deviceString) { |
| 112 | // Note that currently we cannot automatically switch the device into debug mode. |
| 113 | this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging."); |
| 114 | return Q.resolve<void>(void 0); |
| 115 | } |
| 116 | |
| 117 | // Wait until the configuration file exists, and check to see if debugging is enabled |
| 118 | return Q.all<boolean | string>([ |
| 119 | this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName), |
| 120 | this.getBundleId(), |
| 121 | ]) |
| 122 | .spread((debugModeEnabled: boolean, bundleId: string) => { |
| 123 | if (debugModeEnabled) { |
| 124 | return Q.resolve(void 0); |
| 125 | } |
| 126 | |
| 127 | // Debugging must still be enabled |
| 128 | // We enable debugging by writing to a plist file that backs a NSUserDefaults object, |
| 129 | // but that file is written to by the app on occasion. To avoid races, we shut the app |
| 130 | // down before writing to the file. |
| 131 | const childProcess = new ChildProcess(); |
| 132 | |
| 133 | return childProcess.execToString("xcrun simctl spawn booted launchctl list") |
| 134 | .then((output: string) => { |
| 135 | // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37] |
| 136 | const regex = new RegExp(`(\\S+${bundleId}\\S+)`); |
| 137 | const match = regex.exec(output); |
| 138 | |
| 139 | // If we don't find a match, the app must not be running and so we do not need to close it |
| 140 | return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null; |
| 141 | }) |
| 142 | .then(() => { |
| 143 | // Write to the settings file while the app is not running to avoid races |
| 144 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName); |
| 145 | }) |
| 146 | .then(() => { |
| 147 | // Relaunch the app |
| 148 | return this.runApp(); |
| 149 | }); |
| 150 | }); |
| 151 | } |
| 152 | |
| 153 | public disableJSDebuggingMode(): Q.Promise<void> { |
| 154 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName); |
| 155 | } |
| 156 | |
| 157 | public prewarmBundleCache(): Q.Promise<void> { |
| 158 | return this.packager.prewarmBundleCache("ios"); |
| 159 | } |
| 160 | |
| 161 | public getRunArguments(): string[] { |
| 162 | let runArguments: string[] = []; |
| 163 | |
| 164 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { |
| 165 | runArguments = this.runOptions.runArguments; |
| 166 | } else { |
| 167 | if (this.runOptions.target) { |
| 168 | if (this.runOptions.target === IOSPlatform.deviceString || |
| 169 | this.runOptions.target === IOSPlatform.simulatorString) { |
| 170 | |
| 171 | runArguments.push(`--${this.runOptions.target}`); |
| 172 | } else { |
| 173 | runArguments.push("--simulator", `${this.runOptions.target}`); |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (this.runOptions.iosRelativeProjectPath) { |
| 178 | runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath); |
| 179 | } |
| 180 | |
| 181 | // provide any defined scheme |
| 182 | if (this.runOptions.scheme) { |
| 183 | runArguments.push("--scheme", this.runOptions.scheme); |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return runArguments; |
| 188 | } |
| 189 | |
| 190 | private generateSuccessPatterns(): Q.Promise<string[]> { |
| 191 | return this.targetType === IOSPlatform.deviceString ? |
| 192 | Q(IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat("INSTALLATION SUCCEEDED")) : |
| 193 | this.getBundleId() |
| 194 | .then(bundleId => IOSPlatform.RUN_IOS_SUCCESS_PATTERNS |
| 195 | .concat([`Launching ${bundleId}\n${bundleId}: `])); |
| 196 | } |
| 197 | |
| 198 | private getConfiguration(): string { |
| 199 | return this.getOptFromRunArgs(this.configurationArgumentName) || this.defaultConfiguration; |
| 200 | } |
| 201 | |
| 202 | private getBundleId(): Q.Promise<string> { |
| 203 | return this.plistBuddy.getBundleId(this.iosProjectRoot, true, this.runOptions.configuration, this.runOptions.productName, this.runOptions.scheme); |
| 204 | } |
| 205 | |
| 206 | private static remote(fsPath: string): RemoteExtension { |
| 207 | if (this.remoteExtension) { |
| 208 | return this.remoteExtension; |
| 209 | } else { |
| 210 | return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath)); |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |