microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/debugger/ios/iOSPlatform.ts
127lines · 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 | import * as path from "path"; |
| 6 | |
| 7 | import {Log} from "../../common/log/log"; |
| 8 | import {ChildProcess} from "../../common/node/childProcess"; |
| 9 | import {CommandExecutor} from "../../common/commandExecutor"; |
| 10 | import {GeneralMobilePlatform} from "../../common/generalMobilePlatform"; |
| 11 | import {Compiler} from "./compiler"; |
| 12 | import {DeviceDeployer} from "./deviceDeployer"; |
| 13 | import {DeviceRunner} from "./deviceRunner"; |
| 14 | import {IRunOptions} from "../../common/launchArgs"; |
| 15 | import {PlistBuddy} from "../../common/ios/plistBuddy"; |
| 16 | import {IOSDebugModeManager} from "../../common/ios/iOSDebugModeManager"; |
| 17 | import {OutputVerifier, PatternToFailure} from "../../common/outputVerifier"; |
| 18 | |
| 19 | export class IOSPlatform extends GeneralMobilePlatform { |
| 20 | public static DEFAULT_IOS_PROJECT_RELATIVE_PATH = "ios"; |
| 21 | |
| 22 | private static deviceString = "device"; |
| 23 | private static simulatorString = "simulator"; |
| 24 | |
| 25 | private plistBuddy = new PlistBuddy(); |
| 26 | |
| 27 | private simulatorTarget: string; |
| 28 | private isSimulator: boolean; |
| 29 | private iosProjectPath: 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 | "No devices are booted": "Unable to launch iOS simulator. Try specifying a different target.", |
| 34 | "FBSOpenApplicationErrorDomain": "Unable to launch iOS simulator. Try specifying a different target.", |
| 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(runOptions: IRunOptions, { remoteExtension = null } = {}) { |
| 41 | super(runOptions, { remoteExtension: remoteExtension }); |
| 42 | this.simulatorTarget = this.runOptions.target || IOSPlatform.simulatorString; |
| 43 | this.isSimulator = this.simulatorTarget.toLowerCase() !== IOSPlatform.deviceString; |
| 44 | this.iosProjectPath = path.join(this.projectPath, this.runOptions.iosRelativeProjectPath); |
| 45 | } |
| 46 | |
| 47 | public runApp(): Q.Promise<void> { |
| 48 | // Compile, deploy, and launch the app on either a simulator or a device |
| 49 | if (this.isSimulator) { |
| 50 | // React native supports running on the iOS simulator from the command line |
| 51 | let runArguments: string[] = []; |
| 52 | if (this.simulatorTarget.toLowerCase() !== IOSPlatform.simulatorString) { |
| 53 | runArguments.push("--simulator", this.simulatorTarget); |
| 54 | } |
| 55 | |
| 56 | runArguments.push("--project-path", this.runOptions.iosRelativeProjectPath); |
| 57 | |
| 58 | const runIosSpawn = new CommandExecutor(this.projectPath).spawnReactCommand("run-ios", runArguments); |
| 59 | return new OutputVerifier( |
| 60 | () => |
| 61 | this.generateSuccessPatterns(), |
| 62 | () => |
| 63 | Q(IOSPlatform.RUN_IOS_FAILURE_PATTERNS)).process(runIosSpawn); |
| 64 | } |
| 65 | |
| 66 | return new Compiler(this.iosProjectPath).compile().then(() => { |
| 67 | return new DeviceDeployer(this.iosProjectPath).deploy(); |
| 68 | }).then(() => { |
| 69 | return new DeviceRunner(this.iosProjectPath).run(); |
| 70 | }); |
| 71 | } |
| 72 | |
| 73 | public enableJSDebuggingMode(): Q.Promise<void> { |
| 74 | // Configure the app for debugging |
| 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 | const iosDebugModeManager = new IOSDebugModeManager(this.iosProjectPath); |
| 82 | |
| 83 | // Wait until the configuration file exists, and check to see if debugging is enabled |
| 84 | return Q.all([ |
| 85 | iosDebugModeManager.getSimulatorJSDebuggingModeSetting(), |
| 86 | this.getBundleId(), |
| 87 | ]).spread((debugModeSetting: string, bundleId: string) => { |
| 88 | if (debugModeSetting !== IOSDebugModeManager.WEBSOCKET_EXECUTOR_NAME) { |
| 89 | // Debugging must still be enabled |
| 90 | // We enable debugging by writing to a plist file that backs a NSUserDefaults object, |
| 91 | // but that file is written to by the app on occasion. To avoid races, we shut the app |
| 92 | // down before writing to the file. |
| 93 | const childProcess = new ChildProcess(); |
| 94 | |
| 95 | return childProcess.execToString("xcrun simctl spawn booted launchctl list").then((output: string) => { |
| 96 | // Try to find an entry that looks like UIKitApplication:com.example.myApp[0x4f37] |
| 97 | const regex = new RegExp(`(\\S+${bundleId}\\S+)`); |
| 98 | const match = regex.exec(output); |
| 99 | |
| 100 | // If we don't find a match, the app must not be running and so we do not need to close it |
| 101 | if (match) { |
| 102 | return childProcess.exec(`xcrun simctl spawn booted launchctl stop ${match[1]}`); |
| 103 | } |
| 104 | }).then(() => { |
| 105 | // Write to the settings file while the app is not running to avoid races |
| 106 | return iosDebugModeManager.setSimulatorJSDebuggingModeSetting(/*enable=*/ true); |
| 107 | }).then(() => { |
| 108 | // Relaunch the app |
| 109 | return this.runApp(); |
| 110 | }); |
| 111 | } |
| 112 | }); |
| 113 | } |
| 114 | |
| 115 | public prewarmBundleCache(): Q.Promise<void> { |
| 116 | return this.remoteExtension.prewarmBundleCache(this.platformName); |
| 117 | } |
| 118 | |
| 119 | private generateSuccessPatterns(): Q.Promise<string[]> { |
| 120 | return this.getBundleId().then(bundleId => |
| 121 | IOSPlatform.RUN_IOS_SUCCESS_PATTERNS.concat([`Launching ${bundleId}\n${bundleId}: `])); |
| 122 | } |
| 123 | |
| 124 | private getBundleId(): Q.Promise<string> { |
| 125 | return this.plistBuddy.getBundleId(this.iosProjectPath); |
| 126 | } |
| 127 | } |
| 128 | |