如何使用脚本在 Xcode 11 中读取当前应用程序版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/56722677/
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-15 10:59:20  来源:igfitidea点击:

How to read current app version in Xcode 11 with script

iosxcodeshbuild-settingsxcode11

提问by Mojtaba Hosseini

Until Xcode 11, I used a script that reads the current app version (for the AppStore) and help me change the LaunchScreen since we can't use swift for that.

Xcode 11 之前,我使用了一个脚本来读取当前的应用程序版本(用于 AppStore)并帮助我更改 LaunchScreen,因为我们无法为此使用 swift。

sourceFilePath="$PROJECT_DIR/$PROJECT_NAME/App/Base.lproj/LaunchScreen.storyboard"
versionNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "$INFOPLIST_FILE")
buildNumber=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "$INFOPLIST_FILE")

sed -i .bak -e "/userLabel=\"APP_VERSION_LABEL\"/s/text=\"[^\"]*\"/text=\"v$versionNumber\"/" "$PROJECT_DIR/$PROJECT_NAME/App/Base.lproj/LaunchScreen.storyboard"

But in Xcode 11there is a new section inside the project's build settings called Versioning

但是在Xcode 11 中,项目的构建设置中有一个名为Versioning的新部分

enter image description here

在此处输入图片说明

And CFBundleShortVersionStringautomatically changed to $(MARKETING_VERSION). Xcode automatically handles that and I don't want to change it manually to an static numberand let Xcode do it's work.

CFBundleShortVersionString自动更改为$(MARKETING_VERSION). Xcode 会自动处理,我不想手动将其更改为静态数字并让 Xcode 完成它的工作。

11

11

So the question is how can I access this new MARKETING_VERSIONand set it to my launchScreen label using run script?

所以问题是我如何访问这个新的MARKETING_VERSION并使用运行脚本将它设置为我的 launchScreen 标签?

采纳答案by Nanunana

You can use it like any other project variable:

您可以像使用任何其他项目变量一样使用它:

sourceFilePath="$PROJECT_DIR/$PROJECT_NAME/App/Base.lproj/LaunchScreen.storyboard"
versionNumber="$MARKETING_VERSION"
buildNumber="$CURRENT_PROJECT_VERSION"

sed -i .bak -e "/userLabel=\"APP_VERSION_LABEL\"/s/text=\"[^\"]*\"/text=\"v$versionNumber\"/" "$PROJECT_DIR/$PROJECT_NAME/App/Base.lproj/LaunchScreen.storyboard"

回答by Babac

Couldn't find right answer on the internet, so I started digging.

在互联网上找不到正确的答案,所以我开始挖掘。

Version and build numbers are displayed in ./PROJECTNAME.xcodeproj/project.pbxprojas MARKETING VERSION (MV) and CURRENT PROJECT VERSION (CPV).

版本和内部版本号显示./PROJECTNAME.xcodeproj/project.pbxproj为营销版本 (MV) 和当前项目版本 (CPV)。

version number

版本号

build number

版本号

I used sedto get the numbers. It finds first occurrenceof MV or CPV, removes everything except the number, and returns result. In order for this to work, you need to do 2 things:

我用sed来获取数字。它找到第一次出现的 MV 或 CPV,删除除数字之外的所有内容,并返回结果。为了使其工作,您需要做两件事:

  • navigate to projects root folder
  • change PROJECTNAME to your project's name
  • 导航到项目根文件夹
  • 将 PROJECTNAME 更改为您的项目名称

Commands:

命令:

version_number=`sed -n '/MARKETING_VERSION/{s/MARKETING_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' ./PROJECTNAME.xcodeproj/project.pbxproj`
build_number=`sed -n '/CURRENT_PROJECT_VERSION/{s/CURRENT_PROJECT_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' ./PROJECTNAME.xcodeproj/project.pbxproj`

Result:

结果:

version and build numbers

版本号和构建号

Note: If you have more targets in your workspace with different version and build numbers, this might or might not work for you, because it stops on first occurrence. In that case, good luck :)

注意:如果您的工作区中有更多具有不同版本和内部版本号的目标,这可能适合您,也可能不适合,因为它在第一次出现时停止。在这种情况下,祝你好运:)

回答by A.Kant

Xcode 11.3

Xcode 11.3

In terminalor bash script in your projectyou can use:

在项目中的终端bash 脚本中,您可以使用:

App version

应用版本

xcodebuild -showBuildSettings | grep MARKETING_VERSION | tr -d 'MARKETING_VERSION =' // will be displayed 1.1.6

xcodebuild -showBuildSettings | grep MARKETING_VERSION | tr -d 'MARKETING_VERSION =' // will be displayed 1.1.6

Build version

构建版本

xcodebuild -showBuildSettings | grep CURRENT_PROJECT_VERSION | tr -d 'CURRENT_PROJECT_VERSION =' // will be displayed 7

xcodebuild -showBuildSettings | grep CURRENT_PROJECT_VERSION | tr -d 'CURRENT_PROJECT_VERSION =' // will be displayed 7

Or (don't forget to change YouProjectNameto your project name):

或者(不要忘记将YouProjectName更改为您的项目名称):

App version

应用版本

cat YouProjectName.xcodeproj/project.pbxproj | grep -m1 'MARKETING_VERSION' | cut -d'=' -f2 | tr -d ';' | tr -d ' '

cat YouProjectName.xcodeproj/project.pbxproj | grep -m1 'MARKETING_VERSION' | cut -d'=' -f2 | tr -d ';' | tr -d ' '

Build version

构建版本

cat YouProjectName.xcodeproj/project.pbxproj | grep -m1 'CURRENT_PROJECT_VERSION' | cut -d'=' -f2 | tr -d ';' | tr -d ' '

cat YouProjectName.xcodeproj/project.pbxproj | grep -m1 'CURRENT_PROJECT_VERSION' | cut -d'=' -f2 | tr -d ';' | tr -d ' '

Or slower method(Thx Joshua Kaden):

或者更慢的方法(Thx Joshua Kaden):

App version

应用版本

xcodebuild -project YouProjectName.xcodeproj -showBuildSettings | grep "MARKETING_VERSION" | sed 's/[ ]*MARKETING_VERSION = //'

xcodebuild -project YouProjectName.xcodeproj -showBuildSettings | grep "MARKETING_VERSION" | sed 's/[ ]*MARKETING_VERSION = //'

Build version

构建版本

xcodebuild -project YouProjectName.xcodeproj -showBuildSettings | grep "CURRENT_PROJECT_VERSION" | sed 's/[ ]*CURRENT_PROJECT_VERSION = //'

xcodebuild -project YouProjectName.xcodeproj -showBuildSettings | grep "CURRENT_PROJECT_VERSION" | sed 's/[ ]*CURRENT_PROJECT_VERSION = //'

回答by Sudipta Sahoo

If you use Bitrisethen the following script will save you:

如果您使用Bitrise,那么以下脚本将为您节省:

Extracting App Marketing Version

提取应用营销版本

envman add --key=APP_VERSION_NO --value=`sed -n '/MARKETING_VERSION/{s/MARKETING_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' ./${PATH_TO_YOUR_PROJECT_XCODEPROJ_FILE}/project.pbxproj`

envman add --key=APP_VERSION_NO --value=`sed -n '/MARKETING_VERSION/{s/MARKETING_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' ./${PATH_TO_YOUR_PROJECT_XCODEPROJ_FILE}/project.pbxproj`

Extracting App Build Number

提取应用程序内部版本号

Extract the Build Number from xcodeproj file only if you use Apple Genericversioning system otherwise extract the Build Number from the XCode project info.plist file.

仅当您使用Apple 通用版本控制系统时才从 xcodeproj 文件中提取内部版本号,否则从 XCode 项目 info.plist 文件中提取内部版本号。

Note: The following script extracts the Build Number from the xcodeproj file.

注意:以下脚本从 xcodeproj 文件中提取内部版本号。

envman add --key=APP_BUILD_NO --value=`sed -n '/CURRENT_PROJECT_VERSION/{s/CURRENT_PROJECT_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' ./${PATH_TO_YOUR_PROJECT_XCODEPROJ_FILE}/project.pbxproj`

envman add --key=APP_BUILD_NO --value=`sed -n '/CURRENT_PROJECT_VERSION/{s/CURRENT_PROJECT_VERSION = //;s/;//;s/^[[:space:]]*//;p;q;}' ./${PATH_TO_YOUR_PROJECT_XCODEPROJ_FILE}/project.pbxproj`

Printing to console:

打印到控制台:

envman run bash -c 'echo "App Version: $APP_VERSION_NO"'

envman run bash -c 'echo "App Version: $APP_VERSION_NO"'

envman run bash -c 'echo "App Build No: $APP_BUILD_NO"'

envman run bash -c 'echo "App Build No: $APP_BUILD_NO"'

Thanks to the answer by @babac

感谢@babac 的回答

回答by rickrvo

How about saving a value to CURRENT_PROJECT_VERSION ? did anyone managed to do this?

将值保存到 CURRENT_PROJECT_VERSION 怎么样?有没有人设法做到这一点?

I can get the value like

我可以得到这样的价值

buildNumber=$CURRENT_PROJECT_VERSION

but this doesn't work:

但这不起作用:

CURRENT_PROJECT_VERSION=""    or   $CURRENT_PROJECT_VERSION=""

In my case I'm trying to set it to ""

就我而言,我试图将其设置为“”

This line doesn't set the CURRENT_PROJECT_VERSION field too

这一行也没有设置 CURRENT_PROJECT_VERSION 字段

/usr/libexec/PlistBuddy -c "Set :CFBundleVersion $buildNumber" "$appInfoPlist"

回答by buguibu

I'm developing a framework with this scenario:

我正在开发一个具有这种场景的框架:

  • A workspace
  • A framework target
  • An aggregate target with 2 external scripts:
    • one for build a fat framework
    • other for prepare the release framework
  • 一个工作空间
  • 框架目标
  • 带有 2 个外部脚本的聚合目标:
    • 一个用于构建胖框架
    • 其他用于准备发布框架

The key is that in XCode 11 the aggregate framework script doesn't get run environment variables for other workspace targetsso it's impossible to read the $MARKETING_VERSION from my framework target.

关键是在 XCode 11 中,聚合框架脚本不会为其他工作区目标获取运行环境变量,因此不可能从我的框架目标中读取 $MARKETING_VERSION。

So the solution that works for me has been use PlistBuddy specifying the Info.plist result of the framework target buildin this way:

所以对我有用的解决方案是使用 PlistBuddy以这种方式指定框架目标构建的 Info.plist 结果

FAT_FRAMEWORK="${SRCROOT}/build/${FRAMEWORK_NAME}.framework"
BUILD_NUMBER=$(/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${FAT_FRAMEWORK}/Info.plist")
VERSION_NUMBER=$(/usr/libexec/PlistBuddy -c "Print CFBundleShortVersionString" "${FAT_FRAMEWORK}/Info.plist")

回答by Georgiy T.

For Node.js: there is xcode package. Example of usage:

对于 Node.js:有xcode 包。用法示例:

const xcode = require('xcode');

const project = xcode.project('ios/PROJECT_NAME.xcodeproj/project.pbxproj').parse(() => {
  const config = project.pbxXCBuildConfigurationSection();
  const releaseScheme = Object.keys(config).find(key => config[key].name === 'Release');

  const version = config[releaseScheme].buildSettings.MARKETING_VERSION;
});

Previously I used the plistpackage, but with latest xCode changes it became outdated, since I'm not able to extract a version from Info.plistfor React Native projects.

以前我使用过这个plist包,但随着最新的 xCode 更改,它变得过时了,因为我无法从Info.plistReact Native 项目中提取版本。

回答by Nagarjun

I had similar issue and made it work by displaying MARKETING_VERSION itself:

我有类似的问题,并通过显示 MARKETING_VERSION 本身使其工作:

version="$MARKETING_VERSION"
version+=" ("
version+=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" $SRCROOT/MyApp/Info.plist`
version+=")"

/usr/libexec/PlistBuddy "$SRCROOT/MyApp/Settings.bundle/Root.plist" -c "set PreferenceSpecifiers:1:DefaultValue $version"