microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/iOSPlatform.ts
211lines · 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"; |
0a68f8dbArtem Egorov8 years ago | 15 | import {ErrorHelper} from "../../common/error/errorHelper"; |
a41f5c68Artem Egorov8 years ago | 16 | import {SettingsHelper} from "../settingsHelper"; |
7daed3fcArtem Egorov8 years ago | 17 | import {RemoteExtension} from "../../common/remoteExtension"; |
df8c800dArtem Egorov8 years ago | 18 | import {ReactNativeProjectHelper} from "../../common/reactNativeProjectHelper"; |
031832ffArtem Egorov8 years ago | 19 | import {TelemetryHelper} from "../../common/telemetryHelper"; |
8022afdfVladimir Kotikov8 years ago | 20 | |
8a67e140Artem Egorov8 years ago | 21 | export class IOSPlatform extends GeneralMobilePlatform { |
| 22 | public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios"; | |
7daed3fcArtem Egorov8 years ago | 23 | private static remoteExtension: RemoteExtension; |
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; |
| 29 | | |
db6fd42aRuslan Bikkinin7 years ago | 30 | private defaultConfiguration: string = "Debug"; |
| 31 | private configurationArgumentName: string = "--configuration"; | |
8a67e140Artem Egorov8 years ago | 32 | |
0a68f8dbArtem Egorov8 years ago | 33 | // We should add the common iOS build/run errors we find to this list |
8a67e140Artem Egorov8 years ago | 34 | private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{ |
| 35 | pattern: "No devices are booted", | |
3c172a05Artem Egorov8 years ago | 36 | message: ErrorHelper.ERROR_STRINGS.IOSSimulatorNotLaunchable, |
8a67e140Artem Egorov8 years ago | 37 | }, { |
| 38 | pattern: "FBSOpenApplicationErrorDomain", | |
3c172a05Artem Egorov8 years ago | 39 | message: ErrorHelper.ERROR_STRINGS.IOSSimulatorNotLaunchable, |
8a67e140Artem Egorov8 years ago | 40 | }, { |
| 41 | pattern: "ios-deploy", | |
3c172a05Artem Egorov8 years ago | 42 | message: ErrorHelper.ERROR_STRINGS.IOSDeployNotFound, |
8a67e140Artem Egorov8 years ago | 43 | }]; |
| 44 | | |
| 45 | private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"]; | |
| 46 | | |
db6fd42aRuslan Bikkinin7 years ago | 47 | public showDevMenu(deviceId?: string): Q.Promise<void> { |
| 48 | return IOSPlatform.remote(this.runOptions.projectRoot).showDevMenu(deviceId); | |
7daed3fcArtem Egorov8 years ago | 49 | } |
| 50 | | |
db6fd42aRuslan Bikkinin7 years ago | 51 | public reloadApp(deviceId?: string): Q.Promise<void> { |
| 52 | return IOSPlatform.remote(this.runOptions.projectRoot).reloadApp(deviceId); | |
7daed3fcArtem Egorov8 years ago | 53 | } |
| 54 | | |
0a68f8dbArtem Egorov8 years ago | 55 | constructor(protected runOptions: IIOSRunOptions, platformDeps: MobilePlatformDeps = {}) { |
| 56 | super(runOptions, platformDeps); | |
8a67e140Artem Egorov8 years ago | 57 | |
db6fd42aRuslan Bikkinin7 years ago | 58 | this.runOptions.configuration = this.getConfiguration(); |
| 59 | | |
0db0be15Artem Egorov8 years ago | 60 | if (this.runOptions.iosRelativeProjectPath) { // Deprecated option |
0a68f8dbArtem Egorov8 years ago | 61 | this.logger.warning("'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead"); |
8a67e140Artem Egorov8 years ago | 62 | } |
| 63 | | |
0a68f8dbArtem Egorov8 years ago | 64 | this.iosProjectRoot = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath || IOSPlatform.DEFAULT_IOS_PROJECT_RELATIVE_PATH); |
7daed3fcArtem Egorov8 years ago | 65 | this.iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot); |
8a67e140Artem Egorov8 years ago | 66 | |
db6fd42aRuslan Bikkinin7 years ago | 67 | if (this.runArguments && this.runArguments.length > 0) { |
| 68 | this.targetType = (this.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ? | |
8022afdfVladimir Kotikov8 years ago | 69 | IOSPlatform.deviceString : IOSPlatform.simulatorString; |
| 70 | return; | |
| 71 | } | |
0db0be15Artem Egorov8 years ago | 72 | |
8022afdfVladimir Kotikov8 years ago | 73 | if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString && |
| 74 | this.runOptions.target !== IOSPlatform.deviceString)) { | |
0db0be15Artem Egorov8 years ago | 75 | |
8022afdfVladimir Kotikov8 years ago | 76 | this.targetType = IOSPlatform.simulatorString; |
| 77 | return; | |
8a67e140Artem Egorov8 years ago | 78 | } |
8022afdfVladimir Kotikov8 years ago | 79 | |
| 80 | this.targetType = this.runOptions.target || IOSPlatform.simulatorString; | |
8a67e140Artem Egorov8 years ago | 81 | } |
| 82 | | |
| 83 | public runApp(): Q.Promise<void> { | |
031832ffArtem Egorov8 years ago | 84 | const extProps = { |
| 85 | platform: { | |
| 86 | value: "ios", | |
| 87 | isPii: false, | |
| 88 | }, | |
| 89 | }; | |
| 90 | | |
| 91 | return TelemetryHelper.generate("iOSPlatform.runApp", extProps, () => { | |
| 92 | // Compile, deploy, and launch the app on either a simulator or a device | |
| 93 | const env = this.getEnvArgument(); | |
| 94 | | |
| 95 | return ReactNativeProjectHelper.getReactNativeVersion(this.runOptions.projectRoot) | |
| 96 | .then(version => { | |
| 97 | if (!semver.valid(version) /*Custom RN implementations should support this flag*/ || semver.gte(version, IOSPlatform.NO_PACKAGER_VERSION)) { | |
db6fd42aRuslan Bikkinin7 years ago | 98 | this.runArguments.push("--no-packager"); |
031832ffArtem Egorov8 years ago | 99 | } |
db6fd42aRuslan Bikkinin7 years ago | 100 | const runIosSpawn = new CommandExecutor(this.projectPath, this.logger).spawnReactCommand("run-ios", this.runArguments, {env}); |
031832ffArtem Egorov8 years ago | 101 | return new OutputVerifier(() => this.generateSuccessPatterns(), () => Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS), "ios") |
| 102 | .process(runIosSpawn); | |
| 103 | }); | |
| 104 | }); | |
8a67e140Artem Egorov8 years ago | 105 | } |
| 106 | | |
| 107 | public enableJSDebuggingMode(): Q.Promise<void> { | |
| 108 | // Configure the app for debugging | |
| 109 | if (this.targetType === IOSPlatform.deviceString) { | |
| 110 | // Note that currently we cannot automatically switch the device into debug mode. | |
7daed3fcArtem Egorov8 years ago | 111 | this.logger.info("Application is running on a device, please shake device and select 'Debug JS Remotely' to enable debugging."); |
8a67e140Artem Egorov8 years ago | 112 | return Q.resolve<void>(void 0); |
| 113 | } | |
| 114 | | |
| 115 | // Wait until the configuration file exists, and check to see if debugging is enabled | |
| 116 | return Q.all<boolean | string>([ | |
db6fd42aRuslan Bikkinin7 years ago | 117 | this.iosDebugModeManager.getSimulatorRemoteDebuggingSetting(this.runOptions.configuration, this.runOptions.productName), |
8a67e140Artem Egorov8 years ago | 118 | this.getBundleId(), |
| 119 | ]) | |
| 120 | .spread((debugModeEnabled: boolean, bundleId: string) => { | |
| 121 | if (debugModeEnabled) { | |
| 122 | return Q.resolve(void 0); | |
| 123 | } | |
| 124 | | |
| 125 | // Debugging must still be enabled | |
| 126 | // We enable debugging by writing to a plist file that backs a NSUserDefaults object, | |
| 127 | // but that file is written to by the app on occasion. To avoid races, we shut the app | |
| 128 | // down before writing to the file. | |
| 129 | const childProcess = new ChildProcess(); | |
| 130 | | |
| 131 | return childProcess.execToString("xcrun simctl spawn booted launchctl list") | |
| 132 | .then((output: string) => { | |
| 133 | // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37] | |
| 134 | const regex = new RegExp(`(\\S+${bundleId}\\S+)`); | |
| 135 | const match = regex.exec(output); | |
| 136 | | |
| 137 | // If we don't find a match, the app must not be running and so we do not need to close it | |
| 138 | return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null; | |
| 139 | }) | |
| 140 | .then(() => { | |
| 141 | // Write to the settings file while the app is not running to avoid races | |
db6fd42aRuslan Bikkinin7 years ago | 142 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true, this.runOptions.configuration, this.runOptions.productName); |
8a67e140Artem Egorov8 years ago | 143 | }) |
| 144 | .then(() => { | |
| 145 | // Relaunch the app | |
| 146 | return this.runApp(); | |
| 147 | }); | |
| 148 | }); | |
| 149 | } | |
| 150 | | |
0a68f8dbArtem Egorov8 years ago | 151 | public disableJSDebuggingMode(): Q.Promise<void> { |
db6fd42aRuslan Bikkinin7 years ago | 152 | return this.iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ false, this.runOptions.configuration, this.runOptions.productName); |
0a68f8dbArtem Egorov8 years ago | 153 | } |
| 154 | | |
8a67e140Artem Egorov8 years ago | 155 | public prewarmBundleCache(): Q.Promise<void> { |
0a68f8dbArtem Egorov8 years ago | 156 | return this.packager.prewarmBundleCache("ios"); |
8a67e140Artem Egorov8 years ago | 157 | } |
| 158 | | |
cbc7ac5bArtem Egorov7 years ago | 159 | public getRunArguments(): string[] { |
8a67e140Artem Egorov8 years ago | 160 | let runArguments: string[] = []; |
0db0be15Artem Egorov8 years ago | 161 | |
| 162 | if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) { | |
b57ea017Artem Egorov8 years ago | 163 | runArguments = this.runOptions.runArguments; |
| 164 | } else { | |
| 165 | if (this.runOptions.target) { | |
| 166 | if (this.runOptions.target === IOSPlatform.deviceString || | |
| 167 | this.runOptions.target === IOSPlatform.simulatorString) { | |
| 168 | | |
| 169 | runArguments.push(`--${this.runOptions.target}`); | |
| 170 | } else { | |
| 171 | runArguments.push("--simulator", `${this.runOptions.target}`); | |
| 172 | } | |
| 173 | } | |
8abbd163Artem Egorov8 years ago | 174 | |
b57ea017Artem Egorov8 years ago | 175 | if (this.runOptions.iosRelativeProjectPath) { |
| 176 | runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath); | |
8022afdfVladimir Kotikov8 years ago | 177 | } |
8a67e140Artem Egorov8 years ago | 178 | |
b57ea017Artem Egorov8 years ago | 179 | // provide any defined scheme |
| 180 | if (this.runOptions.scheme) { | |
| 181 | runArguments.push("--scheme", this.runOptions.scheme); | |
| 182 | } | |
8a67e140Artem Egorov8 years ago | 183 | } |
| 184 | | |
| 185 | return runArguments; | |
| 186 | } | |
| 187 | | |
| 188 | private generateSuccessPatterns(): Q.Promise<string[]> { | |
ed8367fdVladimir Kotikov8 years ago | 189 | return this.targetType === IOSPlatform.deviceString ? |
| 190 | Q(IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat("INSTALLATION SUCCEEDED")) : | |
| 191 | this.getBundleId() | |
| 192 | .then(bundleId => IOSPlatform.RUN_IOS_SUCCESS_PATTERNS | |
| 193 | .concat([`Launching ${bundleId}\n${bundleId}: `])); | |
8a67e140Artem Egorov8 years ago | 194 | } |
| 195 | | |
db6fd42aRuslan Bikkinin7 years ago | 196 | private getConfiguration(): string { |
| 197 | return this.getOptFromRunArgs(this.configurationArgumentName) || this.defaultConfiguration; | |
| 198 | } | |
| 199 | | |
8a67e140Artem Egorov8 years ago | 200 | private getBundleId(): Q.Promise<string> { |
db6fd42aRuslan Bikkinin7 years ago | 201 | return this.plistBuddy.getBundleId(this.iosProjectRoot, true, this.runOptions.configuration, this.runOptions.productName); |
8a67e140Artem Egorov8 years ago | 202 | } |
7daed3fcArtem Egorov8 years ago | 203 | |
4edcda70Artem Egorov8 years ago | 204 | private static remote(fsPath: string): RemoteExtension { |
7daed3fcArtem Egorov8 years ago | 205 | if (this.remoteExtension) { |
| 206 | return this.remoteExtension; | |
| 207 | } else { | |
4edcda70Artem Egorov8 years ago | 208 | return this.remoteExtension = RemoteExtension.atProjectRootPath(SettingsHelper.getReactNativeProjectRoot(fsPath)); |
7daed3fcArtem Egorov8 years ago | 209 | } |
| 210 | } | |
8a67e140Artem Egorov8 years ago | 211 | } |