Merge pull request #10376 from SeekingMeaning/livecheck-multi-version

livecheck: split cask versions into sub-versions
This commit is contained in:
Seeker 2021-01-25 10:08:27 -08:00 committed by GitHub
commit a308c6da73
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 107 additions and 17 deletions

View File

@ -2,6 +2,7 @@
# frozen_string_literal: true
require "livecheck/error"
require "livecheck/livecheck_version"
require "livecheck/skip_conditions"
require "livecheck/strategy"
require "ruby-progressbar"
@ -156,7 +157,7 @@ module Homebrew
end
current_str = current.to_s
current = actual_version(formula_or_cask, current)
current = LivecheckVersion.create(formula_or_cask, current)
latest = if formula&.head_only?
formula.head.downloader.fetch_last_commit
@ -182,7 +183,7 @@ module Homebrew
end
latest_str = latest.to_s
latest = actual_version(formula_or_cask, latest)
latest = LivecheckVersion.create(formula_or_cask, latest)
is_outdated = if formula&.head_only?
# A HEAD-only formula is considered outdated if the latest upstream
@ -546,7 +547,7 @@ module Homebrew
next if match_version_map.blank?
version_info = {
latest: Version.new(match_version_map.values.max_by { |v| actual_version(formula_or_cask, v) }),
latest: Version.new(match_version_map.values.max_by { |v| LivecheckVersion.create(formula_or_cask, v) }),
}
if json && verbose
@ -570,17 +571,5 @@ module Homebrew
nil
end
sig { params(formula_or_cask: T.any(Formula, Cask::Cask), version: Version).returns(Version) }
def actual_version(formula_or_cask, version)
case formula_or_cask
when Formula
version
when Cask::Cask
Version.new(Cask::DSL::Version.new(version.to_s).before_comma)
else
T.absurd(formula_or_cask)
end
end
end
end

View File

@ -0,0 +1,69 @@
# typed: strict
# frozen_string_literal: true
module Homebrew
module Livecheck
# A formula or cask version, split into its component sub-versions.
#
# @api private
class LivecheckVersion
extend T::Sig
include Comparable
sig { params(formula_or_cask: T.any(Formula, Cask::Cask), version: Version).returns(LivecheckVersion) }
def self.create(formula_or_cask, version)
versions = case formula_or_cask
when Formula
[version]
when Cask::Cask
version.to_s.split(/[,:]/).map { |s| Version.new(s) }
else
T.absurd(formula_or_cask)
end
new(versions)
end
sig { returns(T::Array[Version]) }
attr_reader :versions
sig { params(versions: T::Array[Version]).void }
def initialize(versions)
@versions = versions
end
sig { params(other: T.untyped).returns(T.nilable(Integer)) }
def <=>(other)
return unless other.is_a?(LivecheckVersion)
lversions = versions
rversions = other.versions
max = [lversions.count, rversions.count].max
l = r = 0
while l < max
a = lversions[l] || Version::NULL
b = rversions[r] || Version::NULL
if a == b
l += 1
r += 1
next
elsif !a.null? && b.null?
return 1 if a > Version::NULL
l += 1
elsif a.null? && !b.null?
return -1 if b > Version::NULL
r += 1
else
return a <=> b
end
end
0
end
end
end
end

View File

@ -1,5 +1,5 @@
# typed: false
# Frozen_string_literal: true
# frozen_string_literal: true
require "livecheck/livecheck"

View File

@ -0,0 +1,32 @@
# typed: false
# frozen_string_literal: true
require "livecheck/livecheck_version"
describe Homebrew::Livecheck::LivecheckVersion do
let(:formula) { instance_double(Formula) }
let(:cask) { instance_double(Cask::Cask) }
before do
# Case statements use #=== for case equality purposes
allow(Formula).to receive(:===).and_call_original
allow(Formula).to receive(:===).with(formula).and_return(true)
allow(Cask::Cask).to receive(:===).and_call_original
allow(Cask::Cask).to receive(:===).with(cask).and_return(true)
end
specify "::create" do
expect(described_class.create(formula, Version.new("1.1.6")).versions).to eq ["1.1.6"]
expect(described_class.create(formula, Version.new("2.19.0,1.8.0")).versions).to eq ["2.19.0,1.8.0"]
expect(described_class.create(formula, Version.new("1.0,100:1426778671")).versions).to eq ["1.0,100:1426778671"]
expect(described_class.create(formula, Version.new("0.17.0,20210111183933,226")).versions)
.to eq ["0.17.0,20210111183933,226"]
expect(described_class.create(cask, Version.new("1.1.6")).versions).to eq ["1.1.6"]
expect(described_class.create(cask, Version.new("2.19.0,1.8.0")).versions).to eq ["2.19.0", "1.8.0"]
expect(described_class.create(cask, Version.new("1.0,100:1426778671")).versions)
.to eq ["1.0", "100", "1426778671"]
expect(described_class.create(cask, Version.new("0.17.0,20210111183933,226")).versions)
.to eq ["0.17.0", "20210111183933", "226"]
end
end

View File

@ -1,5 +1,5 @@
# typed: false
# Frozen_string_literal: true
# frozen_string_literal: true
require "livecheck/skip_conditions"