Class: Pod::Command::Search

Inherits:
Pod::Command show all
Defined in:
lib/cocoapods-search/command/search.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ Search

Returns a new instance of Search.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/cocoapods-search/command/search.rb', line 31

def initialize(argv)
  @use_regex = argv.flag?('regex')
  @simple_search = argv.flag?('simple')
  @stats = argv.flag?('stats')
  @web = argv.flag?('web')
  @platform_filters = Platform.all.map do |platform|
    argv.flag?(platform.name.to_s) ? platform.to_sym : nil
  end.compact
  @query = argv.arguments! unless argv.arguments.empty?
  config.silent = false
  @use_pager = argv.flag?('pager', true)
  super
end

Class Method Details

.optionsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cocoapods-search/command/search.rb', line 17

def self.options
  options = [
    ['--regex',   'Interpret the `QUERY` as a regular expression'],
    ['--simple',    'Search only by name'],
    ['--stats',   'Show additional stats (like GitHub watchers and forks)'],
    ['--web',     'Searches on cocoapods.org'],
  ]
  options += Platform.all.map do |platform|
    ["--#{platform.name.to_s}",     "Restricts the search to Pods supported on #{Platform.string_name(platform.to_sym)}"]
  end
  options << ['--no-pager',    'Do not pipe search results into a pager']
  options.concat(super.reject { |option, _| option == '--silent' })
end

Instance Method Details

#local_searchObject



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/cocoapods-search/command/search.rb', line 82

def local_search
  query_regex = @query.reduce([]) { |result, q|
    result << (@use_regex ? q : Regexp.escape(q))
  }.join(' ').strip

  sets = sources_manager.search_by_name(query_regex, !@simple_search)

  @platform_filters.each do |platform|
    sets.reject! { |set| !set.specification.available_platforms.map(&:name).include?(platform) }
  end

  if(@use_pager)
    UI.with_pager { print_sets(sets) }
  else
    print_sets(sets)
  end
end


100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/cocoapods-search/command/search.rb', line 100

def print_sets(sets)
  sets.each do |set|
    begin
      if @stats
        UI.pod(set, :stats)
      else
        UI.pod(set, :normal)
      end
    rescue DSLError
      UI.warn "Skipping `#{set.name}` because the podspec contains errors."
    end
  end
end

#runObject



58
59
60
61
62
63
64
65
# File 'lib/cocoapods-search/command/search.rb', line 58

def run
  ensure_master_spec_repo_exists!
  if @web
    web_search
  else
    local_search
  end
end

#sources_managerObject



67
68
69
# File 'lib/cocoapods-search/command/search.rb', line 67

def sources_manager
  defined?(Pod::SourcesManager) ? Pod::SourcesManager : config.sources_manager
end

#validate!Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/cocoapods-search/command/search.rb', line 45

def validate!
  super
  help! 'A search query is required.' unless @query

  unless @web || !@use_regex
    begin
      /#{@query.join(' ').strip}/
    rescue RegexpError
      help! 'A valid regular expression is required.'
    end
  end
end

#web_searchObject



71
72
73
74
75
76
77
78
79
80
# File 'lib/cocoapods-search/command/search.rb', line 71

def web_search
  queries = @platform_filters.map do |platform|
    "on:#{platform}"
  end
  queries += @query
  query_parameter = queries.compact.flatten.join(' ')
  url = "https://cocoapods.org/?q=#{CGI.escape(query_parameter).gsub('+', '%20')}"
  UI.puts("Opening #{url}")
  Executable.execute_command(:open, [url])
end