microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/ios/iOSPlatform.ts
193lines · 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 | |
| 7 | import {Log} from "../../common/log/log"; |
| 8 | import {ChildProcess} from "../../common/node/childProcess"; |
| 9 | import {CommandExecutor} from "../../common/commandExecutor"; |
| 10 | import {GeneralMobilePlatform} from "../../common/generalMobilePlatform"; |
| 11 | import {IIOSRunOptions} from "../../common/launchArgs"; |
| 12 | import {PlistBuddy} from "../../common/ios/plistBuddy"; |
| 13 | import {IOSDebugModeManager} from "../../common/ios/iOSDebugModeManager"; |
| 14 | import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier"; |
| 15 | import {RemoteExtension} from "../../common/remoteExtension"; |
| 16 | import {ErrorHelper} from "../../common/error/errorHelper"; |
| 17 | |
| 18 | export class IOSPlatform extends GeneralMobilePlatform { |
| 19 | public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios"; |
| 20 | public static DEFAULT_IOS_SIMULATOR_TARGET = "iPhone 5"; |
| 21 | |
| 22 | private static deviceString = "device"; |
| 23 | private static simulatorString = "simulator"; |
| 24 | |
| 25 | private plistBuddy = new PlistBuddy(); |
| 26 | private target: string = ""; |
| 27 | private targetType: string = "simulator"; |
| 28 | private iosProjectRoot: string; |
| 29 | |
| 30 | // We should add the common iOS build/run erros we find to this list |
| 31 | private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{ |
| 32 | pattern: "No devices are booted", |
| 33 | message: ErrorHelper.ERROR_STRINGS.IOSSimulatorNotLaunchable, |
| 34 | }, { |
| 35 | pattern: "FBSOpenApplicationErrorDomain", |
| 36 | message: ErrorHelper.ERROR_STRINGS.IOSSimulatorNotLaunchable, |
| 37 | }, { |
| 38 | pattern: "ios-deploy", |
| 39 | message: ErrorHelper.ERROR_STRINGS.IOSDeployNotFound, |
| 40 | }]; |
| 41 | |
| 42 | private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"]; |
| 43 | |
| 44 | // We set remoteExtension = null so that if there is an instance of iOSPlatform that wants to have it's custom remoteExtension it can. This is specifically useful for tests. |
| 45 | constructor(protected runOptions: IIOSRunOptions, { remoteExtension = undefined }: {remoteExtension?: RemoteExtension} = {}) { |
| 46 | super(runOptions, { remoteExtension: remoteExtension }); |
| 47 | |
| 48 | if (this.runOptions.iosRelativeProjectPath) { // Deprecated option |
| 49 | Log.logMessage("'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead"); |
| 50 | } |
| 51 | |
| 52 | this.iosProjectRoot = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath || "ios"); |
| 53 | |
| 54 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { |
| 55 | if (this.runOptions.runArguments.indexOf(`--${IOSPlatform.deviceString}`) > -1) { |
| 56 | this.targetType = IOSPlatform.deviceString; |
| 57 | } else { |
| 58 | this.targetType = IOSPlatform.simulatorString; |
| 59 | } |
| 60 | } else { |
| 61 | if (this.runOptions.targetType) { |
| 62 | if (this.runOptions.targetType !== IOSPlatform.simulatorString && |
| 63 | this.runOptions.targetType !== IOSPlatform.deviceString) { |
| 64 | throw Error(`Invalid Run iOS targetType: '${this.runOptions.targetType}' in .vscode/launch.json.` + |
| 65 | "Please use 'simulator' or 'device' targetType instead"); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | if (this.runOptions.target === IOSPlatform.simulatorString) { |
| 70 | this.targetType = this.runOptions.target; |
| 71 | this.target = IOSPlatform.DEFAULT_IOS_SIMULATOR_TARGET; |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | if (this.runOptions.target === IOSPlatform.deviceString) { |
| 76 | this.targetType = this.runOptions.target; |
| 77 | this.target = ""; |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | this.targetType = this.runOptions.targetType || IOSPlatform.simulatorString; |
| 82 | |
| 83 | if (this.runOptions.target) { |
| 84 | this.target = this.runOptions.target; |
| 85 | } else if (this.targetType === IOSPlatform.simulatorString) { |
| 86 | this.target = IOSPlatform.DEFAULT_IOS_SIMULATOR_TARGET; |
| 87 | } else { |
| 88 | this.target = ""; |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | public runApp(): Q.Promise<void> { |
| 94 | // Compile, deploy, and launch the app on either a simulator or a device |
| 95 | const runArguments = this.getRunArgument(); |
| 96 | |
| 97 | const runIosSpawn = new CommandExecutor(this.projectPath).spawnReactCommand("run-ios", runArguments); |
| 98 | return new OutputVerifier( |
| 99 | () => |
| 100 | this.generateSuccessPatterns(), |
| 101 | () => |
| 102 | Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS)).process(runIosSpawn); |
| 103 | } |
| 104 | |
| 105 | public enableJSDebuggingMode(): Q.Promise<void> { |
| 106 | // Configure the app for debugging |
| 107 | if (this.targetType === IOSPlatform.deviceString) { |
| 108 | // Note that currently we cannot automatically switch the device into debug mode. |
| 109 | Log.logMessage("Application is running on a device, please shake device and select 'Debug in Chrome' to enable debugging."); |
| 110 | return Q.resolve<void>(void 0); |
| 111 | } |
| 112 | |
| 113 | const iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot); |
| 114 | |
| 115 | // Wait until the configuration file exists, and check to see if debugging is enabled |
| 116 | return Q.all<boolean | string>([ |
| 117 | iosDebugModeManager.getSimulatorRemoteDebuggingSetting(), |
| 118 | this.getBundleId(), |
| 119 | ]) |
| 120 | .spread((debugModeEnabled: boolean, bundleId: string) => { |
| 121 | if (debugModeEnabled) { |
| 122 | return Q.resolve(void 0); |
| 123 | } |
| 124 | |
| 125 | // Debugging must still be enabled |
| 126 | // We enable debugging by writing to a plist file that backs a NSUserDefaults object, |
| 127 | // but that file is written to by the app on occasion. To avoid races, we shut the app |
| 128 | // down before writing to the file. |
| 129 | const childProcess = new ChildProcess(); |
| 130 | |
| 131 | return childProcess.execToString("xcrun simctl spawn booted launchctl list") |
| 132 | .then((output: string) => { |
| 133 | // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37] |
| 134 | const regex = new RegExp(`(\\S+${bundleId}\\S+)`); |
| 135 | const match = regex.exec(output); |
| 136 | |
| 137 | // If we don't find a match, the app must not be running and so we do not need to close it |
| 138 | return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null; |
| 139 | }) |
| 140 | .then(() => { |
| 141 | // Write to the settings file while the app is not running to avoid races |
| 142 | return iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true); |
| 143 | }) |
| 144 | .then(() => { |
| 145 | // Relaunch the app |
| 146 | return this.runApp(); |
| 147 | }); |
| 148 | }); |
| 149 | } |
| 150 | |
| 151 | public prewarmBundleCache(): Q.Promise<void> { |
| 152 | return this.remoteExtension.prewarmBundleCache(this.platformName); |
| 153 | } |
| 154 | |
| 155 | public getRunArgument(): string[] { |
| 156 | let runArguments: string[] = []; |
| 157 | |
| 158 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { |
| 159 | return this.runOptions.runArguments; |
| 160 | } |
| 161 | |
| 162 | if (this.targetType) { |
| 163 | runArguments.push(`--${this.targetType}`); |
| 164 | } |
| 165 | |
| 166 | if (this.target) { |
| 167 | runArguments.push(this.target); |
| 168 | } |
| 169 | |
| 170 | if (this.runOptions.iosRelativeProjectPath) { |
| 171 | runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath); |
| 172 | } |
| 173 | |
| 174 | // provide any defined scheme |
| 175 | if (this.runOptions.scheme) { |
| 176 | runArguments.push("--scheme", this.runOptions.scheme); |
| 177 | } |
| 178 | |
| 179 | return runArguments; |
| 180 | } |
| 181 | |
| 182 | private generateSuccessPatterns(): Q.Promise<string[]> { |
| 183 | return this.targetType === IOSPlatform.deviceString ? |
| 184 | Q(IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat("INSTALLATION SUCCEEDED")) : |
| 185 | this.getBundleId() |
| 186 | .then(bundleId => IOSPlatform.RUN_IOS_SUCCESS_PATTERNS |
| 187 | .concat([`Launching ${bundleId}\n${bundleId}: `])); |
| 188 | } |
| 189 | |
| 190 | private getBundleId(): Q.Promise<string> { |
| 191 | return this.plistBuddy.getBundleId(this.iosProjectRoot); |
| 192 | } |
| 193 | } |