microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/ios/iOSPlatform.ts
94lines · modecode
| 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 | |
| 6 | import {Log} from "../../common/log"; |
| 7 | import {CommandExecutor} from "../../common/commandExecutor"; |
| 8 | import {IAppPlatform} from "../platformResolver"; |
| 9 | import {Compiler} from "./compiler"; |
| 10 | import {DeviceDeployer} from "./deviceDeployer"; |
| 11 | import {DeviceRunner} from "./deviceRunner"; |
| 12 | import {IRunOptions} from "../launchArgs"; |
| 13 | import {PlistBuddy} from "../../common/ios/plistBuddy"; |
| 14 | import {IOSDebugModeManager} from "../../common/ios/iOSDebugModeManager"; |
| 15 | |
| 16 | export class IOSPlatform implements IAppPlatform { |
| 17 | private static deviceString = "device"; |
| 18 | private static simulatorString = "simulator"; |
| 19 | |
| 20 | private plistBuddy = new PlistBuddy(); |
| 21 | |
| 22 | private projectPath: string; |
| 23 | private simulatorTarget: string; |
| 24 | private isSimulator: boolean; |
| 25 | |
| 26 | public runApp(launchArgs: IRunOptions): Q.Promise<void> { |
| 27 | // Compile, deploy, and launch the app on either a simulator or a device |
| 28 | this.consumeArguments(launchArgs); |
| 29 | |
| 30 | if (this.isSimulator) { |
| 31 | // React native supports running on the iOS simulator from the command line |
| 32 | let runArguments: string[] = []; |
| 33 | if (this.simulatorTarget.toLowerCase() !== IOSPlatform.simulatorString) { |
| 34 | runArguments.push("--simulator"); |
| 35 | runArguments.push(this.simulatorTarget); |
| 36 | } |
| 37 | |
| 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 | }); |
| 61 | } |
| 62 | |
| 63 | return new Compiler(this.projectPath).compile().then(() => { |
| 64 | return new DeviceDeployer(this.projectPath).deploy(); |
| 65 | }).then(() => { |
| 66 | return new DeviceRunner(this.projectPath).run(); |
| 67 | }); |
| 68 | } |
| 69 | |
| 70 | public enableJSDebuggingMode(launchArgs: IRunOptions): Q.Promise<void> { |
| 71 | // Configure the app for debugging |
| 72 | this.consumeArguments(launchArgs); |
| 73 | |
| 74 | if (this.simulatorTarget.toLowerCase() === IOSPlatform.deviceString) { |
| 75 | // Note that currently we cannot automatically switch the device into debug mode. |
| 76 | Log.logMessage("Application is running on a device, please shake device and select 'Debug in Chrome' to enable debugging."); |
| 77 | return Q.resolve<void>(void 0); |
| 78 | } |
| 79 | |
| 80 | return new IOSDebugModeManager(this.projectPath).setSimulatorJSDebuggingModeSetting(/*enable=*/ true) |
| 81 | .then(() => { |
| 82 | return this.plistBuddy.getBundleId(launchArgs.projectRoot); |
| 83 | }).then((bundleId: string) => { |
| 84 | // Relaunch the app so the new setting can take effect |
| 85 | return new CommandExecutor().execute(`xcrun simctl launch booted ${bundleId}`); |
| 86 | }); |
| 87 | } |
| 88 | |
| 89 | private consumeArguments(launchArgs: IRunOptions): void { |
| 90 | this.projectPath = launchArgs.projectRoot; |
| 91 | this.simulatorTarget = launchArgs.target || IOSPlatform.simulatorString; |
| 92 | this.isSimulator = this.simulatorTarget.toLowerCase() !== IOSPlatform.deviceString; |
| 93 | } |
| 94 | } |
| 95 | |