microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/simulatorPlist.ts
92lines · 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"; | |
09f6024fHeniker4 years ago | 5 | import * as nls from "vscode-nls"; |
34472878RedMickey5 years ago | 6 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 7 | import { OutputChannelLogger } from "../log/OutputChannelLogger"; | |
| 8 | import { FileSystem } from "../../common/node/fileSystem"; | |
| 9 | import { ChildProcess } from "../../common/node/childProcess"; | |
| 10 | import { TelemetryHelper } from "../../common/telemetryHelper"; | |
1c2424f4RedMickey5 years ago | 11 | import { PlatformType } from "../launchArgs"; |
09f6024fHeniker4 years ago | 12 | import { PlistBuddy } from "./plistBuddy"; |
| 13 | | |
34472878RedMickey5 years ago | 14 | nls.config({ |
| 15 | messageFormat: nls.MessageFormat.bundle, | |
| 16 | bundleFormat: nls.BundleFormat.standalone, | |
| 17 | })(); | |
d7d405aeYuri Skorokhodov7 years ago | 18 | const localize = nls.loadMessageBundle(); |
0a68f8dbArtem Egorov8 years ago | 19 | |
| 20 | export class SimulatorPlist { | |
764655c9Yuri Skorokhodov6 years ago | 21 | private iosProjectRoot: string; |
4dfb1c4cetatanova5 years ago | 22 | private projectRoot: string; |
a949e43fRuslan Bikkinin7 years ago | 23 | private scheme?: string; |
0a68f8dbArtem Egorov8 years ago | 24 | private logger: OutputChannelLogger = OutputChannelLogger.getMainChannel(); |
| 25 | private nodeFileSystem: FileSystem; | |
| 26 | private plistBuddy: PlistBuddy; | |
| 27 | private nodeChildProcess: ChildProcess; | |
| 28 | | |
34472878RedMickey5 years ago | 29 | constructor( |
| 30 | iosProjectRoot: string, | |
| 31 | projectRoot: string, | |
| 32 | scheme?: string, | |
| 33 | { | |
| 34 | nodeFileSystem = new FileSystem(), | |
4dfb1c4cetatanova5 years ago | 35 | plistBuddy = undefined, |
34472878RedMickey5 years ago | 36 | nodeChildProcess = new ChildProcess(), |
| 37 | } = {}, | |
| 38 | ) { | |
764655c9Yuri Skorokhodov6 years ago | 39 | this.iosProjectRoot = iosProjectRoot; |
4dfb1c4cetatanova5 years ago | 40 | this.projectRoot = projectRoot; |
| 41 | this.scheme = scheme; | |
0a68f8dbArtem Egorov8 years ago | 42 | this.nodeFileSystem = nodeFileSystem; |
4dfb1c4cetatanova5 years ago | 43 | this.plistBuddy = plistBuddy || new PlistBuddy(); |
0a68f8dbArtem Egorov8 years ago | 44 | this.nodeChildProcess = nodeChildProcess; |
| 45 | } | |
| 46 | | |
0d77292aJiglioNero4 years ago | 47 | public async findPlistFile(configuration?: string, productName?: string): Promise<string> { |
| 48 | const [bundleId, pathBuffer] = await Promise.all([ | |
34472878RedMickey5 years ago | 49 | this.plistBuddy.getBundleId( |
| 50 | this.iosProjectRoot, | |
| 51 | this.projectRoot, | |
1c2424f4RedMickey5 years ago | 52 | PlatformType.iOS, |
34472878RedMickey5 years ago | 53 | true, |
| 54 | configuration, | |
| 55 | productName, | |
| 56 | this.scheme, | |
| 57 | ), // Find the name of the application | |
ce5e88eeYuri Skorokhodov5 years ago | 58 | this.nodeChildProcess.exec("xcrun simctl getenv booted HOME").then(res => res.outcome), // Find the path of the simulator we are running |
0d77292aJiglioNero4 years ago | 59 | ]); |
| 60 | const pathBefore = path.join( | |
| 61 | pathBuffer.toString().trim(), | |
| 62 | "Containers", | |
| 63 | "Data", | |
| 64 | "Application", | |
| 65 | ); | |
| 66 | const pathAfter = path.join("Library", "Preferences", `${bundleId}.plist`); | |
| 67 | // Look through $SIMULATOR_HOME/Containers/Data/Application/*/Library/Preferences to find $BUNDLEID.plist | |
| 68 | const apps = await this.nodeFileSystem.readDir(pathBefore); | |
| 69 | this.logger.info( | |
09f6024fHeniker4 years ago | 70 | `About to search for plist in base folder: ${pathBefore} pathAfter: ${pathAfter} in each of the apps: ${String( |
| 71 | apps, | |
| 72 | )}`, | |
0d77292aJiglioNero4 years ago | 73 | ); |
| 74 | const plistCandidates = apps | |
| 75 | .map((app: string) => path.join(pathBefore, app, pathAfter)) | |
| 76 | .filter(filePath => this.nodeFileSystem.existsSync(filePath)); | |
| 77 | if (plistCandidates.length === 0) { | |
| 78 | throw new Error(`Unable to find plist file for ${bundleId}`); | |
| 79 | } else if (plistCandidates.length > 1) { | |
| 80 | TelemetryHelper.sendSimpleEvent("multipleDebugPlistFound"); | |
| 81 | this.logger.warning( | |
| 82 | ErrorHelper.getWarning( | |
| 83 | localize( | |
| 84 | "MultiplePlistCandidatesFoundAppMayNotBeDebuggedInDebugMode", | |
| 85 | "Multiple plist candidates found. Application may not be in debug mode.", | |
| 86 | ), | |
| 87 | ), | |
34472878RedMickey5 years ago | 88 | ); |
0d77292aJiglioNero4 years ago | 89 | } |
| 90 | return plistCandidates[0]; | |
0a68f8dbArtem Egorov8 years ago | 91 | } |
| 92 | } |