diff --git a/Library/Homebrew/cask/lib/hbc.rb b/Library/Homebrew/cask/lib/hbc.rb index 916a7ef349..a013f4a323 100644 --- a/Library/Homebrew/cask/lib/hbc.rb +++ b/Library/Homebrew/cask/lib/hbc.rb @@ -19,7 +19,6 @@ require "hbc/locations" require "hbc/config" require "hbc/macos" require "hbc/pkg" -require "hbc/scopes" require "hbc/staged" require "hbc/system_command" require "hbc/topological_hash" @@ -30,7 +29,6 @@ require "hbc/version" module Hbc include Locations - include Scopes include Utils def self.init diff --git a/Library/Homebrew/cask/lib/hbc/cask.rb b/Library/Homebrew/cask/lib/hbc/cask.rb index 341f80047f..410dcfe649 100644 --- a/Library/Homebrew/cask/lib/hbc/cask.rb +++ b/Library/Homebrew/cask/lib/hbc/cask.rb @@ -52,11 +52,13 @@ module Hbc end def full_name - if tap.nil? || tap == Hbc.default_tap - token - else - "#{tap.name}/#{token}" - end + return token if tap == Hbc.default_tap + qualified_token + end + + def qualified_token + return token if tap.nil? + "#{tap.name}/#{token}" end def installed? diff --git a/Library/Homebrew/cask/lib/hbc/caskroom.rb b/Library/Homebrew/cask/lib/hbc/caskroom.rb index b5c947f4b1..6c69e5fb8e 100644 --- a/Library/Homebrew/cask/lib/hbc/caskroom.rb +++ b/Library/Homebrew/cask/lib/hbc/caskroom.rb @@ -15,5 +15,17 @@ module Hbc SystemCommand.run("/usr/sbin/chown", args: [Utils.current_user, Hbc.caskroom], sudo: sudo) SystemCommand.run("/usr/bin/chgrp", args: ["admin", Hbc.caskroom], sudo: sudo) end + + def casks + Pathname.glob(Hbc.caskroom.join("*")).sort.select(&:directory?).map do |path| + token = path.basename.to_s + + if tap_path = CaskLoader.tap_paths(token).first + next CaskLoader::FromTapPathLoader.new(tap_path).load + end + + CaskLoader::FromPathLoader.new(Pathname.glob(path.join(".metadata/*/*/*/*.rb")).first).load + end + end end end diff --git a/Library/Homebrew/cask/lib/hbc/cli/audit.rb b/Library/Homebrew/cask/lib/hbc/cli/audit.rb index 35d82800c6..2b89a8d2c3 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/audit.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/audit.rb @@ -9,7 +9,7 @@ module Hbc end def run - failed_casks = casks(alternative: -> { Hbc.all }) + failed_casks = casks(alternative: -> { Cask.to_a }) .reject { |cask| audit(cask) } return if failed_casks.empty? diff --git a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb index bdb1c0c30a..355f1a4d3f 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/internal_stanza.rb @@ -58,7 +58,7 @@ module Hbc @stanza = :artifacts end - casks(alternative: -> { Hbc.all }).each do |cask| + casks(alternative: -> { Cask.to_a }).each do |cask| print "#{cask}\t" if table? begin diff --git a/Library/Homebrew/cask/lib/hbc/cli/list.rb b/Library/Homebrew/cask/lib/hbc/cli/list.rb index 4a50ae74ad..788b0eede5 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/list.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/list.rb @@ -37,7 +37,7 @@ module Hbc end def list_installed - installed_casks = Hbc.installed + installed_casks = Caskroom.casks if one? puts installed_casks.map(&:to_s) diff --git a/Library/Homebrew/cask/lib/hbc/cli/outdated.rb b/Library/Homebrew/cask/lib/hbc/cli/outdated.rb index b0a84e8d26..593fea19da 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/outdated.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/outdated.rb @@ -10,7 +10,7 @@ module Hbc end def run - casks(alternative: -> { Hbc.installed }).each do |cask| + casks(alternative: -> { Caskroom.casks }).each do |cask| odebug "Checking update info of Cask #{cask}" self.class.list_if_outdated(cask, greedy?, verbose?) end diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb index d7fac8cd72..480d7b5e54 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/search.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/search.rb @@ -3,7 +3,7 @@ module Hbc class Search < AbstractCommand def run if args.empty? - puts Formatter.columns(CLI.nice_listing(Hbc.all_tokens)) + puts Formatter.columns(CLI.nice_listing(Cask.map(&:qualified_token))) else results = self.class.search(*args) self.class.render_results(*results) @@ -43,7 +43,7 @@ module Hbc partial_matches = [] search_term = arguments.join(" ") search_regexp = extract_regexp arguments.first - all_tokens = CLI.nice_listing(Hbc.all_tokens) + all_tokens = CLI.nice_listing(Cask.map(&:qualified_token)) if search_regexp search_term = arguments.first partial_matches = all_tokens.grep(/#{search_regexp}/i) diff --git a/Library/Homebrew/cask/lib/hbc/cli/style.rb b/Library/Homebrew/cask/lib/hbc/cli/style.rb index 261bed50b9..cb2e5e0537 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/style.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/style.rb @@ -27,7 +27,7 @@ module Hbc def cask_paths @cask_paths ||= if args.empty? - Hbc.all_tapped_cask_dirs + Tap.map(&:cask_dir).select(&:directory?) elsif args.any? { |file| File.exist?(file) } args else diff --git a/Library/Homebrew/cask/lib/hbc/cli/upgrade.rb b/Library/Homebrew/cask/lib/hbc/cli/upgrade.rb index 808b64c4b7..276b180aa1 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/upgrade.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/upgrade.rb @@ -13,7 +13,7 @@ module Hbc def run outdated_casks = casks(alternative: lambda { - Hbc.installed.select do |cask| + Caskroom.casks.select do |cask| cask.outdated?(greedy?) end }).select { |cask| cask.outdated?(true) } diff --git a/Library/Homebrew/cask/lib/hbc/scopes.rb b/Library/Homebrew/cask/lib/hbc/scopes.rb deleted file mode 100644 index a469ae0fa6..0000000000 --- a/Library/Homebrew/cask/lib/hbc/scopes.rb +++ /dev/null @@ -1,47 +0,0 @@ -module Hbc - module Scopes - def self.included(base) - base.extend(ClassMethods) - end - - module ClassMethods - def all - all_tokens.map(&CaskLoader.public_method(:load)) - end - - def all_tapped_cask_dirs - Tap.map(&:cask_dir).select(&:directory?) - end - - def all_tokens - Tap.flat_map do |t| - t.cask_files.map do |p| - "#{t.name}/#{File.basename(p, ".rb")}" - end - end - end - - def installed - # CaskLoader.load has some DWIM which is slow. Optimize here - # by spoon-feeding CaskLoader.load fully-qualified paths. - # TODO: speed up Hbc::Source::Tapped (main perf drag is calling Hbc.all_tokens repeatedly) - # TODO: ability to specify expected source when calling CaskLoader.load (minor perf benefit) - Pathname.glob(caskroom.join("*")) - .sort - .map do |caskroom_path| - token = caskroom_path.basename.to_s - - path_to_cask = all_tapped_cask_dirs.find do |tap_dir| - tap_dir.join("#{token}.rb").exist? - end - - if path_to_cask - CaskLoader.load(path_to_cask.join("#{token}.rb")) - else - CaskLoader.load(token) - end - end - end - end - end -end diff --git a/Library/Homebrew/test/cask/cask_spec.rb b/Library/Homebrew/test/cask/cask_spec.rb index e8d5aff8f8..4b9393d6cc 100644 --- a/Library/Homebrew/test/cask/cask_spec.rb +++ b/Library/Homebrew/test/cask/cask_spec.rb @@ -70,14 +70,6 @@ describe Hbc::Cask, :cask do end end - describe "all_tokens" do - it "returns a token for every Cask" do - all_cask_tokens = Hbc.all_tokens - expect(all_cask_tokens.count).to be > 20 - all_cask_tokens.each { |token| expect(token).to be_kind_of(String) } - end - end - describe "metadata" do it "proposes a versioned metadata directory name for each instance" do cask_token = "local-caffeine" diff --git a/Library/Homebrew/test/cask/cli/audit_spec.rb b/Library/Homebrew/test/cask/cli/audit_spec.rb index da8bf12737..2ed4ea9f11 100644 --- a/Library/Homebrew/test/cask/cli/audit_spec.rb +++ b/Library/Homebrew/test/cask/cli/audit_spec.rb @@ -7,9 +7,7 @@ describe Hbc::CLI::Audit, :cask do describe "selection of Casks to audit" do it "audits all Casks if no tokens are given" do - expect(cask).to be_a Hbc::Cask - - allow(Hbc).to receive(:all).and_return([cask, cask]) + allow(Hbc::Cask).to receive(:to_a).and_return([cask, cask]) expect(Hbc::Auditor).to receive(:audit).twice.and_return(true) diff --git a/Library/Homebrew/test/cask/cli/style_spec.rb b/Library/Homebrew/test/cask/cli/style_spec.rb index aaf3248f17..6f486ea939 100644 --- a/Library/Homebrew/test/cask/cli/style_spec.rb +++ b/Library/Homebrew/test/cask/cli/style_spec.rb @@ -83,11 +83,13 @@ describe Hbc::CLI::Style, :cask do context "when no cask tokens are given" do let(:tokens) { [] } - before do - allow(Hbc).to receive(:all_tapped_cask_dirs).and_return(%w[Casks MoreCasks]) + matcher :a_path_ending_with do |end_string| + match do |actual| + expect(actual.to_s).to end_with(end_string) + end end - it { is_expected.to eq(%w[Casks MoreCasks]) } + it { is_expected.to contain_exactly(a_path_ending_with("/caskroom/homebrew-spec/Casks"), a_path_ending_with("/third-party/homebrew-tap/Casks")) } end context "when at least one cask token is a path that exists" do diff --git a/Library/Homebrew/test/cask/scopes_spec.rb b/Library/Homebrew/test/cask/scopes_spec.rb deleted file mode 100644 index d1c03b9b0c..0000000000 --- a/Library/Homebrew/test/cask/scopes_spec.rb +++ /dev/null @@ -1,37 +0,0 @@ -describe Hbc::Scopes, :cask do - describe "installed" do - it "returns a list installed Casks by loading Casks for all the dirs that exist in the caskroom" do - allow(Hbc::CaskLoader).to receive(:load) { |token| "loaded-#{token}" } - - Hbc.caskroom.join("cask-bar").mkpath - Hbc.caskroom.join("cask-foo").mkpath - - installed_casks = Hbc.installed - - expect(Hbc::CaskLoader).to have_received(:load).with("cask-bar") - expect(Hbc::CaskLoader).to have_received(:load).with("cask-foo") - expect(installed_casks).to eq( - %w[ - loaded-cask-bar - loaded-cask-foo - ], - ) - end - - it "optimizes performance by resolving to a fully qualified path before calling Hbc::CaskLoader.load" do - fake_tapped_cask_dir = Pathname.new(Dir.mktmpdir).join("Casks") - absolute_path_to_cask = fake_tapped_cask_dir.join("some-cask.rb") - - allow(Hbc::CaskLoader).to receive(:load) - allow(Hbc).to receive(:all_tapped_cask_dirs) { [fake_tapped_cask_dir] } - - Hbc.caskroom.join("some-cask").mkdir - fake_tapped_cask_dir.mkdir - FileUtils.touch(absolute_path_to_cask) - - Hbc.installed - - expect(Hbc::CaskLoader).to have_received(:load).with(absolute_path_to_cask) - end - end -end