microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/common/ios/simulatorPlist.ts
57lines · modeblame
488f1908Jimmy Thomson10 years ago | 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 path from "path"; | |
| 5 | import * as Q from "q"; | |
| 6 | | |
a4a7e387Meena Kunnathur Balakrishnan10 years ago | 7 | import {ErrorHelper} from "../../common/error/errorHelper"; |
bc7a32ceJimmy Thomson10 years ago | 8 | import {PlistBuddy} from "./plistBuddy"; |
190e393cMeena Kunnathur Balakrishnan10 years ago | 9 | import {Log} from "../../common/log/log"; |
db80cd4eJimmy Thomson10 years ago | 10 | import {FileSystem} from "../../common/node/fileSystem"; |
| 11 | import {ChildProcess} from "../../common/node/childProcess"; | |
488f1908Jimmy Thomson10 years ago | 12 | |
24c4c0aaJimmy Thomson10 years ago | 13 | import {TelemetryHelper} from "../../common/telemetryHelper"; |
| 14 | | |
488f1908Jimmy Thomson10 years ago | 15 | export class SimulatorPlist { |
| 16 | private projectRoot: string; | |
| 17 | | |
db80cd4eJimmy Thomson10 years ago | 18 | private nodeFileSystem: FileSystem; |
| 19 | private plistBuddy: PlistBuddy; | |
| 20 | private nodeChildProcess: ChildProcess; | |
| 21 | | |
| 22 | constructor(projectRoot: string, { | |
| 23 | nodeFileSystem = new FileSystem(), | |
| 24 | plistBuddy = new PlistBuddy(), | |
| 25 | nodeChildProcess = new ChildProcess() | |
| 26 | } = {}) { | |
488f1908Jimmy Thomson10 years ago | 27 | this.projectRoot = projectRoot; |
db80cd4eJimmy Thomson10 years ago | 28 | |
| 29 | this.nodeFileSystem = nodeFileSystem; | |
| 30 | this.plistBuddy = plistBuddy; | |
| 31 | this.nodeChildProcess = nodeChildProcess; | |
488f1908Jimmy Thomson10 years ago | 32 | } |
| 33 | | |
| 34 | public findPlistFile(): Q.Promise<string> { | |
| 35 | | |
| 36 | return Q.all<any>([ | |
db80cd4eJimmy Thomson10 years ago | 37 | this.plistBuddy.getBundleId(this.projectRoot), // Find the name of the application |
| 38 | this.nodeChildProcess.exec("xcrun simctl getenv booted HOME").outcome]) // Find the path of the simulator we are running | |
b031edc7digeff10 years ago | 39 | .spread((bundleId: string, pathBuffer: Buffer) => { |
| 40 | const pathBefore = path.join(pathBuffer.toString().trim(), "Containers", "Data", "Application"); | |
| 41 | const pathAfter = path.join("Library", "Preferences", `${bundleId}.plist`); | |
| 42 | | |
| 43 | // Look through $SIMULATOR_HOME/Containers/Data/Application/*/Library/Preferences to find $BUNDLEID.plist | |
db80cd4eJimmy Thomson10 years ago | 44 | return this.nodeFileSystem.readDir(pathBefore).then((apps: string[]) => { |
| 45 | const plistCandidates = apps.map((app: string) => path.join(pathBefore, app, pathAfter)).filter(this.nodeFileSystem.existsSync); | |
b031edc7digeff10 years ago | 46 | if (plistCandidates.length === 0) { |
| 47 | throw new Error(`Unable to find plist file for ${bundleId}`); | |
| 48 | } else if (plistCandidates.length > 1) { | |
a6db1e9bJimmy Thomson10 years ago | 49 | TelemetryHelper.sendSimpleEvent("multipleDebugPlistFound"); |
a4a7e387Meena Kunnathur Balakrishnan10 years ago | 50 | Log.logWarning(ErrorHelper.getWarning("Multiple plist candidates found. Application may not be in debug mode.")); |
b031edc7digeff10 years ago | 51 | } |
| 52 | | |
| 53 | return plistCandidates[0]; | |
| 54 | }); | |
ee21460bJimmy Thomson10 years ago | 55 | }); |
| 56 | } | |
488f1908Jimmy Thomson10 years ago | 57 | } |