Class Gem::Commands::QueryCommand
In: lib/rubygems/commands/query_command.rb
Parent: Gem::Command

Methods

execute   new  

Included Modules

Gem::Text Gem::LocalRemoteOptions Gem::VersionOption

Public Class methods

[Source]

    # File lib/rubygems/commands/query_command.rb, line 13
13:   def initialize(name = 'query',
14:                  summary = 'Query gem information in local or remote repositories')
15:     super name, summary,
16:          :name => //, :domain => :local, :details => false, :versions => true,
17:          :installed => false, :version => Gem::Requirement.default
18: 
19:     add_option('-i', '--[no-]installed',
20:                'Check for installed gem') do |value, options|
21:       options[:installed] = value
22:     end
23: 
24:     add_version_option command, "for use with --installed"
25: 
26:     add_option('-n', '--name-matches REGEXP',
27:                'Name of gem(s) to query on matches the',
28:                'provided REGEXP') do |value, options|
29:       options[:name] = /#{value}/i
30:     end
31: 
32:     add_option('-d', '--[no-]details',
33:                'Display detailed information of gem(s)') do |value, options|
34:       options[:details] = value
35:     end
36: 
37:     add_option(      '--[no-]versions',
38:                'Display only gem names') do |value, options|
39:       options[:versions] = value
40:       options[:details] = false unless value
41:     end
42: 
43:     add_option('-a', '--all',
44:                'Display all gem versions') do |value, options|
45:       options[:all] = value
46:     end
47: 
48:     add_option(      '--[no-]prerelease',
49:                'Display prerelease versions') do |value, options|
50:       options[:prerelease] = value
51:     end
52: 
53:     add_local_remote_options
54:   end

Public Instance methods

[Source]

     # File lib/rubygems/commands/query_command.rb, line 60
 60:   def execute
 61:     exit_code = 0
 62: 
 63:     name = options[:name]
 64:     prerelease = options[:prerelease]
 65: 
 66:     if options[:installed] then
 67:       if name.source.empty? then
 68:         alert_error "You must specify a gem name"
 69:         exit_code |= 4
 70:       elsif installed? name, options[:version] then
 71:         say "true"
 72:       else
 73:         say "false"
 74:         exit_code |= 1
 75:       end
 76: 
 77:       raise Gem::SystemExitException, exit_code
 78:     end
 79: 
 80:     dep = Gem::Dependency.new name, Gem::Requirement.default
 81: 
 82:     if local? then
 83:       if prerelease and not both? then
 84:         alert_warning "prereleases are always shown locally"
 85:       end
 86: 
 87:       if ui.outs.tty? or both? then
 88:         say
 89:         say "*** LOCAL GEMS ***"
 90:         say
 91:       end
 92: 
 93:       specs = Gem.source_index.search dep
 94: 
 95:       spec_tuples = specs.map do |spec|
 96:         [[spec.name, spec.version, spec.original_platform, spec], :local]
 97:       end
 98: 
 99:       output_query_results spec_tuples
100:     end
101: 
102:     if remote? then
103:       if ui.outs.tty? or both? then
104:         say
105:         say "*** REMOTE GEMS ***"
106:         say
107:       end
108: 
109:       all = options[:all]
110: 
111:       begin
112:         fetcher = Gem::SpecFetcher.fetcher
113:         spec_tuples = fetcher.find_matching dep, all, false, prerelease
114: 
115:         spec_tuples += fetcher.find_matching dep, false, false, true if
116:           prerelease and all
117:       rescue Gem::RemoteFetcher::FetchError => e
118:         if prerelease then
119:           raise Gem::OperationNotSupportedError,
120:                 "Prereleases not supported on legacy repositories"
121:         end
122: 
123:         raise unless fetcher.warn_legacy e do
124:           require 'rubygems/source_info_cache'
125: 
126:           dep.name = '' if dep.name == //
127: 
128:           specs = Gem::SourceInfoCache.search_with_source dep, false, all
129: 
130:           spec_tuples = specs.map do |spec, source_uri|
131:             [[spec.name, spec.version, spec.original_platform, spec],
132:              source_uri]
133:           end
134:         end
135:       end
136: 
137:       output_query_results spec_tuples
138:     end
139:   end

[Validate]