From da193d2f7efc2079db8da8056e53fc458d5e2036 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 9 Jun 2018 12:20:58 +0200 Subject: [PATCH 1/7] Merge `brew cask search` into `brew search`. --- Library/Homebrew/cask/lib/hbc.rb | 2 - Library/Homebrew/cask/lib/hbc/cask.rb | 1 + Library/Homebrew/cask/lib/hbc/cli.rb | 3 + Library/Homebrew/cask/lib/hbc/container.rb | 2 + Library/Homebrew/cask/lib/hbc/dsl.rb | 5 ++ Library/Homebrew/cmd/search.rb | 66 +++++++++++++++------- Library/Homebrew/search.rb | 5 +- Library/Homebrew/test/cmd/search_spec.rb | 4 +- 8 files changed, 59 insertions(+), 29 deletions(-) diff --git a/Library/Homebrew/cask/lib/hbc.rb b/Library/Homebrew/cask/lib/hbc.rb index 9c8c67f518..737c930d10 100644 --- a/Library/Homebrew/cask/lib/hbc.rb +++ b/Library/Homebrew/cask/lib/hbc.rb @@ -15,13 +15,11 @@ require "hbc/download" require "hbc/download_strategy" require "hbc/exceptions" require "hbc/installer" -require "hbc/config" require "hbc/macos" require "hbc/pkg" require "hbc/staged" require "hbc/system_command" require "hbc/topological_hash" -require "hbc/url" require "hbc/utils" require "hbc/verify" require "hbc/version" diff --git a/Library/Homebrew/cask/lib/hbc/cask.rb b/Library/Homebrew/cask/lib/hbc/cask.rb index d41e2d5221..ca8e51db0d 100644 --- a/Library/Homebrew/cask/lib/hbc/cask.rb +++ b/Library/Homebrew/cask/lib/hbc/cask.rb @@ -1,4 +1,5 @@ require "hbc/cask_loader" +require "hbc/config" require "hbc/dsl" require "hbc/metadata" require "searchable" diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index 975f8a7dfd..df3271a5d9 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -2,6 +2,9 @@ require "optparse" require "shellwords" require "extend/optparse" + +require "hbc/config" + require "hbc/cli/options" require "hbc/cli/abstract_command" diff --git a/Library/Homebrew/cask/lib/hbc/container.rb b/Library/Homebrew/cask/lib/hbc/container.rb index fab3a3c1c3..3806df33f6 100644 --- a/Library/Homebrew/cask/lib/hbc/container.rb +++ b/Library/Homebrew/cask/lib/hbc/container.rb @@ -1,3 +1,5 @@ +require "hbc/utils" + require "hbc/container/base" require "hbc/container/air" require "hbc/container/bzip2" diff --git a/Library/Homebrew/cask/lib/hbc/dsl.rb b/Library/Homebrew/cask/lib/hbc/dsl.rb index 7492b59017..5ed0fb5e99 100644 --- a/Library/Homebrew/cask/lib/hbc/dsl.rb +++ b/Library/Homebrew/cask/lib/hbc/dsl.rb @@ -2,6 +2,9 @@ require "locale" require "hbc/artifact" +require "hbc/caskroom" +require "hbc/exceptions" + require "hbc/dsl/appcast" require "hbc/dsl/base" require "hbc/dsl/caveats" @@ -16,6 +19,8 @@ require "hbc/dsl/uninstall_postflight" require "hbc/dsl/uninstall_preflight" require "hbc/dsl/version" +require "hbc/url" + module Hbc class DSL ORDINARY_ARTIFACT_CLASSES = [ diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb index dd45fe63ad..a6117b5029 100644 --- a/Library/Homebrew/cmd/search.rb +++ b/Library/Homebrew/cmd/search.rb @@ -18,6 +18,7 @@ require "missing_formula" require "descriptions" require "cli_parser" require "search" +require "hbc/cask_loader" module Homebrew module_function @@ -58,32 +59,57 @@ module Homebrew query = args.remaining.join(" ") string_or_regex = query_regexp(query) Descriptions.search(string_or_regex, :desc).print - elsif args.remaining.first =~ HOMEBREW_TAP_FORMULA_REGEX - query = args.remaining.first - - results = begin - [Formulary.factory(query).name] - rescue FormulaUnavailableError - _, _, name = query.split("/", 3) - remote_results = search_taps(name) - [*remote_results[:formulae], *remote_results[:casks]].sort - end - - puts Formatter.columns(results) unless results.empty? else query = args.remaining.join(" ") string_or_regex = query_regexp(query) - local_results = search_formulae(string_or_regex) - puts Formatter.columns(local_results.sort) unless local_results.empty? - remote_results = search_taps(query) - tap_results = [*remote_results[:formulae], *remote_results[:casks]].sort - puts Formatter.columns(tap_results) unless tap_results.empty? + remote_results = if query.match?(HOMEBREW_TAP_FORMULA_REGEX) || query.match?(HOMEBREW_TAP_CASK_REGEX) + _, _, name = query.split("/", 3) + search_taps(name, silent: true) + else + search_taps(query, silent: true) + end + + local_formulae = if query.match?(HOMEBREW_TAP_FORMULA_REGEX) + begin + [Formulary.factory(query).name] + rescue FormulaUnavailableError + [] + end + else + search_formulae(string_or_regex) + end + + remote_formulae = remote_results[:formulae] + all_formulae = local_formulae + remote_formulae + + local_casks = if query.match?(HOMEBREW_TAP_CASK_REGEX) + begin + [Hbc::CaskLoader.load(query).token] + rescue Hbc::CaskUnavailableError + [] + end + else + search_casks(string_or_regex) + end + + remote_casks = remote_results[:casks] + all_casks = local_casks + remote_casks + + if all_formulae.any? + ohai "Formulae" + puts Formatter.columns(all_formulae) + end + + if all_casks.any? + puts if all_formulae.any? + ohai "Casks" + puts Formatter.columns(all_casks) + end if $stdout.tty? - count = local_results.length + tap_results.length + count = all_formulae.count + all_casks.count - ohai "Searching blacklisted, migrated and deleted formulae..." if reason = MissingFormula.reason(query, silent: true) if count.positive? puts @@ -91,7 +117,7 @@ module Homebrew end puts reason elsif count.zero? - puts "No formula found for #{query.inspect}." + puts "No formula or cask found for #{query.inspect}." GitHub.print_pull_requests_matching(query) end end diff --git a/Library/Homebrew/search.rb b/Library/Homebrew/search.rb index 736a854f81..f43edaf871 100644 --- a/Library/Homebrew/search.rb +++ b/Library/Homebrew/search.rb @@ -40,7 +40,7 @@ module Homebrew tap = Tap.fetch(match["repository"]["full_name"]) full_name = "#{tap.name}/#{name}" - next if tap.installed? && !match["path"].start_with?("Casks/") + next if tap.installed? if match["path"].start_with?("Casks/") results[:casks] = [*results[:casks], full_name].sort @@ -53,9 +53,6 @@ module Homebrew end def search_formulae(string_or_regex) - # Use stderr to avoid breaking parsed output - $stderr.puts Formatter.headline("Searching local taps...", color: :blue) - aliases = Formula.alias_full_names results = (Formula.full_names + aliases) .extend(Searchable) diff --git a/Library/Homebrew/test/cmd/search_spec.rb b/Library/Homebrew/test/cmd/search_spec.rb index 556c863ca2..cae8e97fdc 100644 --- a/Library/Homebrew/test/cmd/search_spec.rb +++ b/Library/Homebrew/test/cmd/search_spec.rb @@ -15,7 +15,6 @@ describe "brew search", :integration_test do it "supports searching by name" do expect { brew "search", "testball" } .to output(/testball/).to_stdout - .and output(/Searching/).to_stderr .and be_a_success end @@ -26,12 +25,11 @@ describe "brew search", :integration_test do .and be_a_success end - it "falls back to a GitHub tap search when no formula is found", :needs_network, retry: 3 do + it "falls back to a GitHub tap search when no formula is found", :needs_macos, :needs_network, retry: 3 do setup_remote_tap "homebrew/cask" expect { brew "search", "homebrew/cask/firefox" } .to output(/firefox/).to_stdout - .and output(/Searching/).to_stderr .and be_a_success end From 8c620d8b973a588961001e01312ad65c1ca1276c Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Sat, 9 Jun 2018 15:40:44 +0200 Subject: [PATCH 2/7] Redirect `brew cask search` to `brew search`. --- Library/Homebrew/cask/lib/hbc/cli.rb | 1 - .../cask/lib/hbc/cli/abstract_command.rb | 4 +- Library/Homebrew/cask/lib/hbc/cli/search.rb | 53 ------------------- Library/Homebrew/compat/hbc.rb | 1 + Library/Homebrew/compat/hbc/cli.rb | 2 +- Library/Homebrew/compat/hbc/cli/search.rb | 21 ++++++++ Library/Homebrew/compat/hbc/cli/update.rb | 4 +- Library/Homebrew/utils.rb | 1 + 8 files changed, 29 insertions(+), 58 deletions(-) delete mode 100644 Library/Homebrew/cask/lib/hbc/cli/search.rb create mode 100644 Library/Homebrew/compat/hbc/cli/search.rb diff --git a/Library/Homebrew/cask/lib/hbc/cli.rb b/Library/Homebrew/cask/lib/hbc/cli.rb index df3271a5d9..d88e365ab2 100644 --- a/Library/Homebrew/cask/lib/hbc/cli.rb +++ b/Library/Homebrew/cask/lib/hbc/cli.rb @@ -21,7 +21,6 @@ require "hbc/cli/install" require "hbc/cli/list" require "hbc/cli/outdated" require "hbc/cli/reinstall" -require "hbc/cli/search" require "hbc/cli/style" require "hbc/cli/uninstall" require "hbc/cli/upgrade" diff --git a/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb b/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb index e6313b5fc5..59268715fa 100644 --- a/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb +++ b/Library/Homebrew/cask/lib/hbc/cli/abstract_command.rb @@ -1,9 +1,11 @@ require_relative "options" +require "search" module Hbc class CLI class AbstractCommand include Options + include Homebrew::Search option "--[no-]binaries", :binaries, true option "--debug", :debug, false @@ -50,7 +52,7 @@ module Hbc end def suggestion_message(cask_token) - matches, = Search.search(cask_token) + matches = search_casks(cask_token) if matches.one? "Did you mean “#{matches.first}”?" diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb deleted file mode 100644 index d7bd079bd1..0000000000 --- a/Library/Homebrew/cask/lib/hbc/cli/search.rb +++ /dev/null @@ -1,53 +0,0 @@ -require "search" - -module Hbc - class CLI - class Search < AbstractCommand - extend Homebrew::Search - - def run - if args.empty? - puts Formatter.columns(CLI.nice_listing(Cask.map(&:qualified_token))) - else - results = self.class.search(*args) - self.class.render_results(*results) - end - end - - def self.search(*arguments) - query = arguments.join(" ") - string_or_regex = query_regexp(query) - local_results = search_casks(string_or_regex) - - remote_matches = search_taps(query, silent: true)[:casks] - - [local_results, remote_matches, query] - end - - def self.render_results(partial_matches, remote_matches, search_term) - unless $stdout.tty? - puts [*partial_matches, *remote_matches] - return - end - - if partial_matches.empty? && remote_matches.empty? - puts "No Cask found for \"#{search_term}\"." - return - end - - unless partial_matches.empty? - ohai "Matches" - puts Formatter.columns(partial_matches) - end - - return if remote_matches.empty? - ohai "Remote Matches" - puts Formatter.columns(remote_matches) - end - - def self.help - "searches all known Casks" - end - end - end -end diff --git a/Library/Homebrew/compat/hbc.rb b/Library/Homebrew/compat/hbc.rb index b64c3e4377..e41f39f856 100644 --- a/Library/Homebrew/compat/hbc.rb +++ b/Library/Homebrew/compat/hbc.rb @@ -1,4 +1,5 @@ require "compat/hbc/cask_loader" +require "compat/hbc/cli/search" require "compat/hbc/cli/update" require "compat/hbc/cache" require "compat/hbc/caskroom" diff --git a/Library/Homebrew/compat/hbc/cli.rb b/Library/Homebrew/compat/hbc/cli.rb index fb17a1d050..b80fd409d0 100644 --- a/Library/Homebrew/compat/hbc/cli.rb +++ b/Library/Homebrew/compat/hbc/cli.rb @@ -1,4 +1,4 @@ -require "cask/lib/hbc/cli/options" +require "hbc/cli/options" module Hbc class CLI diff --git a/Library/Homebrew/compat/hbc/cli/search.rb b/Library/Homebrew/compat/hbc/cli/search.rb new file mode 100644 index 0000000000..9d9f3d52fe --- /dev/null +++ b/Library/Homebrew/compat/hbc/cli/search.rb @@ -0,0 +1,21 @@ +require "hbc/cli/abstract_command" +require "cmd/search" + +module Hbc + class CLI + module Compat + class Search < AbstractCommand + def run + odeprecated "`brew cask search`", "`brew search`" + Homebrew.search(args) + end + + def self.visible + false + end + end + end + + prepend Compat + end +end diff --git a/Library/Homebrew/compat/hbc/cli/update.rb b/Library/Homebrew/compat/hbc/cli/update.rb index 348e3a2c76..5f0f255d86 100644 --- a/Library/Homebrew/compat/hbc/cli/update.rb +++ b/Library/Homebrew/compat/hbc/cli/update.rb @@ -1,10 +1,10 @@ -require "cask/lib/hbc/cli/abstract_command" +require "hbc/cli/abstract_command" module Hbc class CLI module Compat class Update < AbstractCommand - def self.run(*_ignored) + def run odisabled "`brew cask update`", "`brew update`" end diff --git a/Library/Homebrew/utils.rb b/Library/Homebrew/utils.rb index fb4b764d98..7421fb5c73 100644 --- a/Library/Homebrew/utils.rb +++ b/Library/Homebrew/utils.rb @@ -16,6 +16,7 @@ require "time" def require?(path) return false if path.nil? require path + true rescue LoadError => e # we should raise on syntax errors but not if the file doesn't exist. raise unless e.message.include?(path) From a8bfe09c499d725972543c1857365768d5739329 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Wed, 13 Jun 2018 07:20:32 +0200 Subject: [PATCH 3/7] Remove tests for `brew cask search`. --- Library/Homebrew/test/cask/cli/search_spec.rb | 123 ------------------ 1 file changed, 123 deletions(-) delete mode 100644 Library/Homebrew/test/cask/cli/search_spec.rb diff --git a/Library/Homebrew/test/cask/cli/search_spec.rb b/Library/Homebrew/test/cask/cli/search_spec.rb deleted file mode 100644 index 092c19b737..0000000000 --- a/Library/Homebrew/test/cask/cli/search_spec.rb +++ /dev/null @@ -1,123 +0,0 @@ -require_relative "shared_examples/invalid_option" - -describe Hbc::CLI::Search, :cask do - before do - allow(Tty).to receive(:width).and_return(0) - end - - it_behaves_like "a command that handles invalid options" - - it "lists the available Casks that match the search term" do - allow(GitHub).to receive(:search_code).and_return([]) - - expect { - Hbc::CLI::Search.run("local") - }.to output(<<~EOS).to_stdout.as_tty - ==> Matches - local-caffeine - local-transmission - EOS - end - - it "outputs a plain list when stdout is not a TTY" do - allow(GitHub).to receive(:search_code).and_return([]) - - expect { - Hbc::CLI::Search.run("local") - }.to output(<<~EOS).to_stdout - local-caffeine - local-transmission - EOS - end - - it "returns matches even when online search failed" do - allow(GitHub).to receive(:search_code).and_raise(GitHub::Error.new("reason")) - - expect { - Hbc::CLI::Search.run("local") - }.to output(<<~EOS).to_stdout - local-caffeine - local-transmission - EOS - .and output(/^Warning: Error searching on GitHub: reason/).to_stderr - end - - it "shows that there are no Casks matching a search term that did not result in anything" do - expect { - Hbc::CLI::Search.run("foo-bar-baz") - }.to output(<<~EOS).to_stdout.as_tty - No Cask found for "foo-bar-baz". - EOS - end - - it "doesn't output anything to non-TTY stdout when there are no matches" do - allow(GitHub).to receive(:search_code).and_return([]) - - expect { Hbc::CLI::Search.run("foo-bar-baz") } - .to not_to_output.to_stdout - .and not_to_output.to_stderr - end - - it "lists all Casks available offline with no search term" do - allow(GitHub).to receive(:search_code).and_raise(GitHub::Error.new("reason")) - expect { Hbc::CLI::Search.run } - .to output(/local-caffeine/).to_stdout.as_tty - .and not_to_output.to_stderr - end - - it "ignores hyphens in search terms" do - expect { - Hbc::CLI::Search.run("lo-cal-caffeine") - }.to output(/local-caffeine/).to_stdout.as_tty - end - - it "ignores hyphens in Cask tokens" do - expect { - Hbc::CLI::Search.run("localcaffeine") - }.to output(/local-caffeine/).to_stdout.as_tty - end - - it "accepts multiple arguments" do - expect { - Hbc::CLI::Search.run("local caffeine") - }.to output(/local-caffeine/).to_stdout.as_tty - end - - it "accepts a regexp argument" do - expect { - Hbc::CLI::Search.run("/^local-c[a-z]ffeine$/") - }.to output(<<~EOS).to_stdout.as_tty - ==> Matches - local-caffeine - EOS - end - - it "returns both exact and partial matches" do - expect { - Hbc::CLI::Search.run("test-opera") - }.to output(<<~EOS).to_stdout.as_tty - ==> Matches - test-opera - test-opera-mail - EOS - end - - it "does not search the Tap name" do - expect { - Hbc::CLI::Search.run("caskroom") - }.to output(<<~EOS).to_stdout.as_tty - No Cask found for "caskroom". - EOS - end - - it "highlights installed packages" do - Hbc::CLI::Install.run("local-caffeine") - - expect { - Hbc::CLI::Search.run("local-caffeine") - }.to output(<<~EOS).to_stdout.as_tty - ==> Matches - local-caffeine ✔ - EOS - end -end From 1d629c69780255d23d95d7ca5af1e5a5c59015a5 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 11 Jun 2018 22:24:41 +0200 Subject: [PATCH 4/7] =?UTF-8?q?Don=E2=80=99t=20show=20Casks=20on=20Linux.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Library/Homebrew/cmd/search.rb | 4 +++- Library/Homebrew/test/cmd/search_spec.rb | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb index a6117b5029..68302f2672 100644 --- a/Library/Homebrew/cmd/search.rb +++ b/Library/Homebrew/cmd/search.rb @@ -83,7 +83,9 @@ module Homebrew remote_formulae = remote_results[:formulae] all_formulae = local_formulae + remote_formulae - local_casks = if query.match?(HOMEBREW_TAP_CASK_REGEX) + local_casks = if !OS.mac? + [] + elsif query.match?(HOMEBREW_TAP_CASK_REGEX) begin [Hbc::CaskLoader.load(query).token] rescue Hbc::CaskUnavailableError diff --git a/Library/Homebrew/test/cmd/search_spec.rb b/Library/Homebrew/test/cmd/search_spec.rb index cae8e97fdc..97c4460635 100644 --- a/Library/Homebrew/test/cmd/search_spec.rb +++ b/Library/Homebrew/test/cmd/search_spec.rb @@ -25,7 +25,7 @@ describe "brew search", :integration_test do .and be_a_success end - it "falls back to a GitHub tap search when no formula is found", :needs_macos, :needs_network, retry: 3 do + it "falls back to a GitHub tap search when no formula is found", :needs_network, retry: 3 do setup_remote_tap "homebrew/cask" expect { brew "search", "homebrew/cask/firefox" } From 18e46b3ec2068310a133f183564f41d183995346 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Wed, 13 Jun 2018 07:35:04 +0200 Subject: [PATCH 5/7] Fix usage of `HOMEBREW_LOAD_PATH`. --- Library/Homebrew/brew.rb | 7 ++++--- Library/Homebrew/config.rb | 5 ++++- Library/Homebrew/dev-cmd/ruby.rb | 2 +- Library/Homebrew/test/spec_helper.rb | 2 +- .../support/helper/spec/shared_context/integration_test.rb | 4 +--- Library/Homebrew/test/support/lib/config.rb | 6 +++++- 6 files changed, 16 insertions(+), 10 deletions(-) diff --git a/Library/Homebrew/brew.rb b/Library/Homebrew/brew.rb index 42ac131e14..92f50411bd 100644 --- a/Library/Homebrew/brew.rb +++ b/Library/Homebrew/brew.rb @@ -16,14 +16,15 @@ require "pathname" HOMEBREW_LIBRARY_PATH = Pathname.new(__FILE__).realpath.parent require "English" -unless $LOAD_PATH.include?(HOMEBREW_LIBRARY_PATH.to_s) - $LOAD_PATH.unshift(HOMEBREW_LIBRARY_PATH.to_s) -end unless $LOAD_PATH.include?("#{HOMEBREW_LIBRARY_PATH}/cask/lib") $LOAD_PATH.unshift("#{HOMEBREW_LIBRARY_PATH}/cask/lib") end +unless $LOAD_PATH.include?(HOMEBREW_LIBRARY_PATH.to_s) + $LOAD_PATH.unshift(HOMEBREW_LIBRARY_PATH.to_s) +end + require "global" begin diff --git a/Library/Homebrew/config.rb b/Library/Homebrew/config.rb index 38d7c80438..753616a8c8 100644 --- a/Library/Homebrew/config.rb +++ b/Library/Homebrew/config.rb @@ -47,4 +47,7 @@ unless defined? HOMEBREW_LIBRARY_PATH end # Load path used by standalone scripts to access the Homebrew code base -HOMEBREW_LOAD_PATH = HOMEBREW_LIBRARY_PATH +HOMEBREW_LOAD_PATH = [ + HOMEBREW_LIBRARY_PATH, + HOMEBREW_LIBRARY_PATH/"cask/lib", +].join(File::PATH_SEPARATOR) diff --git a/Library/Homebrew/dev-cmd/ruby.rb b/Library/Homebrew/dev-cmd/ruby.rb index c5696d6bba..c244bcdf13 100755 --- a/Library/Homebrew/dev-cmd/ruby.rb +++ b/Library/Homebrew/dev-cmd/ruby.rb @@ -8,6 +8,6 @@ module Homebrew module_function def ruby - exec ENV["HOMEBREW_RUBY_PATH"], "-I#{HOMEBREW_LIBRARY_PATH}", "-rglobal", "-rdev-cmd/irb", *ARGV + exec ENV["HOMEBREW_RUBY_PATH"], "-I", HOMEBREW_LOAD_PATH, "-rglobal", "-rdev-cmd/irb", *ARGV end end diff --git a/Library/Homebrew/test/spec_helper.rb b/Library/Homebrew/test/spec_helper.rb index a09afa11b6..b7a657bf60 100644 --- a/Library/Homebrew/test/spec_helper.rb +++ b/Library/Homebrew/test/spec_helper.rb @@ -14,8 +14,8 @@ require "rubocop" require "rubocop/rspec/support" require "find" -$LOAD_PATH.unshift(File.expand_path("#{ENV["HOMEBREW_LIBRARY"]}/Homebrew")) $LOAD_PATH.unshift(File.expand_path("#{ENV["HOMEBREW_LIBRARY"]}/Homebrew/cask/lib")) +$LOAD_PATH.unshift(File.expand_path("#{ENV["HOMEBREW_LIBRARY"]}/Homebrew")) $LOAD_PATH.unshift(File.expand_path("#{ENV["HOMEBREW_LIBRARY"]}/Homebrew/test/support/lib")) require "global" diff --git a/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb b/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb index 41d7174cc3..f8e59ad4b4 100644 --- a/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb +++ b/Library/Homebrew/test/support/helper/spec/shared_context/integration_test.rb @@ -83,9 +83,7 @@ RSpec.shared_context "integration test" do @ruby_args ||= begin ruby_args = [ "-W0", - "-I", "#{HOMEBREW_LIBRARY_PATH}/test/support/lib", - "-I", HOMEBREW_LIBRARY_PATH.to_s, - "-I", "#{HOMEBREW_LIBRARY_PATH}/cask/lib", + "-I", HOMEBREW_LOAD_PATH, "-rconfig" ] if ENV["HOMEBREW_TESTS_COVERAGE"] diff --git a/Library/Homebrew/test/support/lib/config.rb b/Library/Homebrew/test/support/lib/config.rb index 7408b3e34a..c83a0eabaa 100644 --- a/Library/Homebrew/test/support/lib/config.rb +++ b/Library/Homebrew/test/support/lib/config.rb @@ -15,7 +15,11 @@ end # Paths pointing into the Homebrew code base that persist across test runs HOMEBREW_LIBRARY_PATH = Pathname.new(File.expand_path("../../..", __dir__)) HOMEBREW_SHIMS_PATH = HOMEBREW_LIBRARY_PATH.parent+"Homebrew/shims" -HOMEBREW_LOAD_PATH = [File.expand_path(__dir__), HOMEBREW_LIBRARY_PATH].join(":") +HOMEBREW_LOAD_PATH = [ + File.expand_path(__dir__), + HOMEBREW_LIBRARY_PATH, + HOMEBREW_LIBRARY_PATH.join("cask/lib"), +].join(File::PATH_SEPARATOR) # Paths redirected to a temporary directory and wiped at the end of the test run HOMEBREW_PREFIX = Pathname.new(TEST_TMPDIR).join("prefix") From b21430b13cd6c2a77cd316fca4e3e21084d0e81f Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Wed, 13 Jun 2018 07:49:01 +0200 Subject: [PATCH 6/7] Use `extend/os` pattern. --- Library/Homebrew/cmd/search.rb | 32 +++--------------------- Library/Homebrew/extend/os/mac/search.rb | 26 +++++++++++++++++++ Library/Homebrew/extend/os/search.rb | 1 + Library/Homebrew/search.rb | 27 ++++++++++++-------- 4 files changed, 46 insertions(+), 40 deletions(-) create mode 100644 Library/Homebrew/extend/os/mac/search.rb create mode 100644 Library/Homebrew/extend/os/search.rb diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb index 68302f2672..2771d06f79 100644 --- a/Library/Homebrew/cmd/search.rb +++ b/Library/Homebrew/cmd/search.rb @@ -18,7 +18,6 @@ require "missing_formula" require "descriptions" require "cli_parser" require "search" -require "hbc/cask_loader" module Homebrew module_function @@ -63,38 +62,13 @@ module Homebrew query = args.remaining.join(" ") string_or_regex = query_regexp(query) - remote_results = if query.match?(HOMEBREW_TAP_FORMULA_REGEX) || query.match?(HOMEBREW_TAP_CASK_REGEX) - _, _, name = query.split("/", 3) - search_taps(name, silent: true) - else - search_taps(query, silent: true) - end - - local_formulae = if query.match?(HOMEBREW_TAP_FORMULA_REGEX) - begin - [Formulary.factory(query).name] - rescue FormulaUnavailableError - [] - end - else - search_formulae(string_or_regex) - end + remote_results = search_taps(query, silent: true) + local_formulae = search_formulae(string_or_regex) remote_formulae = remote_results[:formulae] all_formulae = local_formulae + remote_formulae - local_casks = if !OS.mac? - [] - elsif query.match?(HOMEBREW_TAP_CASK_REGEX) - begin - [Hbc::CaskLoader.load(query).token] - rescue Hbc::CaskUnavailableError - [] - end - else - search_casks(string_or_regex) - end - + local_casks = search_casks(string_or_regex) remote_casks = remote_results[:casks] all_casks = local_casks + remote_casks diff --git a/Library/Homebrew/extend/os/mac/search.rb b/Library/Homebrew/extend/os/mac/search.rb new file mode 100644 index 0000000000..aae84dd7e1 --- /dev/null +++ b/Library/Homebrew/extend/os/mac/search.rb @@ -0,0 +1,26 @@ +require "hbc/cask" +require "hbc/cask_loader" + +module Homebrew + module Search + def search_casks(string_or_regex) + if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_CASK_REGEX) + return begin + [Hbc::CaskLoader.load(string_or_regex).token] + rescue Hbc::CaskUnavailableError + [] + end + end + + results = Hbc::Cask.search(string_or_regex, &:token).sort_by(&:token) + + results.map do |cask| + if cask.installed? + pretty_installed(cask.token) + else + cask.token + end + end + end + end +end diff --git a/Library/Homebrew/extend/os/search.rb b/Library/Homebrew/extend/os/search.rb new file mode 100644 index 0000000000..69c6b02543 --- /dev/null +++ b/Library/Homebrew/extend/os/search.rb @@ -0,0 +1 @@ +require "extend/os/mac/search" if OS.mac? diff --git a/Library/Homebrew/search.rb b/Library/Homebrew/search.rb index f43edaf871..3c78faf27e 100644 --- a/Library/Homebrew/search.rb +++ b/Library/Homebrew/search.rb @@ -1,5 +1,4 @@ require "searchable" -require "hbc/cask" module Homebrew module Search @@ -14,6 +13,10 @@ module Homebrew end def search_taps(query, silent: false) + if query.match?(Regexp.union(HOMEBREW_TAP_FORMULA_REGEX, HOMEBREW_TAP_CASK_REGEX)) + _, _, query = query.split("/", 3) + end + results = { formulae: [], casks: [] } return results if ENV["HOMEBREW_NO_GITHUB_API"] @@ -53,6 +56,14 @@ module Homebrew end def search_formulae(string_or_regex) + if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_FORMULA_REGEX) + return begin + [Formulary.factory(string_or_regex).name] + rescue FormulaUnavailableError + [] + end + end + aliases = Formula.alias_full_names results = (Formula.full_names + aliases) .extend(Searchable) @@ -78,16 +89,10 @@ module Homebrew end.compact end - def search_casks(string_or_regex) - results = Hbc::Cask.search(string_or_regex, &:token).sort_by(&:token) - - results.map do |cask| - if cask.installed? - pretty_installed(cask.token) - else - cask.token - end - end + def search_casks(_string_or_regex) + [] end end end + +require "extend/os/search" From b265d870ed30a97a1c37e9267efe648d661761c0 Mon Sep 17 00:00:00 2001 From: Markus Reiter Date: Mon, 18 Jun 2018 16:09:13 +0200 Subject: [PATCH 7/7] Allow searching Casks by name. --- Library/Homebrew/cmd/search.rb | 17 +++++----- Library/Homebrew/extend/os/mac/search.rb | 41 +++++++++++++++++------- Library/Homebrew/search.rb | 5 +++ 3 files changed, 43 insertions(+), 20 deletions(-) diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb index 2771d06f79..3914b455a5 100644 --- a/Library/Homebrew/cmd/search.rb +++ b/Library/Homebrew/cmd/search.rb @@ -52,16 +52,17 @@ module Homebrew return end - if args.remaining.empty? + if args.remaining.empty? && !args.desc? puts Formatter.columns(Formula.full_names.sort) - elsif args.desc? - query = args.remaining.join(" ") - string_or_regex = query_regexp(query) - Descriptions.search(string_or_regex, :desc).print - else - query = args.remaining.join(" ") - string_or_regex = query_regexp(query) + return + end + query = args.remaining.join(" ") + string_or_regex = query_regexp(query) + + if args.desc? + search_descriptions(string_or_regex) + else remote_results = search_taps(query, silent: true) local_formulae = search_formulae(string_or_regex) diff --git a/Library/Homebrew/extend/os/mac/search.rb b/Library/Homebrew/extend/os/mac/search.rb index aae84dd7e1..5d1d7cc78a 100644 --- a/Library/Homebrew/extend/os/mac/search.rb +++ b/Library/Homebrew/extend/os/mac/search.rb @@ -3,24 +3,41 @@ require "hbc/cask_loader" module Homebrew module Search - def search_casks(string_or_regex) - if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_CASK_REGEX) - return begin - [Hbc::CaskLoader.load(string_or_regex).token] - rescue Hbc::CaskUnavailableError - [] + module Extension + def search_descriptions(string_or_regex) + super + + puts + + ohai "Casks" + Hbc::Cask.to_a.extend(Searchable) + .search(string_or_regex, &:name) + .each do |cask| + puts "#{Tty.bold}#{cask.token}:#{Tty.reset} #{cask.name.join(", ")}" end end - results = Hbc::Cask.search(string_or_regex, &:token).sort_by(&:token) + def search_casks(string_or_regex) + if string_or_regex.is_a?(String) && string_or_regex.match?(HOMEBREW_TAP_CASK_REGEX) + return begin + [Hbc::CaskLoader.load(string_or_regex).token] + rescue Hbc::CaskUnavailableError + [] + end + end - results.map do |cask| - if cask.installed? - pretty_installed(cask.token) - else - cask.token + results = Hbc::Cask.search(string_or_regex, &:token).sort_by(&:token) + + results.map do |cask| + if cask.installed? + pretty_installed(cask.token) + else + cask.token + end end end end + + prepend Extension end end diff --git a/Library/Homebrew/search.rb b/Library/Homebrew/search.rb index 3c78faf27e..6362e3114f 100644 --- a/Library/Homebrew/search.rb +++ b/Library/Homebrew/search.rb @@ -12,6 +12,11 @@ module Homebrew raise "#{query} is not a valid regex." end + def search_descriptions(string_or_regex) + ohai "Formulae" + Descriptions.search(string_or_regex, :desc).print + end + def search_taps(query, silent: false) if query.match?(Regexp.union(HOMEBREW_TAP_FORMULA_REGEX, HOMEBREW_TAP_CASK_REGEX)) _, _, query = query.split("/", 3)