Skip to content

Instantly share code, notes, and snippets.

@narath
Created November 28, 2013 16:35
Show Gist options
  • Save narath/7694671 to your computer and use it in GitHub Desktop.
Save narath/7694671 to your computer and use it in GitHub Desktop.
Easier releases with git and git flow
# Create a file in config/initializers/version.rb, with the following string in it
# APP_VERSION = "0.0.0"
#
# Add this file to lib/tasks
#
# Add the following to ~/.bash_profile
# function release() {
# version=`bundle exec rake version:next`
# git flow release start $version && bundle exec rake version:bump && git commit -a -m "version bump" && git flow release finish $version && git push origin master
# }
#
# Now when you are on "develop" and are ready to prep a release, just do:
# release
namespace :version do
EXAMPLE_VERSION = "0.0.1"
desc "Get the current version"
task :current do
puts version_to_str(read_version)
end
desc "Get the next release version"
task :next do
puts version_to_str(next_release)
end
desc "Bumps the release (as in Major.Minor.Release)"
task :bump do
version = next_release
write_version(version)
puts "The new version is #{version_to_str(version)}"
puts "Written to #{version_filename}"
end
def version_filename
result = File.expand_path(File.join(File.dirname(__FILE__), '../../config/initializers/version.rb'))
raise "Could not find version file: was looking for #{result}" if !File.exists?(result)
result
end
def read_version
file_str = IO::read(version_filename)
if file_str=~/APP_VERSION\s*=\s*"([^"]*)"/
version_str = $1
versions = version_str.split('.')
major = 0
minor = 0
release = 0
major = versions[0].to_i if versions.count>=1
minor = versions[1].to_i if versions.count>=2
release = versions[2].to_i if versions.count>=3
else
raise "No version information in file #{version_filename}. Add something like this to the file:\n#{EXAMPLE_VERSION}"
end
{ :major => major, :minor => minor, :release => release }
end
def write_version(version={})
file_str = IO::read(version_filename)
if file_str=~/APP_VERSION\s*=\s*"([^"]*)"/
new_file_str = file_str.gsub(/APP_VERSION\s*=\s*"([^"]*)"/, "APP_VERSION = \"#{version[:major]}.#{version[:minor]}.#{version[:release]}\"")
else
new_file_str = "#{file_str}\nAPP_VERSION = \"#{version[:major]}.#{version[:minor]}.#{version[:release]}\""
end
open(version_filename, 'w') { |f| f.puts new_file_str }
end
def next_release
version = read_version
version[:release] += 1
version
end
def version_to_str(version)
"#{version[:major]}.#{version[:minor]}.#{version[:release]}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment