Class: ThorSCMVersion::ScmVersion
- Inherits:
-
Object
- Object
- ThorSCMVersion::ScmVersion
- Includes:
- Comparable
- Defined in:
- lib/thor-scmversion/scm_version.rb
Overview
author Josiah Kiehl <[email protected]>
Direct Known Subclasses
Constant Summary collapse
- VERSION_FORMAT =
Tags not matching this format will not show up in the tags list
Examples:
1.2.3 #=> valid 1.2.3.4 #=> invalid 1.2.3-alpha.1 #=> valid 1.2.3-alpha #=> invalid /^(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)-?(?<prerelease>#{Prerelease::FORMAT})?(\+build\.)?(?<build>\d+)?$/- VERSION_FILENAME =
Default file to write the current version to
'VERSION'
Instance Attribute Summary collapse
-
#build ⇒ Object
Returns the value of attribute build.
-
#major ⇒ Object
Returns the value of attribute major.
-
#minor ⇒ Object
Returns the value of attribute minor.
-
#patch ⇒ Object
Returns the value of attribute patch.
-
#prerelease ⇒ Object
Returns the value of attribute prerelease.
Class Method Summary collapse
-
.from_path(path = '.') ⇒ Array<ScmVersion>
Retrieve all versions from the repository contained at path.
-
.from_tag(tag) ⇒ ScmVersion
Create an ScmVersion object from a tag.
-
.retrieve_tags ⇒ Object
In distributed SCMs, tags must be fetched from the server to ensure that the latest tags are being used to calculate the next version.
Instance Method Summary collapse
- #<=>(other) ⇒ Object
-
#auto_bump(options) ⇒ Object
Perform a bump by reading recent commit messages in the SCM Abstract method.
-
#bump!(type, options = {}) ⇒ ScmVersion
Bumps the version in place.
-
#initialize(major = 0, minor = 0, patch = 0, prerelease = nil, build = 1) ⇒ ScmVersion
constructor
A new instance of ScmVersion.
-
#reset_for(type) ⇒ Object
Reset levels lower than the type being reset for.
-
#tag ⇒ Object
Create the tag in the SCM corresponding to the version contained in self.
- #to_s ⇒ Object (also: #version)
-
#write_version(files = [ScmVersion::VERSION_FILENAME]) ⇒ Object
Write the version to the passed in file paths.
Constructor Details
#initialize(major = 0, minor = 0, patch = 0, prerelease = nil, build = 1) ⇒ ScmVersion
Returns a new instance of ScmVersion.
75 76 77 78 79 80 81 |
# File 'lib/thor-scmversion/scm_version.rb', line 75 def initialize(major = 0, minor = 0, patch = 0, prerelease = nil, build = 1) @major = major.to_i @minor = minor.to_i @patch = patch.to_i @prerelease = prerelease @build = build.nil? ? 1 : build.to_i end |
Instance Attribute Details
#build ⇒ Object
Returns the value of attribute build.
73 74 75 |
# File 'lib/thor-scmversion/scm_version.rb', line 73 def build @build end |
#major ⇒ Object
Returns the value of attribute major.
69 70 71 |
# File 'lib/thor-scmversion/scm_version.rb', line 69 def major @major end |
#minor ⇒ Object
Returns the value of attribute minor.
70 71 72 |
# File 'lib/thor-scmversion/scm_version.rb', line 70 def minor @minor end |
#patch ⇒ Object
Returns the value of attribute patch.
71 72 73 |
# File 'lib/thor-scmversion/scm_version.rb', line 71 def patch @patch end |
#prerelease ⇒ Object
Returns the value of attribute prerelease.
72 73 74 |
# File 'lib/thor-scmversion/scm_version.rb', line 72 def prerelease @prerelease end |
Class Method Details
.from_path(path = '.') ⇒ Array<ScmVersion>
Retrieve all versions from the repository contained at path
48 49 50 51 |
# File 'lib/thor-scmversion/scm_version.rb', line 48 def from_path(path = '.') all_from_path(path).first || new(0,0,1) end |
.from_tag(tag) ⇒ ScmVersion
Create an ScmVersion object from a tag
57 58 59 60 |
# File 'lib/thor-scmversion/scm_version.rb', line 57 def from_tag(tag) matchdata = tag.match VERSION_FORMAT new(matchdata[:major], matchdata[:minor], matchdata[:patch], Prerelease.from_string(matchdata[:prerelease]), matchdata[:build]) end |
.retrieve_tags ⇒ Object
In distributed SCMs, tags must be fetched from the server to ensure that the latest tags are being used to calculate the next version.
65 66 67 |
# File 'lib/thor-scmversion/scm_version.rb', line 65 def # noop end |
Instance Method Details
#<=>(other) ⇒ Object
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/thor-scmversion/scm_version.rb', line 176 def <=>(other) return unless other.is_a?(self.class) return 0 if self.version == other.version [:major, :minor, :patch, :prerelease, :build].each do |segment| next if self.send(segment) == other.send(segment) return 1 if self.send(segment) > other.send(segment) return -1 if self.send(segment) < other.send(segment) end return 0 end |
#auto_bump(options) ⇒ Object
Perform a bump by reading recent commit messages in the SCM Abstract method. Must be implemented by subclasses.
164 165 166 |
# File 'lib/thor-scmversion/scm_version.rb', line 164 def auto_bump() raise NotImplementedError end |
#bump!(type, options = {}) ⇒ ScmVersion
Bumps the version in place
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 |
# File 'lib/thor-scmversion/scm_version.rb', line 88 def bump!(type, = {}) case type.to_sym when :auto self.auto_bump() when :major self.major += 1 when :minor self.minor += 1 when :patch self.patch += 1 when :prerelease prerelease_type = [:prerelease_type] if self.prerelease if prerelease_type.nil? || prerelease_type == self.prerelease.type self.prerelease += 1 else self.prerelease = Prerelease.new(prerelease_type) end else self.patch += 1 self.prerelease = Prerelease.new(prerelease_type) end when :build self.build += 1 else raise "Invalid release type: #{type}. Valid types are: major, minor, patch, or auto" end raise "Version: #{self.to_s} is less than or equal to the existing version." if self <= self.class.from_path reset_for type unless type == :auto self end |
#reset_for(type) ⇒ Object
Reset levels lower than the type being reset for
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/thor-scmversion/scm_version.rb', line 123 def reset_for(type) matched = false [[:major, Proc.new { self.minor = 0 }], [:minor, Proc.new { self.patch = 0 }], [:patch, Proc.new { self.prerelease = nil }], [:prerelease, Proc.new { self.build = 1 }]].each do |matcher, reset_proc| next unless matched or type.to_sym == matcher matched = true reset_proc.call end self end |
#tag ⇒ Object
Create the tag in the SCM corresponding to the version contained in self. Abstract method. Must be implemented by subclasses.
158 159 160 |
# File 'lib/thor-scmversion/scm_version.rb', line 158 def tag raise NotImplementedError end |
#to_s ⇒ Object Also known as: version
168 169 170 171 172 173 |
# File 'lib/thor-scmversion/scm_version.rb', line 168 def to_s s = "#{major}.#{minor}.#{patch}" s << "-#{prerelease}" unless prerelease.nil? s << "+build.#{build}" unless build < 2 s end |
#write_version(files = [ScmVersion::VERSION_FILENAME]) ⇒ Object
Write the version to the passed in file paths
147 148 149 150 151 152 153 154 |
# File 'lib/thor-scmversion/scm_version.rb', line 147 def write_version(files = [ScmVersion::VERSION_FILENAME]) files.each do |ver_file| File.open(ver_file, 'w+') do |f| f.write self.to_s end end self end |