microsoft/vscode-react-native

Public

mirrored from https://github.com/microsoft/vscode-react-nativeAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
10bc5320aeda86f9f9aee59bb732e61314aaad7e

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/debugger/ios/iOSPlatform.ts

135lines · modeblame

488f1908Jimmy Thomson10 years ago1// 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";
c96fc63eMark Oswald10 years ago5import * as path from "path";
488f1908Jimmy Thomson10 years ago6
190e393cMeena Kunnathur Balakrishnan10 years ago7import {Log} from "../../common/log/log";
dbe130e4Jimmy Thomson10 years ago8import {ChildProcess} from "../../common/node/childProcess";
b0061ac6Meena Kunnathur Balakrishnan10 years ago9import {CommandExecutor} from "../../common/commandExecutor";
ac7fef0cPatricio Beltran9 years ago10import {GeneralMobilePlatform} from "../../common/generalMobilePlatform";
488f1908Jimmy Thomson10 years ago11import {Compiler} from "./compiler";
12import {DeviceDeployer} from "./deviceDeployer";
13import {DeviceRunner} from "./deviceRunner";
acf08bc2dlebu10 years ago14import {IRunOptions} from "../../common/launchArgs";
a9d96b7cdigeff10 years ago15import {PlistBuddy} from "../../common/ios/plistBuddy";
1ed272e1digeff10 years ago16import {IOSDebugModeManager} from "../../common/ios/iOSDebugModeManager";
f8b7022ddigeff10 years ago17import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier";
5c8365a6Artem Egorov8 years ago18import {RemoteExtension} from "../../common/remoteExtension";
488f1908Jimmy Thomson10 years ago19
299b0557Patricio Beltran10 years ago20export class IOSPlatform extends GeneralMobilePlatform {
bb77358cMark Oswald10 years ago21public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios";
22
bc7a32ceJimmy Thomson10 years ago23private static deviceString = "device";
24private static simulatorString = "simulator";
25
a9d96b7cdigeff10 years ago26private plistBuddy = new PlistBuddy();
27
110558c0Jimmy Thomson10 years ago28private simulatorTarget: string;
29private isSimulator: boolean;
398e2b0bMark Oswald10 years ago30private iosProjectPath: string;
110558c0Jimmy Thomson10 years ago31
7cc67271digeff10 years ago32// We should add the common iOS build/run erros we find to this list
ef902673Vladimir Kotikov9 years ago33private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{
34pattern: "No devices are booted",
35message: "Unable to launch iOS simulator. Try specifying a different target.",
36}, {
37pattern: "FBSOpenApplicationErrorDomain",
38message: "Unable to launch iOS simulator. Try specifying a different target.",
39}];
7cc67271digeff10 years ago40
41private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
42
299b0557Patricio Beltran10 years ago43// 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.
5c8365a6Artem Egorov8 years ago44constructor(runOptions: IRunOptions, { remoteExtension = undefined }: {remoteExtension?: RemoteExtension} = {}) {
299b0557Patricio Beltran10 years ago45super(runOptions, { remoteExtension: remoteExtension });
c764d5e2digeff10 years ago46this.simulatorTarget = this.runOptions.target || IOSPlatform.simulatorString;
47this.isSimulator = this.simulatorTarget.toLowerCase() !== IOSPlatform.deviceString;
5c8365a6Artem Egorov8 years ago48this.iosProjectPath = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath || "");
7be1388cdigeff10 years ago49}
488f1908Jimmy Thomson10 years ago50
7be1388cdigeff10 years ago51public runApp(): Q.Promise<void> {
52// Compile, deploy, and launch the app on either a simulator or a device
110558c0Jimmy Thomson10 years ago53if (this.isSimulator) {
488f1908Jimmy Thomson10 years ago54// React native supports running on the iOS simulator from the command line
f8d32439dlebu10 years ago55let runArguments: string[] = [];
110558c0Jimmy Thomson10 years ago56if (this.simulatorTarget.toLowerCase() !== IOSPlatform.simulatorString) {
398e2b0bMark Oswald10 years ago57runArguments.push("--simulator", this.simulatorTarget);
58}
59
5c8365a6Artem Egorov8 years ago60runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath || "");
488f1908Jimmy Thomson10 years ago61
60d501abNick Hodapp9 years ago62// provide any defined scheme
63if (this.runOptions.scheme) {
64runArguments.push("--scheme", this.runOptions.scheme);
65}
66
9596aa53digeff10 years ago67const runIosSpawn = new CommandExecutor(this.projectPath).spawnReactCommand("run-ios", runArguments);
f8b7022ddigeff10 years ago68return new OutputVerifier(
7cc67271digeff10 years ago69() =>
7be1388cdigeff10 years ago70this.generateSuccessPatterns(),
7cc67271digeff10 years ago71() =>
72Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS)).process(runIosSpawn);
488f1908Jimmy Thomson10 years ago73}
74
60d501abNick Hodapp9 years ago75return new Compiler(this.runOptions, this.iosProjectPath).compile().then(() => {
bb77358cMark Oswald10 years ago76return new DeviceDeployer(this.iosProjectPath).deploy();
488f1908Jimmy Thomson10 years ago77}).then(() => {
bb77358cMark Oswald10 years ago78return new DeviceRunner(this.iosProjectPath).run();
488f1908Jimmy Thomson10 years ago79});
80}
81
7be1388cdigeff10 years ago82public enableJSDebuggingMode(): Q.Promise<void> {
488f1908Jimmy Thomson10 years ago83// Configure the app for debugging
110558c0Jimmy Thomson10 years ago84if (this.simulatorTarget.toLowerCase() === IOSPlatform.deviceString) {
488f1908Jimmy Thomson10 years ago85// Note that currently we cannot automatically switch the device into debug mode.
b044f0b9Jimmy Thomson10 years ago86Log.logMessage("Application is running on a device, please shake device and select 'Debug in Chrome' to enable debugging.");
488f1908Jimmy Thomson10 years ago87return Q.resolve<void>(void 0);
88}
89
bb77358cMark Oswald10 years ago90const iosDebugModeManager = new IOSDebugModeManager(this.iosProjectPath);
dbe130e4Jimmy Thomson10 years ago91
92// Wait until the configuration file exists, and check to see if debugging is enabled
93return Q.all([
94iosDebugModeManager.getSimulatorJSDebuggingModeSetting(),
b7773a6fMark Oswald10 years ago95this.getBundleId(),
dbe130e4Jimmy Thomson10 years ago96]).spread((debugModeSetting: string, bundleId: string) => {
97if (debugModeSetting !== IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME) {
98// Debugging must still be enabled
771dc596Jimmy Thomson10 years ago99// We enable debugging by writing to a plist file that backs a NSUserDefaults object,
100// but that file is written to by the app on occasion. To avoid races, we shut the app
101// down before writing to the file.
dbe130e4Jimmy Thomson10 years ago102const childProcess = new ChildProcess();
b3db6f6bJimmy Thomson10 years ago103
8bff2a55Jimmy Thomson10 years ago104return childProcess.execToString("xcrun simctl spawn booted launchctl list").then((output: string) => {
b3db6f6bJimmy Thomson10 years ago105// Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
106const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
8bff2a55Jimmy Thomson10 years ago107const match = regex.exec(output);
b3db6f6bJimmy Thomson10 years ago108
109// If we don't find a match, the app must not be running and so we do not need to close it
5c8365a6Artem Egorov8 years ago110return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null;
dbe130e4Jimmy Thomson10 years ago111}).then(() => {
112// Write to the settings file while the app is not running to avoid races
113return iosDebugModeManager.setSimulatorJSDebuggingModeSetting(/*enable=*/ true);
114}).then(() => {
115// Relaunch the app
7be1388cdigeff10 years ago116return this.runApp();
dbe130e4Jimmy Thomson10 years ago117});
118}
5c8365a6Artem Egorov8 years ago119return Q.resolve(void 0);
dbe130e4Jimmy Thomson10 years ago120});
488f1908Jimmy Thomson10 years ago121}
110558c0Jimmy Thomson10 years ago122
299b0557Patricio Beltran10 years ago123public prewarmBundleCache(): Q.Promise<void> {
124return this.remoteExtension.prewarmBundleCache(this.platformName);
125}
126
7be1388cdigeff10 years ago127private generateSuccessPatterns(): Q.Promise<string[]> {
128return this.getBundleId().then(bundleId =>
7cc67271digeff10 years ago129IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat([`Launching ${bundleId}\n${bundleId}: `]));
130}
7be1388cdigeff10 years ago131
132private getBundleId(): Q.Promise<string> {
5c32e64cMark Oswald10 years ago133return this.plistBuddy.getBundleId(this.iosProjectPath);
7be1388cdigeff10 years ago134}
a9d96b7cdigeff10 years ago135}