【译】在应用的icon上加上编译相关信息

#####加那些信息

  • 版本号
  • 代码分支名字
  • 最后一次commit的hash值

######版本号
我们可以使用PlistBuddy工具直接从项目文件info.plist中取出版本号

1
version=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${INFOPLIST_FILE}"`

(当然了,你可以通过这个工具取出info.plist中的任何值,把CFBundleVersion改为其他的key就行了,key值可以在xcode中查看)

######代码分支名字和commit的hash值
这两个值我们可以通过git的命令行git rev-parse来获取

1
2
commit=`git rev-parse --short HEAD`
branch=`git rev-parse --abbrev-ref HEAD`

######怎么把这些信息放到icon上去?
ImageMagic是一个操作图片的命令行工具,这个工具有一堆牛逼的功能

首先要安装ImageMagick和ghostscript(fonts)工具,可以使用brew来快速安装

1
2
brew install imagemagick
brew install ghostscript

我们可以使用convert工具,通过配置imagemagic的caption参数把这些文字信息放到icon上,顺便设置底部对齐以及默认高度。

1
2
3
convert -background '#0008' -fill white -gravity center -size ${width}x40 \
caption:"${version} ${branch} ${commit}" \
${base_file} +swap -gravity south -composite "${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/${target_file}"

#####xcode 项目的配置
通过几部简单的配置把这些步骤加到xcode编译中

  1. 把项目中的Icon的文件名Icon改为Icon_base(Icon@2x.png to Icon@2x_base.png)
  2. 在编译目标的build phase中添加下面的脚本:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    commit=`git rev-parse --short HEAD`
    branch=`git rev-parse --abbrev-ref HEAD`
    version=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${INFOPLIST_FILE}"`

    function processIcon() {
    export PATH=$PATH:/usr/local/bin
    base_file=$1
    base_path=`find ${SRCROOT} -name $base_file`

    if [[ ! -f ${base_path} || -z ${base_path} ]]; then
    return;
    fi

    target_file=`echo $base_file | sed "s/_base//"`
    target_path="${CONFIGURATION_BUILD_DIR}/${UNLOCALIZED_RESOURCES_FOLDER_PATH}/${target_file}"

    if [ $CONFIGURATION = "Release" ]; then
    cp ${base_file} $target_path
    return
    fi

    width=`identify -format %w ${base_path}`

    convert -background '#0008' -fill white -gravity center -size ${width}x40\
    caption:"${version} ${branch} ${commit}"\
    ${base_path} +swap -gravity south -composite ${target_path}
    }

    processIcon "Icon_base.png"
    processIcon "Icon@2x_base.png"
    processIcon "Icon-72_base.png"
    processIcon "Icon-72@2x_base.png"

#####结尾

关于这个脚本的几个小点:

  1. 自动跳过不存在的icon文件
  2. 通过sed去掉文件名末尾的_base字符串
  3. release版本则不会添加这些信息到icon上
  4. xcode貌似吧path信息弄乱了,所以在这个脚本使用中添加了usr/local/bin路径

脚本和例子看这里

原文地址:http://merowing.info/2013/03/overlaying-application-version-on-top-of-your-icon/

Author: y500
Link: https://www.y500.me/2016/10/21/add-infomation-on-app-icon/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.