Skip to content

Instantly share code, notes, and snippets.

@ngsctt
Last active May 30, 2020 08:35
Show Gist options
  • Save ngsctt/a3a40787d31ea7bc5e0429206cc34f1e to your computer and use it in GitHub Desktop.
Save ngsctt/a3a40787d31ea7bc5e0429206cc34f1e to your computer and use it in GitHub Desktop.
namespace "version" do
path = 'lib/gem-name/version.rb'
content = File.read(path)
regex = /(VERSION\s*=\s*['"])([0-9.]+)(['"])/
match = content.match(regex)
version = match[2]
vparts = version.split('.')
major = vparts[0] ? Integer(vparts[0]) : 0
minor = vparts[1] ? Integer(vparts[1]) : 0
patch = vparts[2] ? Integer(vparts[2]) : 0
def git_commit (version, path)
@target = false
@Gemfile = false
if !`git status --porcelain #{path}`.match(/^[?]{2}/)
@target = true
end
if !`git status --porcelain Gemfile.lock`.match(/^[?]{2}/)
@Gemfile = true
end
`git commit -m '#{version}' -o #{@target ? path : ""} #{@Gemfile ? "Gemfile.lock" : ""}`
`git tag -a 'v#{version}' -m '#{version}'`
end
task :major do
new_version = "#{major + 1}.0.0"
File.write(path, content.sub(regex, "\\1#{new_version}\\3"))
git_commit(new_version, path)
puts "Version bumped from #{version} to #{new_version}"
end
task :minor do
new_version = "#{major}.#{minor + 1}.0"
File.write(path, content.sub(regex, "\\1#{new_version}\\3"))
git_commit(new_version, path)
puts "Version bumped from #{version} to #{new_version}"
end
task :patch do
new_version = "#{major}.#{minor}.#{patch + 1}"
File.write(path, content.sub(regex, "\\1#{new_version}\\3"))
git_commit(new_version, path)
puts "Version bumped from #{version} to #{new_version}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment