Last active
June 10, 2024 07:46
-
-
Save zdk/7b478309dfe2ee8b3408 to your computer and use it in GitHub Desktop.
Rake task for Git based Rails app semantic versioning.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#gem 'colorize' | |
module Version | |
VERSION_FILE = "#{Rails.root}/config/initializers/version.rb" | |
PATTERN = /(\d+)\.(\d+)\.(\d+)-(.*)/ | |
PATTERN_NOMETA = /(\d+\.\d+\.\d+)/ | |
@@major = 0; @@minor = 0; @@patch = 0; @@build = 'a' | |
@@version_rb = File.read(VERSION_FILE) | |
def self.version_rb | |
@@version_rb || "" | |
end | |
def self.set(major, minor, patch, build) | |
@@major, @@minor, @@patch, @@build = major.to_i, minor.to_i, patch.to_i, build.to_s | |
end | |
def self.increment_major | |
@@major += 1 | |
end | |
def self.increment_minor | |
@@minor += 1 | |
end | |
def self.increment_patch | |
@@patch += 1 | |
end | |
def self.semantic | |
"#{@@major}.#{@@minor}.#{@@patch}-#{@@build}" | |
end | |
def self.semantic_short | |
"#{@@major}.#{@@minor}.#{@@patch}" | |
end | |
def self.valid? version | |
self::PATTERN =~ version | |
end | |
def self.current_version | |
self.version_rb.split('=')[1].tr("\'\" ",'').strip | |
end | |
end | |
module Repo | |
class Cmd | |
def perform_and_parse cmd | |
parse `#{cmd}` | |
end | |
def parse result | |
result[1..-1].strip if result =~ /^\w+/ #Specific logic | |
end | |
end | |
def self.version | |
cmd = Cmd.new | |
cmd.perform_and_parse( 'git describe --always --tags' ) | |
end | |
end | |
def describe version | |
return version if version and Version.valid? version | |
Repo.version | |
end | |
def check_version | |
raise "Please update version file or tag.".red if Version::PATTERN_NOMETA.match(Version.current_version)[1] != Version::PATTERN_NOMETA.match(Repo.version)[1] | |
end | |
def version_info | |
ver = describe Version.current_version | |
semver = Version::PATTERN.match( ver ) | |
Version.set(semver[1], semver[2], semver[3], semver[4]) | |
end | |
def bump args | |
check_version | |
version_info | |
args.to_hash.each { |k,v| Version.class_eval("increment_#{k}") unless v.empty? } | |
bumped_version = "0.0.0" | |
STDOUT.puts "Updating to version: #{Version.semantic.green}, Is it the correct version? (y/n)" | |
input = STDIN.gets.chomp | |
if input.downcase == 'y' | |
File.open Version::VERSION_FILE, 'w' do |file| | |
file.write "VERSION='#{Version.semantic}'" | |
bumped_version = Version.semantic | |
puts "File #{Version::VERSION_FILE.red} updated!" | |
end | |
else | |
puts "Bye.".green | |
end | |
bumped_version | |
end | |
desc "Prints the current application version" | |
task :version do | |
puts <<HELP | |
Available commands are: | |
----------------------- | |
rake version:bump[major, minor, patch] # Update patch version, run: rake version:bump[,,patch] | |
rake version:create_tag[version] # Create v0.8.1 tag, run: rake version:create_tag[0.8.1] | |
rake version:tag # Get latest git tag version, run: rake version:tag | |
HELP | |
msg = Version.current_version ? "Current version is: #{Version.current_version.green}" : "There is no version set in #{VERSION_FILE}" | |
puts msg | |
end | |
namespace :version do | |
desc "Bump version" | |
task :bump, [:major, :minor, :patch] do |task, args| | |
bumped_version = bump args | |
if ( bumped_version > Repo.version ) | |
STDOUT.puts 'Good idea to create new git tag too now? (y/n)' | |
input = STDIN.gets.chomp | |
Rake::Task['version:create_tag'].invoke(Version.semantic_short) if input.downcase == 'y' | |
end | |
end | |
desc "Get the latest tag version" | |
task :tag do | |
puts "Version from git tag: #{git_describe_tag.green}" | |
end | |
desc "Create a new git tag" | |
task :create_tag, [:version] do |task, args| | |
`git tag v#{args[:version]}` | |
puts "Tag: " + "v#{args[:version]}".green + " created!" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment