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) $LOAD_PATH.unshift(HOMEBREW_LIBRARY_PATH.to_s)
end end
unless $LOAD_PATH.include?("#{HOMEBREW_LIBRARY_PATH}/cask/lib")
$LOAD_PATH.unshift("#{HOMEBREW_LIBRARY_PATH}/cask/lib")
end
require "global" require "global"
begin begin

View File

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

View File

@ -14,31 +14,14 @@ module Hbc
end end
end end
def self.extract_regexp(string)
if string =~ %r{^/(.*)/$}
Regexp.last_match[1]
else
false
end
end
def self.search(*arguments) def self.search(*arguments)
partial_matches = [] query = arguments.join(" ")
search_term = arguments.join(" ") string_or_regex = query_regexp(query)
search_regexp = extract_regexp arguments.first local_results = search_casks(string_or_regex)
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
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 end
def self.render_results(partial_matches, remote_matches, search_term) def self.render_results(partial_matches, remote_matches, search_term)
@ -53,22 +36,13 @@ module Hbc
end end
unless partial_matches.empty? unless partial_matches.empty?
if extract_regexp search_term ohai "Matches"
ohai "Regexp Matches" puts Formatter.columns(partial_matches)
else
ohai "Matches"
end
puts Formatter.columns(partial_matches.map(&method(:highlight_installed)))
end end
return if remote_matches.empty? return if remote_matches.empty?
ohai "Remote Matches" ohai "Remote Matches"
puts Formatter.columns(remote_matches.map(&method(:highlight_installed))) puts Formatter.columns(remote_matches)
end
def self.highlight_installed(token)
return token unless Cask.new(token).installed?
pretty_installed token
end end
def self.help def self.help

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -15,6 +15,7 @@ require "rubocop/rspec/support"
require "find" require "find"
$LOAD_PATH.unshift(File.expand_path("#{ENV["HOMEBREW_LIBRARY"]}/Homebrew")) $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")) $LOAD_PATH.unshift(File.expand_path("#{ENV["HOMEBREW_LIBRARY"]}/Homebrew/test/support/lib"))
require "global" require "global"

View File

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

View File

@ -85,6 +85,7 @@ RSpec.shared_context "integration test" do
"-W0", "-W0",
"-I", "#{HOMEBREW_LIBRARY_PATH}/test/support/lib", "-I", "#{HOMEBREW_LIBRARY_PATH}/test/support/lib",
"-I", HOMEBREW_LIBRARY_PATH.to_s, "-I", HOMEBREW_LIBRARY_PATH.to_s,
"-I", "#{HOMEBREW_LIBRARY_PATH}/cask/lib",
"-rconfig" "-rconfig"
] ]
if ENV["HOMEBREW_TESTS_COVERAGE"] 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) } before { allow($stdout).to receive(:tty?).and_return(true) }
context "with HOMEBREW_NO_EMOJI unset" do context "with HOMEBREW_NO_EMOJI unset" do
before { ENV.delete("HOMEBREW_NO_EMOJI") }
it "returns a string with a colored checkmark" do it "returns a string with a colored checkmark" do
expect(subject) expect(subject)
.to match(/#{esc 1}foo #{esc 32}#{esc 0}/) .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) } before { allow($stdout).to receive(:tty?).and_return(true) }
context "with HOMEBREW_NO_EMOJI unset" do context "with HOMEBREW_NO_EMOJI unset" do
before { ENV.delete("HOMEBREW_NO_EMOJI") }
it "returns a string with a colored checkmark" do it "returns a string with a colored checkmark" do
expect(subject) expect(subject)
.to match(/#{esc 1}foo #{esc 31}#{esc 0}/) .to match(/#{esc 1}foo #{esc 31}#{esc 0}/)