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