microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/iOSPlatform.ts
213lines · modeblame
8a67e140Artem Egorov8 years ago | 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"; | |
df8c800dArtem Egorov8 years ago | 6 | import * as semver from "semver"; |
8a67e140Artem Egorov8 years ago | 7 | |
| 8 | import {ChildProcess} from "../../common/node/childProcess"; | |
| 9 | import {CommandExecutor} from "../../common/commandExecutor"; | |
0a68f8dbArtem Egorov8 years ago | 10 | import {GeneralMobilePlatform, MobilePlatformDeps, TargetType} from "../generalMobilePlatform"; |
| 11 | import {IIOSRunOptions} from "../launchArgs"; | |
| 12 | import {PlistBuddy} from "./plistBuddy"; | |
| 13 | import {IOSDebugModeManager} from "./iOSDebugModeManager"; | |
8a67e140Artem Egorov8 years ago | 14 | import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier"; |
a41f5c68Artem Egorov8 years ago | 15 | import {SettingsHelper} from "../settingsHelper"; |
7daed3fcArtem Egorov8 years ago | 16 | import {RemoteExtension} from "../../common/remoteExtension"; |
df8c800dArtem Egorov8 years ago | 17 | import {ReactNativeProjectHelper} from "../../common/reactNativeProjectHelper"; |
031832ffArtem Egorov7 years ago | 18 | import {TelemetryHelper} from "../../common/telemetryHelper"; |
fc602bb6Yuri Skorokhodov7 years ago | 19 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
d7d405aeYuri Skorokhodov7 years ago | 20 | import * as nls from "vscode-nls"; |
| 21 | const localize = nls.loadMessageBundle(); | |
8022afdfVladimir Kotikov8 years ago | 22 | |
8a67e140Artem Egorov8 years ago | 23 | export class IOSPlatform extends GeneralMobilePlatform { |
| 24 | public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios"; | |
7daed3fcArtem Egorov8 years ago | 25 | private static remoteExtension: RemoteExtension; |
0a68f8dbArtem Egorov8 years ago | 26 | |
8a67e140Artem Egorov8 years ago | 27 | private plistBuddy = new PlistBuddy(); |
8022afdfVladimir Kotikov8 years ago | 28 | private targetType: TargetType = "simulator"; |
0db0be15Artem Egorov8 years ago | 29 | private iosProjectRoot: string; |
7daed3fcArtem Egorov8 years ago | 30 | private iosDebugModeManager: IOSDebugModeManager; |
| 31 | | |
db6fd42aRuslan Bikkinin7 years ago | 32 | private defaultConfiguration: string = "Debug"; |
| 33 | private configurationArgumentName: string = "--configuration"; | |
8a67e140Artem Egorov8 years ago | 34 | |
0a68f8dbArtem Egorov8 years ago | 35 | // We should add the common iOS build/run errors we find to this list |
8a67e140Artem Egorov8 years ago | 36 | private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{ |
| 37 | pattern: "No devices are booted", | |
d7d405aeYuri Skorokhodov7 years ago | 38 | errorCode: InternalErrorCode.IOSSimulatorNotLaunchable, |
8a67e140Artem Egorov8 years ago | 39 | }, { |
| 40 | pattern: "FBSOpenApplicationErrorDomain", | |
d7d405aeYuri Skorokhodov7 years ago | 41 | errorCode: InternalErrorCode.IOSSimulatorNotLaunchable, |
8a67e140Artem Egorov8 years ago | 42 | }, { |
| 43 | pattern: "ios-deploy", | |
d7d405aeYuri Skorokhodov7 years ago | 44 | errorCode: InternalErrorCode.IOSDeployNotFound, |
8a67e140Artem Egorov8 years ago | 45 | }]; |
| 46 | | |
| 47 | private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"]; | |
| 48 | | |
db6fd42aRuslan Bikkinin7 years ago | 49 | public showDevMenu(deviceId?: string): Q.Promise<void> { |
| 50 | return IOSPlatform.remote(this.runOptions.projectRoot).showDevMenu(deviceId); | |
7daed3fcArtem Egorov8 years ago | 51 | } |
| 52 | | |
db6fd42aRuslan Bikkinin7 years ago | 53 | public reloadApp(deviceId?: string): Q.Promise<void> { |
| 54 | return IOSPlatform.remote(this.runOptions.projectRoot).reloadApp(deviceId); | |
7daed3fcArtem Egorov8 years ago | 55 | } |
| 56 | | |
0a68f8dbArtem Egorov8 years ago | 57 | constructor(protected runOptions: IIOSRunOptions, platformDeps: MobilePlatformDeps = {}) { |
| 58 | super(runOptions, platformDeps); | |
8a67e140Artem Egorov8 years ago | 59 | |
db6fd42aRuslan Bikkinin7 years ago | 60 | this.runOptions.configuration = this.getConfiguration(); |
| 61 | | |
0db0be15Artem Egorov8 years ago | 62 | if (this.runOptions.iosRelativeProjectPath) { // Deprecated option |
d7d405aeYuri Skorokhodov7 years ago | 63 | this.logger.warning(localize("iosRelativeProjectPathOptionIsDeprecatedUseRunArgumentsInstead", "'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead.")); |
8a67e140Artem Egorov8 years ago | 64 | } |
| 65 | | |
0a68f8dbArtem Egorov8 years ago | 66 | this.iosProjectRoot = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath || IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH); |
7daed3fcArtem Egorov8 years ago | 67 | this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot); |
8a67e140Artem Egorov8 years ago | 68 | |
db6fd42aRuslan Bikkinin7 years ago | 69 | if (this.runArguments && this.runArguments.length > 0) { |
| 70 | this.targetType = (this.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ? | |
8022afdfVladimir Kotikov8 years ago | 71 | IOSPlatform.deviceString : IOSPlatform.simulatorString; |
| 72 | return; | |
| 73 | } | |
0db0be15Artem Egorov8 years ago | 74 | |
8022afdfVladimir Kotikov8 years ago | 75 | if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString && |
| 76 | this.runOptions.target !== IOSPlatform.deviceString)) { | |
0db0be15Artem Egorov8 years ago | 77 | |
8022afdfVladimir Kotikov8 years ago | 78 | this.targetType = IOSPlatform.simulatorString; |
| 79 | return; | |
8a67e140Artem Egorov8 years ago | 80 | } |
8022afdfVladimir Kotikov8 years ago | 81 | |
| 82 | this.targetType = this.runOptions.target || IOSPlatform.simulatorString; | |
8a67e140Artem Egorov8 years ago | 83 | } |
| 84 | | |
| 85 | public runApp(): Q.Promise<void> { | |
031832ffArtem Egorov7 years ago | 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)) { | |
db6fd42aRuslan Bikkinin7 years ago | 100 | this.runArguments.push("--no-packager"); |
031832ffArtem Egorov7 years ago | 101 | } |
db6fd42aRuslan Bikkinin7 years ago | 102 | const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env}); |
031832ffArtem Egorov7 years ago | 103 | return new OutputVerifier(() => this.generateSuccessPatterns(), () => Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), "ios") |
| 104 | .process(runIosSpawn); | |
| 105 | }); | |
| 106 | }); | |
8a67e140Artem Egorov8 years ago | 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. | |
7daed3fcArtem Egorov8 years ago | 113 | this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging."); |
8a67e140Artem Egorov8 years ago | 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>([ | |
db6fd42aRuslan Bikkinin7 years ago | 119 | this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName), |
8a67e140Artem Egorov8 years ago | 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 | |
db6fd42aRuslan Bikkinin7 years ago | 144 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName); |
8a67e140Artem Egorov8 years ago | 145 | }) |
| 146 | .then(() => { | |
| 147 | // Relaunch the app | |
| 148 | return this.runApp(); | |
| 149 | }); | |
| 150 | }); | |
| 151 | } | |
| 152 | | |
0a68f8dbArtem Egorov8 years ago | 153 | public disableJSDebuggingMode(): Q.Promise<void> { |
db6fd42aRuslan Bikkinin7 years ago | 154 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName); |
0a68f8dbArtem Egorov8 years ago | 155 | } |
| 156 | | |
8a67e140Artem Egorov8 years ago | 157 | public prewarmBundleCache(): Q.Promise<void> { |
0a68f8dbArtem Egorov8 years ago | 158 | return this.packager.prewarmBundleCache("ios"); |
8a67e140Artem Egorov8 years ago | 159 | } |
| 160 | | |
cbc7ac5bArtem Egorov7 years ago | 161 | public getRunArguments(): string[] { |
8a67e140Artem Egorov8 years ago | 162 | let runArguments: string[] = []; |
0db0be15Artem Egorov8 years ago | 163 | |
| 164 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { | |
b57ea017Artem Egorov8 years ago | 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 | } | |
8abbd163Artem Egorov8 years ago | 176 | |
b57ea017Artem Egorov8 years ago | 177 | if (this.runOptions.iosRelativeProjectPath) { |
| 178 | runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath); | |
8022afdfVladimir Kotikov8 years ago | 179 | } |
8a67e140Artem Egorov8 years ago | 180 | |
b57ea017Artem Egorov8 years ago | 181 | // provide any defined scheme |
| 182 | if (this.runOptions.scheme) { | |
| 183 | runArguments.push("--scheme", this.runOptions.scheme); | |
| 184 | } | |
8a67e140Artem Egorov8 years ago | 185 | } |
| 186 | | |
| 187 | return runArguments; | |
| 188 | } | |
| 189 | | |
| 190 | private generateSuccessPatterns(): Q.Promise<string[]> { | |
ed8367fdVladimir Kotikov8 years ago | 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}: `])); | |
8a67e140Artem Egorov8 years ago | 196 | } |
| 197 | | |
db6fd42aRuslan Bikkinin7 years ago | 198 | private getConfiguration(): string { |
| 199 | return this.getOptFromRunArgs(this.configurationArgumentName) || this.defaultConfiguration; | |
| 200 | } | |
| 201 | | |
8a67e140Artem Egorov8 years ago | 202 | private getBundleId(): Q.Promise<string> { |
db6fd42aRuslan Bikkinin7 years ago | 203 | return this.plistBuddy.getBundleId(this.iosProjectRoot, true, this.runOptions.configuration, this.runOptions.productName); |
8a67e140Artem Egorov8 years ago | 204 | } |
7daed3fcArtem Egorov8 years ago | 205 | |
4edcda70Artem Egorov8 years ago | 206 | private static remote(fsPath: string): RemoteExtension { |
7daed3fcArtem Egorov8 years ago | 207 | if (this.remoteExtension) { |
| 208 | return this.remoteExtension; | |
| 209 | } else { | |
4edcda70Artem Egorov8 years ago | 210 | return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath)); |
7daed3fcArtem Egorov8 years ago | 211 | } |
| 212 | } | |
8a67e140Artem Egorov8 years ago | 213 | } |