github-changelog-generator/bump_gemfile.rb

222 lines
4.9 KiB
Ruby
Raw Normal View History

2014-11-10 07:10:39 +00:00
#!/usr/bin/env ruby
require "optparse"
2014-11-10 07:10:39 +00:00
SPEC_TYPE = "gemspec"
2014-11-10 08:08:32 +00:00
2014-11-10 11:11:35 +00:00
:major
:minor
:patch
2015-03-26 13:43:47 +00:00
@options = { dry_run: false, bump_number: :patch }
2014-11-10 07:10:39 +00:00
OptionParser.new { |opts|
opts.banner = "Usage: bump.rb [options]"
2014-11-10 07:10:39 +00:00
opts.on("-d", "--dry-run", "Dry run") do |v|
2014-11-10 07:10:39 +00:00
@options[:dry_run] = v
end
opts.on("-a", "--major", "Bump major version") do |_v|
2014-11-10 11:11:35 +00:00
@options[:bump_number] = :major
2014-11-10 07:10:39 +00:00
end
opts.on("-m", "--minor", "Bump minor version") do |_v|
2014-11-10 11:11:35 +00:00
@options[:bump_number] = :minor
2014-11-10 07:10:39 +00:00
end
opts.on("-p", "--patch", "Bump patch version") do |_v|
2014-11-10 11:11:35 +00:00
@options[:bump_number] = :patch
2014-11-10 07:10:39 +00:00
end
opts.on("-r", "--revert", "Revert last bump") do |v|
2014-11-10 11:23:23 +00:00
@options[:revert] = v
end
2014-11-10 07:10:39 +00:00
}.parse!
p @options
def check_repo_is_clean_or_dry_run
value = `#{"git status --porcelain"}`
2014-11-10 07:10:39 +00:00
if value.empty?
puts "Repo is clean -> continue"
2014-11-10 07:10:39 +00:00
else
if @options[:dry_run]
puts 'Repo not clean, "Dry run" enabled -> continue'
else
puts "Repository not clean -> exit"
2014-11-10 07:10:39 +00:00
exit
end
end
end
2014-11-10 08:08:32 +00:00
def find_spec_file
2014-11-10 11:35:20 +00:00
list_of_specs = execute_line("find . -name '*.#{SPEC_TYPE}'")
arr = list_of_specs.split("\n")
spec_file = ""
2014-11-10 11:35:20 +00:00
case arr.count
when 0
puts "No #{SPEC_TYPE} files found. -> Exit."
exit
when 1
spec_file = arr[0]
else
puts "Which spec should be used?"
2015-03-26 13:43:47 +00:00
arr.each_with_index { |file, index| puts "#{index + 1}. #{file}" }
2014-11-10 11:35:20 +00:00
input_index = Integer(gets.chomp)
2015-03-26 13:43:47 +00:00
spec_file = arr[input_index - 1]
2014-11-10 11:35:20 +00:00
end
2015-03-26 13:43:47 +00:00
if spec_file.nil?
2014-11-10 11:35:20 +00:00
puts "Can't find specified spec file -> exit"
exit
end
spec_file.sub("./", "")
2014-11-10 11:35:20 +00:00
end
def find_current_gem_file
list_of_specs = execute_line("find . -name '*.gem'")
arr = list_of_specs.split("\n")
2014-11-10 08:08:32 +00:00
spec_file = ""
2014-11-10 08:08:32 +00:00
case arr.count
when 0
puts "No #{SPEC_TYPE} files found. -> Exit."
exit
when 1
spec_file = arr[0]
else
puts "Which spec should be used?"
2015-03-26 13:43:47 +00:00
arr.each_with_index { |file, index| puts "#{index + 1}. #{file}" }
2014-11-10 08:08:32 +00:00
input_index = Integer(gets.chomp)
2015-03-26 13:43:47 +00:00
spec_file = arr[input_index - 1]
2014-11-10 08:08:32 +00:00
end
2015-03-26 13:43:47 +00:00
if spec_file.nil?
2014-11-10 08:08:32 +00:00
puts "Can't find specified spec file -> exit"
exit
end
spec_file.sub("./", "")
2014-11-10 08:08:32 +00:00
end
def find_version_in_podspec(podspec)
2014-11-10 11:11:35 +00:00
readme = File.read(podspec)
2014-11-10 07:10:39 +00:00
2015-03-26 13:43:47 +00:00
# try to find version in format 1.22.333
2014-11-10 07:10:39 +00:00
re = /(\d+)\.(\d+)\.(\d+)/m
match_result = re.match(readme)
unless match_result
puts "Not found any versions"
2014-11-10 07:10:39 +00:00
exit
end
puts "Found version #{match_result[0]}"
2015-03-26 13:43:47 +00:00
[match_result[0], match_result.captures]
2014-11-10 07:10:39 +00:00
end
2014-11-10 11:11:35 +00:00
def bump_version(versions_array)
bumped_result = versions_array.dup
2015-03-26 13:43:47 +00:00
bumped_result.map!(&:to_i)
2014-11-10 07:10:39 +00:00
2014-11-10 11:11:35 +00:00
case @options[:bump_number]
when :major
bumped_result[0] += 1
bumped_result[1] = 0
bumped_result[2] = 0
when :minor
2014-11-10 07:10:39 +00:00
bumped_result[1] += 1
bumped_result[2] = 0
2014-11-10 11:11:35 +00:00
when :patch
bumped_result[2] += 1
2014-11-10 07:10:39 +00:00
else
fail("unknown bump_number")
2014-11-10 07:10:39 +00:00
end
2014-11-10 11:11:35 +00:00
bumped_version = bumped_result.join(".")
2014-11-10 11:11:35 +00:00
puts "Bump version: #{versions_array.join('.')} -> #{bumped_version}"
2014-11-10 07:10:39 +00:00
bumped_version
end
2014-11-10 11:11:35 +00:00
def execute_line(line)
output = `#{line}`
2014-11-10 11:23:23 +00:00
check_exit_status(output)
2014-11-10 11:11:35 +00:00
output
end
2014-11-10 08:08:32 +00:00
def execute_line_if_not_dry_run(line)
2014-11-10 07:10:39 +00:00
if @options[:dry_run]
2014-11-10 11:11:35 +00:00
puts "Dry run: #{line}"
nil
2014-11-10 07:10:39 +00:00
else
puts line
2015-03-26 13:43:47 +00:00
value = `#{line}`
2014-11-10 07:10:39 +00:00
puts value
2014-11-10 11:23:23 +00:00
check_exit_status(value)
2014-11-10 11:11:35 +00:00
value
2014-11-10 07:10:39 +00:00
end
end
2014-11-10 11:23:23 +00:00
def check_exit_status(output)
2015-03-26 13:43:47 +00:00
if $CHILD_STATUS.exitstatus != 0
puts "Output:\n#{output}\nExit status = #{$CHILD_STATUS.exitstatus} ->Terminate script."
2014-11-10 11:11:35 +00:00
exit
end
end
2014-11-10 07:10:39 +00:00
2014-11-10 08:08:32 +00:00
def run_bumping_script
check_repo_is_clean_or_dry_run
2014-11-10 11:11:35 +00:00
spec_file = find_spec_file
result, versions_array = find_version_in_podspec(spec_file)
bumped_version = bump_version(versions_array)
2014-11-10 08:08:32 +00:00
unless @options[:dry_run]
puts "Are you sure? Press Y to continue:"
2014-11-10 08:08:32 +00:00
str = gets.chomp
if str != "Y"
puts "-> exit"
2014-11-10 08:08:32 +00:00
exit
end
2014-11-10 07:10:39 +00:00
end
2014-11-10 08:08:32 +00:00
execute_line_if_not_dry_run("sed -i \"\" \"s/#{result}/#{bumped_version}/\" README.md")
2014-11-10 11:11:35 +00:00
execute_line_if_not_dry_run("sed -i \"\" \"s/#{result}/#{bumped_version}/\" #{spec_file}")
2014-11-10 08:08:32 +00:00
execute_line_if_not_dry_run("git commit --all -m \"Update #{$SPEC_TYPE} to version #{bumped_version}\"")
execute_line_if_not_dry_run("git tag #{bumped_version}")
execute_line_if_not_dry_run("git push")
execute_line_if_not_dry_run("git push --tags")
2014-11-10 11:35:20 +00:00
execute_line_if_not_dry_run("gem build #{spec_file}")
gem = find_current_gem_file
execute_line_if_not_dry_run("gem push #{gem}")
2014-11-10 11:11:35 +00:00
# execute_line_if_not_dry_run("pod trunk push #{spec_file}")
2014-11-10 07:10:39 +00:00
end
2014-11-10 11:23:23 +00:00
def revert_last_bump
spec_file = find_spec_file
2014-11-10 11:35:20 +00:00
result, _ = find_version_in_podspec(spec_file)
2014-11-10 12:39:31 +00:00
puts "DELETE tag #{result} and HARD reset HEAD~1?\nPress Y to continue:"
2014-11-10 11:23:23 +00:00
str = gets.chomp
if str != "Y"
puts "-> exit"
2014-11-10 11:23:23 +00:00
exit
end
2014-11-10 11:35:20 +00:00
execute_line_if_not_dry_run("git tag -d #{result}")
execute_line_if_not_dry_run("git reset --hard HEAD~1")
2014-11-10 11:35:20 +00:00
execute_line_if_not_dry_run("git push --delete origin #{result}")
2014-11-10 11:23:23 +00:00
end
2015-03-26 13:43:47 +00:00
if __FILE__ == $PROGRAM_NAME
2014-11-10 07:10:39 +00:00
2014-11-10 11:23:23 +00:00
if @options[:revert]
revert_last_bump
else
run_bumping_script
end
2014-11-10 11:11:35 +00:00
2014-11-10 08:08:32 +00:00
end