brew/Library/Homebrew/pkg_version.rb

66 lines
1.4 KiB
Ruby
Raw Normal View History

# typed: strict
# frozen_string_literal: true
require "version"
2020-08-17 20:08:45 +02:00
# Combination of a version and a revision.
class PkgVersion
include Comparable
extend Forwardable
REGEX = /\A(.+?)(?:_(\d+))?\z/
2020-08-17 20:08:45 +02:00
private_constant :REGEX
sig { returns(Version) }
attr_reader :version
sig { returns(Integer) }
attr_reader :revision
2016-07-13 10:11:59 +03:00
2020-08-17 20:08:45 +02:00
delegate [:major, :minor, :patch, :major_minor, :major_minor_patch] => :version
sig { params(path: String).returns(PkgVersion) }
def self.parse(path)
2020-08-17 20:08:45 +02:00
_, version, revision = *path.match(REGEX)
version = Version.new(version.to_s)
new(version, revision.to_i)
end
sig { params(version: Version, revision: Integer).void }
def initialize(version, revision)
@version = T.let(version, Version)
@revision = T.let(revision, Integer)
end
sig { returns(T::Boolean) }
def head?
version.head?
end
2024-04-26 14:04:55 +02:00
sig { returns(String) }
def to_str
2017-09-24 20:12:58 +01:00
if revision.positive?
"#{version}_#{revision}"
else
version.to_s
end
end
2024-04-26 14:04:55 +02:00
sig { returns(String) }
def to_s = to_str
sig { params(other: PkgVersion).returns(T.nilable(Integer)) }
def <=>(other)
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? ==
sig { returns(Integer) }
def hash
2022-04-23 01:48:15 +01:00
[version, revision].hash
end
end