diff --git a/Library/Homebrew/livecheck/strategy/electron_builder.rb b/Library/Homebrew/livecheck/strategy/electron_builder.rb index 551951d841..b03fdb81a6 100644 --- a/Library/Homebrew/livecheck/strategy/electron_builder.rb +++ b/Library/Homebrew/livecheck/strategy/electron_builder.rb @@ -1,4 +1,4 @@ -# typed: false +# typed: true # frozen_string_literal: true module Homebrew @@ -19,7 +19,7 @@ module Homebrew PRIORITY = 0 # The `Regexp` used to determine if the strategy applies to the URL. - URL_MATCH_REGEX = %r{^https?://.+?/.+?\.yml}i.freeze + URL_MATCH_REGEX = %r{^https?://.+/.+\.ya?ml$}i.freeze # Whether the strategy can be applied to the provided URL. # @@ -34,13 +34,25 @@ module Homebrew # # @param content [String] the content to check # @return [String] - sig { params(content: String).returns(T.nilable(String)) } - def self.version_from_content(content) + sig { + params( + content: String, + block: T.nilable(T.proc.params(arg0: Hash).returns(String)), + ).returns(T.nilable(String)) + } + def self.version_from_content(content, &block) require "yaml" - return unless (item = YAML.safe_load(content)) + return unless (yaml = YAML.safe_load(content)) - item["version"] + if block + value = block.call(yaml) + return value if value.is_a?(String) + + raise TypeError, "Return value of `strategy :electron_builder` block must be a string." + end + + yaml["version"] end # Checks the content at the URL for new versions. @@ -48,7 +60,13 @@ module Homebrew # @param url [String] the URL of the content to check # @param regex [Regexp] a regex used for matching versions in content # @return [Hash] - sig { params(url: String, regex: T.nilable(Regexp)).returns(T::Hash[Symbol, T.untyped]) } + sig { + params( + url: String, + regex: T.nilable(Regexp), + block: T.nilable(T.proc.params(arg0: Hash).returns(String)), + ).returns(T::Hash[Symbol, T.untyped]) + } def self.find_versions(url, regex = nil, &block) raise ArgumentError, "The #{T.must(name).demodulize} strategy does not support a regex." if regex @@ -57,15 +75,8 @@ module Homebrew match_data.merge!(Strategy.page_content(url)) content = match_data.delete(:content) - if (item = version_from_content(content)) - match = if block - block.call(item)&.to_s - else - item - end - - match_data[:matches][match] = Version.new(match) if match - end + version = version_from_content(content, &block) + match_data[:matches][version] = Version.new(version) if version match_data end diff --git a/Library/Homebrew/test/livecheck/strategy/electron_builder_spec.rb b/Library/Homebrew/test/livecheck/strategy/electron_builder_spec.rb index fa4a503435..22db3dddc2 100644 --- a/Library/Homebrew/test/livecheck/strategy/electron_builder_spec.rb +++ b/Library/Homebrew/test/livecheck/strategy/electron_builder_spec.rb @@ -7,7 +7,7 @@ describe Homebrew::Livecheck::Strategy::ElectronBuilder do subject(:electron_builder) { described_class } let(:valid_url) { "https://www.example.com/example/latest-mac.yml" } - let(:invalid_url) { "https://www.example.com/example/example" } + let(:invalid_url) { "https://brew.sh/test" } let(:electron_builder_yaml) { <<~EOS @@ -46,5 +46,13 @@ describe Homebrew::Livecheck::Strategy::ElectronBuilder do it "returns a version string when given YAML data" do expect(version_from_electron_builder_yaml).to be_a(String) end + + it "returns a version string when given YAML data and a block" do + version = electron_builder.version_from_content(electron_builder_yaml) do |yaml| + yaml["version"].sub("3", "4") + end + + expect(version).to eq "1.2.4" + end end end