Module: VersionBump

Defined in:
lib/tasks/helpers/version_bump.rb

Overview

This file is distributed under New Relic’s license terms. See github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details. frozen_string_literal: true

Constant Summary collapse

MAJOR =
0
MINOR =
1
TINY =
2
VERSION =
{major: MAJOR, minor: MINOR, tiny: TINY}

Class Method Summary collapse

Class Method Details

.determine_bump_typeObject

Determined version based on if changelog has a feature or not for version



46
47
48
49
50
51
52
53
# File 'lib/tasks/helpers/version_bump.rb', line 46

def self.determine_bump_type
  file = read_file('CHANGELOG.md')
  lines = file.split('## ')[1].split('- **')
  return MAJOR if lines.first.include?('Major version')
  return MINOR if lines.any? { |line| line.include?('Feature:') }

  TINY
end

.read_file(path) ⇒ Object



37
38
39
# File 'lib/tasks/helpers/version_bump.rb', line 37

def self.read_file(path)
  File.read(File.expand_path(path))
end

.update_changelog(version) ⇒ Object

Replace dev with version number in changelog



56
57
58
59
60
61
# File 'lib/tasks/helpers/version_bump.rb', line 56

def self.update_changelog(version)
  file = read_file('CHANGELOG.md')
  file.gsub!('## dev', "## v#{version}")
  file.gsub!('Version <dev>', "Version #{version}")
  write_file('CHANGELOG.md', file)
end

.update_versionObject

Updates version.rb with new version number



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tasks/helpers/version_bump.rb', line 12

def self.update_version
  bump_type = determine_bump_type
  file = read_file('lib/new_relic/version.rb')
  new_version = {}

  VERSION.each do |key, current|
    file.gsub!(/(#{key.to_s.upcase} = )(\d+)/) do
      match = Regexp.last_match

      new_version[key] = if bump_type == current # bump type, increase by 1
        match[2].to_i + 1
      elsif bump_type < current # right of bump type, goes to 0
        0
      else # left of bump type, stays the same
        match[2].to_i
      end

      match[1] + new_version[key].to_s
    end
  end

  write_file('lib/new_relic/version.rb', file)
  new_version.values.join('.')
end

.write_file(path, file) ⇒ Object



41
42
43
# File 'lib/tasks/helpers/version_bump.rb', line 41

def self.write_file(path, file)
  File.write(File.expand_path(path), file)
end