2020-10-10 14:16:11 +02:00
|
|
|
# typed: true
|
2020-03-16 01:37:49 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-02 10:09:25 +02:00
|
|
|
# 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
|
|
|
|
# is provided.
|
2020-08-07 17:27:31 -04:00
|
|
|
#
|
|
|
|
# This information is used by the `brew livecheck` command to control its
|
2021-01-11 17:12:48 -05:00
|
|
|
# behavior. Example `livecheck` blocks can be found in the
|
|
|
|
# [`brew livecheck` documentation](https://docs.brew.sh/Brew-Livecheck).
|
2020-03-16 01:37:49 +05:30
|
|
|
class Livecheck
|
2020-12-15 17:25:16 +01:00
|
|
|
extend Forwardable
|
|
|
|
|
2022-07-02 10:09:25 +02:00
|
|
|
# A very brief description of why the formula/cask/resource is skipped (e.g.
|
|
|
|
# `No longer developed or maintained`).
|
2020-08-07 17:27:31 -04:00
|
|
|
# @return [String, nil]
|
2020-03-16 01:37:49 +05:30
|
|
|
attr_reader :skip_msg
|
|
|
|
|
2022-07-02 10:09:25 +02:00
|
|
|
def initialize(package_or_resource)
|
|
|
|
@package_or_resource = package_or_resource
|
2021-07-19 11:21:29 -04:00
|
|
|
@referenced_cask_name = nil
|
|
|
|
@referenced_formula_name = nil
|
2020-03-16 01:37:49 +05:30
|
|
|
@regex = nil
|
|
|
|
@skip = false
|
|
|
|
@skip_msg = nil
|
2020-08-05 11:54:37 -04:00
|
|
|
@strategy = nil
|
2020-03-16 01:37:49 +05:30
|
|
|
@url = nil
|
|
|
|
end
|
|
|
|
|
2021-07-19 11:21:29 -04:00
|
|
|
# Sets the `@referenced_cask_name` instance variable to the provided `String`
|
|
|
|
# or returns the `@referenced_cask_name` instance variable when no argument
|
|
|
|
# is provided. Inherited livecheck values from the referenced cask
|
|
|
|
# (e.g. regex) can be overridden in the livecheck block.
|
|
|
|
#
|
|
|
|
# @param cask_name [String] name of cask to inherit livecheck info from
|
|
|
|
# @return [String, nil]
|
|
|
|
def cask(cask_name = nil)
|
|
|
|
case cask_name
|
|
|
|
when nil
|
|
|
|
@referenced_cask_name
|
|
|
|
when String
|
|
|
|
@referenced_cask_name = cask_name
|
|
|
|
else
|
|
|
|
raise TypeError, "Livecheck#cask expects a String"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Sets the `@referenced_formula_name` instance variable to the provided
|
|
|
|
# `String` or returns the `@referenced_formula_name` instance variable when
|
|
|
|
# no argument is provided. Inherited livecheck values from the referenced
|
|
|
|
# formula (e.g. regex) can be overridden in the livecheck block.
|
|
|
|
#
|
|
|
|
# @param formula_name [String] name of formula to inherit livecheck info from
|
|
|
|
# @return [String, nil]
|
|
|
|
def formula(formula_name = nil)
|
|
|
|
case formula_name
|
|
|
|
when nil
|
|
|
|
@referenced_formula_name
|
|
|
|
when String
|
|
|
|
@referenced_formula_name = formula_name
|
|
|
|
else
|
|
|
|
raise TypeError, "Livecheck#formula expects a String"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-07 17:27:31 -04:00
|
|
|
# Sets the `@regex` instance variable to the provided `Regexp` or returns the
|
|
|
|
# `@regex` instance variable when no argument is provided.
|
2020-11-05 17:17:03 -05:00
|
|
|
#
|
2020-08-07 17:27:31 -04:00
|
|
|
# @param pattern [Regexp] regex to use for matching versions in content
|
|
|
|
# @return [Regexp, nil]
|
2020-03-16 01:37:49 +05:30
|
|
|
def regex(pattern = nil)
|
2020-08-07 17:25:08 -04:00
|
|
|
case pattern
|
|
|
|
when nil
|
|
|
|
@regex
|
|
|
|
when Regexp
|
|
|
|
@regex = pattern
|
|
|
|
else
|
|
|
|
raise TypeError, "Livecheck#regex expects a Regexp"
|
|
|
|
end
|
2020-03-16 01:37:49 +05:30
|
|
|
end
|
|
|
|
|
2020-08-07 17:27:31 -04:00
|
|
|
# Sets the `@skip` instance variable to `true` and sets the `@skip_msg`
|
|
|
|
# instance variable if a `String` is provided. `@skip` is used to indicate
|
2022-07-02 10:09:25 +02:00
|
|
|
# that the formula/cask/resource should be skipped and the `skip_msg` very
|
|
|
|
# briefly describes why it is skipped (e.g. "No longer developed or
|
|
|
|
# maintained").
|
2020-11-05 17:17:03 -05:00
|
|
|
#
|
2020-09-02 12:24:21 -07:00
|
|
|
# @param skip_msg [String] string describing why the formula/cask is skipped
|
2020-08-07 17:27:31 -04:00
|
|
|
# @return [Boolean]
|
2020-03-16 01:37:49 +05:30
|
|
|
def skip(skip_msg = nil)
|
2020-08-07 17:25:08 -04:00
|
|
|
if skip_msg.is_a?(String)
|
|
|
|
@skip_msg = skip_msg
|
|
|
|
elsif skip_msg.present?
|
|
|
|
raise TypeError, "Livecheck#skip expects a String"
|
|
|
|
end
|
|
|
|
|
2020-03-16 01:37:49 +05:30
|
|
|
@skip = true
|
|
|
|
end
|
|
|
|
|
2022-07-02 10:09:25 +02:00
|
|
|
# Should `livecheck` skip this formula/cask/resource?
|
2020-03-16 01:37:49 +05:30
|
|
|
def skip?
|
|
|
|
@skip
|
|
|
|
end
|
|
|
|
|
2020-08-07 17:27:31 -04:00
|
|
|
# Sets the `@strategy` instance variable to the provided `Symbol` or returns
|
|
|
|
# the `@strategy` instance variable when no argument is provided. The strategy
|
2020-11-05 17:17:03 -05:00
|
|
|
# symbols use snake case (e.g. `:page_match`) and correspond to the strategy
|
2020-08-05 11:54:37 -04:00
|
|
|
# file name.
|
2020-11-05 17:17:03 -05:00
|
|
|
#
|
2020-08-05 11:54:37 -04:00
|
|
|
# @param symbol [Symbol] symbol for the desired strategy
|
2020-08-07 17:27:31 -04:00
|
|
|
# @return [Symbol, nil]
|
2020-12-13 12:23:20 +01:00
|
|
|
def strategy(symbol = nil, &block)
|
|
|
|
@strategy_block = block if block
|
|
|
|
|
2020-08-05 11:54:37 -04:00
|
|
|
case symbol
|
|
|
|
when nil
|
|
|
|
@strategy
|
|
|
|
when Symbol
|
|
|
|
@strategy = symbol
|
|
|
|
else
|
|
|
|
raise TypeError, "Livecheck#strategy expects a Symbol"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-13 12:23:20 +01:00
|
|
|
attr_reader :strategy_block
|
|
|
|
|
2020-08-07 17:27:31 -04:00
|
|
|
# Sets the `@url` instance variable to the provided argument or returns the
|
|
|
|
# `@url` instance variable when no argument is provided. The argument can be
|
|
|
|
# a `String` (a URL) or a supported `Symbol` corresponding to a URL in the
|
2022-07-02 10:09:25 +02:00
|
|
|
# formula/cask/resource (e.g. `:stable`, `:homepage`, `:head`, `:url`).
|
2020-08-07 17:27:31 -04:00
|
|
|
# @param val [String, Symbol] URL to check for version information
|
|
|
|
# @return [String, nil]
|
2020-03-16 01:37:49 +05:30
|
|
|
def url(val = nil)
|
2021-01-12 15:00:49 -05:00
|
|
|
case val
|
2020-08-07 17:25:08 -04:00
|
|
|
when nil
|
2021-01-12 15:00:49 -05:00
|
|
|
@url
|
|
|
|
when String, :head, :homepage, :stable, :url
|
|
|
|
@url = val
|
2020-08-07 17:25:08 -04:00
|
|
|
else
|
|
|
|
raise TypeError, "Livecheck#url expects a String or valid Symbol"
|
2020-05-31 00:10:46 +05:30
|
|
|
end
|
2020-03-16 01:37:49 +05:30
|
|
|
end
|
|
|
|
|
2022-12-18 18:11:51 -05:00
|
|
|
# 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
|
|
|
|
"<FORMULA_LATEST_VERSION>"
|
|
|
|
end
|
|
|
|
|
2022-07-02 10:09:25 +02:00
|
|
|
delegate version: :@package_or_resource
|
2022-08-05 15:51:02 -04:00
|
|
|
delegate arch: :@package_or_resource
|
|
|
|
private :version, :arch
|
2020-12-15 17:25:16 +01:00
|
|
|
|
2020-08-07 17:27:31 -04:00
|
|
|
# Returns a `Hash` of all instance variable values.
|
|
|
|
# @return [Hash]
|
2020-03-16 01:37:49 +05:30
|
|
|
def to_hash
|
|
|
|
{
|
2021-07-19 11:21:29 -04:00
|
|
|
"cask" => @referenced_cask_name,
|
|
|
|
"formula" => @referenced_formula_name,
|
2020-03-16 01:37:49 +05:30
|
|
|
"regex" => @regex,
|
|
|
|
"skip" => @skip,
|
|
|
|
"skip_msg" => @skip_msg,
|
2020-08-05 11:54:37 -04:00
|
|
|
"strategy" => @strategy,
|
2020-03-16 01:37:49 +05:30
|
|
|
"url" => @url,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|