microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/ios/iOSPlatform.ts
127lines · modeblame
488f1908Jimmy Thomson10 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"; | |
c96fc63eMark Oswald10 years ago | 5 | import * as path from "path"; |
488f1908Jimmy Thomson10 years ago | 6 | |
190e393cMeena Kunnathur Balakrishnan10 years ago | 7 | import {Log} from "../../common/log/log"; |
dbe130e4Jimmy Thomson10 years ago | 8 | import {ChildProcess} from "../../common/node/childProcess"; |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 9 | import {CommandExecutor} from "../../common/commandExecutor"; |
ac7fef0cPatricio Beltran9 years ago | 10 | import {GeneralMobilePlatform} from "../../common/generalMobilePlatform"; |
488f1908Jimmy Thomson10 years ago | 11 | import {Compiler} from "./compiler"; |
| 12 | import {DeviceDeployer} from "./deviceDeployer"; | |
| 13 | import {DeviceRunner} from "./deviceRunner"; | |
acf08bc2dlebu10 years ago | 14 | import {IRunOptions} from "../../common/launchArgs"; |
a9d96b7cdigeff10 years ago | 15 | import {PlistBuddy} from "../../common/ios/plistBuddy"; |
1ed272e1digeff10 years ago | 16 | import {IOSDebugModeManager} from "../../common/ios/iOSDebugModeManager"; |
f8b7022ddigeff10 years ago | 17 | import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier"; |
488f1908Jimmy Thomson10 years ago | 18 | |
299b0557Patricio Beltran10 years ago | 19 | export class IOSPlatform extends GeneralMobilePlatform { |
bb77358cMark Oswald10 years ago | 20 | public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios"; |
| 21 | | |
bc7a32ceJimmy Thomson10 years ago | 22 | private static deviceString = "device"; |
| 23 | private static simulatorString = "simulator"; | |
| 24 | | |
a9d96b7cdigeff10 years ago | 25 | private plistBuddy = new PlistBuddy(); |
| 26 | | |
110558c0Jimmy Thomson10 years ago | 27 | private simulatorTarget: string; |
| 28 | private isSimulator: boolean; | |
398e2b0bMark Oswald10 years ago | 29 | private iosProjectPath: string; |
110558c0Jimmy Thomson10 years ago | 30 | |
7cc67271digeff10 years ago | 31 | // We should add the common iOS build/run erros we find to this list |
| 32 | private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure = { | |
| 33 | "No devices are booted": "Unable to launch iOS simulator. Try specifying a different target.", | |
cdf34447digeff10 years ago | 34 | "FBSOpenApplicationErrorDomain": "Unable to launch iOS simulator. Try specifying a different target.", |
| 35 | }; | |
7cc67271digeff10 years ago | 36 | |
| 37 | private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"]; | |
| 38 | | |
299b0557Patricio Beltran10 years ago | 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. |
| 40 | constructor(runOptions: IRunOptions, { remoteExtension = null } = {}) { | |
| 41 | super(runOptions, { remoteExtension: remoteExtension }); | |
c764d5e2digeff10 years ago | 42 | this.simulatorTarget = this.runOptions.target || IOSPlatform.simulatorString; |
| 43 | this.isSimulator = this.simulatorTarget.toLowerCase() !== IOSPlatform.deviceString; | |
bb77358cMark Oswald10 years ago | 44 | this.iosProjectPath = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath); |
7be1388cdigeff10 years ago | 45 | } |
488f1908Jimmy Thomson10 years ago | 46 | |
7be1388cdigeff10 years ago | 47 | public runApp(): Q.Promise<void> { |
| 48 | // Compile, deploy, and launch the app on either a simulator or a device | |
110558c0Jimmy Thomson10 years ago | 49 | if (this.isSimulator) { |
488f1908Jimmy Thomson10 years ago | 50 | // React native supports running on the iOS simulator from the command line |
f8d32439dlebu10 years ago | 51 | let runArguments: string[] = []; |
110558c0Jimmy Thomson10 years ago | 52 | if (this.simulatorTarget.toLowerCase() !== IOSPlatform.simulatorString) { |
398e2b0bMark Oswald10 years ago | 53 | runArguments.push("--simulator", this.simulatorTarget); |
| 54 | } | |
| 55 | | |
b7773a6fMark Oswald10 years ago | 56 | runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath); |
488f1908Jimmy Thomson10 years ago | 57 | |
9596aa53digeff10 years ago | 58 | const runIosSpawn = new CommandExecutor(this.projectPath).spawnReactCommand("run-ios", runArguments); |
f8b7022ddigeff10 years ago | 59 | return new OutputVerifier( |
7cc67271digeff10 years ago | 60 | () => |
7be1388cdigeff10 years ago | 61 | this.generateSuccessPatterns(), |
7cc67271digeff10 years ago | 62 | () => |
| 63 | Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS)).process(runIosSpawn); | |
488f1908Jimmy Thomson10 years ago | 64 | } |
| 65 | | |
bb77358cMark Oswald10 years ago | 66 | return new Compiler(this.iosProjectPath).compile().then(() => { |
| 67 | return new DeviceDeployer(this.iosProjectPath).deploy(); | |
488f1908Jimmy Thomson10 years ago | 68 | }).then(() => { |
bb77358cMark Oswald10 years ago | 69 | return new DeviceRunner(this.iosProjectPath).run(); |
488f1908Jimmy Thomson10 years ago | 70 | }); |
| 71 | } | |
| 72 | | |
7be1388cdigeff10 years ago | 73 | public enableJSDebuggingMode(): Q.Promise<void> { |
488f1908Jimmy Thomson10 years ago | 74 | // Configure the app for debugging |
110558c0Jimmy Thomson10 years ago | 75 | if (this.simulatorTarget.toLowerCase() === IOSPlatform.deviceString) { |
488f1908Jimmy Thomson10 years ago | 76 | // Note that currently we cannot automatically switch the device into debug mode. |
b044f0b9Jimmy Thomson10 years ago | 77 | Log.logMessage("Application is running on a device, please shake device and select 'Debug in Chrome' to enable debugging."); |
488f1908Jimmy Thomson10 years ago | 78 | return Q.resolve<void>(void 0); |
| 79 | } | |
| 80 | | |
bb77358cMark Oswald10 years ago | 81 | const iosDebugModeManager = new IOSDebugModeManager(this.iosProjectPath); |
dbe130e4Jimmy Thomson10 years ago | 82 | |
| 83 | // Wait until the configuration file exists, and check to see if debugging is enabled | |
| 84 | return Q.all([ | |
| 85 | iosDebugModeManager.getSimulatorJSDebuggingModeSetting(), | |
b7773a6fMark Oswald10 years ago | 86 | this.getBundleId(), |
dbe130e4Jimmy Thomson10 years ago | 87 | ]).spread((debugModeSetting: string, bundleId: string) => { |
| 88 | if (debugModeSetting !== IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME) { | |
| 89 | // Debugging must still be enabled | |
771dc596Jimmy Thomson10 years ago | 90 | // We enable debugging by writing to a plist file that backs a NSUserDefaults object, |
| 91 | // but that file is written to by the app on occasion. To avoid races, we shut the app | |
| 92 | // down before writing to the file. | |
dbe130e4Jimmy Thomson10 years ago | 93 | const childProcess = new ChildProcess(); |
b3db6f6bJimmy Thomson10 years ago | 94 | |
8bff2a55Jimmy Thomson10 years ago | 95 | return childProcess.execToString("xcrun simctl spawn booted launchctl list").then((output: string) => { |
b3db6f6bJimmy Thomson10 years ago | 96 | // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37] |
| 97 | const regex = new RegExp(`(\\S+${bundleId}\\S+)`); | |
8bff2a55Jimmy Thomson10 years ago | 98 | const match = regex.exec(output); |
b3db6f6bJimmy Thomson10 years ago | 99 | |
| 100 | // If we don't find a match, the app must not be running and so we do not need to close it | |
| 101 | if (match) { | |
| 102 | return childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`); | |
| 103 | } | |
dbe130e4Jimmy Thomson10 years ago | 104 | }).then(() => { |
| 105 | // Write to the settings file while the app is not running to avoid races | |
| 106 | return iosDebugModeManager.setSimulatorJSDebuggingModeSetting(/*enable=*/ true); | |
| 107 | }).then(() => { | |
| 108 | // Relaunch the app | |
7be1388cdigeff10 years ago | 109 | return this.runApp(); |
dbe130e4Jimmy Thomson10 years ago | 110 | }); |
| 111 | } | |
| 112 | }); | |
488f1908Jimmy Thomson10 years ago | 113 | } |
110558c0Jimmy Thomson10 years ago | 114 | |
299b0557Patricio Beltran10 years ago | 115 | public prewarmBundleCache(): Q.Promise<void> { |
| 116 | return this.remoteExtension.prewarmBundleCache(this.platformName); | |
| 117 | } | |
| 118 | | |
7be1388cdigeff10 years ago | 119 | private generateSuccessPatterns(): Q.Promise<string[]> { |
| 120 | return this.getBundleId().then(bundleId => | |
7cc67271digeff10 years ago | 121 | IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat([`Launching ${bundleId}\n${bundleId}: `])); |
| 122 | } | |
7be1388cdigeff10 years ago | 123 | |
| 124 | private getBundleId(): Q.Promise<string> { | |
5c32e64cMark Oswald10 years ago | 125 | return this.plistBuddy.getBundleId(this.iosProjectPath); |
7be1388cdigeff10 years ago | 126 | } |
a9d96b7cdigeff10 years ago | 127 | } |