Class: Raketeer::BumpTask

Inherits:
Rake::TaskLib
  • Object
show all
Defined in:
lib/raketeer/bump_task.rb

Overview

Author:

  • Jonathan Bradley Whited

Since:

  • 0.2.4

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = :bump) {|_self| ... } ⇒ BumpTask

Returns a new instance of BumpTask.

Yields:

  • (_self)

Yield Parameters:

Since:

  • 0.2.4



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/raketeer/bump_task.rb', line 41

def initialize(name=:bump)
  super()

  @bump_bundle = false
  @bump_files = Rake::FileList[]
  @bundle_cmd = 'bundle'
  @changelogs = Rake::FileList['CHANGELOG.md']
  @dry_run = false
  @git_msg = "git commit -m 'Bump version to v%{version}'"
  @name = name
  @ruby_files = Rake::FileList[File.join('lib','**','version.rb')]
  @ruby_var = 'VERSION'
  @strict = false

  yield self if block_given?
  define
end

Instance Attribute Details

#bump_bundleObject Also known as: bump_bundle?

Since:

  • 0.2.4



26
27
28
# File 'lib/raketeer/bump_task.rb', line 26

def bump_bundle
  @bump_bundle
end

#bump_filesObject

Looks for a version number

Since:

  • 0.2.4



27
28
29
# File 'lib/raketeer/bump_task.rb', line 27

def bump_files
  @bump_files
end

#bundle_cmdObject

Since:

  • 0.2.4



28
29
30
# File 'lib/raketeer/bump_task.rb', line 28

def bundle_cmd
  @bundle_cmd
end

#changelogsObject

Looks for ‘Unreleased’ & a Markdown header

Since:

  • 0.2.4



29
30
31
# File 'lib/raketeer/bump_task.rb', line 29

def changelogs
  @changelogs
end

#dry_runObject Also known as: dry_run?

Since:

  • 0.2.4



30
31
32
# File 'lib/raketeer/bump_task.rb', line 30

def dry_run
  @dry_run
end

#git_msgObject

Since:

  • 0.2.4



31
32
33
# File 'lib/raketeer/bump_task.rb', line 31

def git_msg
  @git_msg
end

#nameObject

Since:

  • 0.2.4



32
33
34
# File 'lib/raketeer/bump_task.rb', line 32

def name
  @name
end

#ruby_filesObject

Looks for #ruby_var

Since:

  • 0.2.4



33
34
35
# File 'lib/raketeer/bump_task.rb', line 33

def ruby_files
  @ruby_files
end

#ruby_varObject

Since:

  • 0.2.4



34
35
36
# File 'lib/raketeer/bump_task.rb', line 34

def ruby_var
  @ruby_var
end

#strictObject Also known as: strict?

Since:

  • 0.2.4



35
36
37
# File 'lib/raketeer/bump_task.rb', line 35

def strict
  @strict
end

Instance Method Details

#bump_all(bump_ver) ⇒ Object

Since:

  • 0.2.4



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
# File 'lib/raketeer/bump_task.rb', line 128

def bump_all(bump_ver)
  check_env

  sem_vers = []

  # Order matters for outputting the most accurate version
  sem_vers << bump_ruby_files(bump_ver)
  sem_vers << bump_bump_files(bump_ver)
  sem_vers << bump_changelogs(bump_ver)

  sem_vers.compact!

  if sem_vers.empty?
    puts '! No versions found'

    return
  end

  if @bump_bundle && !bump_ver.empty?
    bump_bundle_file
  end

  # Always output it, in case the user just wants to see what the git message
  # should be without making changes.
  if !@git_msg.nil?
    puts '[Git]:'
    puts '= ' + (@git_msg % {version: sem_vers[0].to_s})
  end
end

#bump_bump_files(bump_ver) ⇒ Object

Since:

  • 0.2.4



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

def bump_bump_files(bump_ver)
  return nil if @bump_files.empty?

  bumper = FilesBumper.new(@bump_files,bump_ver,@dry_run,@strict)

  bumper.bump_files do
    next if bumper.changes > 0 || !bumper.sem_ver.nil?
    next if bumper.line !~ SemVer.regex(@strict)

    break if bumper.bump_line! != :no_ver && bumper.bump_ver_empty?
  end

  return bumper.version
end

#bump_bundle_fileObject

Since:

  • 0.2.4



173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/raketeer/bump_task.rb', line 173

def bump_bundle_file
  sh_cmd = [@bundle_cmd,'list']

  puts "[#{sh_cmd.join(' ')}]:"

  if @dry_run
    puts '= Nothing written (dry run)'

    return
  end

  sh(*sh_cmd,verbose: false)
end

#bump_changelogs(bump_ver) ⇒ Object

See Also:

Since:

  • 0.2.4



188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/raketeer/bump_task.rb', line 188

def bump_changelogs(bump_ver)
  return nil if @changelogs.empty?

  bumper = FilesBumper.new(@changelogs,bump_ver,@dry_run,@strict) do
    @header_bumped = false
    @unreleased_bumped = false
  end

  header_regex = /\A(\s*##\s*\[+\D*)(#{SemVer.regex(@strict)})(.*)\z/m
  unreleased_regex = /\A.*Unreleased.*http.*\.{3}/

  bumper.bump_files do
    if @header_bumped && @unreleased_bumped
      break if bumper.bump_ver_empty?
      next
    end

    if bumper.line =~ unreleased_regex
      next if @unreleased_bumped

      # Match from the end, in case the URL has a number (like '%20')
      match = bumper.line.match(/(#{SemVer.regex(@strict)})(\.{3}.*)\z/m)

      next if match.nil? || match.length < 3 || (i = match.begin(0)) < 1

      match = [bumper.line[0..i - 1],match[1],match[-1]]

      next if match.any? {|m| m.nil? || m.strip.empty? }

      orig_line = bumper.line.dup
      bumper.line = match[1]

      if (result = bumper.bump_line!(add_change: false)) != :no_ver
        @unreleased_bumped = true

        if result == :same_ver
          bumper.line = orig_line

          next
        end

        bumper.line = match[0] << bumper.line << match[2]

        bumper.add_change(bumper.line,push: false)
      else
        bumper.line = orig_line
      end
    elsif !(match = bumper.line.match(header_regex)).nil?
      next if @header_bumped || match.length < 4

      match = [match[1],match[2],match[-1]]

      next if match.any? {|m| m.nil? || m.strip.empty? }

      orig_line = bumper.line.dup
      bumper.line = match[1]

      if (result = bumper.bump_line!(add_change: false)) != :no_ver
        @header_bumped = true

        if result == :same_ver
          bumper.line = orig_line

          next
        end

        bumper.line = (match[0] << bumper.line)

        # Replace the date with today's date for the new Markdown header, if it exists
        match[2].sub!(/\d+\s*\-\s*\d+\s*\-\s*\d+(.*)\z/m,"#{Date.today.strftime('%F')}\\1")

        # Fix the link if there is one:
        #   https://github.com/esotericpig/raketeer/compare/v0.2.10...v0.2.11
        versions_regex = /
          (?<beg_ver>#{SemVer.regex(@strict)})
          (?<sep>\.{3}[^\d\s]{,11}) # 11 for 'v', 'version', or something else.
          (?<end_ver>#{SemVer.regex(@strict)})
        /xmi
        versions_match = match[2].match(versions_regex)

        if versions_match
          match[2].sub!(versions_regex,
            "#{versions_match[:end_ver]}" \
            "#{versions_match[:sep]}" \
            "#{bumper.sem_ver}"
          )
        end

        bumper.line << match[2]

        bumper.add_change(bumper.line,push: true)

        # Add after add_change(), so not printed to console.
        bumper.line << "\n\n"
      end

      # We are adding a new Markdown header, so always set the line back to its original value
      bumper.line = orig_line
    end
  end

  return bumper.version
end

#bump_ruby_files(bump_ver) ⇒ Object

Since:

  • 0.2.4



292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# File 'lib/raketeer/bump_task.rb', line 292

def bump_ruby_files(bump_ver)
  return nil if @ruby_files.empty?

  bumper = FilesBumper.new(@ruby_files,bump_ver,@dry_run,@strict)
  version_var_regex = /\A(\s*#{Regexp.quote(@ruby_var)}\s*=\D*)(#{SemVer.regex(@strict)})(.*)\z/m

  bumper.bump_files do
    next if bumper.changes > 0 || !bumper.sem_ver.nil?
    next if (match = bumper.line.match(version_var_regex)).nil?
    next if match.length < 4
    next if match[1..2].any? {|m| m.nil? || m.strip.empty? }

    orig_line = bumper.line.dup
    bumper.line = match[2]

    if (result = bumper.bump_line!(add_change: false)) != :no_ver
      if result == :same_ver
        bumper.line = orig_line

        break if bumper.bump_ver_empty?
        next
      end

      bumper.line = match[1] << bumper.line
      bumper.line << match[-1] unless match[-1].nil?

      bumper.add_change(bumper.line,push: false)
    else
      bumper.line = orig_line
    end
  end

  return bumper.version
end

#check_envObject

Since:

  • 0.2.4



327
328
329
330
# File 'lib/raketeer/bump_task.rb', line 327

def check_env
  @dry_run = Util.get_env_bool('dryrun',@dry_run)
  @strict = Util.get_env_bool('strict',@strict)
end

#defineObject

Since:

  • 0.2.4



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
# File 'lib/raketeer/bump_task.rb', line 59

def define
  desc 'Show/Set/Bump the version'
  task @name,[:version] do |task,args|
    bump_all(BumpVer.new(
      version: args.version,
      major: ENV['major'],
      minor: ENV['minor'],
      patch: ENV['patch'],
      prerelease: ENV['pre'],
      build_meta: ENV['build']
    ))
  end

  namespace @name do
    desc 'Bump/Set the major version'
    task :major,[:major] do |task,args|
      bump_ver = BumpVer.new(major: args.major)

      # You can't erase the major version (required)
      bump_ver.major = '+1' if bump_ver.major.nil? || bump_ver.major.empty?

      bump_all(bump_ver)
    end

    desc 'Bump/Set the minor version'
    task :minor,[:minor] do |task,args|
      bump_ver = BumpVer.new(minor: args.minor)
      bump_ver.minor = '+1' if bump_ver.minor.nil?

      bump_all(bump_ver)
    end

    desc 'Bump/Set the patch version'
    task :patch,[:patch] do |task,args|
      bump_ver = BumpVer.new(patch: args.patch)
      bump_ver.patch = '+1' if bump_ver.patch.nil?

      bump_all(bump_ver)
    end

    desc 'Set/Erase the pre-release version'
    task :pre,[:pre] do |task,args|
      bump_ver = BumpVer.new(prerelease: args.pre)
      bump_ver.prerelease = '' if bump_ver.prerelease.nil?

      bump_all(bump_ver)
    end

    desc 'Set/Erase the build metadata'
    task :build,[:build] do |task,args|
      bump_ver = BumpVer.new(build_meta: args.build)
      bump_ver.build_meta = '' if bump_ver.build_meta.nil?

      bump_all(bump_ver)
    end

    desc 'Bump the Gemfile.lock version'
    task :bundle do
      check_env
      bump_bundle_file
    end

    desc "Show the help/usage for #{name} tasks"
    task :help do
      print_help
    end
  end
end

Since:

  • 0.2.4



332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/raketeer/bump_task.rb', line 332

def print_help
  puts <<-HELP
rake #{@name}  # Print the current version

# You can run a dry run for any task (will not write to files)
rake #{@name} dryrun=true

rake #{@name}[1.2.3-alpha.4-beta.5]       # Set the version manually
rake #{@name} major=1 minor=2 patch=3     # Set the version numbers
rake #{@name} pre=alpha.4 build=beta.5    # Set the version extensions
rake #{@name} major=+1 minor=+1 patch=+1  # Bump the version numbers by 1
rake #{@name} major=+2 minor=+3 patch=+4  # Bump the version numbers by X

rake #{@name}:major          # Bump the major version by 1
rake #{@name}:major[1]       # Set the major version to 1
rake #{@name}:major[+2]      # Bump the major version by 2
rake #{@name}:minor          # Bump the minor version by 1
rake #{@name}:minor[2]       # Set the minor version to 2
rake #{@name}:minor[+3]      # Bump the minor version by 3
rake #{@name}:patch          # Bump the patch version by 1
rake #{@name}:patch[3]       # Set the patch version to 3
rake #{@name}:patch[+4]      # Bump the patch version by 4
rake #{@name}:pre            # Erase the pre-release version
rake #{@name}:pre[alpha.4]   # Set the pre-release version
rake #{@name}:build          # Erase the build metadata
rake #{@name}:build[beta.5]  # Set the build metadata
rake #{@name}:bundle         # Bump the Gemfile.lock version
  HELP
end