microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/iOSPlatform.ts
307lines · 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 path from "path"; | |
df8c800dArtem Egorov8 years ago | 5 | import * as semver from "semver"; |
8a67e140Artem Egorov8 years ago | 6 | |
| 7 | import {ChildProcess} from "../../common/node/childProcess"; | |
| 8 | import {CommandExecutor} from "../../common/commandExecutor"; | |
0a68f8dbArtem Egorov8 years ago | 9 | import {GeneralMobilePlatform, MobilePlatformDeps, TargetType} from "../generalMobilePlatform"; |
259c018fYuri Skorokhodov5 years ago | 10 | import {IIOSRunOptions, PlatformType} from "../launchArgs"; |
0a68f8dbArtem Egorov8 years ago | 11 | import {PlistBuddy} from "./plistBuddy"; |
| 12 | import {IOSDebugModeManager} from "./iOSDebugModeManager"; | |
8a67e140Artem Egorov8 years ago | 13 | import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier"; |
031832ffArtem Egorov8 years ago | 14 | import {TelemetryHelper} from "../../common/telemetryHelper"; |
fc602bb6Yuri Skorokhodov7 years ago | 15 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
d7d405aeYuri Skorokhodov7 years ago | 16 | import * as nls from "vscode-nls"; |
7e74daf7Yuri Skorokhodov6 years ago | 17 | import { AppLauncher } from "../appLauncher"; |
119d7878JiglioNero5 years ago | 18 | import { IiOSSimulator, IOSSimulatorManager } from "./iOSSimulatorManager"; |
2d8af448Yuri Skorokhodov6 years ago | 19 | nls.config({ messageFormat: nls.MessageFormat.bundle, bundleFormat: nls.BundleFormat.standalone })(); |
d7d405aeYuri Skorokhodov7 years ago | 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"; | |
0a68f8dbArtem Egorov8 years ago | 24 | |
8a67e140Artem Egorov8 years ago | 25 | private plistBuddy = new PlistBuddy(); |
8022afdfVladimir Kotikov8 years ago | 26 | private targetType: TargetType = "simulator"; |
0db0be15Artem Egorov8 years ago | 27 | private iosProjectRoot: string; |
7daed3fcArtem Egorov8 years ago | 28 | private iosDebugModeManager: IOSDebugModeManager; |
119d7878JiglioNero5 years ago | 29 | private simulatorManager: IOSSimulatorManager; |
7daed3fcArtem Egorov8 years ago | 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 | |
ce5e88eeYuri Skorokhodov5 years ago | 48 | public showDevMenu(appLauncher: AppLauncher): Promise<void> { |
7e74daf7Yuri Skorokhodov6 years ago | 49 | const worker = appLauncher.getAppWorker(); |
| 50 | if (worker) { | |
| 51 | worker.showDevMenuCommand(); | |
| 52 | } | |
| 53 | | |
ce5e88eeYuri Skorokhodov5 years ago | 54 | return Promise.resolve(); |
7daed3fcArtem Egorov8 years ago | 55 | } |
| 56 | | |
ce5e88eeYuri Skorokhodov5 years ago | 57 | public reloadApp(appLauncher: AppLauncher): Promise<void> { |
7e74daf7Yuri Skorokhodov6 years ago | 58 | const worker = appLauncher.getAppWorker(); |
| 59 | if (worker) { | |
| 60 | worker.reloadAppCommand(); | |
| 61 | } | |
ce5e88eeYuri Skorokhodov5 years ago | 62 | return Promise.resolve(); |
7daed3fcArtem Egorov8 years ago | 63 | } |
| 64 | | |
0a68f8dbArtem Egorov8 years ago | 65 | constructor(protected runOptions: IIOSRunOptions, platformDeps: MobilePlatformDeps = {}) { |
| 66 | super(runOptions, platformDeps); | |
8a67e140Artem Egorov8 years ago | 67 | |
119d7878JiglioNero5 years ago | 68 | this.simulatorManager = new IOSSimulatorManager(); |
db6fd42aRuslan Bikkinin7 years ago | 69 | this.runOptions.configuration = this.getConfiguration(); |
| 70 | | |
0db0be15Artem Egorov8 years ago | 71 | if (this.runOptions.iosRelativeProjectPath) { // Deprecated option |
d7d405aeYuri Skorokhodov7 years ago | 72 | this.logger.warning(localize("iosRelativeProjectPathOptionIsDeprecatedUseRunArgumentsInstead", "'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead.")); |
8a67e140Artem Egorov8 years ago | 73 | } |
| 74 | | |
764655c9Yuri Skorokhodov6 years ago | 75 | const iosProjectFolderPath = IOSPlatform.getOptFromRunArgs(this.runArguments, "--project-path", false); |
| 76 | this.iosProjectRoot = path.join(this.projectPath, iosProjectFolderPath || this.runOptions.iosRelativeProjectPath || IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH); | |
116c3cb0Ruslan Bikkinin7 years ago | 77 | const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false); |
764655c9Yuri Skorokhodov6 years ago | 78 | this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot, this.projectPath, schemeFromArgs ? schemeFromArgs : this.runOptions.scheme); |
8a67e140Artem Egorov8 years ago | 79 | |
db6fd42aRuslan Bikkinin7 years ago | 80 | if (this.runArguments && this.runArguments.length > 0) { |
| 81 | this.targetType = (this.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ? | |
8022afdfVladimir Kotikov8 years ago | 82 | IOSPlatform.deviceString : IOSPlatform.simulatorString; |
| 83 | return; | |
| 84 | } | |
0db0be15Artem Egorov8 years ago | 85 | |
8022afdfVladimir Kotikov8 years ago | 86 | if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString && |
| 87 | this.runOptions.target !== IOSPlatform.deviceString)) { | |
0db0be15Artem Egorov8 years ago | 88 | |
8022afdfVladimir Kotikov8 years ago | 89 | this.targetType = IOSPlatform.simulatorString; |
| 90 | return; | |
8a67e140Artem Egorov8 years ago | 91 | } |
8022afdfVladimir Kotikov8 years ago | 92 | |
| 93 | this.targetType = this.runOptions.target || IOSPlatform.simulatorString; | |
8a67e140Artem Egorov8 years ago | 94 | } |
| 95 | | |
119d7878JiglioNero5 years ago | 96 | public resolveVirtualDevice(target: string): Promise<IiOSSimulator | null> { |
| 97 | if (target === "simulator") { | |
| 98 | return this.simulatorManager.startSelection() | |
| 99 | .then((simulatorName: string | undefined) => { | |
| 100 | if (simulatorName) { | |
| 101 | const simulator = this.simulatorManager.findSimulator(simulatorName); | |
| 102 | if (simulator) { | |
| 103 | GeneralMobilePlatform.removeRunArgument(this.runArguments, "--simulator", true); | |
| 104 | GeneralMobilePlatform.setRunArgument(this.runArguments, "--udid", simulator.id); | |
| 105 | } | |
| 106 | return simulator; | |
| 107 | } | |
| 108 | else { | |
| 109 | return null; | |
| 110 | } | |
| 111 | }); | |
| 112 | } | |
| 113 | else if (!target.includes("device")) { | |
| 114 | return this.simulatorManager.collectSimulators() | |
| 115 | .then((simulators) => { | |
| 116 | let simulator = this.simulatorManager.getSimulatorById(target, simulators); | |
| 117 | if (simulator) { | |
| 118 | GeneralMobilePlatform.removeRunArgument(this.runArguments, "--simulator", false); | |
| 119 | GeneralMobilePlatform.setRunArgument(this.runArguments, "--udid", simulator.id); | |
| 120 | } | |
3b728847JiglioNero5 years ago | 121 | return null; |
119d7878JiglioNero5 years ago | 122 | }); |
| 123 | } | |
| 124 | else { | |
| 125 | return Promise.resolve(null); | |
| 126 | } | |
| 127 | } | |
| 128 | | |
ce5e88eeYuri Skorokhodov5 years ago | 129 | public runApp(): Promise<void> { |
ba953e9fRedMickey6 years ago | 130 | let extProps = { |
031832ffArtem Egorov8 years ago | 131 | platform: { |
259c018fYuri Skorokhodov5 years ago | 132 | value: PlatformType.iOS, |
031832ffArtem Egorov8 years ago | 133 | isPii: false, |
| 134 | }, | |
| 135 | }; | |
| 136 | | |
341dba36Yuri Skorokhodov5 years ago | 137 | extProps = TelemetryHelper.addPlatformPropertiesToTelemetryProperties(this.runOptions, this.runOptions.reactNativeVersions, extProps); |
ba953e9fRedMickey6 years ago | 138 | |
031832ffArtem Egorov8 years ago | 139 | return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => { |
| 140 | // Compile, deploy, and launch the app on either a simulator or a device | |
de838bbfJiglioNero6 years ago | 141 | const env = GeneralMobilePlatform.getEnvArgument(process.env, this.runOptions.env, this.runOptions.envFile); |
031832ffArtem Egorov8 years ago | 142 | |
7fa90b3bRedMickey6 years ago | 143 | if (!semver.valid(this.runOptions.reactNativeVersions.reactNativeVersion) /*Custom RN implementations should support this flag*/ || semver.gte(this.runOptions.reactNativeVersions.reactNativeVersion, IOSPlatform.NO_PACKAGER_VERSION)) { |
| 144 | this.runArguments.push("--no-packager"); | |
| 145 | } | |
| 146 | // Since @react-native-community/cli@2.1.0 build output are hidden by default | |
| 147 | // we are using `--verbose` to show it as it contains `BUILD SUCCESSFUL` and other patterns | |
| 148 | if (semver.gte(this.runOptions.reactNativeVersions.reactNativeVersion, "0.60.0")) { | |
| 149 | this.runArguments.push("--verbose"); | |
| 150 | } | |
| 151 | const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env}); | |
ce5e88eeYuri Skorokhodov5 years ago | 152 | return new OutputVerifier( |
| 153 | () => | |
| 154 | this.generateSuccessPatterns(this.runOptions.reactNativeVersions.reactNativeVersion), | |
| 155 | () => | |
259c018fYuri Skorokhodov5 years ago | 156 | Promise.resolve(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), PlatformType.iOS) |
7fa90b3bRedMickey6 years ago | 157 | .process(runIosSpawn); |
031832ffArtem Egorov8 years ago | 158 | }); |
8a67e140Artem Egorov8 years ago | 159 | } |
| 160 | | |
ce5e88eeYuri Skorokhodov5 years ago | 161 | public enableJSDebuggingMode(): Promise<void> { |
8a67e140Artem Egorov8 years ago | 162 | // Configure the app for debugging |
| 163 | if (this.targetType === IOSPlatform.deviceString) { | |
| 164 | // Note that currently we cannot automatically switch the device into debug mode. | |
7daed3fcArtem Egorov8 years ago | 165 | this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging."); |
ce5e88eeYuri Skorokhodov5 years ago | 166 | return Promise.resolve(); |
8a67e140Artem Egorov8 years ago | 167 | } |
| 168 | | |
| 169 | // Wait until the configuration file exists, and check to see if debugging is enabled | |
ce5e88eeYuri Skorokhodov5 years ago | 170 | return Promise.all<boolean | string>([ |
db6fd42aRuslan Bikkinin7 years ago | 171 | this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName), |
8a67e140Artem Egorov8 years ago | 172 | this.getBundleId(), |
| 173 | ]) | |
ce5e88eeYuri Skorokhodov5 years ago | 174 | .then(([debugModeEnabled, bundleId]) => { |
8a67e140Artem Egorov8 years ago | 175 | if (debugModeEnabled) { |
ce5e88eeYuri Skorokhodov5 years ago | 176 | return Promise.resolve(); |
8a67e140Artem Egorov8 years ago | 177 | } |
| 178 | | |
| 179 | // Debugging must still be enabled | |
| 180 | // We enable debugging by writing to a plist file that backs a NSUserDefaults object, | |
| 181 | // but that file is written to by the app on occasion. To avoid races, we shut the app | |
| 182 | // down before writing to the file. | |
| 183 | const childProcess = new ChildProcess(); | |
| 184 | | |
| 185 | return childProcess.execToString("xcrun simctl spawn booted launchctl list") | |
| 186 | .then((output: string) => { | |
| 187 | // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37] | |
| 188 | const regex = new RegExp(`(\\S+${bundleId}\\S+)`); | |
| 189 | const match = regex.exec(output); | |
| 190 | | |
| 191 | // If we don't find a match, the app must not be running and so we do not need to close it | |
| 192 | return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null; | |
| 193 | }) | |
| 194 | .then(() => { | |
| 195 | // Write to the settings file while the app is not running to avoid races | |
db6fd42aRuslan Bikkinin7 years ago | 196 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName); |
8a67e140Artem Egorov8 years ago | 197 | }) |
| 198 | .then(() => { | |
| 199 | // Relaunch the app | |
| 200 | return this.runApp(); | |
| 201 | }); | |
| 202 | }); | |
| 203 | } | |
| 204 | | |
ce5e88eeYuri Skorokhodov5 years ago | 205 | public disableJSDebuggingMode(): Promise<void> { |
c73f53cbJiglioNero5 years ago | 206 | if (this.targetType === IOSPlatform.deviceString) { |
| 207 | return Promise.resolve(); | |
| 208 | } | |
db6fd42aRuslan Bikkinin7 years ago | 209 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName); |
0a68f8dbArtem Egorov8 years ago | 210 | } |
| 211 | | |
ce5e88eeYuri Skorokhodov5 years ago | 212 | public prewarmBundleCache(): Promise<void> { |
259c018fYuri Skorokhodov5 years ago | 213 | return this.packager.prewarmBundleCache(PlatformType.iOS); |
8a67e140Artem Egorov8 years ago | 214 | } |
| 215 | | |
cbc7ac5bArtem Egorov7 years ago | 216 | public getRunArguments(): string[] { |
8a67e140Artem Egorov8 years ago | 217 | let runArguments: string[] = []; |
0db0be15Artem Egorov8 years ago | 218 | |
| 219 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { | |
b57ea017Artem Egorov8 years ago | 220 | runArguments = this.runOptions.runArguments; |
116c3cb0Ruslan Bikkinin7 years ago | 221 | if (this.runOptions.scheme) { |
| 222 | const schemeFromArgs = IOSPlatform.getOptFromRunArgs(runArguments, "--scheme", false); | |
| 223 | if (!schemeFromArgs) { | |
| 224 | runArguments.push("--scheme", this.runOptions.scheme); | |
| 225 | } else { | |
| 226 | this.logger.warning(localize("iosSchemeParameterAlreadySetInRunArguments", "'--scheme' is set as 'runArguments' configuration parameter value, 'scheme' configuration parameter value will be omitted")); | |
| 227 | } | |
| 228 | } | |
b57ea017Artem Egorov8 years ago | 229 | } else { |
| 230 | if (this.runOptions.target) { | |
de838bbfJiglioNero6 years ago | 231 | runArguments.push(...this.handleTargetArg(this.runOptions.target)); |
b57ea017Artem Egorov8 years ago | 232 | } |
8abbd163Artem Egorov8 years ago | 233 | |
b57ea017Artem Egorov8 years ago | 234 | if (this.runOptions.iosRelativeProjectPath) { |
| 235 | runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath); | |
8022afdfVladimir Kotikov8 years ago | 236 | } |
8a67e140Artem Egorov8 years ago | 237 | |
b57ea017Artem Egorov8 years ago | 238 | // provide any defined scheme |
| 239 | if (this.runOptions.scheme) { | |
| 240 | runArguments.push("--scheme", this.runOptions.scheme); | |
| 241 | } | |
8a67e140Artem Egorov8 years ago | 242 | } |
| 243 | | |
| 244 | return runArguments; | |
| 245 | } | |
| 246 | | |
de838bbfJiglioNero6 years ago | 247 | private handleTargetArg(target: string): string[] { |
| 248 | if (target === IOSPlatform.deviceString || | |
| 249 | target === IOSPlatform.simulatorString) { | |
| 250 | return [`--${this.runOptions.target}`]; | |
| 251 | } else { | |
| 252 | if (target.indexOf(IOSPlatform.deviceString) !== -1) { | |
| 253 | const deviceArgs = target.split("="); | |
| 254 | return deviceArgs[1] ? [`--${IOSPlatform.deviceString}`, deviceArgs[1]] : [`--${IOSPlatform.deviceString}`]; | |
| 255 | } else { | |
| 256 | return [`--${IOSPlatform.simulatorString}`, `${this.runOptions.target}`]; | |
| 257 | } | |
| 258 | } | |
| 259 | } | |
| 260 | | |
ce5e88eeYuri Skorokhodov5 years ago | 261 | private generateSuccessPatterns(version: string): Promise<string[]> { |
3021756bYuri Skorokhodov6 years ago | 262 | // Clone RUN_IOS_SUCCESS_PATTERNS to avoid its runtime mutation |
| 263 | let successPatterns = [...IOSPlatform.RUN_IOS_SUCCESS_PATTERNS]; | |
| 264 | if (this.targetType === IOSPlatform.deviceString) { | |
| 265 | if (semver.gte(version, "0.60.0")) { | |
| 266 | successPatterns.push("success Installed the app on the device"); | |
| 267 | } else { | |
| 268 | successPatterns.push("INSTALLATION SUCCEEDED"); | |
| 269 | } | |
ce5e88eeYuri Skorokhodov5 years ago | 270 | return Promise.resolve(successPatterns); |
3021756bYuri Skorokhodov6 years ago | 271 | } else { |
| 272 | return this.getBundleId() | |
| 273 | .then(bundleId => { | |
| 274 | if (semver.gte(version, "0.60.0")) { | |
| 275 | successPatterns.push(`Launching "${bundleId}"\nsuccess Successfully launched the app `); | |
| 276 | } else { | |
| 277 | successPatterns.push(`Launching ${bundleId}\n${bundleId}: `); | |
| 278 | } | |
| 279 | return successPatterns; | |
| 280 | }); | |
| 281 | } | |
| 282 | | |
8a67e140Artem Egorov8 years ago | 283 | } |
| 284 | | |
db6fd42aRuslan Bikkinin7 years ago | 285 | private getConfiguration(): string { |
116c3cb0Ruslan Bikkinin7 years ago | 286 | return IOSPlatform.getOptFromRunArgs(this.runArguments, this.configurationArgumentName) || this.defaultConfiguration; |
db6fd42aRuslan Bikkinin7 years ago | 287 | } |
| 288 | | |
ce5e88eeYuri Skorokhodov5 years ago | 289 | private getBundleId(): Promise<string> { |
116c3cb0Ruslan Bikkinin7 years ago | 290 | let scheme = this.runOptions.scheme; |
| 291 | if (!scheme) { | |
| 292 | const schemeFromArgs = IOSPlatform.getOptFromRunArgs(this.runArguments, "--scheme", false); | |
| 293 | if (schemeFromArgs) { | |
| 294 | scheme = schemeFromArgs; | |
| 295 | } | |
| 296 | } | |
764655c9Yuri Skorokhodov6 years ago | 297 | return this.plistBuddy.getBundleId(this.iosProjectRoot, this.projectPath, true, this.runOptions.configuration, this.runOptions.productName, scheme); |
8a67e140Artem Egorov8 years ago | 298 | } |
7daed3fcArtem Egorov8 years ago | 299 | |
f872f4d5RedMickey6 years ago | 300 | /*private static remote(fsPath: string): RemoteExtension { // TODO replace with a new implementation from appLauncher |
7daed3fcArtem Egorov8 years ago | 301 | if (this.remoteExtension) { |
| 302 | return this.remoteExtension; | |
| 303 | } else { | |
4edcda70Artem Egorov8 years ago | 304 | return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath)); |
7daed3fcArtem Egorov8 years ago | 305 | } |
f872f4d5RedMickey6 years ago | 306 | }*/ |
8a67e140Artem Egorov8 years ago | 307 | } |