Simplify assert_only_one_stanza_allowed.
This commit is contained in:
parent
896f41f7ed
commit
8d7ab78a75
@ -89,14 +89,20 @@ module Hbc
|
|||||||
@name.concat(args.flatten)
|
@name.concat(args.flatten)
|
||||||
end
|
end
|
||||||
|
|
||||||
def assert_only_one_stanza_allowed(stanza, arg_given)
|
def set_unique_stanza(stanza, should_return)
|
||||||
return unless instance_variable_defined?("@#{stanza}") && arg_given
|
return instance_variable_get("@#{stanza}") if should_return
|
||||||
|
|
||||||
|
if instance_variable_defined?("@#{stanza}")
|
||||||
raise CaskInvalidError.new(token, "'#{stanza}' stanza may only appear once")
|
raise CaskInvalidError.new(token, "'#{stanza}' stanza may only appear once")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
instance_variable_set("@#{stanza}", yield)
|
||||||
|
rescue StandardError => e
|
||||||
|
raise CaskInvalidError.new(token, "'#{stanza}' stanza failed with: #{e}")
|
||||||
|
end
|
||||||
|
|
||||||
def homepage(homepage = nil)
|
def homepage(homepage = nil)
|
||||||
assert_only_one_stanza_allowed :homepage, !homepage.nil?
|
set_unique_stanza(:homepage, homepage.nil?) { homepage }
|
||||||
@homepage ||= homepage
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def language(*args, default: false, &block)
|
def language(*args, default: false, &block)
|
||||||
@ -135,72 +141,51 @@ module Hbc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def url(*args, &block)
|
def url(*args, &block)
|
||||||
url_given = !args.empty? || block_given?
|
set_unique_stanza(:url, args.empty? && !block_given?) do
|
||||||
return @url unless url_given
|
begin
|
||||||
assert_only_one_stanza_allowed :url, url_given
|
|
||||||
@url ||= begin
|
|
||||||
URL.from(*args, &block)
|
URL.from(*args, &block)
|
||||||
rescue StandardError => e
|
end
|
||||||
raise CaskInvalidError.new(token, "'url' stanza failed with: #{e}")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def appcast(*args)
|
def appcast(*args)
|
||||||
return @appcast if args.empty?
|
set_unique_stanza(:appcast, args.empty?) { DSL::Appcast.new(*args) }
|
||||||
assert_only_one_stanza_allowed :appcast, !args.empty?
|
|
||||||
@appcast ||= begin
|
|
||||||
DSL::Appcast.new(*args) unless args.empty?
|
|
||||||
rescue StandardError => e
|
|
||||||
raise CaskInvalidError.new(token, e)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def gpg(*args)
|
def gpg(*args)
|
||||||
return @gpg if args.empty?
|
set_unique_stanza(:gpg, args.empty?) { DSL::Gpg.new(*args) }
|
||||||
assert_only_one_stanza_allowed :gpg, !args.empty?
|
|
||||||
@gpg ||= begin
|
|
||||||
DSL::Gpg.new(*args) unless args.empty?
|
|
||||||
rescue StandardError => e
|
|
||||||
raise CaskInvalidError.new(token, e)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def container(*args)
|
def container(*args)
|
||||||
return @container if args.empty?
|
|
||||||
# TODO: remove this constraint, and instead merge multiple container stanzas
|
# TODO: remove this constraint, and instead merge multiple container stanzas
|
||||||
assert_only_one_stanza_allowed :container, !args.empty?
|
set_unique_stanza(:container, args.empty?) do
|
||||||
@container ||= begin
|
begin
|
||||||
DSL::Container.new(*args) unless args.empty?
|
DSL::Container.new(*args).tap do |container|
|
||||||
rescue StandardError => e
|
|
||||||
raise CaskInvalidError.new(token, e)
|
|
||||||
end
|
|
||||||
# TODO: remove this backward-compatibility section after removing nested_container
|
# TODO: remove this backward-compatibility section after removing nested_container
|
||||||
if @container && @container.nested
|
if container && container.nested
|
||||||
artifacts[:nested_container] << @container.nested
|
artifacts[:nested_container] << container.nested
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
@container
|
|
||||||
end
|
end
|
||||||
|
|
||||||
SYMBOLIC_VERSIONS = Set.new [
|
|
||||||
:latest,
|
|
||||||
]
|
|
||||||
|
|
||||||
def version(arg = nil)
|
def version(arg = nil)
|
||||||
return @version if arg.nil?
|
set_unique_stanza(:version, arg.nil?) do
|
||||||
assert_only_one_stanza_allowed :version, !arg.nil?
|
if !arg.is_a?(String) && arg != :latest
|
||||||
raise CaskInvalidError.new(token, "invalid 'version' value: '#{arg.inspect}'") if !arg.is_a?(String) && !SYMBOLIC_VERSIONS.include?(arg)
|
raise CaskInvalidError.new(token, "invalid 'version' value: '#{arg.inspect}'")
|
||||||
@version ||= DSL::Version.new(arg)
|
end
|
||||||
|
DSL::Version.new(arg)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
SYMBOLIC_SHA256S = Set.new [
|
|
||||||
:no_check,
|
|
||||||
]
|
|
||||||
|
|
||||||
def sha256(arg = nil)
|
def sha256(arg = nil)
|
||||||
return @sha256 if arg.nil?
|
set_unique_stanza(:sha256, arg.nil?) do
|
||||||
assert_only_one_stanza_allowed :sha256, !arg.nil?
|
if !arg.is_a?(String) && arg != :no_check
|
||||||
raise CaskInvalidError.new(token, "invalid 'sha256' value: '#{arg.inspect}'") if !arg.is_a?(String) && !SYMBOLIC_SHA256S.include?(arg)
|
raise CaskInvalidError.new(token, "invalid 'sha256' value: '#{arg.inspect}'")
|
||||||
@sha256 ||= arg
|
end
|
||||||
|
arg
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# depends_on uses a load method so that multiple stanzas can be merged
|
# depends_on uses a load method so that multiple stanzas can be merged
|
||||||
@ -216,14 +201,8 @@ module Hbc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def conflicts_with(*args)
|
def conflicts_with(*args)
|
||||||
return @conflicts_with if args.empty?
|
|
||||||
# TODO: remove this constraint, and instead merge multiple conflicts_with stanzas
|
# TODO: remove this constraint, and instead merge multiple conflicts_with stanzas
|
||||||
assert_only_one_stanza_allowed :conflicts_with, !args.empty?
|
set_unique_stanza(:conflicts_with, args.empty?) { DSL::ConflictsWith.new(*args) }
|
||||||
@conflicts_with ||= begin
|
|
||||||
DSL::ConflictsWith.new(*args) unless args.empty?
|
|
||||||
rescue StandardError => e
|
|
||||||
raise CaskInvalidError.new(token, e)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def artifacts
|
def artifacts
|
||||||
@ -251,13 +230,11 @@ module Hbc
|
|||||||
end
|
end
|
||||||
|
|
||||||
def accessibility_access(accessibility_access = nil)
|
def accessibility_access(accessibility_access = nil)
|
||||||
assert_only_one_stanza_allowed :accessibility_access, !accessibility_access.nil?
|
set_unique_stanza(:accessibility_access, accessibility_access.nil?) { accessibility_access }
|
||||||
@accessibility_access ||= accessibility_access
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def auto_updates(auto_updates = nil)
|
def auto_updates(auto_updates = nil)
|
||||||
assert_only_one_stanza_allowed :auto_updates, !auto_updates.nil?
|
set_unique_stanza(:auto_updates, auto_updates.nil?) { auto_updates }
|
||||||
@auto_updates ||= auto_updates
|
|
||||||
end
|
end
|
||||||
|
|
||||||
ORDINARY_ARTIFACT_TYPES.each do |type|
|
ORDINARY_ARTIFACT_TYPES.each do |type|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user