Skip to content
Browse files

Add search command which finds pods who's name matches.

  • Loading branch information...
1 parent 43094e7 commit 9a6268ffc8929525ae8240c993a8e3d6bdd66aab @alloy alloy committed Sep 17, 2011
Showing with 71 additions and 0 deletions.
  1. +3 −0 lib/cocoapods/command.rb
  2. +17 −0 lib/cocoapods/command/search.rb
  3. +15 −0 lib/cocoapods/source.rb
  4. +36 −0 spec/functional/command_spec.rb
View
3 lib/cocoapods/command.rb
@@ -2,6 +2,7 @@ module Pod
class Command
autoload :Install, 'cocoapods/command/install'
autoload :Repo, 'cocoapods/command/repo'
+ autoload :Search, 'cocoapods/command/search'
autoload :Setup, 'cocoapods/command/setup'
autoload :Spec, 'cocoapods/command/spec'
@@ -33,6 +34,7 @@ def self.banner
"To see help for the available commands run:\n" \
"\n" \
" * $ pod setup --help\n" \
+ " * $ pod search --help\n" \
" * $ pod install --help\n" \
" * $ pod repo --help"
end
@@ -68,6 +70,7 @@ def self.parse(*argv)
command_class = case argv.shift_argument
when 'install' then Install
when 'repo' then Repo
+ when 'search' then Search
when 'setup' then Setup
when 'spec' then Spec
end
View
17 lib/cocoapods/command/search.rb
@@ -0,0 +1,17 @@
+module Pod
+ class Command
+ class Search < Command
+ def initialize(argv)
+ unless @query = argv.arguments.first
+ super
+ end
+ end
+
+ def run
+ Source.search_by_name(@query.strip).each do |set|
+ puts "#{set.name} (#{set.versions.reverse.join(", ")})"
+ end
+ end
+ end
+ end
+end
View
15 lib/cocoapods/source.rb
@@ -16,6 +16,14 @@ def self.search(dependency)
raise(Informative, "Unable to find a pod named `#{dependency.name}'")
end
+ def self.search_by_name(query)
+ result = all.map { |source| source.search_by_name(query) }.flatten
+ if result.empty?
+ raise(Informative, "Unable to find a pod who's name matches `#{query}'")
+ end
+ result
+ end
+
attr_reader :repo
def initialize(repo)
@@ -27,5 +35,12 @@ def search(dependency)
Specification::Set.by_pod_dir(dir)
end
end
+
+ def search_by_name(query)
+ dirs = @repo.children.select do |child|
+ child.basename.to_s.downcase.include?(query.downcase)
+ end
+ dirs.map { |dir| Specification::Set.by_pod_dir(dir) }
+ end
end
end
View
36 spec/functional/command_spec.rb
@@ -37,4 +37,40 @@ def command.master_repo_url; SpecHelper.fixture('spec-repos/master'); end
(repo2.dir + 'README').read.should.include 'Added!'
(repo3.dir + 'README').read.should.include 'Added!'
end
+
+ before do
+ config.repos_dir = fixture('spec-repos')
+ end
+
+ after do
+ config.repos_dir = tmp_repos_path
+ end
+
+ it "searches for a pod who's name matches the given query ignoring case" do
+ [
+ [
+ ' s ',
+ [
+ "ASIHTTPRequest (1.8, 1.8.1)",
+ "ASIWebPageRequest (1.8, 1.8.1)",
+ "JSONKit (1.4)",
+ "SSZipArchive (1.0)"
+ ]
+ ],
+ [
+ 'json',
+ [
+ "JSONKit (1.4)"
+ ]
+ ]
+ ].each do |query, result|
+ command = Pod::Command.parse('search', query)
+ def command.puts(msg)
+ (@printed ||= []) << msg
+ end
+ command.run
+ printed = command.instance_variable_get(:@printed)
+ printed.should == result.sort
+ end
+ end
end

0 comments on commit 9a6268f

Please sign in to comment.
Something went wrong with that request. Please try again.