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
# File 'lib/puppet_blacksmith/version_helper.rb', line 13

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

  version, parts = version_str.split '-'
  if !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) ⇒ Object



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

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

#<=(other) ⇒ Object



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

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

#<=>(other) ⇒ Object



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

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

  v1 = dup
  v2 = other.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) ⇒ Object



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

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

#>(other) ⇒ Object



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

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

#>=(other) ⇒ Object



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

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

#full!Object



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

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



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

def increment!(term)
  new_version = clone

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

  new_version.minor = 0 if term == :major
  new_version.patch = 0 if i[major minor].include?(term)
  new_version.build = new_version.pre = nil

  new_version
end

#satisfies(other_version) ⇒ Object



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

def satisfies(other_version)
  return true if other_version.strip == '*'

  parts = other_version.split(/(\d(.+)?)/, 2)
  comparator = parts[0].strip
  other_version_string = 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



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

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

#to_hObject Also known as: to_hash



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

def to_h
  keys = i[major minor patch pre build]
  keys.zip(to_a).to_h
end

#to_sObject Also known as: to_string



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

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

  str
end