Class: Semvergen::Bump

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/semvergen/bump.rb

Constant Summary collapse

PATCH =
"Patch: Bug fixes, recommended for all (default)"
MINOR =
"Minor: New features, but backwards compatible"
MAJOR =
"Major: Breaking changes"
RELEASE_TYPES =
[
  PATCH,
  MINOR,
  MAJOR
]

Instance Method Summary collapse

Constructor Details

#initialize(interface, version_file, change_log_file, shell, gem_name, gem_server, notifier) ⇒ Bump

Returns a new instance of Bump.



19
20
21
22
23
24
25
26
27
# File 'lib/semvergen/bump.rb', line 19

def initialize(interface, version_file, change_log_file, shell, gem_name, gem_server, notifier)
  @interface = interface
  @version_file = version_file
  @change_log_file = change_log_file
  @shell = shell
  @gem_name = gem_name
  @gem_server = gem_server
  @notifier = notifier
end

Instance Method Details

#next_version(current_version, release_type) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/semvergen/bump.rb', line 159

def next_version(current_version, release_type)
  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) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/semvergen/bump.rb', line 29

def run!(options)
  unless @shell.current_branch == "master"
    say color("You are not on master. It is not recommended to create releases from a branch", :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

    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)

    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}"] + 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 = ask("Git commit subject line: ") do |q|
      q.validate                 = /.{10,}/
      q.responses[:not_valid]    = color("Message must be more than 10 chars", :red)
      q.responses[:invalid_type] = color("Message must be more than 10 chars", :red)
    end

    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("* lib/quattro/version.rb", :green)
    say color("* CHANGELOG.md", :green)
    newline

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

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

      @change_log_file << change_log_message

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

      @shell.push(new_version)

      newline
    end

    if agree("Release? ")
      say "Found gemspec: #{color(@gem_name, :green)}"
      newline

      say color("Building gem: ")
      @shell.build_gem(@gem_name)
      say color("OK", :green, :bold)

      say color("Publishing: ")
      @shell.publish(@gem_name, @version_file.version, @gem_server)
      say color("OK", :green, :bold)

      @notifier.gem_published(@gem_name, new_version, change_log_message)
    end

  end
end