microsoft/vscode-react-native

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
297822a1eaadca4f95c0a56cc22ee8f12d0362bd

Branches

Tags

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

Clone

HTTPS

Download ZIP

src/common/ios/iOSPlatform.ts

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