Class: NuGetVersions::SemanticVersion

Inherits:
Object
  • Object
show all
Defined in:
lib/nuget_versions/semantic_version.rb

Overview

A strict SemVer implementation Based on: NuGet.Client/NuGet.Core/NuGet.Versioning/SemanticVersion.cs from the NuGet source code.

Direct Known Subclasses

NuGetVersion

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(major, minor, patch, release_labels = nil, metadata = nil) ⇒ SemanticVersion

Creates a SemanticVersion X.Y.Z, X.Y.Z-alpha, X.Y.Z-alpha+metadata. Parameters:

  • major: X.y.z

  • minor: x.Y.z

  • patch: x.y.Z

  • release_labels: Prerelease label

  • metadata: Build metadata



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/nuget_versions/semantic_version.rb', line 19

def initialize(major, minor, patch, release_labels = nil,  = nil)
  @major = Integer(major, major.is_a?(String) ? 10 : 0)
  @minor = Integer(minor, minor.is_a?(String) ? 10 : 0)
  @patch = Integer(patch, patch.is_a?(String) ? 10 : 0)

  if !release_labels.nil?
    release_labels = release_labels.to_s.split('.') if !release_labels.kind_of? Array
    SemanticVersion.validate_identifiers release_labels
    @release_labels = release_labels
  end

  SemanticVersion.validate_identifier  if !.nil?
  @metadata = 
end

Instance Attribute Details

#majorObject

Returns the value of attribute major.



34
35
36
# File 'lib/nuget_versions/semantic_version.rb', line 34

def major
  @major
end

#metadataObject

Returns the value of attribute metadata.



38
39
40
# File 'lib/nuget_versions/semantic_version.rb', line 38

def 
  @metadata
end

#minorObject

Returns the value of attribute minor.



35
36
37
# File 'lib/nuget_versions/semantic_version.rb', line 35

def minor
  @minor
end

#patchObject

Returns the value of attribute patch.



36
37
38
# File 'lib/nuget_versions/semantic_version.rb', line 36

def patch
  @patch
end

#release_labelsObject

Returns the value of attribute release_labels.



37
38
39
# File 'lib/nuget_versions/semantic_version.rb', line 37

def release_labels
  @release_labels
end

Class Method Details

.copy_of(version) ⇒ Object

Creates a SemanticVersion from an existing SemanticVersion Parameters:

  • version: Version to clone.



8
9
10
# File 'lib/nuget_versions/semantic_version.rb', line 8

def self.copy_of(version)
  return SemanticVersion.new(version.major, version.minor, version.patch, version.release_labels, version.)
end

.parse(value) ⇒ Object



121
122
123
124
125
126
# File 'lib/nuget_versions/semantic_version.rb', line 121

def self.parse(value)
  ver = self.try_parse(value)
  raise "Invalid semantic version value" if ver.nil?

  ver
end

.try_parse(value) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/nuget_versions/semantic_version.rb', line 128

def self.try_parse(value)
  return nil if value.nil?

  value = value.to_s if !value.is_a? String

  return nil if value.empty?

  value = value.split("+", 2)

   = (value.length == 2) ? value.last : nil

  value = value.first.split("-", 2)

  return nil if value.empty?

  release_labels = (value.length == 2) ? value.last.split('.') : nil

  parts = value.first.split(".")

  return nil if parts.length != 3
  return nil if release_labels && !SemanticVersion.try_validate_identifiers(release_labels)
  return nil if  && !SemanticVersion.try_validate_identifier()

  begin
    major = Integer(parts[0], 10)
    minor = (parts.length >= 2) ? Integer(parts[1], 10) : 0
    patch = (parts.length >= 3) ? Integer(parts[2], 10) : 0
  rescue
    return nil
  end

  return SemanticVersion.new(major, minor, patch, release_labels, )
end

Instance Method Details

#!=(other) ⇒ Object



97
98
99
# File 'lib/nuget_versions/semantic_version.rb', line 97

def !=(other)
  !(self == other)
end

#<(other) ⇒ Object



101
102
103
# File 'lib/nuget_versions/semantic_version.rb', line 101

def <(other)
  VersionComparer.compare(self, other) == -1
end

#<=(other) ⇒ Object



109
110
111
# File 'lib/nuget_versions/semantic_version.rb', line 109

def <=(other)
  !(other < self)
end

#<=>(other) ⇒ Object



117
118
119
# File 'lib/nuget_versions/semantic_version.rb', line 117

def <=>(other)
  VersionComparer.compare(self, other)
end

#==(other) ⇒ Object



93
94
95
# File 'lib/nuget_versions/semantic_version.rb', line 93

def ==(other)
  VersionComparer.compare(self, other) == 0
end

#>(other) ⇒ Object



105
106
107
# File 'lib/nuget_versions/semantic_version.rb', line 105

def >(other)
  other < self
end

#>=(other) ⇒ Object



113
114
115
# File 'lib/nuget_versions/semantic_version.rb', line 113

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

#has_metadata?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/nuget_versions/semantic_version.rb', line 81

def has_metadata?
  !@metadata.nil? && !@metadata.empty?
end

#is_prerelease?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/nuget_versions/semantic_version.rb', line 77

def is_prerelease?
  !@release_labels.nil? && !@release_labels.all?(&:empty?)
end

#releaseObject



65
66
67
68
# File 'lib/nuget_versions/semantic_version.rb', line 65

def release
  return nil if @release_labels.nil?
  @release_labels.join('.')
end

#release=(new_value) ⇒ Object



70
71
72
73
74
75
# File 'lib/nuget_versions/semantic_version.rb', line 70

def release=(new_value)
  return @release_labels = nil if new_value.nil?
  new_value = new_value.split('.')
  SemanticVersion.validate_identifiers new_value
  @release_labels = new_value
end

#to_sObject



85
86
87
88
89
90
91
# File 'lib/nuget_versions/semantic_version.rb', line 85

def to_s
  version = "#{@major}.#{@minor}.#{@patch}"
  version += "-#{release}" if is_prerelease?
  version += "+#{}" if has_metadata?

  version
end