Parent

Class Index [+]

Quicksearch

Gem::Commands::QueryCommand

Public Class Methods

new(name = 'query', summary = 'Query gem information in local or remote repositories') click to toggle 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}/
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

execute() click to toggle 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

Private Instance Methods

installed?(name, version = Gem::Requirement.default) click to toggle source

Check if gem name version version is installed.

     # File lib/rubygems/commands/query_command.rb, line 146
146:   def installed?(name, version = Gem::Requirement.default)
147:     dep = Gem::Dependency.new name, version
148:     !Gem.source_index.search(dep).empty?
149:   end
output_query_results(spec_tuples) click to toggle source
     # File lib/rubygems/commands/query_command.rb, line 151
151:   def output_query_results(spec_tuples)
152:     output = []
153:     versions = Hash.new { |h,name| h[name] = [] }
154: 
155:     spec_tuples.each do |spec_tuple, source_uri|
156:       versions[spec_tuple.first] << [spec_tuple, source_uri]
157:     end
158: 
159:     versions = versions.sort_by do |(name,_),_|
160:       name.downcase
161:     end
162: 
163:     versions.each do |gem_name, matching_tuples|
164:       matching_tuples = matching_tuples.sort_by do |(name, version,_),_|
165:         version
166:       end.reverse
167: 
168:       platforms = Hash.new { |h,version| h[version] = [] }
169: 
170:       matching_tuples.map do |(name, version, platform,_),_|
171:         platforms[version] << platform if platform
172:       end
173: 
174:       seen = {}
175: 
176:       matching_tuples.delete_if do |(name, version,_),_|
177:         if seen[version] then
178:           true
179:         else
180:           seen[version] = true
181:           false
182:         end
183:       end
184: 
185:       entry = gem_name.dup
186: 
187:       if options[:versions] then
188:         list = if platforms.empty? or options[:details] then
189:                  matching_tuples.map { |(name, version,_),_| version }.uniq
190:                else
191:                  platforms.sort.reverse.map do |version, pls|
192:                    if pls == [Gem::Platform::RUBY] then
193:                      version
194:                    else
195:                      ruby = pls.delete Gem::Platform::RUBY
196:                      platform_list = [ruby, *pls.sort].compact
197:                      "#{version} #{platform_list.join ' '}"
198:                    end
199:                  end
200:                end.join ', '
201: 
202:         entry << " (#{list})"
203:       end
204: 
205:       if options[:details] then
206:         detail_tuple = matching_tuples.first
207: 
208:         spec = if detail_tuple.first.length == 4 then
209:                  detail_tuple.first.last
210:                else
211:                  uri = URI.parse detail_tuple.last
212:                  Gem::SpecFetcher.fetcher.fetch_spec detail_tuple.first, uri
213:                end
214: 
215:         entry << "\n"
216: 
217:         non_ruby = platforms.any? do |_, pls|
218:           pls.any? { |pl| pl != Gem::Platform::RUBY }
219:         end
220: 
221:         if non_ruby then
222:           if platforms.length == 1 then
223:             title = platforms.values.length == 1 ? 'Platform' : 'Platforms'
224:             entry << "    #{title}: #{platforms.values.sort.join ', '}\n"
225:           else
226:             entry << "    Platforms:\n"
227:             platforms.sort_by do |version,|
228:               version
229:             end.each do |version, pls|
230:               label = "        #{version}: "
231:               data = format_text pls.sort.join(', '), 68, label.length
232:               data[0, label.length] = label
233:               entry << data << "\n"
234:             end
235:           end
236:         end
237: 
238:         authors = "Author#{spec.authors.length > 1 ? 's' : ''}: "
239:         authors << spec.authors.join(', ')
240:         entry << format_text(authors, 68, 4)
241: 
242:         if spec.rubyforge_project and not spec.rubyforge_project.empty? then
243:           rubyforge = "Rubyforge: http://rubyforge.org/projects/#{spec.rubyforge_project}"
244:           entry << "\n" << format_text(rubyforge, 68, 4)
245:         end
246: 
247:         if spec.homepage and not spec.homepage.empty? then
248:           entry << "\n" << format_text("Homepage: #{spec.homepage}", 68, 4)
249:         end
250: 
251:         if spec.license and not spec.license.empty? then
252:           licenses = "License#{spec.licenses.length > 1 ? 's' : ''}: "
253:           licenses << spec.licenses.join(', ')
254:           entry << "\n" << format_text(licenses, 68, 4)
255:         end
256: 
257:         if spec.loaded_from then
258:           if matching_tuples.length == 1 then
259:             loaded_from = File.dirname File.dirname(spec.loaded_from)
260:             entry << "\n" << "    Installed at: #{loaded_from}"
261:           else
262:             label = 'Installed at'
263:             matching_tuples.each do |(_,version,_,s),|
264:               loaded_from = File.dirname File.dirname(s.loaded_from)
265:               entry << "\n" << "    #{label} (#{version}): #{loaded_from}"
266:               label = ' ' * label.length
267:             end
268:           end
269:         end
270: 
271:         entry << "\n\n" << format_text(spec.summary, 68, 4)
272:       end
273:       output << entry
274:     end
275: 
276:     say output.join(options[:details] ? "\n\n" : "\n")
277:   end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.