Merge pull request #4306 from reitermarkus/cask-search

Refactor `brew cask search`.
This commit is contained in:
Markus Reiter 2018-06-08 14:18:32 +02:00 committed by GitHub
commit 61bcec42a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 38 additions and 49 deletions

View File

@ -20,6 +20,10 @@ 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
require "global"
begin

View File

@ -1,10 +1,13 @@
require "hbc/cask_loader"
require "hbc/dsl"
require "hbc/metadata"
require "searchable"
module Hbc
class Cask
extend Enumerable
extend Forwardable
extend Searchable
include Metadata
attr_reader :token, :sourcefile_path, :config

View File

@ -14,31 +14,14 @@ module Hbc
end
end
def self.extract_regexp(string)
if string =~ %r{^/(.*)/$}
Regexp.last_match[1]
else
false
end
end
def self.search(*arguments)
partial_matches = []
search_term = arguments.join(" ")
search_regexp = extract_regexp arguments.first
all_tokens = CLI.nice_listing(Cask.map(&:qualified_token))
if search_regexp
search_term = arguments.first
partial_matches = all_tokens.grep(/#{search_regexp}/i)
else
simplified_tokens = all_tokens.map { |t| t.sub(%r{^.*\/}, "").gsub(/[^a-z0-9]+/i, "") }
simplified_search_term = search_term.sub(/\.rb$/i, "").gsub(/[^a-z0-9]+/i, "")
partial_matches = simplified_tokens.grep(/#{simplified_search_term}/i) { |t| all_tokens[simplified_tokens.index(t)] }
end
query = arguments.join(" ")
string_or_regex = query_regexp(query)
local_results = search_casks(string_or_regex)
remote_matches = search_taps(search_term, silent: true)[:casks]
remote_matches = search_taps(query, silent: true)[:casks]
[partial_matches, remote_matches, search_term]
[local_results, remote_matches, query]
end
def self.render_results(partial_matches, remote_matches, search_term)
@ -53,22 +36,13 @@ module Hbc
end
unless partial_matches.empty?
if extract_regexp search_term
ohai "Regexp Matches"
else
ohai "Matches"
end
puts Formatter.columns(partial_matches.map(&method(:highlight_installed)))
ohai "Matches"
puts Formatter.columns(partial_matches)
end
return if remote_matches.empty?
ohai "Remote Matches"
puts Formatter.columns(remote_matches.map(&method(:highlight_installed)))
end
def self.highlight_installed(token)
return token unless Cask.new(token).installed?
pretty_installed token
puts Formatter.columns(remote_matches)
end
def self.help

View File

@ -1,4 +1,3 @@
$LOAD_PATH.unshift("#{HOMEBREW_LIBRARY_PATH}/cask/lib")
require "hbc"
module Homebrew

View File

@ -47,7 +47,6 @@ module Homebrew
require "formula"
require "keg"
$LOAD_PATH.unshift("#{HOMEBREW_LIBRARY_PATH}/cask/lib")
require "hbc"
ohai "Interactive Homebrew Shell"

View File

@ -40,6 +40,7 @@ module Homebrew
ENV.delete("HOMEBREW_TEMP")
ENV.delete("HOMEBREW_LINKAGE_CACHE")
ENV.delete("HOMEBREW_NO_GITHUB_API")
ENV.delete("HOMEBREW_NO_EMOJI")
ENV["HOMEBREW_NO_ANALYTICS_THIS_RUN"] = "1"
ENV["HOMEBREW_DEVELOPER"] = "1"
ENV["HOMEBREW_NO_COMPAT"] = "1" if args.no_compat?

View File

@ -1,4 +1,5 @@
require "searchable"
require "hbc/cask"
module Homebrew
module Search
@ -79,5 +80,17 @@ module Homebrew
end
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
end
end
end

View File

@ -87,7 +87,7 @@ describe Hbc::CLI::Search, :cask do
expect {
Hbc::CLI::Search.run("/^local-c[a-z]ffeine$/")
}.to output(<<~EOS).to_stdout.as_tty
==> Regexp Matches
==> Matches
local-caffeine
EOS
end
@ -110,13 +110,14 @@ describe Hbc::CLI::Search, :cask do
EOS
end
it "doesn't highlight packages that aren't installed" do
expect(Hbc::CLI::Search.highlight_installed("local-caffeine")).to eq("local-caffeine")
end
it "highlights installed packages" do
Hbc::CLI::Install.run("local-caffeine")
expect(Hbc::CLI::Search.highlight_installed("local-caffeine")).to eq(pretty_installed("local-caffeine"))
expect {
Hbc::CLI::Search.run("local-caffeine")
}.to output(<<~EOS).to_stdout.as_tty
==> Matches
local-caffeine
EOS
end
end

View File

@ -5,7 +5,6 @@ describe Emoji do
subject { described_class.install_badge }
before do
ENV.delete("HOMEBREW_NO_EMOJI")
ENV.delete("HOMEBREW_INSTALL_BADGE")
end

View File

@ -15,6 +15,7 @@ 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/test/support/lib"))
require "global"

View File

@ -1,5 +1,3 @@
$LOAD_PATH.push(HOMEBREW_LIBRARY_PATH.join("cask", "lib").to_s)
require "hbc"
require "test/support/helper/cask/fake_system_command"

View File

@ -85,6 +85,7 @@ RSpec.shared_context "integration test" do
"-W0",
"-I", "#{HOMEBREW_LIBRARY_PATH}/test/support/lib",
"-I", HOMEBREW_LIBRARY_PATH.to_s,
"-I", "#{HOMEBREW_LIBRARY_PATH}/cask/lib",
"-rconfig"
]
if ENV["HOMEBREW_TESTS_COVERAGE"]

View File

@ -32,8 +32,6 @@ describe "globally-scoped helper methods" do
before { allow($stdout).to receive(:tty?).and_return(true) }
context "with HOMEBREW_NO_EMOJI unset" do
before { ENV.delete("HOMEBREW_NO_EMOJI") }
it "returns a string with a colored checkmark" do
expect(subject)
.to match(/#{esc 1}foo #{esc 32}#{esc 0}/)
@ -66,8 +64,6 @@ describe "globally-scoped helper methods" do
before { allow($stdout).to receive(:tty?).and_return(true) }
context "with HOMEBREW_NO_EMOJI unset" do
before { ENV.delete("HOMEBREW_NO_EMOJI") }
it "returns a string with a colored checkmark" do
expect(subject)
.to match(/#{esc 1}foo #{esc 31}#{esc 0}/)