Skip to content

Instantly share code, notes, and snippets.

@kminiatures
Created August 24, 2017 03:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kminiatures/aab679431312680746f7cf7dfb69971c to your computer and use it in GitHub Desktop.
Save kminiatures/aab679431312680746f7cf7dfb69971c to your computer and use it in GitHub Desktop.
bump version script. 1.2.3 => 1.2.4
#!/usr/bin/env ruby
require 'optparse'
begin
OPTS = {unit: 'patch'}
opt = OptionParser.new
units = %w{major minor patch}
opt.on('-u [UNIT]', '--unit', units) {|v| OPTS[:unit] = v}
opt.parse!(ARGV)
if ARGV.count != 1
raise "引数はバージョン番号だけにしてください"
end
version = ARGV[0]
unit = OPTS[:unit]
unless units.include? unit
raise "unit は #{units.join("|")} を指定してください"
end
unless version.match /\A[0-9]+\.[0-9]+\.[0-9]\z/
raise "バージョン番号は 12.34.56 の形式で指定してください"
end
major, minor, patch = version.split "."
case unit
when 'major'
major = major.to_i + 1
minor = 0
patch = 0
when 'minor'
minor = minor.to_i + 1
patch = 0
when 'patch'
patch = patch.to_i + 1
end
puts "#{major}.#{minor}.#{patch}"
rescue => e
puts "Error: #{e.message}"
puts "Example: bump_version.rb -u minor 1.2.3"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment