Skip to content

Commit

Permalink
Refactor version bumping
Browse files Browse the repository at this point in the history
  • Loading branch information
bibendi committed Oct 17, 2016
1 parent 0c1dbf3 commit c35adb4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
14 changes: 11 additions & 3 deletions bin/apress-gem
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
39 changes: 25 additions & 14 deletions lib/apress/gems/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,25 @@ 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)
contents.gsub!(/VERSION\s*=\s*(['"])(.*?)\1/m, "VERSION = '#{version}'")
File.write(file, contents)
spawn "git add #{file}"
end

spawn "git commit -m 'Release #{version}'"

puts "VERSION updated to #{version}"
end

Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit c35adb4

Please sign in to comment.