From a41f15ede7730bad6ae40d6921b37408b1b86f31 Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Sun, 11 Aug 2024 16:19:51 -0700 Subject: [PATCH 1/2] Make FromContentLoader.try_new typesafe --- Library/Homebrew/cask/cask_loader.rb | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cask/cask_loader.rb b/Library/Homebrew/cask/cask_loader.rb index d45d55113f..93f4a67f16 100644 --- a/Library/Homebrew/cask/cask_loader.rb +++ b/Library/Homebrew/cask/cask_loader.rb @@ -51,10 +51,14 @@ module Cask # Loads a cask from a string. class FromContentLoader < AbstractContentLoader + sig { + params(ref: T.any(Pathname, String, URI::Generic), warn: T::Boolean) + .returns(T.nilable(T.attached_class)) + } def self.try_new(ref, warn: false) - return false unless ref.respond_to?(:to_str) + return if ref.is_a?(URI::Generic) - content = T.unsafe(ref).to_str + content = ref.to_str # Cache compiled regex @regex ||= begin From c799f5f8188ae04cdd8b106523bc9c88322eb6fc Mon Sep 17 00:00:00 2001 From: Douglas Eichelberger Date: Sun, 11 Aug 2024 18:01:04 -0700 Subject: [PATCH 2/2] Allow Cask refs --- Library/Homebrew/cask/cask_loader.rb | 29 +++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) 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 }