forked from Homebrew/brew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
electron_builder.rb
92 lines (78 loc) 路 2.92 KB
/
electron_builder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# typed: true
# frozen_string_literal: true
module Homebrew
module Livecheck
module Strategy
# The {ElectronBuilder} strategy fetches content at a URL and parses
# it as an electron-builder appcast in YAML format.
#
# @api private
class ElectronBuilder
extend T::Sig
NICE_NAME = "electron-builder"
# A priority of zero causes livecheck to skip the strategy. We do this
# for {ElectronBuilder} so we can selectively apply the strategy using
# `strategy :electron_builder` in a `livecheck` block.
PRIORITY = 0
# The `Regexp` used to determine if the strategy applies to the URL.
URL_MATCH_REGEX = %r{^https?://.+/[^/]+\.ya?ml(?:\?[^/?]+)?$}i.freeze
# Whether the strategy can be applied to the provided URL.
#
# @param url [String] the URL to match against
# @return [Boolean]
sig { params(url: String).returns(T::Boolean) }
def self.match?(url)
URL_MATCH_REGEX.match?(url)
end
# Extract version information from page content.
#
# @param content [String] the content to check
# @return [String]
sig {
params(
content: String,
block: T.nilable(T.proc.params(arg0: T::Hash[String, T.untyped]).returns(T.nilable(String))),
).returns(T.nilable(String))
}
def self.version_from_content(content, &block)
require "yaml"
yaml = YAML.safe_load(content)
return if yaml.blank?
if block
case (value = block.call(yaml))
when String
return value
when nil
return
else
raise TypeError, "Return value of `strategy :electron_builder` block must be a string."
end
end
yaml["version"]
end
# Checks the content at the URL for new versions.
#
# @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),
cask: T.nilable(Cask::Cask),
block: T.nilable(T.proc.params(arg0: T::Hash[String, T.untyped]).returns(T.nilable(String))),
).returns(T::Hash[Symbol, T.untyped])
}
def self.find_versions(url, regex, cask: nil, &block)
raise ArgumentError, "The #{T.must(name).demodulize} strategy does not support a regex." if regex
match_data = { matches: {}, regex: regex, url: url }
match_data.merge!(Strategy.page_content(url))
content = match_data.delete(:content)
version = version_from_content(content, &block)
match_data[:matches][version] = Version.new(version) if version
match_data
end
end
end
end
end