brew/Library/Homebrew/pkg_version.rb

59 lines
1.0 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "version"
class PkgVersion
include Comparable
extend Forwardable
2018-11-02 17:18:07 +00:00
RX = /\A(.+?)(?:_(\d+))?\z/.freeze
2016-07-13 10:11:59 +03:00
attr_reader :version, :revision
delegate [ # rubocop:disable Layout/HashAlignment
:major,
:minor,
:patch,
:major_minor,
:major_minor_patch,
] => :version
def self.parse(path)
_, version, revision = *path.match(RX)
version = Version.create(version)
new(version, revision.to_i)
end
def initialize(version, revision)
@version = version
@revision = revision
end
def head?
version.head?
end
def to_s
2017-09-24 20:12:58 +01:00
if revision.positive?
"#{version}_#{revision}"
else
version.to_s
end
end
2016-09-23 18:13:48 +02:00
alias to_str to_s
def <=>(other)
2016-09-20 22:03:08 +02:00
return unless other.is_a?(PkgVersion)
2018-09-17 02:45:00 +02:00
2020-03-04 11:28:28 +00:00
version_comparison = (version <=> other.version)
return if version_comparison.nil?
2020-03-04 11:28:28 +00:00
version_comparison.nonzero? || revision <=> other.revision
end
2016-09-23 18:13:48 +02:00
alias eql? ==
def hash
version.hash ^ revision.hash
end
end