2020-10-10 14:16:11 +02:00
|
|
|
# typed: true
|
2020-03-16 01:37:49 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-08-07 17:27:31 -04:00
|
|
|
# The `Livecheck` class implements the DSL methods used in a formula's
|
|
|
|
# `livecheck` block and stores related instance variables. Most of these methods
|
|
|
|
# also return the related instance variable when no argument is provided.
|
|
|
|
#
|
|
|
|
# This information is used by the `brew livecheck` command to control its
|
|
|
|
# behavior.
|
2020-03-16 01:37:49 +05:30
|
|
|
class Livecheck
|
2020-08-07 17:27:31 -04:00
|
|
|
# A very brief description of why the formula is skipped (e.g., `No longer
|
|
|
|
# developed or maintained`).
|
|
|
|
# @return [String, nil]
|
2020-03-16 01:37:49 +05:30
|
|
|
attr_reader :skip_msg
|
|
|
|
|
2020-05-31 00:10:46 +05:30
|
|
|
def initialize(formula)
|
|
|
|
@formula = formula
|
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
|
|
|
|
|
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.
|
|
|
|
# @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
|
|
|
|
# that the formula should be skipped and the `skip_msg` very briefly describes
|
|
|
|
# why the formula is skipped (e.g., `No longer developed or maintained`).
|
|
|
|
# @param skip_msg [String] string describing why the formula is skipped
|
|
|
|
# @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
|
|
|
|
|
2020-08-07 17:27:31 -04:00
|
|
|
# Should `livecheck` skip this formula?
|
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-08-05 11:54:37 -04:00
|
|
|
# symbols use snake case (e.g., `:page_match`) and correspond to the strategy
|
|
|
|
# file name.
|
|
|
|
# @param symbol [Symbol] symbol for the desired strategy
|
2020-08-07 17:27:31 -04:00
|
|
|
# @return [Symbol, nil]
|
2020-08-05 11:54:37 -04:00
|
|
|
def strategy(symbol = nil)
|
|
|
|
case symbol
|
|
|
|
when nil
|
|
|
|
@strategy
|
|
|
|
when Symbol
|
|
|
|
@strategy = symbol
|
|
|
|
else
|
|
|
|
raise TypeError, "Livecheck#strategy expects a Symbol"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
# formula (e.g., `:stable`, `:homepage`, or `:head`).
|
|
|
|
# @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)
|
2020-05-31 00:10:46 +05:30
|
|
|
@url = case val
|
2020-08-07 17:25:08 -04:00
|
|
|
when nil
|
|
|
|
return @url
|
2020-09-03 10:34:22 +01:00
|
|
|
when :head, :stable
|
2020-05-31 00:10:46 +05:30
|
|
|
@formula.send(val).url
|
|
|
|
when :homepage
|
|
|
|
@formula.homepage
|
2020-08-07 17:25:08 -04:00
|
|
|
when String
|
2020-05-31 00:10:46 +05:30
|
|
|
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
|
|
|
|
|
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
|
|
|
|
{
|
|
|
|
"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
|