Skip to content

Commit

Permalink
Merge pull request #10973 from reitermarkus/nested-url-do
Browse files Browse the repository at this point in the history
Implement nested `url do` API.
  • Loading branch information
reitermarkus committed Apr 2, 2021
2 parents 912f260 + c07205c commit 2bd0b6e
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 51 deletions.
20 changes: 14 additions & 6 deletions Library/Homebrew/cask/audit.rb
Expand Up @@ -308,7 +308,7 @@ def check_latest_with_auto_updates
LIVECHECK_REFERENCE_URL = "https://github.com/Homebrew/homebrew-cask/blob/HEAD/doc/cask_language_reference/stanzas/livecheck.md"

def check_hosting_with_livecheck(livecheck_result:)
return if cask.appcast || cask.livecheckable?
return if block_url_offline? || cask.appcast || cask.livecheckable?
return if livecheck_result == :auto_detected

add_livecheck = "please add a livecheck. See #{Formatter.url(LIVECHECK_REFERENCE_URL)}"
Expand Down Expand Up @@ -419,9 +419,16 @@ def file_url?
URI(cask.url.to_s).scheme == "file"
end

def block_url_offline?
return false if online?

cask.url.from_block?
end

VERIFIED_URL_REFERENCE_URL = "https://github.com/Homebrew/homebrew-cask/blob/master/doc/cask_language_reference/stanzas/url.md#when-url-and-homepage-hostnames-differ-add-verified"

def check_unnecessary_verified
return if block_url_offline?
return unless verified_present?
return unless url_match_homepage?
return unless verified_matches_url?
Expand All @@ -432,7 +439,7 @@ def check_unnecessary_verified
end

def check_missing_verified
return if cask.url.from_block?
return if block_url_offline?
return if file_url?
return if url_match_homepage?
return if verified_present?
Expand All @@ -443,6 +450,7 @@ def check_missing_verified
end

def check_no_match
return if block_url_offline?
return unless verified_present?
return if verified_matches_url?

Expand Down Expand Up @@ -592,7 +600,7 @@ def check_github_prerelease_version
return if cask.tap == "homebrew/cask-versions"

odebug "Auditing GitHub prerelease"
user, repo = get_repo_data(%r{https?://github\.com/([^/]+)/([^/]+)/?.*}) if @online
user, repo = get_repo_data(%r{https?://github\.com/([^/]+)/([^/]+)/?.*}) if online?
return if user.nil?

tag = SharedAudits.github_tag_from_url(cask.url)
Expand All @@ -604,7 +612,7 @@ def check_github_prerelease_version
def check_gitlab_prerelease_version
return if cask.tap == "homebrew/cask-versions"

user, repo = get_repo_data(%r{https?://gitlab\.com/([^/]+)/([^/]+)/?.*}) if @online
user, repo = get_repo_data(%r{https?://gitlab\.com/([^/]+)/([^/]+)/?.*}) if online?
return if user.nil?

odebug "Auditing GitLab prerelease"
Expand All @@ -616,7 +624,7 @@ def check_gitlab_prerelease_version
end

def check_github_repository_archived
user, repo = get_repo_data(%r{https?://github\.com/([^/]+)/([^/]+)/?.*}) if @online
user, repo = get_repo_data(%r{https?://github\.com/([^/]+)/([^/]+)/?.*}) if online?
return if user.nil?

odebug "Auditing GitHub repo archived"
Expand All @@ -636,7 +644,7 @@ def check_github_repository_archived
end

def check_gitlab_repository_archived
user, repo = get_repo_data(%r{https?://gitlab\.com/([^/]+)/([^/]+)/?.*}) if @online
user, repo = get_repo_data(%r{https?://gitlab\.com/([^/]+)/([^/]+)/?.*}) if online?
return if user.nil?

odebug "Auditing GitLab repo archived"
Expand Down
34 changes: 23 additions & 11 deletions Library/Homebrew/cask/dsl.rb
Expand Up @@ -96,13 +96,15 @@ def initialize(cask)
@token = cask.token
end

# @api public
def name(*args)
@name ||= []
return @name if args.empty?

@name.concat(args.flatten)
end

# @api public
def desc(description = nil)
set_unique_stanza(:desc, description.nil?) { description }
end
Expand All @@ -121,6 +123,7 @@ def set_unique_stanza(stanza, should_return)
raise CaskInvalidError.new(cask, "'#{stanza}' stanza failed with: #{e}")
end

# @api public
def homepage(homepage = nil)
set_unique_stanza(:homepage, homepage.nil?) { homepage }
end
Expand Down Expand Up @@ -176,32 +179,32 @@ def languages
@language_blocks.keys.flatten
end

def url(*args, **options)
# @api public
def url(*args, **options, &block)
caller_location = caller_locations[0]

set_unique_stanza(:url, args.empty? && options.empty? && !block_given?) do
if block_given?
LazyObject.new do
*args = yield
options = args.last.is_a?(Hash) ? args.pop : {}
URL.new(*args, **options, from_block: true, caller_location: caller_location)
end
set_unique_stanza(:url, args.empty? && options.empty? && !block) do
if block
URL.new(*args, **options, caller_location: caller_location, dsl: self, &block)
else
URL.new(*args, **options, caller_location: caller_location)
end
end
end

# @api public
def appcast(*args)
set_unique_stanza(:appcast, args.empty?) { DSL::Appcast.new(*args) }
end

# @api public
def container(*args)
set_unique_stanza(:container, args.empty?) do
DSL::Container.new(*args)
end
end

# @api public
def version(arg = nil)
set_unique_stanza(:version, arg.nil?) do
if !arg.is_a?(String) && arg != :latest
Expand All @@ -212,6 +215,7 @@ def version(arg = nil)
end
end

# @api public
def sha256(arg = nil)
set_unique_stanza(:sha256, arg.nil?) do
case arg
Expand All @@ -226,6 +230,7 @@ def sha256(arg = nil)
end

# `depends_on` uses a load method so that multiple stanzas can be merged.
# @api public
def depends_on(*args)
@depends_on ||= DSL::DependsOn.new
return @depends_on if args.empty?
Expand All @@ -238,6 +243,7 @@ def depends_on(*args)
@depends_on
end

# @api public
def conflicts_with(*args)
# TODO: remove this constraint, and instead merge multiple conflicts_with stanzas
set_unique_stanza(:conflicts_with, args.empty?) { DSL::ConflictsWith.new(*args) }
Expand All @@ -251,13 +257,15 @@ def caskroom_path
cask.caskroom_path
end

# @api public
def staged_path
return @staged_path if @staged_path

cask_version = version || :unknown
@staged_path = caskroom_path.join(cask_version.to_s)
end

# @api public
def caveats(*strings, &block)
@caveats ||= DSL::Caveats.new(cask)
if block
Expand All @@ -276,10 +284,12 @@ def discontinued?
@caveats&.discontinued?
end

# @api public
def auto_updates(auto_updates = nil)
set_unique_stanza(:auto_updates, auto_updates.nil?) { auto_updates }
end

# @api public
def livecheck(&block)
@livecheck ||= Livecheck.new(self)
return @livecheck unless block
Expand Down Expand Up @@ -317,8 +327,6 @@ def livecheckable?
end
end

# No need to define it as it's the default/superclass implementation.
# rubocop:disable Style/MissingRespondToMissing
def method_missing(method, *)
if method
Utils.method_missing_message(method, token)
Expand All @@ -327,8 +335,12 @@ def method_missing(method, *)
super
end
end
# rubocop:enable Style/MissingRespondToMissing

def respond_to_missing?(*)
true
end

# @api public
def appdir
cask.config.appdir
end
Expand Down

0 comments on commit 2bd0b6e

Please sign in to comment.