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