Class: Pod::Command::Bin::Search

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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CBin::SpecFilesHelper

#binary_spec, #binary_spec_files, #binary_template_spec, #binary_template_spec_file, #binary_template_spec_files, #clear_binary_spec_file_if_needed, #code_spec, #code_spec_files, #create_binary_spec_file, #find_spec_file, #spec_files

Methods included from CBin::SourcesHelper

#binary_source, #code_source, #sources_manager, #sources_option, #valid_sources

Constructor Details

#initialize(argv) ⇒ Search

Returns a new instance of Search.



22
23
24
25
26
27
28
29
# File 'lib/cocoapods-bin/command/bin/search.rb', line 22

def initialize(argv)
  @code = argv.flag?('code')
  @stats = argv.flag?('stats')
  @use_pager = argv.flag?('pager', true)
  @use_regex = argv.flag?('regex')
  @query = argv.arguments! unless argv.arguments.empty?
  super
end

Class Method Details

.optionsObject



13
14
15
16
17
18
19
20
# File 'lib/cocoapods-bin/command/bin/search.rb', line 13

def self.options
  [
    ['--code', '查找源码 spec'],
    ['--stats', '展示额外信息'],
    ['--no-pager', '不以 pager 形式展示'],
    ['--regex', '`QUERY` 视为正则']
  ]
end

Instance Method Details



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/cocoapods-bin/command/bin/search.rb', line 53

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



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/cocoapods-bin/command/bin/search.rb', line 36

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

  source = @code ? code_source : binary_source

  aggregate = Pod::Source::Aggregate.new([source])
  sets = aggregate.search_by_name(query_regex, true)

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

#validate!Object



31
32
33
34
# File 'lib/cocoapods-bin/command/bin/search.rb', line 31

def validate!
  super
  help! '必须指定查找的组件.' unless @query
end