aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarkus Reiter2017-04-25 12:50:23 +0200
committerGitHub2017-04-25 12:50:23 +0200
commit24a339ee239a4e13e5162085780687fcd2b7bc9f (patch)
tree4a53d351a1135b10032b73c129c29c68c2a3a3f6
parenta38133e5a116ac3e08a856e38ce76dc5453a963f (diff)
parent133e5ddf6ad1b7c931f4686922ba4d61ea450f3a (diff)
downloadbrew-24a339ee239a4e13e5162085780687fcd2b7bc9f.tar.bz2
Merge pull request #2541 from reitermarkus/cask-search
Add remote search to `brew cask search`.
-rw-r--r--Library/Homebrew/cask/lib/hbc/cli/search.rb36
-rw-r--r--Library/Homebrew/cmd/search.rb26
-rw-r--r--Library/Homebrew/test/cask/cli/search_spec.rb6
-rw-r--r--Library/Homebrew/test/cmd/search_remote_tap_spec.rb5
-rw-r--r--Library/Homebrew/test/utils/github_spec.rb13
-rw-r--r--Library/Homebrew/utils/github.rb14
6 files changed, 63 insertions, 37 deletions
diff --git a/Library/Homebrew/cask/lib/hbc/cli/search.rb b/Library/Homebrew/cask/lib/hbc/cli/search.rb
index 992aca583..7abd744e4 100644
--- a/Library/Homebrew/cask/lib/hbc/cli/search.rb
+++ b/Library/Homebrew/cask/lib/hbc/cli/search.rb
@@ -13,6 +13,15 @@ module Hbc
end
end
+ def self.search_remote(query)
+ matches = GitHub.search_code("user:caskroom", "path:Casks", "filename:#{query}", "extension:rb")
+ [*matches].map do |match|
+ tap = Tap.fetch(match["repository"]["full_name"])
+ next if tap.installed?
+ "#{tap.name}/#{File.basename(match["path"], ".rb")}"
+ end.compact
+ end
+
def self.search(*arguments)
exact_match = nil
partial_matches = []
@@ -29,27 +38,34 @@ module Hbc
partial_matches = simplified_tokens.grep(/#{simplified_search_term}/i) { |t| all_tokens[simplified_tokens.index(t)] }
partial_matches.delete(exact_match)
end
- [exact_match, partial_matches, search_term]
+
+ remote_matches = search_remote(search_term)
+
+ [exact_match, partial_matches, remote_matches, search_term]
end
- def self.render_results(exact_match, partial_matches, search_term)
+ def self.render_results(exact_match, partial_matches, remote_matches, search_term)
if !exact_match && partial_matches.empty?
puts "No Cask found for \"#{search_term}\"."
return
end
if exact_match
- ohai "Exact match"
+ ohai "Exact Match"
puts highlight_installed exact_match
end
- return if partial_matches.empty?
-
- if extract_regexp search_term
- ohai "Regexp matches"
- else
- ohai "Partial matches"
+ unless partial_matches.empty?
+ if extract_regexp search_term
+ ohai "Regexp Matches"
+ else
+ ohai "Partial Matches"
+ end
+ puts Formatter.columns(partial_matches.map(&method(:highlight_installed)))
end
- puts Formatter.columns(partial_matches.map(&method(:highlight_installed)))
+
+ return if remote_matches.empty?
+ ohai "Remote Matches"
+ puts Formatter.columns(remote_matches.map(&method(:highlight_installed)))
end
def self.highlight_installed(token)
diff --git a/Library/Homebrew/cmd/search.rb b/Library/Homebrew/cmd/search.rb
index 110e1559e..f71a14ba1 100644
--- a/Library/Homebrew/cmd/search.rb
+++ b/Library/Homebrew/cmd/search.rb
@@ -59,7 +59,7 @@ module Homebrew
local_results = search_formulae(regex)
puts Formatter.columns(local_results) unless local_results.empty?
tap_results = search_taps(query)
- puts Formatter.columns(tap_results) if tap_results && !tap_results.empty?
+ puts Formatter.columns(tap_results) unless tap_results.empty?
if $stdout.tty?
count = local_results.length + tap_results.length
@@ -101,21 +101,15 @@ module Homebrew
end
def search_taps(query)
- valid_dirnames = ["Formula", "HomebrewFormula", "Casks", ".", ""].freeze
- q = "user:Homebrew%20user:caskroom%20filename:#{query}"
- GitHub.open "https://api.github.com/search/code?q=#{q}" do |json|
- json["items"].map do |object|
- dirname, filename = File.split(object["path"])
- next unless valid_dirnames.include?(dirname)
- user = object["repository"]["owner"]["login"]
- user = user.downcase if user == "Homebrew"
- repo = object["repository"]["name"].sub(/^homebrew-/, "")
- tap = Tap.fetch user, repo
- next if tap.installed?
- basename = File.basename(filename, ".rb")
- "#{user}/#{repo}/#{basename}"
- end.compact
- end
+ valid_dirnames = ["Formula", "HomebrewFormula", "Casks", "."].freeze
+ matches = GitHub.search_code("user:Homebrew", "user:caskroom", "filename:#{query}", "extension:rb")
+ [*matches].map do |match|
+ dirname, filename = File.split(match["path"])
+ next unless valid_dirnames.include?(dirname)
+ tap = Tap.fetch(match["repository"]["full_name"])
+ next if tap.installed?
+ "#{tap.name}/#{File.basename(filename, ".rb")}"
+ end.compact
end
def search_formulae(regex)
diff --git a/Library/Homebrew/test/cask/cli/search_spec.rb b/Library/Homebrew/test/cask/cli/search_spec.rb
index 9843a6de6..00fcf7382 100644
--- a/Library/Homebrew/test/cask/cli/search_spec.rb
+++ b/Library/Homebrew/test/cask/cli/search_spec.rb
@@ -3,7 +3,7 @@ describe Hbc::CLI::Search, :cask do
expect {
Hbc::CLI::Search.run("local")
}.to output(<<-EOS.undent).to_stdout
- ==> Partial matches
+ ==> Partial Matches
local-caffeine
local-transmission
EOS
@@ -42,13 +42,13 @@ describe Hbc::CLI::Search, :cask do
it "accepts a regexp argument" do
expect {
Hbc::CLI::Search.run("/^local-c[a-z]ffeine$/")
- }.to output("==> Regexp matches\nlocal-caffeine\n").to_stdout
+ }.to output("==> Regexp Matches\nlocal-caffeine\n").to_stdout
end
it "Returns both exact and partial matches" do
expect {
Hbc::CLI::Search.run("test-opera")
- }.to output(/^==> Exact match\ntest-opera\n==> Partial matches\ntest-opera-mail/).to_stdout
+ }.to output(/^==> Exact Match\ntest-opera\n==> Partial Matches\ntest-opera-mail/).to_stdout
end
it "does not search the Tap name" do
diff --git a/Library/Homebrew/test/cmd/search_remote_tap_spec.rb b/Library/Homebrew/test/cmd/search_remote_tap_spec.rb
index be7c20865..b0beb122c 100644
--- a/Library/Homebrew/test/cmd/search_remote_tap_spec.rb
+++ b/Library/Homebrew/test/cmd/search_remote_tap_spec.rb
@@ -7,10 +7,7 @@ describe Homebrew do
{
"path" => "Formula/some-formula.rb",
"repository" => {
- "name" => "homebrew-foo",
- "owner" => {
- "login" => "Homebrew",
- },
+ "full_name" => "Homebrew/homebrew-foo",
},
},
],
diff --git a/Library/Homebrew/test/utils/github_spec.rb b/Library/Homebrew/test/utils/github_spec.rb
new file mode 100644
index 000000000..9b539262f
--- /dev/null
+++ b/Library/Homebrew/test/utils/github_spec.rb
@@ -0,0 +1,13 @@
+require "utils/github"
+
+describe GitHub do
+ describe "::search_code", :needs_network do
+ it "searches code" do
+ results = subject.search_code("repo:Homebrew/brew", "path:/", "filename:readme", "language:markdown")
+
+ expect(results.count).to eq(1)
+ expect(results.first["name"]).to eq("README.md")
+ expect(results.first["path"]).to eq("README.md")
+ end
+ end
+end
diff --git a/Library/Homebrew/utils/github.rb b/Library/Homebrew/utils/github.rb
index a5ed5394a..88c5199c2 100644
--- a/Library/Homebrew/utils/github.rb
+++ b/Library/Homebrew/utils/github.rb
@@ -4,7 +4,7 @@ require "tempfile"
module GitHub
module_function
- ISSUES_URI = URI.parse("https://api.github.com/search/issues")
+ API_URL = "https://api.github.com".freeze
CREATE_GIST_SCOPES = ["gist"].freeze
CREATE_ISSUE_SCOPES = ["public_repo"].freeze
@@ -228,13 +228,19 @@ module GitHub
end
def issues_matching(query, qualifiers = {})
- uri = ISSUES_URI.dup
+ uri = URI.parse("#{API_URL}/search/issues")
uri.query = build_query_string(query, qualifiers)
open(uri) { |json| json["items"] }
end
def repository(user, repo)
- open(URI.parse("https://api.github.com/repos/#{user}/#{repo}")) { |j| j }
+ open(URI.parse("#{API_URL}/repos/#{user}/#{repo}"))
+ end
+
+ def search_code(*params)
+ uri = URI.parse("#{API_URL}/search/code")
+ uri.query = "q=#{uri_escape(params.join(" "))}"
+ open(uri) { |json| json["items"] }
end
def build_query_string(query, qualifiers)
@@ -286,7 +292,7 @@ module GitHub
end
def private_repo?(user, repo)
- uri = URI.parse("https://api.github.com/repos/#{user}/#{repo}")
+ uri = URI.parse("#{API_URL}/repos/#{user}/#{repo}")
open(uri) { |json| json["private"] }
end
end