xcode 如何从 shell 脚本读取 plist 信息(bundle id)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4328501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:36:53  来源:igfitidea点击:

How to read plist information (bundle id) from a shell script

iphonexcodeshellscriptingpost-processing

提问by Dimitris

I'd like to write a script that can read info like Bundle Identifier or maybe version number from the Info.plist of the app. Xcode doesn't seem to give that information in it's environment variables. Is there any other way to get them in sh/bash?

我想编写一个脚本,可以从应用程序的 Info.plist 中读取 Bundle Identifier 或版本号等信息。Xcode 似乎没有在它的环境变量中提供该信息。有没有其他方法可以让它们进入 sh/bash?

回答by joemaller

The defaultscommand can read/write to any plist file, just give it a path minus the .plistextension:

defaults命令可以读/写任何 plist 文件,只需给它一个减去.plist扩展名的路径:

$ defaults read /Applications/Preview.app/Contents/Info CFBundleIdentifier

com.apple.Preview

This pulls the CFBundleIdentifiervalue directly from the application bundle's Info.plistfile.

这会CFBundleIdentifier直接从应用程序包的Info.plist文件中提取值。

Defaults also works with binary plists without any extra steps.

Defaults 也适用于二进制 plist,无需任何额外步骤。

回答by Dimitris

Using PlistBuddy, an app by Apple it is possible to assign the string to var like this:

使用 PlistBuddy,Apple 的一个应用程序,可以像这样将字符串分配给 var:

#!/bin/sh   
BUNDLE_ID=$(/usr/libexec/PlistBuddy -c "Print :CFBundleIdentifier" "${BUILD_ROOT}/${INFOPLIST_PATH}")

Where BUILD_ROOT and INFOPLIST_PATH are variables set by Xcode if you run this script in a "Run Script" build phase.

如果您在“运行脚本”构建阶段运行此脚本,则 BUILD_ROOT 和 INFOPLIST_PATH 是由 Xcode 设置的变量。

回答by TechZen

You can just read the file directly from the built product. However, if you look at the info.plist file itself in the editor you will see the shell variables themselves. E.g. the Bundle ID is has the following shell command:

您可以直接从构建的产品中读取文件。但是,如果您在编辑器中查看 info.plist 文件本身,您将看到 shell 变量本身。例如,捆绑 ID 具有以下 shell 命令:

com.yourcompany.${PRODUCT_NAME:rfc1034identifier}

You can call ${PRODUCT_NAME:rfc1034identifier}in any shell script that Xcode runs and it should populate.

您可以调用${PRODUCT_NAME:rfc1034identifier}Xcode 运行的任何 shell 脚本,它应该会填充。

回答by donarb

There is a command line program installed on the Mac called PlistBuddy that can read/write values in a plist. Type 'man PlistBuddy' in Terminal to get more info.

Mac 上安装了一个名为 PlistBuddy 的命令行程序,它可以读取/写入 plist 中的值。在终端中输入“man PlistBuddy”以获取更多信息。

回答by djonik1562

This command worked for me:

这个命令对我有用:

/usr/libexec/PlistBuddy -c 'print ":CFBundleIdentifier"' Info.plist