microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
1ca47c7c2d73a604d8f110cdbad1aad4bbdddce6

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/ios/iOSPlatform.ts

167lines · 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";
5import * as path from "path";
6
7import {Log} from "../../common/log/log";
8import {ChildProcess} from "../../common/node/childProcess";
9import {CommandExecutor} from "../../common/commandExecutor";
10import {GeneralMobilePlatform, TargetType} from "../../common/generalMobilePlatform";
11import {IIOSRunOptions} from "../../common/launchArgs";
12import {PlistBuddy} from "../../common/ios/plistBuddy";
13import {IOSDebugModeManager} from "../../common/ios/iOSDebugModeManager";
14import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier";
15import {RemoteExtension} from "../../common/remoteExtension";
16import { ErrorHelper } from "../../common/error/errorHelper";
17
18export class IOSPlatform extends GeneralMobilePlatform {
19 public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios";
20 public static DEFAULT_IOS_SIMULATOR_TARGET = "iPhone 5";
21 private plistBuddy = new PlistBuddy();
22 private targetType: TargetType = "simulator";
23 private iosProjectRoot: string;
24
25 // We should add the common iOS build/run erros we find to this list
26 private static RUN_IOS_FAILURE_PATTERNS: PatternToFailure[] = [{
27 pattern: "No devices are booted",
28 message: ErrorHelper.ERROR_STRINGS.IOSSimulatorNotLaunchable,
29 }, {
30 pattern: "FBSOpenApplicationErrorDomain",
31 message: ErrorHelper.ERROR_STRINGS.IOSSimulatorNotLaunchable,
32 }, {
33 pattern: "ios-deploy",
34 message: ErrorHelper.ERROR_STRINGS.IOSDeployNotFound,
35 }];
36
37 private static RUN_IOS_SUCCESS_PATTERNS = ["BUILD SUCCEEDED"];
38
39 // 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.
40 constructor(protected runOptions: IIOSRunOptions, { remoteExtension = undefined }: { remoteExtension?: RemoteExtension } = {}) {
41 super(runOptions, { remoteExtension: remoteExtension });
42
43 if (this.runOptions.iosRelativeProjectPath) { // Deprecated option
44 Log.logMessage("'iosRelativeProjectPath' option is deprecated. Please use 'runArguments' instead");
45 }
46
47 this.iosProjectRoot = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath || "ios");
48
49 if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
50 this.targetType = (this.runOptions.runArguments.indexOf(`--${IOSPlatform.deviceString}`) >= 0) ?
51 IOSPlatform.deviceString : IOSPlatform.simulatorString;
52 return;
53 }
54
55 if (this.runOptions.target && (this.runOptions.target !== IOSPlatform.simulatorString &&
56 this.runOptions.target !== IOSPlatform.deviceString)) {
57
58 this.targetType = IOSPlatform.simulatorString;
59 return;
60 }
61
62 this.targetType = this.runOptions.target || IOSPlatform.simulatorString;
63 }
64
65 public runApp(): Q.Promise<void> {
66 // Compile, deploy, and launch the app on either a simulator or a device
67 const runArguments = this.getRunArgument();
68
69 const runIosSpawn = new CommandExecutor(this.projectPath).spawnReactCommand("run-ios", runArguments);
70 return new OutputVerifier(
71 () =>
72 this.generateSuccessPatterns(),
73 () =>
74 Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS)).process(runIosSpawn);
75 }
76
77 public enableJSDebuggingMode(): Q.Promise<void> {
78 // Configure the app for debugging
79 if (this.targetType === IOSPlatform.deviceString) {
80 // Note that currently we cannot automatically switch the device into debug mode.
81 Log.logMessage("Application is running on a device, please shake device and select 'Debug in Chrome' to enable debugging.");
82 return Q.resolve<void>(void 0);
83 }
84
85 const iosDebugModeManager = new IOSDebugModeManager(this.iosProjectRoot);
86
87 // Wait until the configuration file exists, and check to see if debugging is enabled
88 return Q.all<boolean | string>([
89 iosDebugModeManager.getSimulatorRemoteDebuggingSetting(),
90 this.getBundleId(),
91 ])
92 .spread((debugModeEnabled: boolean, bundleId: string) => {
93 if (debugModeEnabled) {
94 return Q.resolve(void 0);
95 }
96
97 // Debugging must still be enabled
98 // We enable debugging by writing to a plist file that backs a NSUserDefaults object,
99 // but that file is written to by the app on occasion. To avoid races, we shut the app
100 // down before writing to the file.
101 const childProcess = new ChildProcess();
102
103 return childProcess.execToString("xcrun simctl spawn booted launchctl list")
104 .then((output: string) => {
105 // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37]
106 const regex = new RegExp(`(\\S+${bundleId}\\S+)`);
107 const match = regex.exec(output);
108
109 // If we don't find a match, the app must not be running and so we do not need to close it
110 return match ? childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`) : null;
111 })
112 .then(() => {
113 // Write to the settings file while the app is not running to avoid races
114 return iosDebugModeManager.setSimulatorRemoteDebuggingSetting(/*enable=*/ true);
115 })
116 .then(() => {
117 // Relaunch the app
118 return this.runApp();
119 });
120 });
121 }
122
123 public prewarmBundleCache(): Q.Promise<void> {
124 return this.remoteExtension.prewarmBundleCache(this.platformName);
125 }
126
127 public getRunArgument(): string[] {
128 let runArguments: string[] = [];
129
130 if (this.runOptions.runArguments && this.runOptions.runArguments.length > 0) {
131 return this.runOptions.runArguments;
132 }
133
134 if (this.runOptions.target) {
135 if (this.runOptions.target === IOSPlatform.deviceString ||
136 this.runOptions.target === IOSPlatform.simulatorString) {
137
138 runArguments.push(`--${this.runOptions.target}`);
139 } else {
140 runArguments.push("--simulator", `${this.runOptions.target}`);
141 }
142 }
143
144 if (this.runOptions.iosRelativeProjectPath) {
145 runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath);
146 }
147
148 // provide any defined scheme
149 if (this.runOptions.scheme) {
150 runArguments.push("--scheme", this.runOptions.scheme);
151 }
152
153 return runArguments;
154 }
155
156 private generateSuccessPatterns(): Q.Promise<string[]> {
157 return this.targetType === IOSPlatform.deviceString ?
158 Q(IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat("INSTALLATION SUCCEEDED")) :
159 this.getBundleId()
160 .then(bundleId => IOSPlatform.RUN_IOS_SUCCESS_PATTERNS
161 .concat([`Launching ${bundleId}\n${bundleId}: `]));
162 }
163
164 private getBundleId(): Q.Promise<string> {
165 return this.plistBuddy.getBundleId(this.iosProjectRoot);
166 }
167}
168