microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/ios/iOSPlatform.ts
95lines · 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 | | |
b0061ac6Meena Kunnathur Balakrishnan10 years ago | 6 | import {Log} from "../../common/log"; |
| 7 | import {CommandExecutor} from "../../common/commandExecutor"; | |
f8d32439dlebu10 years ago | 8 | import {IAppPlatform} from "../platformResolver"; |
488f1908Jimmy Thomson10 years ago | 9 | import {Compiler} from "./compiler"; |
| 10 | import {DeviceDeployer} from "./deviceDeployer"; | |
| 11 | import {DeviceRunner} from "./deviceRunner"; | |
| 12 | import {IRunOptions} from "../launchArgs"; | |
a9d96b7cdigeff10 years ago | 13 | import {PlistBuddy} from "../../common/ios/plistBuddy"; |
1ed272e1digeff10 years ago | 14 | import {IOSDebugModeManager} from "../../common/ios/iOSDebugModeManager"; |
488f1908Jimmy Thomson10 years ago | 15 | |
f8d32439dlebu10 years ago | 16 | export class IOSPlatform implements IAppPlatform { |
bc7a32ceJimmy Thomson10 years ago | 17 | private static deviceString = "device"; |
| 18 | private static simulatorString = "simulator"; | |
| 19 | | |
a9d96b7cdigeff10 years ago | 20 | private plistBuddy = new PlistBuddy(); |
| 21 | | |
110558c0Jimmy Thomson10 years ago | 22 | private projectPath: string; |
| 23 | private simulatorTarget: string; | |
| 24 | private isSimulator: boolean; | |
| 25 | | |
488f1908Jimmy Thomson10 years ago | 26 | public runApp(launchArgs: IRunOptions): Q.Promise<void> { |
| 27 | // Compile, deploy, and launch the app on either a simulator or a device | |
110558c0Jimmy Thomson10 years ago | 28 | this.consumeArguments(launchArgs); |
488f1908Jimmy Thomson10 years ago | 29 | |
110558c0Jimmy Thomson10 years ago | 30 | if (this.isSimulator) { |
488f1908Jimmy Thomson10 years ago | 31 | // React native supports running on the iOS simulator from the command line |
f8d32439dlebu10 years ago | 32 | let runArguments: string[] = []; |
110558c0Jimmy Thomson10 years ago | 33 | if (this.simulatorTarget.toLowerCase() !== IOSPlatform.simulatorString) { |
488f1908Jimmy Thomson10 years ago | 34 | runArguments.push("--simulator"); |
110558c0Jimmy Thomson10 years ago | 35 | runArguments.push(this.simulatorTarget); |
488f1908Jimmy Thomson10 years ago | 36 | } |
| 37 | | |
f7208a21Jimmy Thomson10 years ago | 38 | return new CommandExecutor(this.projectPath).spawnReactCommand("run-ios", runArguments).then((runIos) => { |
| 39 | const deferred = Q.defer<void>(); | |
| 40 | runIos.on("error", (err: Error) => { | |
| 41 | deferred.reject(err); | |
| 42 | }); | |
| 43 | runIos.stderr.on("data", (data: Buffer) => { | |
| 44 | const dataString = data.toString(); | |
| 45 | if (dataString.indexOf("No devices are booted") !== -1 // No emulators are started | |
| 46 | || dataString.indexOf("FBSOpenApplicationErrorDomain") !== -1) { // The incorrect emulator is started | |
| 47 | deferred.reject(new Error("Unable to launch iOS simulator. Try specifying a different target.")); | |
| 48 | } | |
| 49 | }); | |
| 50 | runIos.on("exit", (code: number) => { | |
| 51 | if (code !== 0) { | |
| 52 | const err = new Error(`Command failed with exit code ${code}`); | |
| 53 | Log.commandFailed(["react-native", "run-ios"].concat(runArguments).join(" "), err); | |
| 54 | deferred.reject(err); | |
| 55 | } else { | |
| 56 | deferred.resolve(void 0); | |
| 57 | } | |
| 58 | }); | |
| 59 | return deferred.promise; | |
| 60 | }); | |
488f1908Jimmy Thomson10 years ago | 61 | } |
| 62 | | |
| 63 | // TODO: This is currently a stub, device debugging is not yet implemented | |
bc96b26bJimmy Thomson10 years ago | 64 | return new Compiler(this.projectPath).compile().then(() => { |
110558c0Jimmy Thomson10 years ago | 65 | return new DeviceDeployer(this.projectPath).deploy(); |
488f1908Jimmy Thomson10 years ago | 66 | }).then(() => { |
110558c0Jimmy Thomson10 years ago | 67 | return new DeviceRunner(this.projectPath).run(); |
488f1908Jimmy Thomson10 years ago | 68 | }); |
| 69 | } | |
| 70 | | |
| 71 | public enableJSDebuggingMode(launchArgs: IRunOptions): Q.Promise<void> { | |
| 72 | // Configure the app for debugging | |
110558c0Jimmy Thomson10 years ago | 73 | this.consumeArguments(launchArgs); |
488f1908Jimmy Thomson10 years ago | 74 | |
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 | | |
1ed272e1digeff10 years ago | 81 | return new IOSDebugModeManager(this.projectPath).setSimulatorJSDebuggingModeSetting(/*enable=*/ true) |
a9d96b7cdigeff10 years ago | 82 | .then(() => { |
| 83 | return this.plistBuddy.getBundleId(launchArgs.projectRoot); | |
b044f0b9Jimmy Thomson10 years ago | 84 | }).then((bundleId: string) => { |
| 85 | // Relaunch the app so the new setting can take effect | |
| 86 | return new CommandExecutor().execute(`xcrun simctl launch booted ${bundleId}`); | |
| 87 | }); | |
488f1908Jimmy Thomson10 years ago | 88 | } |
110558c0Jimmy Thomson10 years ago | 89 | |
| 90 | private consumeArguments(launchArgs: IRunOptions): void { | |
| 91 | this.projectPath = launchArgs.projectRoot; | |
| 92 | this.simulatorTarget = launchArgs.target || IOSPlatform.simulatorString; | |
| 93 | this.isSimulator = this.simulatorTarget.toLowerCase() !== IOSPlatform.deviceString; | |
| 94 | } | |
a9d96b7cdigeff10 years ago | 95 | } |