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