microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/iOSPlatform.ts
250lines · 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 | | |
3021756bYuri Skorokhodov6 years ago | 47 | private static readonly RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"]; |
8a67e140Artem Egorov8 years ago | 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); |
116c3cb0Ruslan Bikkinin7 years ago | 67 | const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false); |
| 68 | this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot, schemeFromArgs ? schemeFromArgs : this.runOptions.scheme); | |
8a67e140Artem Egorov8 years ago | 69 | |
db6fd42aRuslan Bikkinin7 years ago | 70 | if (this.runArguments && this.runArguments.length > 0) { |
| 71 | this.targetType = (this.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ? | |
8022afdfVladimir Kotikov8 years ago | 72 | IOSPlatform.deviceString : IOSPlatform.simulatorString; |
| 73 | return; | |
| 74 | } | |
0db0be15Artem Egorov8 years ago | 75 | |
8022afdfVladimir Kotikov8 years ago | 76 | if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString && |
| 77 | this.runOptions.target !== IOSPlatform.deviceString)) { | |
0db0be15Artem Egorov8 years ago | 78 | |
8022afdfVladimir Kotikov8 years ago | 79 | this.targetType = IOSPlatform.simulatorString; |
| 80 | return; | |
8a67e140Artem Egorov8 years ago | 81 | } |
8022afdfVladimir Kotikov8 years ago | 82 | |
| 83 | this.targetType = this.runOptions.target || IOSPlatform.simulatorString; | |
8a67e140Artem Egorov8 years ago | 84 | } |
| 85 | | |
| 86 | public runApp(): Q.Promise<void> { | |
031832ffArtem Egorov7 years ago | 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)) { | |
db6fd42aRuslan Bikkinin7 years ago | 101 | this.runArguments.push("--no-packager"); |
031832ffArtem Egorov7 years ago | 102 | } |
3021756bYuri Skorokhodov6 years ago | 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 | } | |
db6fd42aRuslan Bikkinin7 years ago | 108 | const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env}); |
3021756bYuri Skorokhodov6 years ago | 109 | return new OutputVerifier(() => this.generateSuccessPatterns(version), () => Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), "ios") |
031832ffArtem Egorov7 years ago | 110 | .process(runIosSpawn); |
| 111 | }); | |
| 112 | }); | |
8a67e140Artem Egorov8 years ago | 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. | |
7daed3fcArtem Egorov8 years ago | 119 | this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging."); |
8a67e140Artem Egorov8 years ago | 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>([ | |
db6fd42aRuslan Bikkinin7 years ago | 125 | this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName), |
8a67e140Artem Egorov8 years ago | 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 | |
db6fd42aRuslan Bikkinin7 years ago | 150 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName); |
8a67e140Artem Egorov8 years ago | 151 | }) |
| 152 | .then(() => { | |
| 153 | // Relaunch the app | |
| 154 | return this.runApp(); | |
| 155 | }); | |
| 156 | }); | |
| 157 | } | |
| 158 | | |
0a68f8dbArtem Egorov8 years ago | 159 | public disableJSDebuggingMode(): Q.Promise<void> { |
db6fd42aRuslan Bikkinin7 years ago | 160 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName); |
0a68f8dbArtem Egorov8 years ago | 161 | } |
| 162 | | |
8a67e140Artem Egorov8 years ago | 163 | public prewarmBundleCache(): Q.Promise<void> { |
0a68f8dbArtem Egorov8 years ago | 164 | return this.packager.prewarmBundleCache("ios"); |
8a67e140Artem Egorov8 years ago | 165 | } |
| 166 | | |
cbc7ac5bArtem Egorov7 years ago | 167 | public getRunArguments(): string[] { |
8a67e140Artem Egorov8 years ago | 168 | let runArguments: string[] = []; |
0db0be15Artem Egorov8 years ago | 169 | |
| 170 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { | |
b57ea017Artem Egorov8 years ago | 171 | runArguments = this.runOptions.runArguments; |
116c3cb0Ruslan Bikkinin7 years ago | 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 | } | |
b57ea017Artem Egorov8 years ago | 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 | } | |
8abbd163Artem Egorov8 years ago | 190 | |
b57ea017Artem Egorov8 years ago | 191 | if (this.runOptions.iosRelativeProjectPath) { |
| 192 | runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath); | |
8022afdfVladimir Kotikov8 years ago | 193 | } |
8a67e140Artem Egorov8 years ago | 194 | |
b57ea017Artem Egorov8 years ago | 195 | // provide any defined scheme |
| 196 | if (this.runOptions.scheme) { | |
| 197 | runArguments.push("--scheme", this.runOptions.scheme); | |
| 198 | } | |
8a67e140Artem Egorov8 years ago | 199 | } |
| 200 | | |
| 201 | return runArguments; | |
| 202 | } | |
| 203 | | |
3021756bYuri Skorokhodov6 years ago | 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 | | |
8a67e140Artem Egorov8 years ago | 226 | } |
| 227 | | |
db6fd42aRuslan Bikkinin7 years ago | 228 | private getConfiguration(): string { |
116c3cb0Ruslan Bikkinin7 years ago | 229 | return IOSPlatform.getOptFromRunArgs(this.runArguments, this.configurationArgumentName) || this.defaultConfiguration; |
db6fd42aRuslan Bikkinin7 years ago | 230 | } |
| 231 | | |
8a67e140Artem Egorov8 years ago | 232 | private getBundleId(): Q.Promise<string> { |
116c3cb0Ruslan Bikkinin7 years ago | 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); | |
8a67e140Artem Egorov8 years ago | 241 | } |
7daed3fcArtem Egorov8 years ago | 242 | |
4edcda70Artem Egorov8 years ago | 243 | private static remote(fsPath: string): RemoteExtension { |
7daed3fcArtem Egorov8 years ago | 244 | if (this.remoteExtension) { |
| 245 | return this.remoteExtension; | |
| 246 | } else { | |
4edcda70Artem Egorov8 years ago | 247 | return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath)); |
7daed3fcArtem Egorov8 years ago | 248 | } |
| 249 | } | |
8a67e140Artem Egorov8 years ago | 250 | } |