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