Skip to content

Instantly share code, notes, and snippets.

@rgm
Last active September 11, 2023 12:40
Show Gist options
  • Save rgm/5377144 to your computer and use it in GitHub Desktop.
Save rgm/5377144 to your computer and use it in GitHub Desktop.
Add the current version and build number on your iOS app icon
#!/usr/bin/env ruby
# Requires ImageMagick: `brew install imagemagick`
# Requires version.sh from https://gist.github.com/osteslag/1089407
#
# Set RGM_STAMP_VERSION_ON_ICONS=1 in your build settings to enable/disable
# stamping on Debug/Relase configurations.
#
# Make base unstamped versions Icon.base.png, &c. in the source tree. The
# script will make stamped versions Icon.png, &c. It relies on Xcode to copy
# resources so that they get properly compressed, so make sure this script step
# runs before "Copy Bundle Resources."
#
# Thanks Evan Doll (@edog1203), Krzysztof Zabłocki (@merowing), Joachim Bondo
# (@ostelag)
# see also:
# http://www.merowing.info/2013/03/overlaying-application-version-on-top-of-your-icon/
# http://stuff.bondo.net/post/7769890357/using-build-and-version-numbers-and-the-art-of
class NoSourceFileError < ArgumentError; end
class Icon
attr_accessor :src, :dst
def src= path
if File.exist? path
@src = path
else
raise NoSourceFileError, "no icon file at #{path}"
end
end
def stamp_icon_with str
`convert -background '#0008' -fill white -gravity center \
-size #{banner_dims} caption:"#{str}" "#{@src}" +swap \
-gravity south -composite "#{@dst}"`
end
def banner_dims
banner_ht = (dims[:ht] * 0.3).floor
"#{dims[:wd]}x#{banner_ht}" # imagemagick-style format string
end
def dims
return @dims if @dims
data = `identify -format "%w %h" "#{@src}"`.strip.split.map(&:to_i)
@dims = {:wd => data[0], :ht => data[1]}
end
end
if (__FILE__ == $0)
require 'fileutils'
version = `Scripts/version --terse`.strip
# just working from a base version in the source tree. Imagemagick can't
# handle the Xcode-compressed PNGs in the built products directory. So
# just stamp it and rely on Xcode to copy bundle resources normally.
path = ENV['RGM_PATH_TO_ICONS']
if ! path
STDERR.puts "Error: RGM_PATH_TO_ICONS isn't set"
exit 1
end
%w{Icon Icon@2x Icon-72 Icon-72@2x}.each do |stem|
begin
i = Icon.new
i.src = File.join path, "#{stem}.base.png"
i.dst = File.join path, "#{stem}.png"
if (ENV['RGM_STAMP_VERSION_ON_ICONS'] == '1')
i.stamp_icon_with version
else
FileUtils.cp i.src, i.dst
end
rescue NoSourceFileError => msg
STDERR.puts "Warning: #{msg}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment