mirror of
https://github.com/Homebrew/brew.git
synced 2025-07-14 16:09:03 +08:00
Merge pull request #13496 from mohammadzainabbas/mohammad
Extend the `livecheck` DSL to work for resources
This commit is contained in:
commit
39c9e2ddbc
@ -1,9 +1,10 @@
|
|||||||
# typed: true
|
# typed: true
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
# The {Livecheck} class implements the DSL methods used in a formula's or cask's
|
# The {Livecheck} class implements the DSL methods used in a formula's, cask's
|
||||||
# `livecheck` block and stores related instance variables. Most of these methods
|
# or resource's `livecheck` block and stores related instance variables. Most
|
||||||
# also return the related instance variable when no argument is provided.
|
# 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
|
# This information is used by the `brew livecheck` command to control its
|
||||||
# behavior. Example `livecheck` blocks can be found in the
|
# behavior. Example `livecheck` blocks can be found in the
|
||||||
@ -11,13 +12,13 @@
|
|||||||
class Livecheck
|
class Livecheck
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
|
|
||||||
# A very brief description of why the formula/cask is skipped (e.g. `No longer
|
# A very brief description of why the formula/cask/resource is skipped (e.g.
|
||||||
# developed or maintained`).
|
# `No longer developed or maintained`).
|
||||||
# @return [String, nil]
|
# @return [String, nil]
|
||||||
attr_reader :skip_msg
|
attr_reader :skip_msg
|
||||||
|
|
||||||
def initialize(formula_or_cask)
|
def initialize(package_or_resource)
|
||||||
@formula_or_cask = formula_or_cask
|
@package_or_resource = package_or_resource
|
||||||
@referenced_cask_name = nil
|
@referenced_cask_name = nil
|
||||||
@referenced_formula_name = nil
|
@referenced_formula_name = nil
|
||||||
@regex = nil
|
@regex = nil
|
||||||
@ -81,8 +82,9 @@ class Livecheck
|
|||||||
|
|
||||||
# Sets the `@skip` instance variable to `true` and sets the `@skip_msg`
|
# Sets the `@skip` instance variable to `true` and sets the `@skip_msg`
|
||||||
# instance variable if a `String` is provided. `@skip` is used to indicate
|
# instance variable if a `String` is provided. `@skip` is used to indicate
|
||||||
# that the formula/cask should be skipped and the `skip_msg` very briefly
|
# that the formula/cask/resource should be skipped and the `skip_msg` very
|
||||||
# describes why it is skipped (e.g. "No longer developed or maintained").
|
# briefly describes why it is skipped (e.g. "No longer developed or
|
||||||
|
# maintained").
|
||||||
#
|
#
|
||||||
# @param skip_msg [String] string describing why the formula/cask is skipped
|
# @param skip_msg [String] string describing why the formula/cask is skipped
|
||||||
# @return [Boolean]
|
# @return [Boolean]
|
||||||
@ -96,7 +98,7 @@ class Livecheck
|
|||||||
@skip = true
|
@skip = true
|
||||||
end
|
end
|
||||||
|
|
||||||
# Should `livecheck` skip this formula/cask?
|
# Should `livecheck` skip this formula/cask/resource?
|
||||||
def skip?
|
def skip?
|
||||||
@skip
|
@skip
|
||||||
end
|
end
|
||||||
@ -126,7 +128,7 @@ class Livecheck
|
|||||||
# Sets the `@url` instance variable to the provided argument or returns the
|
# Sets the `@url` instance variable to the provided argument or returns the
|
||||||
# `@url` instance variable when no argument is provided. The argument can be
|
# `@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
|
# a `String` (a URL) or a supported `Symbol` corresponding to a URL in the
|
||||||
# formula/cask (e.g. `:stable`, `:homepage`, `:head`, `:url`).
|
# formula/cask/resource (e.g. `:stable`, `:homepage`, `:head`, `:url`).
|
||||||
# @param val [String, Symbol] URL to check for version information
|
# @param val [String, Symbol] URL to check for version information
|
||||||
# @return [String, nil]
|
# @return [String, nil]
|
||||||
def url(val = nil)
|
def url(val = nil)
|
||||||
@ -140,7 +142,7 @@ class Livecheck
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
delegate version: :@formula_or_cask
|
delegate version: :@package_or_resource
|
||||||
private :version
|
private :version
|
||||||
|
|
||||||
# Returns a `Hash` of all instance variable values.
|
# Returns a `Hash` of all instance variable values.
|
||||||
|
@ -5,6 +5,7 @@ require "download_strategy"
|
|||||||
require "checksum"
|
require "checksum"
|
||||||
require "version"
|
require "version"
|
||||||
require "mktemp"
|
require "mktemp"
|
||||||
|
require "livecheck"
|
||||||
require "extend/on_system"
|
require "extend/on_system"
|
||||||
|
|
||||||
# Resource is the fundamental representation of an external resource. The
|
# Resource is the fundamental representation of an external resource. The
|
||||||
@ -36,6 +37,8 @@ class Resource
|
|||||||
@checksum = nil
|
@checksum = nil
|
||||||
@using = nil
|
@using = nil
|
||||||
@patches = []
|
@patches = []
|
||||||
|
@livecheck = nil
|
||||||
|
@livecheckable = false
|
||||||
instance_eval(&block) if block
|
instance_eval(&block) if block
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -168,6 +171,32 @@ class Resource
|
|||||||
EOS
|
EOS
|
||||||
end
|
end
|
||||||
|
|
||||||
|
# @!attribute [w] livecheck
|
||||||
|
# {Livecheck} can be used to check for newer versions of the software.
|
||||||
|
# This method evaluates the DSL specified in the livecheck block of the
|
||||||
|
# {Resource} (if it exists) and sets the instance variables of a {Livecheck}
|
||||||
|
# object accordingly. This is used by `brew livecheck` to check for newer
|
||||||
|
# versions of the software.
|
||||||
|
#
|
||||||
|
# <pre>livecheck do
|
||||||
|
# url "https://example.com/foo/releases"
|
||||||
|
# regex /foo-(\d+(?:\.\d+)+)\.tar/
|
||||||
|
# end</pre>
|
||||||
|
def livecheck(&block)
|
||||||
|
@livecheck ||= Livecheck.new(self) if block
|
||||||
|
return @livecheck unless block
|
||||||
|
|
||||||
|
@livecheckable = true
|
||||||
|
@livecheck.instance_eval(&block)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Whether a livecheck specification is defined or not.
|
||||||
|
# It returns true when a livecheck block is present in the {Resource} and
|
||||||
|
# false otherwise, and is used by livecheck.
|
||||||
|
def livecheckable?
|
||||||
|
@livecheckable == true
|
||||||
|
end
|
||||||
|
|
||||||
def sha256(val)
|
def sha256(val)
|
||||||
@checksum = Checksum.new(val)
|
@checksum = Checksum.new(val)
|
||||||
end
|
end
|
||||||
|
@ -2,10 +2,23 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "resource"
|
require "resource"
|
||||||
|
require "livecheck"
|
||||||
|
|
||||||
describe Resource do
|
describe Resource do
|
||||||
subject(:resource) { described_class.new("test") }
|
subject(:resource) { described_class.new("test") }
|
||||||
|
|
||||||
|
let(:livecheck_resource) {
|
||||||
|
described_class.new do
|
||||||
|
url "https://brew.sh/foo-1.0.tar.gz"
|
||||||
|
sha256 "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||||
|
|
||||||
|
livecheck do
|
||||||
|
url "https://brew.sh/test/releases"
|
||||||
|
regex(/foo[._-]v?(\d+(?:\.\d+)+)\.t/i)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
}
|
||||||
|
|
||||||
describe "#url" do
|
describe "#url" do
|
||||||
it "sets the URL" do
|
it "sets the URL" do
|
||||||
resource.url("foo")
|
resource.url("foo")
|
||||||
@ -52,6 +65,27 @@ describe Resource do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe "#livecheck" do
|
||||||
|
it "returns nil if livecheck block is not set in resource" do
|
||||||
|
expect(resource.livecheck).to be_nil
|
||||||
|
end
|
||||||
|
|
||||||
|
specify "when livecheck block is set" do
|
||||||
|
expect(livecheck_resource.livecheck.url).to eq("https://brew.sh/test/releases")
|
||||||
|
expect(livecheck_resource.livecheck.regex).to eq(/foo[._-]v?(\d+(?:\.\d+)+)\.t/i)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "#livecheckable?" do
|
||||||
|
it "returns false if livecheck block is not set in resource" do
|
||||||
|
expect(resource.livecheckable?).to be false
|
||||||
|
end
|
||||||
|
|
||||||
|
specify "livecheck block defined in resources" do
|
||||||
|
expect(livecheck_resource.livecheckable?).to be true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
describe "#version" do
|
describe "#version" do
|
||||||
it "sets the version" do
|
it "sets the version" do
|
||||||
resource.version("1.0")
|
resource.version("1.0")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user