Class: Blacksmith::VersionHelper::Version

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_blacksmith/version_helper.rb

Overview

See: semver.org

Constant Summary collapse

SemVerRegexp =
/\A(\d+\.\d+\.\d+)(-([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?(\+([0-9A-Za-z-]+(\.[0-9A-Za-z-]+)*))?\Z/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version_str) ⇒ Version

Returns a new instance of Version.

Raises:

  • (ArgumentError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/puppet_blacksmith/version_helper.rb', line 13

def initialize version_str
  raise ArgumentError.new("#{version_str} is not a valid SemVer Version (http://semver.org)") unless version_str =~ SemVerRegexp

  version, parts = version_str.split '-'
  if not parts.nil? and parts.include? '+'
    @pre, @build = parts.split '+'
  elsif version.include? '+'
    version, @build = version.split '+'
  else
    @pre = parts
  end


  @major, @minor, @patch = version.split('.').map(&:to_i)
end

Instance Attribute Details

#buildObject

Returns the value of attribute build.



11
12
13
# File 'lib/puppet_blacksmith/version_helper.rb', line 11

def build
  @build
end

#majorObject

Returns the value of attribute major.



11
12
13
# File 'lib/puppet_blacksmith/version_helper.rb', line 11

def major
  @major
end

#minorObject

Returns the value of attribute minor.



11
12
13
# File 'lib/puppet_blacksmith/version_helper.rb', line 11

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



11
12
13
# File 'lib/puppet_blacksmith/version_helper.rb', line 11

def patch
  @patch
end

#preObject

Returns the value of attribute pre.



11
12
13
# File 'lib/puppet_blacksmith/version_helper.rb', line 11

def pre
  @pre
end

Instance Method Details

#<(other_version) ⇒ Object



69
70
71
# File 'lib/puppet_blacksmith/version_helper.rb', line 69

def < other_version
  (self <=> other_version) == -1
end

#<=(other_version) ⇒ Object



77
78
79
# File 'lib/puppet_blacksmith/version_helper.rb', line 77

def <= other_version
  (self <=> other_version) <= 0
end

#<=>(other_version) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/puppet_blacksmith/version_helper.rb', line 50

def <=> other_version
  other_version = Version.new(other_version) if other_version.is_a? String

  v1 = self.dup
  v2 = other_version.dup

  # The build must be excluded from the comparison, so that e.g. 1.2.3+foo and 1.2.3+bar are semantically equal.
  # "Build metadata SHOULD be ignored when determining version precedence".
  # (SemVer 2.0.0-rc.2, paragraph 10 - http://www.semver.org)
  v1.build = nil
  v2.build = nil

  compare_recursively(v1.to_a, v2.to_a)
end

#==(other_version) ⇒ Object



81
82
83
# File 'lib/puppet_blacksmith/version_helper.rb', line 81

def == other_version
  (self <=> other_version) == 0
end

#>(other_version) ⇒ Object



65
66
67
# File 'lib/puppet_blacksmith/version_helper.rb', line 65

def > other_version
  (self <=> other_version) == 1
end

#>=(other_version) ⇒ Object



73
74
75
# File 'lib/puppet_blacksmith/version_helper.rb', line 73

def >= other_version
  (self <=> other_version) >= 0
end

#full!Object



107
108
109
110
111
112
113
114
# File 'lib/puppet_blacksmith/version_helper.rb', line 107

def full!
  env_var = "BLACKSMITH_FULL_VERSION"
  begin
    ENV.fetch env_var
  rescue KeyError
    raise Exception, "Setting the full version requires setting the #{env_var} environment variable to the new version"
  end
end

#increment!(term) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/puppet_blacksmith/version_helper.rb', line 116

def increment!(term)
  new_version = clone

  if term != :patch || @pre.nil?
    new_version.send("#{term}=", send(term) + 1)
  end

  new_version.minor = 0 if term == :major
  new_version.patch = 0 if term == :major || term == :minor
  new_version.build = new_version.pre = nil

  new_version
end

#satisfies(other_version) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/puppet_blacksmith/version_helper.rb', line 85

def satisfies other_version
  return true if other_version.strip == '*'
  parts = other_version.split(/(\d(.+)?)/, 2)
  comparator, other_version_string = parts[0].strip, parts[1].strip

  begin
    Version.new other_version_string
    comparator.empty? && comparator = '=='
    satisfies_comparator? comparator, other_version_string
  rescue ArgumentError
    if ['<', '>', '<=', '>='].include?(comparator)
      satisfies_comparator? comparator, pad_version_string(other_version_string)
    else
      tilde_matches? other_version_string
    end
  end
end

#to_aObject Also known as: to_array



29
30
31
# File 'lib/puppet_blacksmith/version_helper.rb', line 29

def to_a
  [@major, @minor, @patch, @pre, @build]
end

#to_hObject Also known as: to_hash



41
42
43
44
# File 'lib/puppet_blacksmith/version_helper.rb', line 41

def to_h
  keys = [:major, :minor, :patch, :pre, :build]
  Hash[keys.zip(self.to_a)]
end

#to_sObject Also known as: to_string



33
34
35
36
37
38
39
# File 'lib/puppet_blacksmith/version_helper.rb', line 33

def to_s
  str = [@major, @minor, @patch].join '.'
  str << '-' << @pre unless @pre.nil?
  str << '+' << @build unless @build.nil?

  str
end