microsoft/vscode-react-native
Publicmirrored fromhttps://github.com/microsoft/vscode-react-nativeAvailable
src/common/ios/plistBuddy.ts
55lines · 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 {Node} from "../../common/node/node"; |
| 8 | import {ChildProcess} from "../../common/node/childProcess"; |
| 9 | import {Xcodeproj, IXcodeProjFile} from "./xcodeproj"; |
| 10 | |
| 11 | export class PlistBuddy { |
| 12 | private static plistBuddyExecutable = "/usr/libexec/PlistBuddy"; |
| 13 | |
| 14 | private nodeChildProcess: ChildProcess; |
| 15 | private xcodeproj: Xcodeproj; |
| 16 | |
| 17 | constructor({ |
| 18 | nodeChildProcess = new Node.ChildProcess(), |
| 19 | xcodeproj = new Xcodeproj(), |
| 20 | } = {}) { |
| 21 | this.nodeChildProcess = nodeChildProcess; |
| 22 | this.xcodeproj = xcodeproj; |
| 23 | } |
| 24 | |
| 25 | public getBundleId(projectRoot: string, simulator: boolean = true): Q.Promise<string> { |
| 26 | return this.xcodeproj.findXcodeprojFile(projectRoot).then((projectFile: IXcodeProjFile) => { |
| 27 | const infoPlistPath = path.join(projectRoot, "build", "Build", "Products", |
| 28 | simulator ? "Debug-iphonesimulator" : "Debug-iphoneos", |
| 29 | `${projectFile.projectName}.app`, "Info.plist"); |
| 30 | |
| 31 | return this.invokePlistBuddy("Print:CFBundleIdentifier", infoPlistPath); |
| 32 | }); |
| 33 | } |
| 34 | |
| 35 | public setPlistProperty(plistFile: string, property: string, value: string): Q.Promise<void> { |
| 36 | // Attempt to set the value, and if it fails due to the key not existing attempt to create the key |
| 37 | return this.invokePlistBuddy(`Set ${property} ${value}`, plistFile).fail(() => |
| 38 | this.invokePlistBuddy(`Add ${property} string ${value}`, plistFile) |
| 39 | ).then(() => { }); |
| 40 | } |
| 41 | |
| 42 | public deletePlistProperty(plistFile: string, property: string): Q.Promise<void> { |
| 43 | return this.invokePlistBuddy(`Delete ${property}`, plistFile).then(() => { }); |
| 44 | } |
| 45 | |
| 46 | public readPlistProperty(plistFile: string, property: string): Q.Promise<string> { |
| 47 | return this.invokePlistBuddy(`Print ${property}`, plistFile); |
| 48 | } |
| 49 | |
| 50 | private invokePlistBuddy(command: string, plistFile: string): Q.Promise<string> { |
| 51 | return this.nodeChildProcess.exec(`${PlistBuddy.plistBuddyExecutable} -c '${command}' '${plistFile}'`).outcome.then((result: string) => { |
| 52 | return result.toString().trim(); |
| 53 | }); |
| 54 | } |
| 55 | } |