From c35adb4ea8ccd4108693fe21f14b5cb133d4f80a Mon Sep 17 00:00:00 2001 From: Michail Merkushin Date: Mon, 17 Oct 2016 23:52:40 +0500 Subject: [PATCH] Refactor version bumping --- bin/apress-gem | 14 +++++++++++--- lib/apress/gems/cli.rb | 39 +++++++++++++++++++++++++-------------- 2 files changed, 36 insertions(+), 17 deletions(-) diff --git a/bin/apress-gem b/bin/apress-gem index c809d52..86734c2 100755 --- a/bin/apress-gem +++ b/bin/apress-gem @@ -13,10 +13,10 @@ opt_parser = OptionParser.new do |opt| opt.separator 'Commands' opt.separator ' release: changelog, version, tag, build and upload' opt.separator ' changelog: generate CHANGELOG.md' - opt.separator ' update_version: update const VERSION in lin/**/version.rb' + opt.separator ' bump: update const VERSION in lin/**/version.rb' opt.separator ' build: make gem package' opt.separator ' upload: upload package to gems.railsc.ru' - opt.separator ' current_version: show current gem version' + opt.separator ' current: show current gem version' opt.separator '' opt.separator 'Options' @@ -32,13 +32,21 @@ opt_parser = OptionParser.new do |opt| opt.on('-b', '--branch BRANCH', 'branch, default master') do |value| options[:branch] = value end + + opt.on('-r', '--remote NAME', 'remote server name, default upstream') do |value| + options[:remote] = value + end + + opt.on('-B', '--no-bump', 'no update version in version.rb') do + options[:bump] = false + end end opt_parser.parse! cli = Apress::Gems::Cli.new(options) -if %w(release changelog build upload tag current_version update_version).include?(ARGV[0]) +if %w(release changelog build upload tag current bump).include?(ARGV[0]) cli.public_send(ARGV[0]) else puts opt_parser diff --git a/lib/apress/gems/cli.rb b/lib/apress/gems/cli.rb index 718b6df..61f0335 100644 --- a/lib/apress/gems/cli.rb +++ b/lib/apress/gems/cli.rb @@ -16,12 +16,15 @@ def initialize(options) def changelog Apress::ChangeLogger.new.log_changes + spawn 'git add CHANGELOG.md' + spawn "git commit -m 'Update CHANGELOG.md" + puts 'CHANGELOG.md generated' end - def update_version - return if version == '0.0.1' + def bump + validate_version Dir['lib/**/version.rb'].each do |file| contents = File.read(file) @@ -29,6 +32,9 @@ def update_version File.write(file, contents) spawn "git add #{file}" end + + spawn "git commit -m 'Release #{version}'" + puts "VERSION updated to #{version}" end @@ -49,23 +55,23 @@ def upload def tag tag_name = "v#{version}" + spawn "git tag -a -m \"Version #{version}\" #{tag_name}" - spawn 'git push --tags upstream' + puts "Git tag generated to #{tag_name}" end - def current_version + def current puts "Current version is #{find_version}" end def release - validate_version check_git + bump if @options.fetch(:bump, true) changelog - update_version - commit tag + push build upload end @@ -80,6 +86,10 @@ def branch @branch ||= @options.fetch(:branch, 'master') end + def remote + @remote ||= @options.fetch(:remote, 'upstream') + end + def find_version Dir['lib/**/version.rb'].each do |file| contents = File.read(file) @@ -95,15 +105,16 @@ def upload_uri def check_git `git rev-parse --abbrev-ref HEAD`.chomp.strip == branch || abort("Can be released only from `#{branch}` branch") - `git remote | grep upstream`.chomp.strip == 'upstream' || abort('Can be released only with `upstream` remote') - spawn "git pull upstream #{branch}" - spawn 'git fetch --tags upstream' + `git remote | grep #{remote}`.chomp.strip == remote || abort("Can be released only with `#{remote}` remote") + spawn "git pull #{remote} #{branch}" + spawn "git fetch --tags #{remote}" end - def commit - puts 'Commit and push changes' - spawn "git diff --cached --exit-code > /dev/null || git commit -m \"Release #{version}\" || echo -n" - spawn "git push upstream #{branch}" + def push + puts 'Push changes to repository' + + spawn "git push #{remote} #{branch}" + spawn "git push --tags #{remote}" end def validate_version