自動在app表徵圖上添加應用的版本資訊

來源:互聯網
上載者:User

標籤:

from:http://merowing.info/2013/03/overlaying-application-version-on-top-of-your-icon/
Overlaying application version on top of your iconMar 7, 2013 3 minute read

I’ve just returned from NSConference #5, there were many good talks there, but my favourite one was the one about Flipboard development tools/setup by Evan Doll.

Especially how they add version information on top of the icons.Unfortunately they didn’t share it with us.

So I wrote my own.

What we will be overlaying ?

I’ve decided that 3 parts of information should be enough:

  • version number
  • branch name
  • short commit hash
Version number

We can extract version number straight from our application .plist file by using PlistBuddy tool:

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

PS. You could extract any plist entry with this tool, just change CFBundleVersion to different key (show raw keys in Xcode)

Branch and short commit hash

git command line tool offers rev-parse command which let’s you both of those variables:

commit=`git rev-parse --short HEAD`branch=`git rev-parse --abbrev-ref HEAD`
How to overlay it ?

ImageMagic is my go-to tool for playing with images from command line, it offers crazy amount of functions.

Make sure to install ImageMagick and ghostscript (fonts) first, you can use brew to simplify process:

brew install imagemagickbrew install ghostscript

We can use convert function, by specifing caption parameter imagemagick will layout our text on top of image, we also setup alignment to bottom and default height.

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}"
Setting it up in Xcode project

There are few steps we need to take in order to set this up as part of Xcode build:

  1. Rename your Icon* files (where * is @2x, -568h etc.) to Icon_base*, e.g. [email protected]_base.png
  2. Add run script for your target under Build Phases
  3. Paste this code:
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 "[email protected]_base.png"processIcon "Icon-72_base.png"processIcon "[email protected]_base.png"
Conclusion

Few things about final script:

  • I’ve added support for skipping icons that don’t exist, no need to change script contents.
  • I’m using sed to strip _base string from file name.
  • For release build we don’t want to overlay our development info.
  • Xcode likes messes up path resolution, so I’ve added usr/local/bin to it for this terminal lifetime.

Now run your project and you should see this:

Sample project on GitHub

自動在app表徵圖上添加應用的版本資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.