diff --git a/Library/Homebrew/cask/cask.rb b/Library/Homebrew/cask/cask.rb index b209511a94..4b612bd0c6 100644 --- a/Library/Homebrew/cask/cask.rb +++ b/Library/Homebrew/cask/cask.rb @@ -15,21 +15,20 @@ module Cask class Cask extend T::Sig - extend Enumerable extend Forwardable extend Searchable include Metadata attr_reader :token, :sourcefile_path, :source, :config, :default_config - def self.each(&block) - return to_enum unless block - - Tap.flat_map(&:cask_files).each do |f| - yield CaskLoader::FromTapPathLoader.new(f).load(config: nil) + def self.all + Tap.flat_map(&:cask_files).map do |f| + CaskLoader::FromTapPathLoader.new(f).load(config: nil) rescue CaskUnreadableError => e opoo e.message - end + + nil + end.compact end def tap diff --git a/Library/Homebrew/cmd/deps.rb b/Library/Homebrew/cmd/deps.rb index 534a49d1a8..a9f0ac3cd5 100644 --- a/Library/Homebrew/cmd/deps.rb +++ b/Library/Homebrew/cmd/deps.rb @@ -121,7 +121,7 @@ module Homebrew puts_deps_tree dependents, recursive: recursive, args: args return elsif args.all? - puts_deps sorted_dependents(Formula.to_a + Cask::Cask.to_a), recursive: recursive, args: args + puts_deps sorted_dependents(Formula.all + Cask::Cask.all), recursive: recursive, args: args return elsif !args.no_named? && args.for_each? puts_deps sorted_dependents(args.named.to_formulae_and_casks), recursive: recursive, args: args diff --git a/Library/Homebrew/cmd/info.rb b/Library/Homebrew/cmd/info.rb index 0c36d9b9d0..01433be06d 100644 --- a/Library/Homebrew/cmd/info.rb +++ b/Library/Homebrew/cmd/info.rb @@ -207,7 +207,7 @@ module Homebrew end when :v2 formulae, casks = if args.all? - [Formula.sort, Cask::Cask.to_a.sort_by(&:full_name)] + [Formula.sort, Cask::Cask.all.sort_by(&:full_name)] elsif args.installed? [Formula.installed.sort, Cask::Caskroom.casks.sort_by(&:full_name)] else diff --git a/Library/Homebrew/cmd/options.rb b/Library/Homebrew/cmd/options.rb index a34d055c9d..c97d57d528 100644 --- a/Library/Homebrew/cmd/options.rb +++ b/Library/Homebrew/cmd/options.rb @@ -34,7 +34,7 @@ module Homebrew args = options_args.parse if args.all? - puts_options Formula.to_a.sort, args: args + puts_options Formula.all.sort, args: args elsif args.installed? puts_options Formula.installed.sort, args: args elsif args.command.present? diff --git a/Library/Homebrew/cmd/update-report.rb b/Library/Homebrew/cmd/update-report.rb index fff584325c..aa5fb7efea 100644 --- a/Library/Homebrew/cmd/update-report.rb +++ b/Library/Homebrew/cmd/update-report.rb @@ -593,6 +593,7 @@ class ReporterHub private def dump_formula_report(key, title) + # TODO: 3.4.0: odisabled the old functionality and make this default only_installed = Homebrew::EnvConfig.update_report_only_installed? formulae = select_formula(key).sort.map do |name, new_name| diff --git a/Library/Homebrew/cmd/uses.rb b/Library/Homebrew/cmd/uses.rb index 3840aa361d..926b5991a8 100644 --- a/Library/Homebrew/cmd/uses.rb +++ b/Library/Homebrew/cmd/uses.rb @@ -101,10 +101,10 @@ module Homebrew deps else if show_formulae_and_casks || args.formula? - deps += args.installed? ? Formula.installed : Formula.to_a + deps += args.installed? ? Formula.installed : Formula.all end if show_formulae_and_casks || args.cask? - deps += args.installed? ? Cask::Caskroom.casks : Cask::Cask.to_a + deps += args.installed? ? Cask::Caskroom.casks : Cask::Cask.all end select_used_dependents(dependents(deps), used_formulae, recursive, includes, ignores) diff --git a/Library/Homebrew/compat/cask.rb b/Library/Homebrew/compat/cask.rb new file mode 100644 index 0000000000..daec219d0e --- /dev/null +++ b/Library/Homebrew/compat/cask.rb @@ -0,0 +1,21 @@ +# typed: true +# frozen_string_literal: true + +module Cask + class Cask + extend Enumerable + + def self.each(&block) + # TODO: 3.4.0: odeprecated "`Enumerable` methods on `Cask::Cask`", + # "`Cask::Cask.all` (but avoid looping over all casks, it's slow and insecure)" + + return to_enum unless block + + Tap.flat_map(&:cask_files).each do |f| + yield CaskLoader::FromTapPathLoader.new(f).load(config: nil) + rescue CaskUnreadableError => e + opoo e.message + end + end + end +end diff --git a/Library/Homebrew/compat/formula.rb b/Library/Homebrew/compat/formula.rb new file mode 100644 index 0000000000..013759f55d --- /dev/null +++ b/Library/Homebrew/compat/formula.rb @@ -0,0 +1,20 @@ +# typed: true +# frozen_string_literal: true + +class Formula + extend Enumerable + + def self.each(&_block) + # TODO: 3.4.0: odeprecated "`Enumerable` methods on `Formula`", + # "`Formula.all` (but avoid looping over all formulae, it's slow and insecure)" + + files.each do |file| + yield Formulary.factory(file) + rescue FormulaUnavailableError, FormulaUnreadableError => e + # Don't let one broken formula break commands. But do complain. + onoe "Failed to import: #{file}" + $stderr.puts e + next + end + end +end diff --git a/Library/Homebrew/compat/late.rb b/Library/Homebrew/compat/late.rb index b4806eee61..6e36cebe11 100644 --- a/Library/Homebrew/compat/late.rb +++ b/Library/Homebrew/compat/late.rb @@ -1,2 +1,5 @@ # typed: strict # frozen_string_literal: true + +require "compat/formula" +require "compat/cask" diff --git a/Library/Homebrew/description_cache_store.rb b/Library/Homebrew/description_cache_store.rb index b867f51c09..22a2a6b8dc 100644 --- a/Library/Homebrew/description_cache_store.rb +++ b/Library/Homebrew/description_cache_store.rb @@ -36,7 +36,7 @@ class DescriptionCacheStore < CacheStore def populate_if_empty! return unless database.empty? - Formula.each { |f| update!(f.full_name, f.desc) } + Formula.all.each { |f| update!(f.full_name, f.desc) } end # Use an update report to update the {DescriptionCacheStore}. diff --git a/Library/Homebrew/dev-cmd/audit.rb b/Library/Homebrew/dev-cmd/audit.rb index b3b03d8bb4..9bd543366a 100644 --- a/Library/Homebrew/dev-cmd/audit.rb +++ b/Library/Homebrew/dev-cmd/audit.rb @@ -121,7 +121,7 @@ module Homebrew end elsif args.no_named? no_named_args = true - [Formula, Cask::Cask.to_a] + [Formula.all, Cask::Cask.all] else args.named.to_formulae_and_casks .partition { |formula_or_cask| formula_or_cask.is_a?(Formula) } diff --git a/Library/Homebrew/dev-cmd/bump-formula-pr.rb b/Library/Homebrew/dev-cmd/bump-formula-pr.rb index dc29cc16e2..c2491dba3b 100644 --- a/Library/Homebrew/dev-cmd/bump-formula-pr.rb +++ b/Library/Homebrew/dev-cmd/bump-formula-pr.rb @@ -367,7 +367,7 @@ module Homebrew base_url = url_split.first(components_to_match).join("/") base_url = /#{Regexp.escape(base_url)}/ guesses = [] - Formula.each do |f| + Formula.all.each do |f| guesses << f if f.stable&.url&.match(base_url) end return guesses.shift if guesses.count == 1 diff --git a/Library/Homebrew/dev-cmd/livecheck.rb b/Library/Homebrew/dev-cmd/livecheck.rb index c702bc4fc3..d857bd3d99 100644 --- a/Library/Homebrew/dev-cmd/livecheck.rb +++ b/Library/Homebrew/dev-cmd/livecheck.rb @@ -71,8 +71,8 @@ module Homebrew casks = args.formula? ? [] : Cask::Caskroom.casks formulae + casks elsif args.all? - formulae = args.cask? ? [] : Formula.to_a - casks = args.formula? ? [] : Cask::Cask.to_a + formulae = args.cask? ? [] : Formula.all + casks = args.formula? ? [] : Cask::Cask.all formulae + casks elsif args.named.present? if args.formula? diff --git a/Library/Homebrew/dev-cmd/unbottled.rb b/Library/Homebrew/dev-cmd/unbottled.rb index 882ae23127..950e69d25d 100644 --- a/Library/Homebrew/dev-cmd/unbottled.rb +++ b/Library/Homebrew/dev-cmd/unbottled.rb @@ -79,9 +79,9 @@ module Homebrew if args.named.present? formulae = all_formulae = args.named.to_formulae elsif args.total? - formulae = all_formulae = Formula.to_a + formulae = all_formulae = Formula.all elsif args.dependents? - formulae = all_formulae = Formula.to_a + formulae = all_formulae = Formula.all @sort = " (sorted by number of dependents)" else diff --git a/Library/Homebrew/extend/os/mac/search.rb b/Library/Homebrew/extend/os/mac/search.rb index a2f2af139b..4fee7f8f37 100644 --- a/Library/Homebrew/extend/os/mac/search.rb +++ b/Library/Homebrew/extend/os/mac/search.rb @@ -15,7 +15,7 @@ module Homebrew return if args.formula? ohai "Casks" - Cask::Cask.to_a.extend(Searchable) + Cask::Cask.all.extend(Searchable) .search(string_or_regex, &:name) .each do |cask| puts "#{Tty.bold}#{cask.token}:#{Tty.reset} #{cask.name.join(", ")}" diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 55e6db6273..76b6545a24 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -64,8 +64,7 @@ class Formula include Utils::Shebang include Utils::Shell include Context - include OnOS # TODO: 3.3.0: deprecate OnOS usage in instance methods. - extend Enumerable + include OnOS # TODO: 3.4.0: odeprecate OnOS usage in instance methods. extend Forwardable extend Cachable extend Predicable @@ -1682,16 +1681,18 @@ class Formula @full_names ||= core_names + tap_names end + # an array of all {Formula} # @private - def self.each(&_block) - files.each do |file| - yield Formulary.factory(file) + def self.all + files.map do |file| + Formulary.factory(file) rescue FormulaUnavailableError, FormulaUnreadableError => e # Don't let one broken formula break commands. But do complain. onoe "Failed to import: #{file}" $stderr.puts e - next - end + + nil + end.compact end # An array of all racks currently installed.