implement bump file

This commit is contained in:
Petr Korolev 2014-11-10 13:11:35 +02:00
parent 737774a164
commit b908b07e0a

View File

@ -3,7 +3,11 @@ require 'optparse'
SPEC_TYPE = 'gemspec' SPEC_TYPE = 'gemspec'
@options = {:dry_run => false, :major => false, :minor => false, :patch => true} :major
:minor
:patch
@options = {:dry_run => false, :bump_number => :patch}
OptionParser.new { |opts| OptionParser.new { |opts|
opts.banner = 'Usage: bump.rb [options]' opts.banner = 'Usage: bump.rb [options]'
@ -12,13 +16,13 @@ OptionParser.new { |opts|
@options[:dry_run] = v @options[:dry_run] = v
end end
opts.on('-a', '--major', 'Bump major version') do |v| opts.on('-a', '--major', 'Bump major version') do |v|
@options[:major] = v @options[:bump_number] = :major
end end
opts.on('-m', '--minor', 'Bump minor version') do |v| opts.on('-m', '--minor', 'Bump minor version') do |v|
@options[:minor] = v @options[:bump_number] = :minor
end end
opts.on('-p', '--patch', 'Bump patch version') do |v| opts.on('-p', '--patch', 'Bump patch version') do |v|
@options[:patch] = v @options[:bump_number] = :patch
end end
}.parse! }.parse!
@ -39,14 +43,6 @@ def check_repo_is_clean_or_dry_run
end end
end end
def execute_line(line)
output = `#{line}`
if $?.exitstatus != 0
puts "Output:\n#{output}\nExit status = #{$?.exitstatus} ->Terminate script."
end
output
end
def find_spec_file def find_spec_file
list_of_scpecs = execute_line("find . -name '*.#{SPEC_TYPE}'") list_of_scpecs = execute_line("find . -name '*.#{SPEC_TYPE}'")
@ -77,8 +73,9 @@ def find_spec_file
end end
def find_version_in_podspec(podspec) def find_version_in_podspec(podspec)
readme = File.read(#{podspec}) readme = File.read(podspec)
#try to find version in format 1.22.333
re = /(\d+)\.(\d+)\.(\d+)/m re = /(\d+)\.(\d+)\.(\d+)/m
match_result = re.match(readme) match_result = re.match(readme)
@ -89,78 +86,88 @@ def find_version_in_podspec(podspec)
end end
puts "Found version #{match_result[0]}" puts "Found version #{match_result[0]}"
p match_result[0], match_result.captures return match_result[0], match_result.captures
exit
# return match_result[0], match_result.captures
end end
def bump_version(result_array) def bump_version(versions_array)
bumped_result = result_array.dup bumped_result = versions_array.dup
bumped_result.map! { |x| x.to_i } bumped_result.map! { |x| x.to_i }
if @options[:major] case @options[:bump_number]
when :major
bumped_result[0] += 1 bumped_result[0] += 1
bumped_result[1] = 0 bumped_result[1] = 0
bumped_result[2] = 0 bumped_result[2] = 0
else when :minor
if @options[:minor]
bumped_result[1] += 1 bumped_result[1] += 1
bumped_result[2] = 0 bumped_result[2] = 0
else when :patch
if @options[:patch]
bumped_result[2] += 1 bumped_result[2] += 1
else
raise('unknown bump_number')
end end
end
end
bumped_version = bumped_result.join('.') bumped_version = bumped_result.join('.')
puts "Bump version: #{result_array.join('.')} -> #{bumped_version}" puts "Bump version: #{versions_array.join('.')} -> #{bumped_version}"
bumped_version bumped_version
end end
def execute_line(line)
output = `#{line}`
check_exit_status
output
end
def execute_line_if_not_dry_run(line) def execute_line_if_not_dry_run(line)
if @options[:dry_run] if @options[:dry_run]
puts "Dry run:\n#{line}" puts "Dry run: #{line}"
nil
else else
puts line puts line
value = %x[#{line}] value = %x[#{line}]
puts value puts value
if $?.exitstatus != 0 check_exit_status
puts "Error (exit status = #{$?} -> exit" value
exit
end
end end
end end
def check_exit_status
if $?.exitstatus != 0
puts "Output:\n#{output}\nExit status = #{$?.exitstatus} ->Terminate script."
exit
end
end
def run_bumping_script def run_bumping_script
check_repo_is_clean_or_dry_run check_repo_is_clean_or_dry_run
result, result_array = find_version_in_podspec(find_spec_file) spec_file = find_spec_file
bumped_version = bump_version(result_array) result, versions_array = find_version_in_podspec(spec_file)
bumped_version = bump_version(versions_array)
unless @options[:dry_run] unless @options[:dry_run]
puts 'Are you sure? Click Y to continue:' puts 'Are you sure? Click Y to continue:'
str = gets.chomp str = gets.chomp
if str != 'Y' if str != 'Y'
puts '-> exit' puts '-> exit'
exit exit
end end
end end
execute_line_if_not_dry_run("sed -i \"\" \"s/#{result}/#{bumped_version}/\" README.md") execute_line_if_not_dry_run("sed -i \"\" \"s/#{result}/#{bumped_version}/\" README.md")
execute_line_if_not_dry_run("sed -i \"\" \"s/#{result}/#{bumped_version}/\" ActionSheetPicker-3.0.podspec") execute_line_if_not_dry_run("sed -i \"\" \"s/#{result}/#{bumped_version}/\" #{spec_file}")
execute_line_if_not_dry_run("git commit --all -m \"Update #{$SPEC_TYPE} to version #{bumped_version}\"") 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 tag #{bumped_version}")
execute_line_if_not_dry_run('git push') # execute_line_if_not_dry_run('git push')
execute_line_if_not_dry_run('git push --tags') # execute_line_if_not_dry_run('git push --tags')
execute_line_if_not_dry_run('pod trunk push ./ActionSheetPicker-3.0.podspec') # execute_line_if_not_dry_run("pod trunk push #{spec_file}")
end end
if __FILE__ == $0 if __FILE__ == $0
result, result_array = find_version_in_podspec(find_spec_file) run_bumping_script
end end