Class: Builderator::Control::Version

Inherits:
Object
  • Object
show all
Includes:
Auto, Bump, Comparable
Defined in:
lib/builderator/control/version.rb,
lib/builderator/control/version/git.rb,
lib/builderator/control/version/scm.rb,
lib/builderator/control/version/auto.rb,
lib/builderator/control/version/bump.rb,
lib/builderator/control/version/comparable.rb

Overview

:nodoc:

Defined Under Namespace

Modules: Auto, Bump, Comparable, Git, SCM

Constant Summary collapse

FORMAT =
/(?<major>\d+)\.(?<minor>\d+)\.(?<patch>\d+)(?<prerelease>-(?<prerelease_name>[A-Za-z0-9]+)\.(?<prerelease_iteration>\d+))?(?:\+build\.(?<build>\d+))?$/
DEFAULT_PRERELEASE_NAME =
'alpha'.freeze
RELEASE_TYPES =

Order of precedence for release types

{
  'major' => 0,
  'major-prerelease' => 5,
  'minor' => 10,
  'minor-prerelease' => 15,
  'patch' => 20,
  'patch-prerelease' => 25,
  'release' => 30,
  'prerelease' => 35,
  'build' => 40
}

Constants included from Auto

Auto::DEFAULT_TYPE, Auto::MESSAGE_KEYWORDS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Comparable

#<=>

Methods included from Bump

#bump

Methods included from Auto

#auto_type

Constructor Details

#initialize(major, minor, patch, build = nil, **options) ⇒ Version

Returns a new instance of Version.



83
84
85
86
87
88
89
90
# File 'lib/builderator/control/version.rb', line 83

def initialize(major, minor, patch, build = nil, **options)
  @major = major.to_i
  @minor = minor.to_i
  @patch = patch.to_i
  @build = build.to_i unless build.nil?

  @ref = options[:ref]
end

Instance Attribute Details

#buildObject

Returns the value of attribute build.



106
107
108
# File 'lib/builderator/control/version.rb', line 106

def build
  @build
end

#is_prereleaseObject

Returns the value of attribute is_prerelease.



102
103
104
# File 'lib/builderator/control/version.rb', line 102

def is_prerelease
  @is_prerelease
end

#majorObject

Returns the value of attribute major.



98
99
100
# File 'lib/builderator/control/version.rb', line 98

def major
  @major
end

#minorObject

Returns the value of attribute minor.



99
100
101
# File 'lib/builderator/control/version.rb', line 99

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



100
101
102
# File 'lib/builderator/control/version.rb', line 100

def patch
  @patch
end

#prerelease_iterationObject

Returns the value of attribute prerelease_iteration.



104
105
106
# File 'lib/builderator/control/version.rb', line 104

def prerelease_iteration
  @prerelease_iteration
end

#prerelease_nameObject

Returns the value of attribute prerelease_name.



103
104
105
# File 'lib/builderator/control/version.rb', line 103

def prerelease_name
  @prerelease_name
end

#refObject

Returns the value of attribute ref.



96
97
98
# File 'lib/builderator/control/version.rb', line 96

def ref
  @ref
end

Class Method Details

.bump(type = nil, prerelease_name = nil) ⇒ Object

Alias ‘bump` to the current version



59
60
61
62
63
64
65
66
# File 'lib/builderator/control/version.rb', line 59

def bump(type = nil, prerelease_name = nil)
  @current = current.clone

  current.bump(type, prerelease_name)
  SCM.tags << current

  current
end

.currentObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/builderator/control/version.rb', line 33

def current
  @current ||= SCM.tags.last

  if @current.nil? && Util.relative_path('VERSION').exist?
    @current = Version.from_string(Util.relative_path('VERSION').read)
  end

  if @current.nil?
    fail 'No current version found! Create a VERSION file or set a version tag in your SCM.'
  end

  @current
end

.from_string(arg, options = {}) ⇒ Object

Parse a SemVer string into a Version



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/builderator/control/version.rb', line 69

def from_string(arg, options = {})
  matchdata = arg.match(FORMAT)
  return nil if matchdata.nil?

  new(matchdata[:major], matchdata[:minor], matchdata[:patch], matchdata[:build], options).tap do |version|
    version.is_prerelease = !matchdata[:prerelease].nil?
    if version.is_prerelease
      version.prerelease_name = matchdata[:prerelease_name]
      version.prerelease_iteration = matchdata[:prerelease_iteration].to_i
    end
  end
end

.set_config_versionObject



47
48
49
50
# File 'lib/builderator/control/version.rb', line 47

def set_config_version
  Config.defaults.version = current.to_s
  Config.recompile
end

.writeObject



52
53
54
# File 'lib/builderator/control/version.rb', line 52

def write
  current.write
end

Instance Method Details

#prerelease(name = nil) ⇒ Object

Create or bump a new prerelease train



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/builderator/control/version.rb', line 109

def prerelease(name = nil)
  self.build = nil ## Reset the build counter

  ## Increment current prerelease train
  if is_prerelease && (name.nil? || name == prerelease_name)
    self.prerelease_iteration += 1
    return self
  end

  ## New prerelease train
  self.is_prerelease = true
  self.prerelease_name = name.nil? ? DEFAULT_PRERELEASE_NAME : name
  self.prerelease_iteration = 0

  self
end

#to_sObject



130
131
132
133
134
135
# File 'lib/builderator/control/version.rb', line 130

def to_s
  string = [major, minor, patch].join('.')
  string << "-#{prerelease_name}.#{prerelease_iteration}" if is_prerelease
  string << "+build.#{build}" unless build.nil?
  string
end

#writeObject



126
127
128
# File 'lib/builderator/control/version.rb', line 126

def write
  Util.relative_path('VERSION').write(to_s)
end