microsoft/vscode-react-native
Publicmirrored from https://github.com/microsoft/vscode-react-nativeAvailable
src/extension/ios/plistBuddy.ts
80lines · 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 | import * as glob from "glob"; |
| 7 | |
| 8 | import {Node} from "../../common/node/node"; |
| 9 | import {ChildProcess} from "../../common/node/childProcess"; |
| 10 | import { ErrorHelper } from "../../common/error/errorHelper"; |
| 11 | import { InternalErrorCode } from "../../common/error/internalErrorCode"; |
| 12 | |
| 13 | export class PlistBuddy { |
| 14 | private static plistBuddyExecutable = "/usr/libexec/PlistBuddy"; |
| 15 | |
| 16 | private nodeChildProcess: ChildProcess; |
| 17 | |
| 18 | constructor({ |
| 19 | nodeChildProcess = new Node.ChildProcess(), |
| 20 | } = {}) { |
| 21 | this.nodeChildProcess = nodeChildProcess; |
| 22 | } |
| 23 | |
| 24 | public getBundleId(projectRoot: string, simulator: boolean = true, configuration: string = "Debug", productName?: string): Q.Promise<string> { |
| 25 | const productsFolder = path.join(projectRoot, "build", "Build", "Products"); |
| 26 | const configurationFolder = path.join(productsFolder, `${configuration}${simulator ? "-iphonesimulator" : "-iphoneos"}`); |
| 27 | let executable = ""; |
| 28 | if (productName) { |
| 29 | executable = `${productName}.app`; |
| 30 | } else { |
| 31 | const executableList = this.findExecutable(configurationFolder); |
| 32 | if (!executableList.length) { |
| 33 | throw ErrorHelper.getInternalError(InternalErrorCode.IOSCouldNotFoundExecutableInFolder, configurationFolder); |
| 34 | } else if (executableList.length > 1) { |
| 35 | throw ErrorHelper.getInternalError(InternalErrorCode.IOSFoundMoreThanOneExecutablesCleanupBuildFolder, configurationFolder); |
| 36 | } |
| 37 | executable = `${executableList[0]}`; |
| 38 | } |
| 39 | |
| 40 | const infoPlistPath = path.join(configurationFolder, executable, "Info.plist"); |
| 41 | return this.invokePlistBuddy("Print:CFBundleIdentifier", infoPlistPath); |
| 42 | |
| 43 | } |
| 44 | |
| 45 | public setPlistProperty(plistFile: string, property: string, value: string): Q.Promise<void> { |
| 46 | // Attempt to set the value, and if it fails due to the key not existing attempt to create the key |
| 47 | return this.invokePlistBuddy(`Set ${property} ${value}`, plistFile).fail(() => |
| 48 | this.invokePlistBuddy(`Add ${property} string ${value}`, plistFile) |
| 49 | ).then(() => { }); |
| 50 | } |
| 51 | |
| 52 | public setPlistBooleanProperty(plistFile: string, property: string, value: boolean): Q.Promise<void> { |
| 53 | // Attempt to set the value, and if it fails due to the key not existing attempt to create the key |
| 54 | return this.invokePlistBuddy(`Set ${property} ${value}`, plistFile) |
| 55 | .fail(() => |
| 56 | this.invokePlistBuddy(`Add ${property} bool ${value}`, plistFile) |
| 57 | ) |
| 58 | .then(() => { }); |
| 59 | } |
| 60 | |
| 61 | public deletePlistProperty(plistFile: string, property: string): Q.Promise<void> { |
| 62 | return this.invokePlistBuddy(`Delete ${property}`, plistFile).then(() => { }); |
| 63 | } |
| 64 | |
| 65 | public readPlistProperty(plistFile: string, property: string): Q.Promise<string> { |
| 66 | return this.invokePlistBuddy(`Print ${property}`, plistFile); |
| 67 | } |
| 68 | |
| 69 | private findExecutable(folder: string): string[] { |
| 70 | return glob.sync("*.app", { |
| 71 | cwd: folder, |
| 72 | }); |
| 73 | } |
| 74 | |
| 75 | private invokePlistBuddy(command: string, plistFile: string): Q.Promise<string> { |
| 76 | return this.nodeChildProcess.exec(`${PlistBuddy.plistBuddyExecutable} -c '${command}' '${plistFile}'`).outcome.then((result: string) => { |
| 77 | return result.toString().trim(); |
| 78 | }); |
| 79 | } |
| 80 | } |
| 81 | |