microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/simulatorPlist.ts
66lines · modeblame
0a68f8dbArtem Egorov8 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 | | |
| 7 | import {ErrorHelper} from "../../common/error/errorHelper"; | |
| 8 | import {PlistBuddy} from "./plistBuddy"; | |
| 9 | import {OutputChannelLogger} from "../log/OutputChannelLogger"; | |
| 10 | import {FileSystem} from "../../common/node/fileSystem"; | |
| 11 | import {ChildProcess} from "../../common/node/childProcess"; | |
| 12 | | |
| 13 | import {TelemetryHelper} from "../../common/telemetryHelper"; | |
d7d405aeYuri Skorokhodov7 years ago | 14 | import * as nls from "vscode-nls"; |
| 15 | const localize = nls.loadMessageBundle(); | |
0a68f8dbArtem Egorov8 years ago | 16 | |
| 17 | export class SimulatorPlist { | |
| 18 | private projectRoot: string; | |
764655c9Yuri Skorokhodov6 years ago | 19 | private iosProjectRoot: string; |
a949e43fRuslan Bikkinin7 years ago | 20 | private scheme?: string; |
0a68f8dbArtem Egorov8 years ago | 21 | private logger: OutputChannelLogger = OutputChannelLogger.getMainChannel(); |
| 22 | | |
| 23 | private nodeFileSystem: FileSystem; | |
| 24 | private plistBuddy: PlistBuddy; | |
| 25 | private nodeChildProcess: ChildProcess; | |
| 26 | | |
764655c9Yuri Skorokhodov6 years ago | 27 | constructor(iosProjectRoot: string, projectRoot: string, scheme?: string, { |
0a68f8dbArtem Egorov8 years ago | 28 | nodeFileSystem = new FileSystem(), |
| 29 | plistBuddy = new PlistBuddy(), | |
| 30 | nodeChildProcess = new ChildProcess(), | |
| 31 | } = {}) { | |
| 32 | this.projectRoot = projectRoot; | |
764655c9Yuri Skorokhodov6 years ago | 33 | this.iosProjectRoot = iosProjectRoot; |
0a68f8dbArtem Egorov8 years ago | 34 | |
| 35 | this.nodeFileSystem = nodeFileSystem; | |
| 36 | this.plistBuddy = plistBuddy; | |
| 37 | this.nodeChildProcess = nodeChildProcess; | |
a949e43fRuslan Bikkinin7 years ago | 38 | this.scheme = scheme; |
0a68f8dbArtem Egorov8 years ago | 39 | } |
| 40 | | |
db6fd42aRuslan Bikkinin7 years ago | 41 | public findPlistFile(configuration?: string, productName?: string): Q.Promise<string> { |
0a68f8dbArtem Egorov8 years ago | 42 | |
| 43 | return Q.all<any>([ | |
764655c9Yuri Skorokhodov6 years ago | 44 | this.plistBuddy.getBundleId(this.iosProjectRoot, this.projectRoot, true, configuration, productName, this.scheme), // Find the name of the application |
0a68f8dbArtem Egorov8 years ago | 45 | this.nodeChildProcess.exec("xcrun simctl getenv booted HOME").outcome, // Find the path of the simulator we are running |
| 46 | ]).spread((bundleId: string, pathBuffer: Buffer) => { | |
| 47 | const pathBefore = path.join(pathBuffer.toString().trim(), "Containers", "Data", "Application"); | |
| 48 | const pathAfter = path.join("Library", "Preferences", `${bundleId}.plist`); | |
| 49 | | |
| 50 | // Look through $SIMULATOR_HOME/Containers/Data/Application/*/Library/Preferences to find $BUNDLEID.plist | |
| 51 | return this.nodeFileSystem.readDir(pathBefore).then((apps: string[]) => { | |
| 52 | this.logger.info(`About to search for plist in base folder: ${pathBefore} pathAfter: ${pathAfter} in each of the apps: ${apps}`); | |
| 53 | const plistCandidates = apps.map((app: string) => path.join(pathBefore, app, pathAfter)).filter(filePath => | |
| 54 | this.nodeFileSystem.existsSync(filePath)); | |
| 55 | if (plistCandidates.length === 0) { | |
| 56 | throw new Error(`Unable to find plist file for ${bundleId}`); | |
| 57 | } else if (plistCandidates.length > 1) { | |
| 58 | TelemetryHelper.sendSimpleEvent("multipleDebugPlistFound"); | |
d7d405aeYuri Skorokhodov7 years ago | 59 | this.logger.warning(ErrorHelper.getWarning(localize("MultiplePlistCandidatesFoundAppMayNotBeDebuggedInDebugMode", "Multiple plist candidates found. Application may not be in debug mode."))); |
0a68f8dbArtem Egorov8 years ago | 60 | } |
| 61 | | |
| 62 | return plistCandidates[0]; | |
| 63 | }); | |
| 64 | }); | |
| 65 | } | |
| 66 | } |