class Semvergen::Bump

Constants

MAJOR
MINOR
PATCH

Public Class Methods

new(interface, version_file, node_version_file, change_log_file, shell, gem_name, gem_server, notifier) click to toggle source
# File lib/semvergen/bump.rb, line 13
def initialize(interface, version_file, node_version_file, change_log_file, shell, gem_name, gem_server, notifier)
  @interface = interface
  @version_file = version_file
  @node_version_file = node_version_file
  @change_log_file = change_log_file
  @shell = shell
  @gem_name = gem_name
  @gem_server = gem_server
  @notifier = notifier
end

Public Instance Methods

next_version(current_version, release_type, release_types) click to toggle source
# File lib/semvergen/bump.rb, line 156
def next_version(current_version, release_type, release_types)
  version_tuples = current_version.split(".")

  release_index = 2 - release_types.index(release_type)

  bumping   = version_tuples[release_index]
  unchanged = version_tuples[0...release_index]
  zeroing   = version_tuples[(release_index + 1)..-1]

  new_version_tuples = unchanged + [bumping.to_i + 1] + (["0"] * zeroing.size)

  new_version_tuples.join(".")
end
run!(options) click to toggle source
# File lib/semvergen/bump.rb, line 24
def run!(options)
  master_release = @shell.current_branch == "master"
  unless master_release
    say color("You are not on master. It is not recommended to create releases from a branch unless they're maintenance releases", :red)
    newline
    return unless agree("Proceed anyway? ")
    newline
  end

  unless @shell.git_branch_is_tracking?
    @interface.fail_exit color("This branch is not tracking a remote branch. Aborting...", :red)
  end

  say "Checking for upstream changes..."
  @shell.git_fetch
  newline

  unless @shell.git_up_to_date?
    @interface.fail_exit color("This branch is not up to date with upstream", :red)
  end

  say "Git status: #{color("Up to date", :green)}"
  newline

  if @shell.git_index_dirty? && !options[:ignore_dirty]
    say color("Git index dirty. Commit changes before continuing", :red, :bold)
  else
    say color("Cut new #{@gem_name} Release", :white, :underline, :bold)

    newline

    # Only patches allowed for maintenance releases
    release_types = master_release ? [PATCH, MINOR, MAJOR] : [PATCH]

    release_type = choose do |menu|
      menu.header    = "Select release type"
      menu.default   = "1"
      menu.select_by = :index
      menu.choices *release_types
    end

    new_version = next_version(@version_file.version, release_type, release_types)

    newline

    say "Current version: #{color(@version_file.version, :bold)}"
    say "Bumped version : #{color(new_version, :bold, :green)}"

    newline

    say "Enter change log features (or a blank line to finish):"

    features = []

    while true
      response = ask "* " do |q|
        q.validate                 = lambda { |answer| features.size > 0 || answer.length > 0 }
        q.responses[:not_valid]    = color("Enter at least one feature", :red)
        q.responses[:invalid_type] = color("Enter at least one feature", :red)
        q.responses[:ask_on_error] = "* "
      end

      if response.length == 0
        features << "\n"
        break
      else
        features << "* #{response}"
      end
    end

    change_log_lines   = ["# #{new_version} - Release date: #{Time.now.strftime("%Y-%m-%d")}"] + features
    change_log_message = change_log_lines.join("\n")
    diff_change_log    = change_log_lines.map { |l| color("+++ ", :white) + color(l, :green) }.join("\n")

    newline

    say color("Will add the following to CHANGELOG.md", :underline)
    say color(diff_change_log)

    commit_message = "Version bump: #{new_version}"

    newline

    say color("Summary of actions:", :underline, :green, :red)
    newline

    say "Bumping version: #{color(@version_file.version, :yellow)} -> #{color(new_version, :green)}"
    newline

    say "Adding features to CHANGELOG.md:"
    say color(diff_change_log, :green)

    say "Staging files for commit:"
    say color("* #{@version_file.path}", :green)
    say color("* CHANGELOG.md", :green)
    newline

    say "Committing with message: #{color(commit_message, :green)}"
    newline

    if agree("Proceed? ")
      @version_file.version = new_version

      node_version_file_path = nil
      if @node_version_file
        @node_version_file.version = new_version
        node_version_file_path = @node_version_file.path
      end

      @change_log_file << change_log_message

      @shell.commit(@version_file.path, node_version_file_path, new_version, commit_message, features)

      @shell.push(new_version)

      newline
    end

    Semvergen::Release.new(
      @interface,
      @version_file,
      @node_version_file,
      @change_log_file,
      @shell,
      @gem_name,
      @gem_server,
      @notifier
    ).run!(features: features)

  end
end