Skip to content

Commit

Permalink
Merge pull request #2978 from reitermarkus/simplify-dsl
Browse files Browse the repository at this point in the history
Simplify `assert_only_one_stanza_allowed`.
  • Loading branch information
reitermarkus committed Jul 31, 2017
2 parents af463a0 + 8d7ab78 commit 05e9086
Showing 1 changed file with 41 additions and 64 deletions.
105 changes: 41 additions & 64 deletions Library/Homebrew/cask/lib/hbc/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,20 @@ def name(*args)
@name.concat(args.flatten)
end

def assert_only_one_stanza_allowed(stanza, arg_given)
return unless instance_variable_defined?("@#{stanza}") && arg_given
raise CaskInvalidError.new(token, "'#{stanza}' stanza may only appear once")
def set_unique_stanza(stanza, should_return)
return instance_variable_get("@#{stanza}") if should_return

if instance_variable_defined?("@#{stanza}")
raise CaskInvalidError.new(token, "'#{stanza}' stanza may only appear once")
end

instance_variable_set("@#{stanza}", yield)
rescue StandardError => e
raise CaskInvalidError.new(token, "'#{stanza}' stanza failed with: #{e}")
end

def homepage(homepage = nil)
assert_only_one_stanza_allowed :homepage, !homepage.nil?
@homepage ||= homepage
set_unique_stanza(:homepage, homepage.nil?) { homepage }
end

def language(*args, default: false, &block)
Expand Down Expand Up @@ -135,72 +141,51 @@ def language_eval
end

def url(*args, &block)
url_given = !args.empty? || block_given?
return @url unless url_given
assert_only_one_stanza_allowed :url, url_given
@url ||= begin
URL.from(*args, &block)
rescue StandardError => e
raise CaskInvalidError.new(token, "'url' stanza failed with: #{e}")
set_unique_stanza(:url, args.empty? && !block_given?) do
begin
URL.from(*args, &block)
end
end
end

def appcast(*args)
return @appcast if args.empty?
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
set_unique_stanza(:appcast, args.empty?) { DSL::Appcast.new(*args) }
end

def gpg(*args)
return @gpg if args.empty?
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
set_unique_stanza(:gpg, args.empty?) { DSL::Gpg.new(*args) }
end

def container(*args)
return @container if args.empty?
# TODO: remove this constraint, and instead merge multiple container stanzas
assert_only_one_stanza_allowed :container, !args.empty?
@container ||= begin
DSL::Container.new(*args) unless args.empty?
rescue StandardError => e
raise CaskInvalidError.new(token, e)
end
# TODO: remove this backward-compatibility section after removing nested_container
if @container && @container.nested
artifacts[:nested_container] << @container.nested
set_unique_stanza(:container, args.empty?) do
begin
DSL::Container.new(*args).tap do |container|
# TODO: remove this backward-compatibility section after removing nested_container
if container && container.nested
artifacts[:nested_container] << container.nested
end
end
end
end
@container
end

SYMBOLIC_VERSIONS = Set.new [
:latest,
]

def version(arg = nil)
return @version if arg.nil?
assert_only_one_stanza_allowed :version, !arg.nil?
raise CaskInvalidError.new(token, "invalid 'version' value: '#{arg.inspect}'") if !arg.is_a?(String) && !SYMBOLIC_VERSIONS.include?(arg)
@version ||= DSL::Version.new(arg)
set_unique_stanza(:version, arg.nil?) do
if !arg.is_a?(String) && arg != :latest
raise CaskInvalidError.new(token, "invalid 'version' value: '#{arg.inspect}'")
end
DSL::Version.new(arg)
end
end

SYMBOLIC_SHA256S = Set.new [
:no_check,
]

def sha256(arg = nil)
return @sha256 if arg.nil?
assert_only_one_stanza_allowed :sha256, !arg.nil?
raise CaskInvalidError.new(token, "invalid 'sha256' value: '#{arg.inspect}'") if !arg.is_a?(String) && !SYMBOLIC_SHA256S.include?(arg)
@sha256 ||= arg
set_unique_stanza(:sha256, arg.nil?) do
if !arg.is_a?(String) && arg != :no_check
raise CaskInvalidError.new(token, "invalid 'sha256' value: '#{arg.inspect}'")
end
arg
end
end

# depends_on uses a load method so that multiple stanzas can be merged
Expand All @@ -216,14 +201,8 @@ def depends_on(*args)
end

def conflicts_with(*args)
return @conflicts_with if args.empty?
# TODO: remove this constraint, and instead merge multiple conflicts_with stanzas
assert_only_one_stanza_allowed :conflicts_with, !args.empty?
@conflicts_with ||= begin
DSL::ConflictsWith.new(*args) unless args.empty?
rescue StandardError => e
raise CaskInvalidError.new(token, e)
end
set_unique_stanza(:conflicts_with, args.empty?) { DSL::ConflictsWith.new(*args) }
end

def artifacts
Expand Down Expand Up @@ -251,13 +230,11 @@ def caveats(*string, &block)
end

def accessibility_access(accessibility_access = nil)
assert_only_one_stanza_allowed :accessibility_access, !accessibility_access.nil?
@accessibility_access ||= accessibility_access
set_unique_stanza(:accessibility_access, accessibility_access.nil?) { accessibility_access }
end

def auto_updates(auto_updates = nil)
assert_only_one_stanza_allowed :auto_updates, !auto_updates.nil?
@auto_updates ||= auto_updates
set_unique_stanza(:auto_updates, auto_updates.nil?) { auto_updates }
end

ORDINARY_ARTIFACT_TYPES.each do |type|
Expand Down

0 comments on commit 05e9086

Please sign in to comment.