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