Skip to content

Instantly share code, notes, and snippets.

@elliottwilliams
Last active October 23, 2018 05:47
Show Gist options
  • Save elliottwilliams/0691b9c4a05d35112de785b79ac50c3c to your computer and use it in GitHub Desktop.
Save elliottwilliams/0691b9c4a05d35112de785b79ac50c3c to your computer and use it in GitHub Desktop.
Automatically bump and commit Xcode project version and build numbers
# Follow the instructions at https://developer.apple.com/library/archive/qa/qa1827/_index.html
# ("Technical Q&A QA1827: Automating Version and Build Numbers Using agvtool")
#
# Then manage versions with `rake`:
#
# $ rake bump[build]
# $ rake bump[minor]
# $ rake bump[major]
def perform_bump(level)
versions = `agvtool mvers -terse1`.split(".").map(&:to_i)
# Always bump the build number
sh "agvtool bump"
build = `agvtool vers -terse`
# Then bump the marketing version, if specified
case level
when :build
return versions.join(".") + "-" + build
when :minor
versions[1] += 1
when :major
versions[0] += 1
versions[1] = 0
end
new_version = versions.join(".")
sh "agvtool new-marketing-version #{new_version}"
return new_version + "-" + build
end
task :bump, [:level] => [:_index_clean?] do |t, args|
level = args.level.to_sym
raise "Cannot bump #{level}" unless [:major, :minor, :build].include?(level)
version = perform_bump(level)
sh "git commit -am 'Version #{version}'"
sh "git tag #{version}"
end
task :_index_clean? do
raise "Stash changes to the repository before running" unless system("git diff --quiet")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment