Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

livecheck: split cask versions into sub-versions #10376

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 4 additions & 15 deletions Library/Homebrew/livecheck/livecheck.rb
Expand Up @@ -4,6 +4,7 @@
require "livecheck/error"
require "livecheck/skip_conditions"
require "livecheck/strategy"
require "livecheck/version"
require "ruby-progressbar"
require "uri"

Expand Down Expand Up @@ -150,7 +151,7 @@ def run_checks(
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
Expand All @@ -176,7 +177,7 @@ def run_checks(
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
Expand Down Expand Up @@ -546,7 +547,7 @@ def latest_version(formula_or_cask, json: false, full_name: false, verbose: fals
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
Expand All @@ -570,17 +571,5 @@ def latest_version(formula_or_cask, json: false, full_name: false, verbose: fals

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
69 changes: 69 additions & 0 deletions Library/Homebrew/livecheck/version.rb
@@ -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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
class LivecheckVersion
class Version

or rename the file livecheck_version.rb maybe?

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) }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not blocking but if there's a chance for tests for any of this that might be nice?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We’re considering getting rid of : in version. Pointing that out so there’s something to link to from the other issue, and we can revisit this code when the time comes.

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