Class: ThorSCMVersion::ScmVersion

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/thor-scmversion/scm_version.rb

Overview

author Josiah Kiehl [email protected]

Direct Known Subclasses

GitVersion, P4Version

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major = 0, minor = 0, patch = 0, prerelease = nil, build = 1) ⇒ ScmVersion

Returns a new instance of ScmVersion.



63
64
65
66
67
68
69
# File 'lib/thor-scmversion/scm_version.rb', line 63

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.



61
62
63
# File 'lib/thor-scmversion/scm_version.rb', line 61

def build
  @build
end

#major ⇒ Object

Returns the value of attribute major.



57
58
59
# File 'lib/thor-scmversion/scm_version.rb', line 57

def major
  @major
end

#minor ⇒ Object

Returns the value of attribute minor.



58
59
60
# File 'lib/thor-scmversion/scm_version.rb', line 58

def minor
  @minor
end

#patch ⇒ Object

Returns the value of attribute patch.



59
60
61
# File 'lib/thor-scmversion/scm_version.rb', line 59

def patch
  @patch
end

#prerelease ⇒ Object

Returns the value of attribute prerelease.



60
61
62
# File 'lib/thor-scmversion/scm_version.rb', line 60

def prerelease
  @prerelease
end

Class Method Details

.from_path(path = '.') ⇒ Array<ScmVersion>

Retrieve all versions from the repository contained at path

Parameters:

  • path (String) (defaults to: '.') —

    Path to the repository

Returns:



36
37
38
39
# File 'lib/thor-scmversion/scm_version.rb', line 36

def from_path(path = '.')
  retrieve_tags
  all_from_path(path).first || new(0,0,1)
end

.from_tag(tag) ⇒ ScmVersion

Create an ScmVersion object from a tag

Parameters:

  • tag (String)

Returns:



45
46
47
48
# File 'lib/thor-scmversion/scm_version.rb', line 45

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.



53
54
55
# File 'lib/thor-scmversion/scm_version.rb', line 53

def retrieve_tags
  # noop
end

Instance Method Details

#<=>(other) ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
# File 'lib/thor-scmversion/scm_version.rb', line 164

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.

Raises:

  • (NotImplementedError)


152
153
154
# File 'lib/thor-scmversion/scm_version.rb', line 152

def auto_bump(options)
  raise NotImplementedError
end

#bump!(type, options = {}) ⇒ ScmVersion

Bumps the version in place

Parameters:

  • type (Symbol) —

    Type of bump to be performed

  • prerelease_type (String) —

    Type of prerelease to bump to when doing a :prerelease bump

Returns:



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
# File 'lib/thor-scmversion/scm_version.rb', line 76

def bump!(type, options = {})
  case type.to_sym
  when :auto
    self.auto_bump(options)
  when :major
    self.major += 1        
  when :minor
    self.minor += 1
  when :patch
    self.patch += 1
  when :prerelease
    prerelease_type = options[: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

Parameters:

  • type (Symbol) —

    Type under which all segments are to be reset



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/thor-scmversion/scm_version.rb', line 111

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.

Raises:

  • (NotImplementedError)


146
147
148
# File 'lib/thor-scmversion/scm_version.rb', line 146

def tag
  raise NotImplementedError
end

#to_s ⇒ Object Also known as: version



156
157
158
159
160
161
# File 'lib/thor-scmversion/scm_version.rb', line 156

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

Parameters:

  • files (Array<String>) (defaults to: [ScmVersion::VERSION_FILENAME]) —

    List of files to write



135
136
137
138
139
140
141
142
# File 'lib/thor-scmversion/scm_version.rb', line 135

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