|
| 1 | +///<reference path="./.d.ts"/> |
| 2 | +"use strict"; |
| 3 | + |
| 4 | +import childProcess = require("./child-process"); |
| 5 | +import errors = require("./errors"); |
| 6 | + |
| 7 | +import common = require("./iphone-simulator-common"); |
| 8 | +import options = require("./options"); |
| 9 | +import path = require("path"); |
| 10 | +import { Simctl } from "./simctl"; |
| 11 | +import util = require("util"); |
| 12 | +import utils = require("./utils"); |
| 13 | +import xcode = require("./xcode"); |
| 14 | +import * as _ from "lodash"; |
| 15 | + |
| 16 | +import {IPhoneSimulatorNameGetter} from "./iphone-simulator-name-getter"; |
| 17 | + |
| 18 | +export class XCode8Simulator extends IPhoneSimulatorNameGetter implements ISimulator { |
| 19 | + private static DEVICE_IDENTIFIER_PREFIX = "com.apple.CoreSimulator.SimDeviceType"; |
| 20 | + public defaultDeviceIdentifier = "iPhone 6"; |
| 21 | + |
| 22 | + private simctl: ISimctl = null; |
| 23 | + |
| 24 | + constructor() { |
| 25 | + super(); |
| 26 | + this.simctl = new Simctl(); |
| 27 | + } |
| 28 | + |
| 29 | + public getDevices(): IFuture<IDevice[]> { |
| 30 | + return this.simctl.getDevices(); |
| 31 | + } |
| 32 | + |
| 33 | + public getSdks(): IFuture<ISdk[]> { |
| 34 | + return (() => { |
| 35 | + let devices = this.simctl.getDevices().wait(); |
| 36 | + return _.map(devices, device => { |
| 37 | + return { |
| 38 | + displayName: `iOS ${device.runtimeVersion}`, |
| 39 | + version: device.runtimeVersion |
| 40 | + }; |
| 41 | + }); |
| 42 | + }).future<ISdk[]>()(); |
| 43 | + } |
| 44 | + |
| 45 | + public run(applicationPath: string, applicationIdentifier: string): IFuture<void> { |
| 46 | + return (() => { |
| 47 | + let device = this.getDeviceToRun().wait(); |
| 48 | + let currentBootedDevice = _.find(this.getDevices().wait(), device => this.isDeviceBooted(device)); |
| 49 | + if(currentBootedDevice && (currentBootedDevice.name.toLowerCase() !== device.name.toLowerCase() || currentBootedDevice.runtimeVersion !== device.runtimeVersion)) { |
| 50 | + this.killSimulator().wait(); |
| 51 | + } |
| 52 | + |
| 53 | + this.startSimulator(device).wait(); |
| 54 | + if (!options.skipInstall) { |
| 55 | + this.simctl.install(device.id, applicationPath).wait(); |
| 56 | + } |
| 57 | + let launchResult = this.simctl.launch(device.id, applicationIdentifier).wait(); |
| 58 | + |
| 59 | + if (options.logging) { |
| 60 | + this.printDeviceLog(device.id, launchResult); |
| 61 | + } |
| 62 | + }).future<void>()(); |
| 63 | + } |
| 64 | + |
| 65 | + public sendNotification(notification: string): IFuture<void> { |
| 66 | + return (() => { |
| 67 | + let device = this.getBootedDevice().wait(); |
| 68 | + if (!device) { |
| 69 | + errors.fail("Could not find device."); |
| 70 | + } |
| 71 | + |
| 72 | + this.simctl.notifyPost("booted", notification).wait(); |
| 73 | + }).future<void>()(); |
| 74 | + } |
| 75 | + |
| 76 | + public getApplicationPath(deviceId: string, applicationIdentifier: string): IFuture<string> { |
| 77 | + return this.simctl.getAppContainer(deviceId, applicationIdentifier); |
| 78 | + } |
| 79 | + |
| 80 | + public getInstalledApplications(deviceId: string): IFuture<IApplication[]> { |
| 81 | + return common.getInstalledApplications(deviceId); |
| 82 | + } |
| 83 | + |
| 84 | + public installApplication(deviceId: string, applicationPath: string): IFuture<void> { |
| 85 | + return this.simctl.install(deviceId, applicationPath); |
| 86 | + } |
| 87 | + |
| 88 | + public uninstallApplication(deviceId: string, appIdentifier: string): IFuture<void> { |
| 89 | + return this.simctl.uninstall(deviceId, appIdentifier, {skipError: true}); |
| 90 | + } |
| 91 | + |
| 92 | + public startApplication(deviceId: string, appIdentifier: string): IFuture<string> { |
| 93 | + return this.simctl.launch(deviceId, appIdentifier); |
| 94 | + } |
| 95 | + |
| 96 | + public stopApplication(deviceId: string, cfBundleExecutable: string): IFuture<string> { |
| 97 | + try { |
| 98 | + return childProcess.exec(`killall ${cfBundleExecutable}`, {skipError: true}); |
| 99 | + } catch(e) { |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + public printDeviceLog(deviceId: string, launchResult?: string): void { |
| 104 | + common.printDeviceLog(deviceId, launchResult); |
| 105 | + } |
| 106 | + |
| 107 | + private getDeviceToRun(): IFuture<IDevice> { |
| 108 | + return (() => { |
| 109 | + let devices = this.simctl.getDevices().wait(), |
| 110 | + sdkVersion = options.sdkVersion || options.sdk; |
| 111 | + |
| 112 | + let result = _.find(devices, (device: IDevice) => { |
| 113 | + if(sdkVersion && !options.device) { |
| 114 | + return device.runtimeVersion === sdkVersion; |
| 115 | + } |
| 116 | + |
| 117 | + if(options.device && !sdkVersion) { |
| 118 | + return device.name === options.device; |
| 119 | + } |
| 120 | + |
| 121 | + if(options.device && sdkVersion) { |
| 122 | + return device.runtimeVersion === sdkVersion && device.name === options.device; |
| 123 | + } |
| 124 | + |
| 125 | + if(!sdkVersion && !options.device) { |
| 126 | + return this.isDeviceBooted(device); |
| 127 | + } |
| 128 | + }); |
| 129 | + |
| 130 | + if(!result) { |
| 131 | + result = _.find(devices, (device: IDevice) => device.name === this.defaultDeviceIdentifier); |
| 132 | + } |
| 133 | + |
| 134 | + if(!result) { |
| 135 | + let sortedDevices = _.sortBy(devices, (device) => device.runtimeVersion); |
| 136 | + result = _.last(sortedDevices); |
| 137 | + } |
| 138 | + |
| 139 | + return result; |
| 140 | + }).future<IDevice>()(); |
| 141 | + } |
| 142 | + |
| 143 | + private isDeviceBooted(device: IDevice): boolean { |
| 144 | + return device.state === 'Booted'; |
| 145 | + } |
| 146 | + |
| 147 | + private getBootedDevice(): IFuture<IDevice> { |
| 148 | + return (() => { |
| 149 | + let devices = this.simctl.getDevices().wait(); |
| 150 | + return _.find(devices, device => this.isDeviceBooted(device)); |
| 151 | + }).future<IDevice>()(); |
| 152 | + } |
| 153 | + |
| 154 | + public startSimulator(device?: IDevice): IFuture<void> { |
| 155 | + return (() => { |
| 156 | + device = device || this.getDeviceToRun().wait(); |
| 157 | + if (!this.isDeviceBooted(device)) { |
| 158 | + common.startSimulator(device.id).wait(); |
| 159 | + // startSimulaltor doesn't always finish immediately, and the subsequent |
| 160 | + // install fails since the simulator is not running. |
| 161 | + // Give it some time to start before we attempt installing. |
| 162 | + utils.sleep(1000); |
| 163 | + } |
| 164 | + }).future<void>()(); |
| 165 | + } |
| 166 | + |
| 167 | + private killSimulator(): IFuture<any> { |
| 168 | + return childProcess.spawn("pkill", ["-9", "-f", "Simulator"]); |
| 169 | + } |
| 170 | +} |
0 commit comments