diff --git a/Library/Homebrew/extend/api_hashable.rb b/Library/Homebrew/extend/api_hashable.rb index e1e4098e9516b..9719568adf21c 100644 --- a/Library/Homebrew/extend/api_hashable.rb +++ b/Library/Homebrew/extend/api_hashable.rb @@ -3,6 +3,43 @@ # Used to substitute common paths with generic placeholders when generating JSON for the API. module APIHashable + extend T::Helpers + + requires_ancestor { Module } + + module CaskURLFallbackExtension + extend T::Helpers + + requires_ancestor { Kernel } + requires_ancestor { Cask::Cask } + + def url(...) + url = super + + # URLs with blocks are lazy-evaluated, so we let's force an evaluation to ensure the URL can be determined + begin + url.to_s + rescue + raise unless self.class.generating_hash? + raise unless (json_cask = Homebrew::API::Cask.all_casks[token]) + + prev_url = json_cask["url"] + prev_specs = json_cask["url_specs"] || {} + url = Cask::URL.new(prev_url, **prev_specs) + + opoo "Unable to determine URL for #{token}. Falling back to previous value: #{url}" + end + + url + end + end + + def self.extended(base) + return if base != Cask::Cask + + base.prepend CaskURLFallbackExtension + end + def generating_hash! return if generating_hash?