Class: Reissue::VersionUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/reissue/version_updater.rb

Constant Summary collapse

VERSION_MATCH =

Regular expression pattern for matching the version string.

Returns:

  • (Regexp)

    The version regex pattern.

/(?<major>\d+)\.(?<minor>[a-zA-Z\d]+)\.(?<patch>[a-zA-Z\d]+)(?<add>\.(?<pre>[a-zA-Z\d]+))?/
RELEASE_VERSION_MATCH =
/VERSION\s*=\s*"([^"]*)"/
RELEASE_DATE_MATCH =
/RELEASE_DATE\s*=\s*"([^"]*)"/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_file, version_redo_proc: nil) ⇒ VersionUpdater

Initializes a new instance of the VersionUpdater class.

Parameters:

  • version_file (String)

    The path to the version file.



59
60
61
62
63
64
65
# File 'lib/reissue/version_updater.rb', line 59

def initialize(version_file, version_redo_proc: nil)
  @version_file = version_file
  @original_version = nil
  @new_version = nil
  @updated_body = ""
  @version_redo_proc = version_redo_proc
end

Instance Attribute Details

#version_redo_procObject

A proc that can be used to redo the version string.



90
91
92
# File 'lib/reissue/version_updater.rb', line 90

def version_redo_proc
  @version_redo_proc
end

Instance Method Details

#call(segment, version_file: @version_file) ⇒ String

Updates the version segment and writes the updated version to the file.

This allows you to read from one file and write to another.

Parameters:

  • segment (Symbol)

    The segment to update (:major, :minor, or :patch).

  • version_file (String) (defaults to: @version_file)

    The version_file to the version file (optional, defaults to @version_file).

Returns:

  • (String)

    The updated version string.



74
75
76
77
78
# File 'lib/reissue/version_updater.rb', line 74

def call(segment, version_file: @version_file)
  update(segment)
  write(version_file)
  @new_version
end

#redo(version, segment) ⇒ Object

Creates a new version string based on the original version and the specified segment.



93
94
95
96
97
98
99
# File 'lib/reissue/version_updater.rb', line 93

def redo(version, segment)
  if version_redo_proc
    version_redo_proc.call(version, segment)
  else
    version.redo(segment)
  end
end

#release_date_regexObject



124
# File 'lib/reissue/version_updater.rb', line 124

def release_date_regex = RELEASE_DATE_MATCH

#release_version_regexObject



123
# File 'lib/reissue/version_updater.rb', line 123

def release_version_regex = RELEASE_VERSION_MATCH

#set_version(version_string, version_file: @version_file) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/reissue/version_updater.rb', line 80

def set_version(version_string, version_file: @version_file)
  body = File.read(@version_file)
  updated = body.gsub(release_version_regex) do |match|
    match.sub(/=\s*"[^"]*"/, "= \"#{version_string}\"")
  end
  File.write(version_file, updated)
  version_string
end

#update(segment) ⇒ String

Updates the specified segment of the version string.

Parameters:

  • segment (Symbol)

    The segment to update (:major, :minor, or :patch).

Returns:

  • (String)

    The updated version string.



105
106
107
108
109
110
111
112
# File 'lib/reissue/version_updater.rb', line 105

def update(segment)
  version_file = File.read(@version_file)
  @updated_body = version_file.gsub(version_regex) do |string|
    @original_version = ::Gem::Version.new(string)
    @new_version = self.redo(::Gem::Version.new(string), segment).to_s
  end
  @new_version
end

#update_release_date(date, version_file: @version_file) ⇒ Object



126
127
128
129
130
131
132
133
# File 'lib/reissue/version_updater.rb', line 126

def update_release_date(date, version_file: @version_file)
  body = File.read(@version_file)
  return unless body.match?(release_date_regex)
  updated = body.gsub(release_date_regex) do |match|
    match.sub(/=\s*"[^"]*"/, "= \"#{date}\"")
  end
  File.write(version_file, updated)
end

#version_regexObject



118
# File 'lib/reissue/version_updater.rb', line 118

def version_regex = VERSION_MATCH

#write(version_file = @version_file) ⇒ Object

Writes the updated version to the specified file.

Parameters:

  • version_file (String) (defaults to: @version_file)

    The version_file to the version file (optional, defaults to @version_file).



138
139
140
# File 'lib/reissue/version_updater.rb', line 138

def write(version_file = @version_file)
  File.write(version_file, @updated_body)
end