livecheck/strategy/electron_builder: address changes from review

Co-authored-by: Sam Ford <1584702+samford@users.noreply.github.com>
This commit is contained in:
nandahkrishna 2021-03-17 02:29:53 +05:30
parent f422b6cdc4
commit 15102a3234
No known key found for this signature in database
GPG Key ID: 067E5FCD58ADF3AA
2 changed files with 36 additions and 17 deletions

View File

@ -1,4 +1,4 @@
# typed: false # typed: true
# frozen_string_literal: true # frozen_string_literal: true
module Homebrew module Homebrew
@ -19,7 +19,7 @@ module Homebrew
PRIORITY = 0 PRIORITY = 0
# The `Regexp` used to determine if the strategy applies to the URL. # 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. # Whether the strategy can be applied to the provided URL.
# #
@ -34,13 +34,25 @@ module Homebrew
# #
# @param content [String] the content to check # @param content [String] the content to check
# @return [String] # @return [String]
sig { params(content: String).returns(T.nilable(String)) } sig {
def self.version_from_content(content) 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" 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 end
# Checks the content at the URL for new versions. # 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 url [String] the URL of the content to check
# @param regex [Regexp] a regex used for matching versions in content # @param regex [Regexp] a regex used for matching versions in content
# @return [Hash] # @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) def self.find_versions(url, regex = nil, &block)
raise ArgumentError, "The #{T.must(name).demodulize} strategy does not support a regex." if regex 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)) match_data.merge!(Strategy.page_content(url))
content = match_data.delete(:content) content = match_data.delete(:content)
if (item = version_from_content(content)) version = version_from_content(content, &block)
match = if block match_data[:matches][version] = Version.new(version) if version
block.call(item)&.to_s
else
item
end
match_data[:matches][match] = Version.new(match) if match
end
match_data match_data
end end

View File

@ -7,7 +7,7 @@ describe Homebrew::Livecheck::Strategy::ElectronBuilder do
subject(:electron_builder) { described_class } subject(:electron_builder) { described_class }
let(:valid_url) { "https://www.example.com/example/latest-mac.yml" } 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) { let(:electron_builder_yaml) {
<<~EOS <<~EOS
@ -46,5 +46,13 @@ describe Homebrew::Livecheck::Strategy::ElectronBuilder do
it "returns a version string when given YAML data" do it "returns a version string when given YAML data" do
expect(version_from_electron_builder_yaml).to be_a(String) expect(version_from_electron_builder_yaml).to be_a(String)
end 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
end end