microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/iOSPlatform.ts
249lines · 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"; |
031832ffArtem Egorov8 years ago | 17 | import {TelemetryHelper} from "../../common/telemetryHelper"; |
fc602bb6Yuri Skorokhodov7 years ago | 18 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
d7d405aeYuri Skorokhodov7 years ago | 19 | import * as nls from "vscode-nls"; |
| 20 | const localize = nls.loadMessageBundle(); | |
8022afdfVladimir Kotikov8 years ago | 21 | |
8a67e140Artem Egorov8 years ago | 22 | export class IOSPlatform extends GeneralMobilePlatform { |
| 23 | public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios"; | |
7daed3fcArtem Egorov8 years ago | 24 | private static remoteExtension: RemoteExtension; |
0a68f8dbArtem Egorov8 years ago | 25 | |
8a67e140Artem Egorov8 years ago | 26 | private plistBuddy = new PlistBuddy(); |
8022afdfVladimir Kotikov8 years ago | 27 | private targetType: TargetType = "simulator"; |
0db0be15Artem Egorov8 years ago | 28 | private iosProjectRoot: string; |
7daed3fcArtem Egorov8 years ago | 29 | private iosDebugModeManager: IOSDebugModeManager; |
| 30 | | |
db6fd42aRuslan Bikkinin7 years ago | 31 | private defaultConfiguration: string = "Debug"; |
| 32 | private configurationArgumentName: string = "--configuration"; | |
8a67e140Artem Egorov8 years ago | 33 | |
0a68f8dbArtem Egorov8 years ago | 34 | // We should add the common iOS build/run errors we find to this list |
8a67e140Artem Egorov8 years ago | 35 | private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{ |
| 36 | pattern: "No devices are booted", | |
d7d405aeYuri Skorokhodov7 years ago | 37 | errorCode: InternalErrorCode.IOSSimulatorNotLaunchable, |
8a67e140Artem Egorov8 years ago | 38 | }, { |
| 39 | pattern: "FBSOpenApplicationErrorDomain", | |
d7d405aeYuri Skorokhodov7 years ago | 40 | errorCode: InternalErrorCode.IOSSimulatorNotLaunchable, |
8a67e140Artem Egorov8 years ago | 41 | }, { |
| 42 | pattern: "ios-deploy", | |
d7d405aeYuri Skorokhodov7 years ago | 43 | errorCode: InternalErrorCode.IOSDeployNotFound, |
8a67e140Artem Egorov8 years ago | 44 | }]; |
| 45 | | |
3021756bYuri Skorokhodov6 years ago | 46 | private static readonly RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"]; |
8a67e140Artem Egorov8 years ago | 47 | |
db6fd42aRuslan Bikkinin7 years ago | 48 | public showDevMenu(deviceId?: string): Q.Promise<void> { |
| 49 | return IOSPlatform.remote(this.runOptions.projectRoot).showDevMenu(deviceId); | |
7daed3fcArtem Egorov8 years ago | 50 | } |
| 51 | | |
db6fd42aRuslan Bikkinin7 years ago | 52 | public reloadApp(deviceId?: string): Q.Promise<void> { |
| 53 | return IOSPlatform.remote(this.runOptions.projectRoot).reloadApp(deviceId); | |
7daed3fcArtem Egorov8 years ago | 54 | } |
| 55 | | |
0a68f8dbArtem Egorov8 years ago | 56 | constructor(protected runOptions: IIOSRunOptions, platformDeps: MobilePlatformDeps = {}) { |
| 57 | super(runOptions, platformDeps); | |
8a67e140Artem Egorov8 years ago | 58 | |
db6fd42aRuslan Bikkinin7 years ago | 59 | this.runOptions.configuration = this.getConfiguration(); |
| 60 | | |
0db0be15Artem Egorov8 years ago | 61 | if (this.runOptions.iosRelativeProjectPath) { // Deprecated option |
d7d405aeYuri Skorokhodov7 years ago | 62 | this.logger.warning(localize("iosRelativeProjectPathOptionIsDeprecatedUseRunArgumentsInstead", "'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead.")); |
8a67e140Artem Egorov8 years ago | 63 | } |
| 64 | | |
764655c9Yuri Skorokhodov6 years ago | 65 | const iosProjectFolderPath = IOSPlatform.getOptFromRunArgs(this.runArguments, "--project-path", false); |
| 66 | this.iosProjectRoot = path.join(this.projectPath, iosProjectFolderPath || this.runOptions.iosRelativeProjectPath || IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH); | |
116c3cb0Ruslan Bikkinin7 years ago | 67 | const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false); |
764655c9Yuri Skorokhodov6 years ago | 68 | this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot, this.projectPath, 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> { | |
ba953e9fRedMickey6 years ago | 87 | let extProps = { |
031832ffArtem Egorov8 years ago | 88 | platform: { |
| 89 | value: "ios", | |
| 90 | isPii: false, | |
| 91 | }, | |
| 92 | }; | |
| 93 | | |
7fa90b3bRedMickey6 years ago | 94 | extProps = TelemetryHelper.addPropertyToTelemetryProperties(this.runOptions.reactNativeVersions.reactNativeVersion, "reactNativeVersion", extProps); |
ba953e9fRedMickey6 years ago | 95 | |
031832ffArtem Egorov8 years ago | 96 | return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => { |
| 97 | // Compile, deploy, and launch the app on either a simulator or a device | |
| 98 | const env = this.getEnvArgument(); | |
| 99 | | |
7fa90b3bRedMickey6 years ago | 100 | if (!semver.valid(this.runOptions.reactNativeVersions.reactNativeVersion) /*Custom RN implementations should support this flag*/ || semver.gte(this.runOptions.reactNativeVersions.reactNativeVersion, 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(this.runOptions.reactNativeVersions.reactNativeVersion, "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(this.runOptions.reactNativeVersions.reactNativeVersion), () => Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), "ios") | |
| 110 | .process(runIosSpawn); | |
031832ffArtem Egorov8 years ago | 111 | }); |
8a67e140Artem Egorov8 years ago | 112 | } |
| 113 | | |
| 114 | public enableJSDebuggingMode(): Q.Promise<void> { | |
| 115 | // Configure the app for debugging | |
| 116 | if (this.targetType === IOSPlatform.deviceString) { | |
| 117 | // Note that currently we cannot automatically switch the device into debug mode. | |
7daed3fcArtem Egorov8 years ago | 118 | this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging."); |
8a67e140Artem Egorov8 years ago | 119 | return Q.resolve<void>(void 0); |
| 120 | } | |
| 121 | | |
| 122 | // Wait until the configuration file exists, and check to see if debugging is enabled | |
| 123 | return Q.all<boolean | string>([ | |
db6fd42aRuslan Bikkinin7 years ago | 124 | this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName), |
8a67e140Artem Egorov8 years ago | 125 | this.getBundleId(), |
| 126 | ]) | |
| 127 | .spread((debugModeEnabled: boolean, bundleId: string) => { | |
| 128 | if (debugModeEnabled) { | |
| 129 | return Q.resolve(void 0); | |
| 130 | } | |
| 131 | | |
| 132 | // Debugging must still be enabled | |
| 133 | // We enable debugging by writing to a plist file that backs a NSUserDefaults object, | |
| 134 | // but that file is written to by the app on occasion. To avoid races, we shut the app | |
| 135 | // down before writing to the file. | |
| 136 | const childProcess = new ChildProcess(); | |
| 137 | | |
| 138 | return childProcess.execToString("xcrun simctl spawn booted launchctl list") | |
| 139 | .then((output: string) => { | |
| 140 | // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37] | |
| 141 | const regex = new RegExp(`(\\S+${bundleId}\\S+)`); | |
| 142 | const match = regex.exec(output); | |
| 143 | | |
| 144 | // If we don't find a match, the app must not be running and so we do not need to close it | |
| 145 | return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null; | |
| 146 | }) | |
| 147 | .then(() => { | |
| 148 | // Write to the settings file while the app is not running to avoid races | |
db6fd42aRuslan Bikkinin7 years ago | 149 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName); |
8a67e140Artem Egorov8 years ago | 150 | }) |
| 151 | .then(() => { | |
| 152 | // Relaunch the app | |
| 153 | return this.runApp(); | |
| 154 | }); | |
| 155 | }); | |
| 156 | } | |
| 157 | | |
0a68f8dbArtem Egorov8 years ago | 158 | public disableJSDebuggingMode(): Q.Promise<void> { |
db6fd42aRuslan Bikkinin7 years ago | 159 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName); |
0a68f8dbArtem Egorov8 years ago | 160 | } |
| 161 | | |
8a67e140Artem Egorov8 years ago | 162 | public prewarmBundleCache(): Q.Promise<void> { |
0a68f8dbArtem Egorov8 years ago | 163 | return this.packager.prewarmBundleCache("ios"); |
8a67e140Artem Egorov8 years ago | 164 | } |
| 165 | | |
cbc7ac5bArtem Egorov7 years ago | 166 | public getRunArguments(): string[] { |
8a67e140Artem Egorov8 years ago | 167 | let runArguments: string[] = []; |
0db0be15Artem Egorov8 years ago | 168 | |
| 169 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { | |
b57ea017Artem Egorov8 years ago | 170 | runArguments = this.runOptions.runArguments; |
116c3cb0Ruslan Bikkinin7 years ago | 171 | if (this.runOptions.scheme) { |
| 172 | const schemeFromArgs = IOSPlatform.getOptFromRunArgs(runArguments, "--scheme", false); | |
| 173 | if (!schemeFromArgs) { | |
| 174 | runArguments.push("--scheme", this.runOptions.scheme); | |
| 175 | } else { | |
| 176 | this.logger.warning(localize("iosSchemeParameterAlreadySetInRunArguments", "'--scheme' is set as 'runArguments' configuration parameter value, 'scheme' configuration parameter value will be omitted")); | |
| 177 | } | |
| 178 | } | |
b57ea017Artem Egorov8 years ago | 179 | } else { |
| 180 | if (this.runOptions.target) { | |
| 181 | if (this.runOptions.target === IOSPlatform.deviceString || | |
| 182 | this.runOptions.target === IOSPlatform.simulatorString) { | |
| 183 | | |
| 184 | runArguments.push(`--${this.runOptions.target}`); | |
| 185 | } else { | |
| 186 | runArguments.push("--simulator", `${this.runOptions.target}`); | |
| 187 | } | |
| 188 | } | |
8abbd163Artem Egorov8 years ago | 189 | |
b57ea017Artem Egorov8 years ago | 190 | if (this.runOptions.iosRelativeProjectPath) { |
| 191 | runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath); | |
8022afdfVladimir Kotikov8 years ago | 192 | } |
8a67e140Artem Egorov8 years ago | 193 | |
b57ea017Artem Egorov8 years ago | 194 | // provide any defined scheme |
| 195 | if (this.runOptions.scheme) { | |
| 196 | runArguments.push("--scheme", this.runOptions.scheme); | |
| 197 | } | |
8a67e140Artem Egorov8 years ago | 198 | } |
| 199 | | |
| 200 | return runArguments; | |
| 201 | } | |
| 202 | | |
3021756bYuri Skorokhodov6 years ago | 203 | private generateSuccessPatterns(version: string): Q.Promise<string[]> { |
| 204 | // Clone RUN_IOS_SUCCESS_PATTERNS to avoid its runtime mutation | |
| 205 | let successPatterns = [...IOSPlatform.RUN_IOS_SUCCESS_PATTERNS]; | |
| 206 | if (this.targetType === IOSPlatform.deviceString) { | |
| 207 | if (semver.gte(version, "0.60.0")) { | |
| 208 | successPatterns.push("success Installed the app on the device"); | |
| 209 | } else { | |
| 210 | successPatterns.push("INSTALLATION SUCCEEDED"); | |
| 211 | } | |
| 212 | return Q(successPatterns); | |
| 213 | } else { | |
| 214 | return this.getBundleId() | |
| 215 | .then(bundleId => { | |
| 216 | if (semver.gte(version, "0.60.0")) { | |
| 217 | successPatterns.push(`Launching "${bundleId}"\nsuccess Successfully launched the app `); | |
| 218 | } else { | |
| 219 | successPatterns.push(`Launching ${bundleId}\n${bundleId}: `); | |
| 220 | } | |
| 221 | return successPatterns; | |
| 222 | }); | |
| 223 | } | |
| 224 | | |
8a67e140Artem Egorov8 years ago | 225 | } |
| 226 | | |
db6fd42aRuslan Bikkinin7 years ago | 227 | private getConfiguration(): string { |
116c3cb0Ruslan Bikkinin7 years ago | 228 | return IOSPlatform.getOptFromRunArgs(this.runArguments, this.configurationArgumentName) || this.defaultConfiguration; |
db6fd42aRuslan Bikkinin7 years ago | 229 | } |
| 230 | | |
8a67e140Artem Egorov8 years ago | 231 | private getBundleId(): Q.Promise<string> { |
116c3cb0Ruslan Bikkinin7 years ago | 232 | let scheme = this.runOptions.scheme; |
| 233 | if (!scheme) { | |
| 234 | const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false); | |
| 235 | if (schemeFromArgs) { | |
| 236 | scheme = schemeFromArgs; | |
| 237 | } | |
| 238 | } | |
764655c9Yuri Skorokhodov6 years ago | 239 | return this.plistBuddy.getBundleId(this.iosProjectRoot, this.projectPath, true, this.runOptions.configuration, this.runOptions.productName, scheme); |
8a67e140Artem Egorov8 years ago | 240 | } |
7daed3fcArtem Egorov8 years ago | 241 | |
4edcda70Artem Egorov8 years ago | 242 | private static remote(fsPath: string): RemoteExtension { |
7daed3fcArtem Egorov8 years ago | 243 | if (this.remoteExtension) { |
| 244 | return this.remoteExtension; | |
| 245 | } else { | |
4edcda70Artem Egorov8 years ago | 246 | return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath)); |
7daed3fcArtem Egorov8 years ago | 247 | } |
| 248 | } | |
8a67e140Artem Egorov8 years ago | 249 | } |