Skip to content

Instantly share code, notes, and snippets.

@dulacp
Last active November 19, 2017 11:18
Show Gist options
  • Save dulacp/a52154ac4c007db2be55 to your computer and use it in GitHub Desktop.
Save dulacp/a52154ac4c007db2be55 to your computer and use it in GitHub Desktop.
Overlay the iOS application version on top of the icon

Usage

Requirements

Install the two dependencies, ImageMagick and Ghostscript.

$ brew install imagemagick
$ brew install ghostscript

Integration

Include the tag_icons.sh script in a scripts/ directory in your iOS app project root. Ensure the file is executable by running $ chmod +x scripts/tag_icons.sh.

Add one build phase before the Copy Bundle Resources phase :

if [ $CONFIGURATION != "Release" ] ; then
    ${SRCROOT}/scripts/tag_icons.sh tag YourApp/Images.xcassets/AppIcon.appiconset
fi

And another build phase at the very end, that restore the original icon files

if [ $CONFIGURATION != "Release" ] ; then
    ${SRCROOT}/scripts/tag_icons.sh cleanup YourApp/Images.xcassets/AppIcon.appiconset
fi
# Overlay the build version and the git hash on the app icon
#
# Inspired by @merowing and @bejo script
# who were inspired initially by Evan Doll's talk
#
# http://www.merowing.info/2013/03/overlaying-application-version-on-top-of-your-icon/#.VCg9li5_v6A
# https://github.com/bejo/XcodeIconTagger/blob/master/tagIcons.sh
commit=`git rev-parse --short HEAD`
branch=`git rev-parse --abbrev-ref HEAD`
version=`/usr/libexec/PlistBuddy -c "Print CFBundleVersion" "${INFOPLIST_FILE}"`
# script subcommands
mode=$1
tagMode="tag"
cleanupMode="cleanup"
function processIcon() {
export PATH=$PATH:/usr/local/bin:/opt/boxen/homebrew/bin/
icon_path=$1
width=`identify -format %w ${icon_path}`
convert -background '#0008' -fill white -gravity center -size ${width}x40\
caption:"${version} ${branch} ${commit}"\
${icon_path} +swap -gravity south -composite ${icon_path}
echo "Icon '$icon_path' updated"
}
# retrieve the icon list from the assets catalogue
iconsDirectory=`cd $2 && pwd`
if [ $(echo "${iconsDirectory}" | grep -E "\.appiconset$") ]; then
icons=(`grep 'filename' "${iconsDirectory}/Contents.json" | cut -f2 -d: | tr -d ',' | tr -d '\n' | tr -d '"'`)
else
icons=(`/usr/libexec/PlistBuddy -c "Print CFBundleIconFiles" "${INFOPLIST_FILE}" | grep png | tr -d '\n'`)
fi
iconsCount=${#icons[*]}
for (( i=0; i<iconsCount; i++ ))
do
icon="$iconsDirectory/${icons[$i]}"
if [ -f $icon ]; then
if [ $mode == $tagMode ]; then
# ensure the icon hasn't been already tagged
git checkout $icon
# tag it
processIcon $icon
elif [ $mode == $cleanupMode ]; then
git checkout $icon
else
echo " ! Unknown mode `$mode`"
fi
else
echo " ! No icon for path `$icon`"
fi
done
@rromanchuk
Copy link

@dulaccc how do you deal with build failures? Not a huge deal, but if you build fails and you don't complete a successful build it's really easy to accidently checkin the modified asset

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment