diff --git a/Library/Homebrew/cask/cask_loader.rb b/Library/Homebrew/cask/cask_loader.rb index 93f4a67f16..843c0d3415 100644 --- a/Library/Homebrew/cask/cask_loader.rb +++ b/Library/Homebrew/cask/cask_loader.rb @@ -52,25 +52,28 @@ module Cask # Loads a cask from a string. class FromContentLoader < AbstractContentLoader sig { - params(ref: T.any(Pathname, String, URI::Generic), warn: T::Boolean) + params(ref: T.any(Pathname, String, Cask, URI::Generic), warn: T::Boolean) .returns(T.nilable(T.attached_class)) } def self.try_new(ref, warn: false) - return if ref.is_a?(URI::Generic) + case ref + when Cask, URI::Generic + # do nothing + else + content = ref.to_str - content = ref.to_str + # Cache compiled regex + @regex ||= begin + token = /(?:"[^"]*"|'[^']*')/ + curly = /\(\s*#{token.source}\s*\)\s*\{.*\}/ + do_end = /\s+#{token.source}\s+do(?:\s*;\s*|\s+).*end/ + /\A\s*cask(?:#{curly.source}|#{do_end.source})\s*\Z/m + end - # Cache compiled regex - @regex ||= begin - token = /(?:"[^"]*"|'[^']*')/ - curly = /\(\s*#{token.source}\s*\)\s*\{.*\}/ - do_end = /\s+#{token.source}\s+do(?:\s*;\s*|\s+).*end/ - /\A\s*cask(?:#{curly.source}|#{do_end.source})\s*\Z/m + return unless content.match?(@regex) + + new(content) end - - return unless content.match?(@regex) - - new(content) end sig { params(content: String, tap: Tap).void }