microsoft/vscode-react-native

Public

mirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
b031edc774df5bdef810349bdd6d2c40c7acfadd

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

src/debugger/ios/iOSPlatform.ts

95lines · 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
4import * as Q from "q";
5
6import {Log} from "../../common/log";
7import {CommandExecutor} from "../../common/commandExecutor";
8import {IAppPlatform} from "../platformResolver";
9import {Compiler} from "./compiler";
10import {DeviceDeployer} from "./deviceDeployer";
11import {DeviceRunner} from "./deviceRunner";
12import {IRunOptions} from "../launchArgs";
13import {PlistBuddy} from "../../common/ios/plistBuddy";
14import {IOSDebugModeManager} from "../../common/ios/iOSDebugModeManager";
15
16export 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 // TODO: This is currently a stub, device debugging is not yet implemented
64 return new Compiler(this.projectPath).compile().then(() => {
65 return new DeviceDeployer(this.projectPath).deploy();
66 }).then(() => {
67 return new DeviceRunner(this.projectPath).run();
68 });
69 }
70
71 public enableJSDebuggingMode(launchArgs: IRunOptions): Q.Promise<void> {
72 // Configure the app for debugging
73 this.consumeArguments(launchArgs);
74
75 if (this.simulatorTarget.toLowerCase() === IOSPlatform.deviceString) {
76 // Note that currently we cannot automatically switch the device into debug mode.
77 Log.logMessage("Application is running on a device, please shake device and select 'Debug in Chrome' to enable debugging.");
78 return Q.resolve<void>(void 0);
79 }
80
81 return new IOSDebugModeManager(this.projectPath).setSimulatorJSDebuggingModeSetting(/*enable=*/ true)
82 .then(() => {
83 return this.plistBuddy.getBundleId(launchArgs.projectRoot);
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 });
88 }
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 }
95}
96