From f2fbbfe70ca968d9ebefd2e0b7a7cbbacf586192 Mon Sep 17 00:00:00 2001 From: Nanda H Krishna Date: Sun, 18 Dec 2022 18:11:51 -0500 Subject: [PATCH 1/2] Enable use of latest formula version in resource `livecheck` URLs --- Library/Homebrew/livecheck.rb | 7 +++++++ Library/Homebrew/livecheck/livecheck.rb | 22 ++++++++++++---------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/Library/Homebrew/livecheck.rb b/Library/Homebrew/livecheck.rb index f02eef7e1e..0b83bfbc21 100644 --- a/Library/Homebrew/livecheck.rb +++ b/Library/Homebrew/livecheck.rb @@ -142,6 +142,13 @@ class Livecheck end end + # Returns a placeholder string that will be replaced with a formula's latest + # version in livecheck URLs for its resources. + # @return String + def latest_version + "" + end + delegate version: :@package_or_resource delegate arch: :@package_or_resource private :version, :arch diff --git a/Library/Homebrew/livecheck/livecheck.rb b/Library/Homebrew/livecheck/livecheck.rb index f4ae28f06a..b7f820485a 100644 --- a/Library/Homebrew/livecheck/livecheck.rb +++ b/Library/Homebrew/livecheck/livecheck.rb @@ -295,6 +295,7 @@ module Homebrew else res_version_info = resource_version( resource, + latest.to_s, json: json, debug: debug, quiet: quiet, @@ -836,15 +837,17 @@ module Homebrew # version information. Returns nil if a latest version couldn't be found. sig { params( - resource: Resource, - json: T::Boolean, - debug: T::Boolean, - quiet: T::Boolean, - verbose: T::Boolean, + resource: Resource, + formula_latest: String, + json: T::Boolean, + debug: T::Boolean, + quiet: T::Boolean, + verbose: T::Boolean, ).returns(Hash) } def resource_version( resource, + formula_latest, json: false, debug: false, quiet: false, @@ -874,12 +877,11 @@ module Homebrew checked_urls = [] # rubocop:disable Metrics/BlockLength urls.each_with_index do |original_url, i| + url = original_url.gsub(//, formula_latest) + # Only preprocess the URL when it's appropriate - url = if STRATEGY_SYMBOLS_TO_SKIP_PREPROCESS_URL.include?(livecheck_strategy) - original_url - else - preprocess_url(original_url) - end + url = preprocess_url(url) unless STRATEGY_SYMBOLS_TO_SKIP_PREPROCESS_URL.include?(livecheck_strategy) + next if checked_urls.include?(url) strategies = Strategy.from_url( From f03a400e28f82813e65b02948a61c8b6f74a1d77 Mon Sep 17 00:00:00 2001 From: Sam Ford <1584702+samford@users.noreply.github.com> Date: Mon, 19 Dec 2022 18:15:51 -0500 Subject: [PATCH 2/2] Extract latest_version string to a constant --- Library/Homebrew/formula.rb | 1 + Library/Homebrew/livecheck.rb | 9 ++------- Library/Homebrew/livecheck/constants.rb | 14 ++++++++++++++ Library/Homebrew/livecheck/livecheck.rb | 3 ++- 4 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 Library/Homebrew/livecheck/constants.rb diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 5bac71b555..005d0b38ae 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -3278,6 +3278,7 @@ class Formula def livecheck(&block) return @livecheck unless block + include Homebrew::Livecheck::Constants @livecheckable = true @livecheck.instance_eval(&block) end diff --git a/Library/Homebrew/livecheck.rb b/Library/Homebrew/livecheck.rb index 0b83bfbc21..07d2787472 100644 --- a/Library/Homebrew/livecheck.rb +++ b/Library/Homebrew/livecheck.rb @@ -1,6 +1,8 @@ # typed: true # frozen_string_literal: true +require "livecheck/constants" + # The {Livecheck} class implements the DSL methods used in a formula's, cask's # or resource's `livecheck` block and stores related instance variables. Most # of these methods also return the related instance variable when no argument @@ -142,13 +144,6 @@ class Livecheck end end - # Returns a placeholder string that will be replaced with a formula's latest - # version in livecheck URLs for its resources. - # @return String - def latest_version - "" - end - delegate version: :@package_or_resource delegate arch: :@package_or_resource private :version, :arch diff --git a/Library/Homebrew/livecheck/constants.rb b/Library/Homebrew/livecheck/constants.rb new file mode 100644 index 0000000000..f203ab67b5 --- /dev/null +++ b/Library/Homebrew/livecheck/constants.rb @@ -0,0 +1,14 @@ +# typed: true +# frozen_string_literal: true + +module Homebrew + module Livecheck + # The {Constants} module provides constants that are intended to be used + # in `livecheck` block values (e.g. `url`, `regex`). + module Constants + # A placeholder string used in resource `livecheck` block URLs that will + # be replaced with the latest version from the main formula check. + LATEST_VERSION = "" + end + end +end diff --git a/Library/Homebrew/livecheck/livecheck.rb b/Library/Homebrew/livecheck/livecheck.rb index b7f820485a..bd3e7d4ec7 100644 --- a/Library/Homebrew/livecheck/livecheck.rb +++ b/Library/Homebrew/livecheck/livecheck.rb @@ -1,6 +1,7 @@ # typed: true # frozen_string_literal: true +require "livecheck/constants" require "livecheck/error" require "livecheck/livecheck_version" require "livecheck/skip_conditions" @@ -877,7 +878,7 @@ module Homebrew checked_urls = [] # rubocop:disable Metrics/BlockLength urls.each_with_index do |original_url, i| - url = original_url.gsub(//, formula_latest) + url = original_url.gsub(Constants::LATEST_VERSION, formula_latest) # Only preprocess the URL when it's appropriate url = preprocess_url(url) unless STRATEGY_SYMBOLS_TO_SKIP_PREPROCESS_URL.include?(livecheck_strategy)