Sha256: 4c406d9b9f46c34580a523faa8abce2a7cf2af4ecaddb19d5269b3368ce56482

Contents?: true

Size: 1.54 KB

Versions: 7

Compression:

Stored size: 1.54 KB

Contents

require 'anvil/task'
require 'anvil/versioner'

class Gem::BumpTask < Anvil::Task
  description "Bumps a gem's version"

  parser do
    arguments %w(term)

    on('-p', '--persist', 'Commit tag and push the changes') do |p|
      options[:persist] = true
    end

    on('-f', '--force', 'Act even if the git repo is not clean') do |f|
      options[:force] = true
    end
  end

  attr_reader :term

  def initialize(term, options = {})
    @term = term.to_sym
    @options = options
  end

  def task
    prepare_repo
    version = bump(read_version)
    write_version version

    version
  end

  protected

  def git
    @git ||= Git.open Dir.pwd
  end

  def version_file(mode = 'r')
    File.open('VERSION', mode) do |f|
      yield f
    end
  end

  def read_version
    version_file { |f| f.read.strip }
  end

  def bump(old_version)
    new_version = Anvil::Versioner.new(old_version).bump! term
    logger.info "Bumped from #{old_version} to #{new_version}"

    new_version
  end

  def commit_and_tag(version)
    git.add 'VERSION'
    git.commit "Bump version v#{version}"
    git.add_tag "v#{version}"
    git.push
  end

  def write_version(version)
    version_file('w+') do |f|
      f.puts version
      f.close
    end

    commit_and_tag version if options[:persist]
  end

  def prepare_repo
    if clean? || force?
      git.pull
    else
      fail Anvil::RepoNotClean
    end
  end

  def clean?
    git.status.changed.empty? &&
      git.status.deleted.empty? &&
      git.status.added.empty?
  end

  def force?
    options[:force]
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
anvil-core-0.4.2 lib/tasks/gem/bump_task.rb
anvil-core-0.4.1 lib/tasks/gem/bump_task.rb
anvil-core-0.4.0 lib/tasks/gem/bump_task.rb
anvil-core-0.3.2 lib/tasks/gem/bump_task.rb
anvil-core-0.3.1 lib/tasks/gem/bump_task.rb
anvil-core-0.3.0 lib/tasks/gem/bump_task.rb
anvil-core-0.2.0 lib/tasks/gem/bump_task.rb