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