microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/iOSPlatform.ts
250lines · 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 readonly 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 | const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false); |
| 68 | this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot, schemeFromArgs ? schemeFromArgs : this.runOptions.scheme); |
| 69 | |
| 70 | if (this.runArguments && this.runArguments.length > 0) { |
| 71 | this.targetType = (this.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ? |
| 72 | IOSPlatform.deviceString : IOSPlatform.simulatorString; |
| 73 | return; |
| 74 | } |
| 75 | |
| 76 | if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString && |
| 77 | this.runOptions.target !== IOSPlatform.deviceString)) { |
| 78 | |
| 79 | this.targetType = IOSPlatform.simulatorString; |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | this.targetType = this.runOptions.target || IOSPlatform.simulatorString; |
| 84 | } |
| 85 | |
| 86 | public runApp(): Q.Promise<void> { |
| 87 | const extProps = { |
| 88 | platform: { |
| 89 | value: "ios", |
| 90 | isPii: false, |
| 91 | }, |
| 92 | }; |
| 93 | |
| 94 | return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => { |
| 95 | // Compile, deploy, and launch the app on either a simulator or a device |
| 96 | const env = this.getEnvArgument(); |
| 97 | |
| 98 | return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot) |
| 99 | .then(version => { |
| 100 | if (!semver.valid(version) /*Custom RN implementations should support this flag*/ || semver.gte(version, IOSPlatform.NO_PACKAGER_VERSION)) { |
| 101 | this.runArguments.push("--no-packager"); |
| 102 | } |
| 103 | // Since @react-native-community/cli@2.1.0 build output are hidden by default |
| 104 | // we are using `--verbose` to show it as it contains `BUILD SUCCESSFUL` and other patterns |
| 105 | if (semver.gte(version, "0.60.0")) { |
| 106 | this.runArguments.push("--verbose"); |
| 107 | } |
| 108 | const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env}); |
| 109 | return new OutputVerifier(() => this.generateSuccessPatterns(version), () => Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), "ios") |
| 110 | .process(runIosSpawn); |
| 111 | }); |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | public enableJSDebuggingMode(): Q.Promise<void> { |
| 116 | // Configure the app for debugging |
| 117 | if (this.targetType === IOSPlatform.deviceString) { |
| 118 | // Note that currently we cannot automatically switch the device into debug mode. |
| 119 | this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging."); |
| 120 | return Q.resolve<void>(void 0); |
| 121 | } |
| 122 | |
| 123 | // Wait until the configuration file exists, and check to see if debugging is enabled |
| 124 | return Q.all<boolean | string>([ |
| 125 | this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName), |
| 126 | this.getBundleId(), |
| 127 | ]) |
| 128 | .spread((debugModeEnabled: boolean, bundleId: string) => { |
| 129 | if (debugModeEnabled) { |
| 130 | return Q.resolve(void 0); |
| 131 | } |
| 132 | |
| 133 | // Debugging must still be enabled |
| 134 | // We enable debugging by writing to a plist file that backs a NSUserDefaults object, |
| 135 | // but that file is written to by the app on occasion. To avoid races, we shut the app |
| 136 | // down before writing to the file. |
| 137 | const childProcess = new ChildProcess(); |
| 138 | |
| 139 | return childProcess.execToString("xcrun simctl spawn booted launchctl list") |
| 140 | .then((output: string) => { |
| 141 | // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37] |
| 142 | const regex = new RegExp(`(\\S+${bundleId}\\S+)`); |
| 143 | const match = regex.exec(output); |
| 144 | |
| 145 | // If we don't find a match, the app must not be running and so we do not need to close it |
| 146 | return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null; |
| 147 | }) |
| 148 | .then(() => { |
| 149 | // Write to the settings file while the app is not running to avoid races |
| 150 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName); |
| 151 | }) |
| 152 | .then(() => { |
| 153 | // Relaunch the app |
| 154 | return this.runApp(); |
| 155 | }); |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | public disableJSDebuggingMode(): Q.Promise<void> { |
| 160 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName); |
| 161 | } |
| 162 | |
| 163 | public prewarmBundleCache(): Q.Promise<void> { |
| 164 | return this.packager.prewarmBundleCache("ios"); |
| 165 | } |
| 166 | |
| 167 | public getRunArguments(): string[] { |
| 168 | let runArguments: string[] = []; |
| 169 | |
| 170 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { |
| 171 | runArguments = this.runOptions.runArguments; |
| 172 | if (this.runOptions.scheme) { |
| 173 | const schemeFromArgs = IOSPlatform.getOptFromRunArgs(runArguments, "--scheme", false); |
| 174 | if (!schemeFromArgs) { |
| 175 | runArguments.push("--scheme", this.runOptions.scheme); |
| 176 | } else { |
| 177 | this.logger.warning(localize("iosSchemeParameterAlreadySetInRunArguments", "'--scheme' is set as 'runArguments' configuration parameter value, 'scheme' configuration parameter value will be omitted")); |
| 178 | } |
| 179 | } |
| 180 | } else { |
| 181 | if (this.runOptions.target) { |
| 182 | if (this.runOptions.target === IOSPlatform.deviceString || |
| 183 | this.runOptions.target === IOSPlatform.simulatorString) { |
| 184 | |
| 185 | runArguments.push(`--${this.runOptions.target}`); |
| 186 | } else { |
| 187 | runArguments.push("--simulator", `${this.runOptions.target}`); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | if (this.runOptions.iosRelativeProjectPath) { |
| 192 | runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath); |
| 193 | } |
| 194 | |
| 195 | // provide any defined scheme |
| 196 | if (this.runOptions.scheme) { |
| 197 | runArguments.push("--scheme", this.runOptions.scheme); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | return runArguments; |
| 202 | } |
| 203 | |
| 204 | private generateSuccessPatterns(version: string): Q.Promise<string[]> { |
| 205 | // Clone RUN_IOS_SUCCESS_PATTERNS to avoid its runtime mutation |
| 206 | let successPatterns = [...IOSPlatform.RUN_IOS_SUCCESS_PATTERNS]; |
| 207 | if (this.targetType === IOSPlatform.deviceString) { |
| 208 | if (semver.gte(version, "0.60.0")) { |
| 209 | successPatterns.push("success Installed the app on the device"); |
| 210 | } else { |
| 211 | successPatterns.push("INSTALLATION SUCCEEDED"); |
| 212 | } |
| 213 | return Q(successPatterns); |
| 214 | } else { |
| 215 | return this.getBundleId() |
| 216 | .then(bundleId => { |
| 217 | if (semver.gte(version, "0.60.0")) { |
| 218 | successPatterns.push(`Launching "${bundleId}"\nsuccess Successfully launched the app `); |
| 219 | } else { |
| 220 | successPatterns.push(`Launching ${bundleId}\n${bundleId}: `); |
| 221 | } |
| 222 | return successPatterns; |
| 223 | }); |
| 224 | } |
| 225 | |
| 226 | } |
| 227 | |
| 228 | private getConfiguration(): string { |
| 229 | return IOSPlatform.getOptFromRunArgs(this.runArguments, this.configurationArgumentName) || this.defaultConfiguration; |
| 230 | } |
| 231 | |
| 232 | private getBundleId(): Q.Promise<string> { |
| 233 | let scheme = this.runOptions.scheme; |
| 234 | if (!scheme) { |
| 235 | const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false); |
| 236 | if (schemeFromArgs) { |
| 237 | scheme = schemeFromArgs; |
| 238 | } |
| 239 | } |
| 240 | return this.plistBuddy.getBundleId(this.iosProjectRoot, true, this.runOptions.configuration, this.runOptions.productName, scheme); |
| 241 | } |
| 242 | |
| 243 | private static remote(fsPath: string): RemoteExtension { |
| 244 | if (this.remoteExtension) { |
| 245 | return this.remoteExtension; |
| 246 | } else { |
| 247 | return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath)); |
| 248 | } |
| 249 | } |
| 250 | } |
| 251 | |