Class: LibGems::Commands::QueryCommand

Inherits:
LibGems::Command show all
Includes:
LocalRemoteOptions, Text, VersionOption
Defined in:
lib/libgems/commands/query_command.rb

Direct Known Subclasses

ListCommand, SearchCommand

Instance Attribute Summary

Attributes inherited from LibGems::Command

#command, #defaults, #options, #program_name, #summary

Instance Method Summary collapse

Methods included from VersionOption

#add_platform_option, #add_prerelease_option, #add_version_option

Methods included from LocalRemoteOptions

#accept_uri_http, #add_bulk_threshold_option, #add_local_remote_options, #add_proxy_option, #add_source_option, #add_update_sources_option, #both?, #local?, #remote?

Methods included from Text

#format_text, #levenshtein_distance

Methods inherited from LibGems::Command

add_common_option, #add_extra_args, #add_option, add_specific_extra_args, #arguments, #begins?, build_args, build_args=, common_options, #description, extra_args, extra_args=, #get_all_gem_names, #get_one_gem_name, #get_one_optional_argument, #handle_options, #handles?, #invoke, #merge_options, #remove_option, #show_help, #show_lookup_failure, specific_extra_args, specific_extra_args_hash, #usage, #when_invoked

Methods included from UserInteraction

#methname

Methods included from DefaultUserInteraction

ui, #ui, ui=, #ui=, use_ui, #use_ui

Constructor Details

#initialize(name = 'query', summary = 'Query gem information in local or remote repositories') ⇒ QueryCommand

Returns a new instance of QueryCommand.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/libgems/commands/query_command.rb', line 13

def initialize(name = 'query',
               summary = 'Query gem information in local or remote repositories')
  super name, summary,
       :name => //, :domain => :local, :details => false, :versions => true,
       :installed => false, :version => LibGems::Requirement.default

  add_option('-i', '--[no-]installed',
             'Check for installed gem') do |value, options|
    options[:installed] = value
  end

  add_version_option command, "for use with --installed"

  add_option('-n', '--name-matches REGEXP',
             'Name of gem(s) to query on matches the',
             'provided REGEXP') do |value, options|
    options[:name] = /#{value}/i
  end

  add_option('-d', '--[no-]details',
             'Display detailed information of gem(s)') do |value, options|
    options[:details] = value
  end

  add_option(      '--[no-]versions',
             'Display only gem names') do |value, options|
    options[:versions] = value
    options[:details] = false unless value
  end

  add_option('-a', '--all',
             'Display all gem versions') do |value, options|
    options[:all] = value
  end

  add_option(      '--[no-]prerelease',
             'Display prerelease versions') do |value, options|
    options[:prerelease] = value
  end

  add_local_remote_options
end

Instance Method Details

#defaults_strObject

:nodoc:



56
57
58
# File 'lib/libgems/commands/query_command.rb', line 56

def defaults_str # :nodoc:
  "--local --name-matches // --no-details --versions --no-installed"
end

#executeObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/libgems/commands/query_command.rb', line 60

def execute
  exit_code = 0

  name = options[:name]
  prerelease = options[:prerelease]

  if options[:installed] then
    if name.source.empty? then
      alert_error "You must specify a gem name"
      exit_code |= 4
    elsif installed? name, options[:version] then
      say "true"
    else
      say "false"
      exit_code |= 1
    end

    raise LibGems::SystemExitException, exit_code
  end

  dep = LibGems::Dependency.new name, LibGems::Requirement.default

  if local? then
    if prerelease and not both? then
      alert_warning "prereleases are always shown locally"
    end

    if ui.outs.tty? or both? then
      say
      say "*** LOCAL GEMS ***"
      say
    end

    specs = LibGems.source_index.search dep

    spec_tuples = specs.map do |spec|
      [[spec.name, spec.version, spec.original_platform, spec], :local]
    end

    output_query_results spec_tuples
  end

  if remote? then
    if ui.outs.tty? or both? then
      say
      say "*** REMOTE GEMS ***"
      say
    end

    all = options[:all]

    begin
      fetcher = LibGems::SpecFetcher.fetcher
      spec_tuples = fetcher.find_matching dep, all, false, prerelease

      spec_tuples += fetcher.find_matching dep, false, false, true if
        prerelease and all
    rescue LibGems::RemoteFetcher::FetchError => e
      if prerelease then
        raise LibGems::OperationNotSupportedError,
              "Prereleases not supported on legacy repositories"
      end

      raise unless fetcher.warn_legacy e do
        require 'libgems/source_info_cache'

        dep.name = '' if dep.name == //

        specs = LibGems::SourceInfoCache.search_with_source dep, false, all

        spec_tuples = specs.map do |spec, source_uri|
          [[spec.name, spec.version, spec.original_platform, spec],
           source_uri]
        end
      end
    end

    output_query_results spec_tuples
  end
end