# 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
# 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: terminate_interaction exit_code 78: end 79: 80: req = Gem::Requirement.default 81: # TODO: deprecate for real 82: dep = Gem::Deprecate.skip_during { Gem::Dependency.new name, req } 83: 84: if local? then 85: if prerelease and not both? then 86: alert_warning "prereleases are always shown locally" 87: end 88: 89: if ui.outs.tty? or both? then 90: say 91: say "*** LOCAL GEMS ***" 92: say 93: end 94: 95: specs = Gem::Specification.find_all { |s| 96: s.name =~ name and req =~ s.version 97: } 98: 99: spec_tuples = specs.map do |spec| 100: [[spec.name, spec.version, spec.original_platform, spec], :local] 101: end 102: 103: output_query_results spec_tuples 104: end 105: 106: if remote? then 107: if ui.outs.tty? or both? then 108: say 109: say "*** REMOTE GEMS ***" 110: say 111: end 112: 113: all = options[:all] 114: 115: fetcher = Gem::SpecFetcher.fetcher 116: spec_tuples = fetcher.find_matching dep, all, false, prerelease 117: 118: spec_tuples += fetcher.find_matching dep, false, false, true if 119: prerelease and all 120: 121: output_query_results spec_tuples 122: end 123: end
Check if gem name version version is installed.
# File lib/rubygems/commands/query_command.rb, line 130 130: def installed?(name, req = Gem::Requirement.default) 131: Gem::Specification.any? { |s| s.name =~ name and req =~ s.version } 132: end
# File lib/rubygems/commands/query_command.rb, line 134 134: def output_query_results(spec_tuples) 135: output = [] 136: versions = Hash.new { |h,name| h[name] = [] } 137: 138: spec_tuples.each do |spec_tuple, source_uri| 139: versions[spec_tuple.first] << [spec_tuple, source_uri] 140: end 141: 142: versions = versions.sort_by do |(name,_),_| 143: name.downcase 144: end 145: 146: versions.each do |gem_name, matching_tuples| 147: matching_tuples = matching_tuples.sort_by do |(_, version,_),_| 148: version 149: end.reverse 150: 151: platforms = Hash.new { |h,version| h[version] = [] } 152: 153: matching_tuples.map do |(_, version, platform,_),_| 154: platforms[version] << platform if platform 155: end 156: 157: seen = {} 158: 159: matching_tuples.delete_if do |(_, version,_),_| 160: if seen[version] then 161: true 162: else 163: seen[version] = true 164: false 165: end 166: end 167: 168: entry = gem_name.dup 169: 170: if options[:versions] then 171: list = if platforms.empty? or options[:details] then 172: matching_tuples.map { |(_, version,_),_| version }.uniq 173: else 174: platforms.sort.reverse.map do |version, pls| 175: if pls == [Gem::Platform::RUBY] then 176: version 177: else 178: ruby = pls.delete Gem::Platform::RUBY 179: platform_list = [ruby, *pls.sort].compact 180: "#{version} #{platform_list.join ' '}" 181: end 182: end 183: end.join ', ' 184: 185: entry << " (#{list})" 186: end 187: 188: if options[:details] then 189: detail_tuple = matching_tuples.first 190: 191: spec = if detail_tuple.first.length == 4 then 192: detail_tuple.first.last 193: else 194: uri = URI.parse detail_tuple.last 195: Gem::SpecFetcher.fetcher.fetch_spec detail_tuple.first, uri 196: end 197: 198: entry << "\n" 199: 200: non_ruby = platforms.any? do |_, pls| 201: pls.any? { |pl| pl != Gem::Platform::RUBY } 202: end 203: 204: if non_ruby then 205: if platforms.length == 1 then 206: title = platforms.values.length == 1 ? 'Platform' : 'Platforms' 207: entry << " #{title}: #{platforms.values.sort.join ', '}\n" 208: else 209: entry << " Platforms:\n" 210: platforms.sort_by do |version,| 211: version 212: end.each do |version, pls| 213: label = " #{version}: " 214: data = format_text pls.sort.join(', '), 68, label.length 215: data[0, label.length] = label 216: entry << data << "\n" 217: end 218: end 219: end 220: 221: authors = "Author#{spec.authors.length > 1 ? 's' : ''}: " 222: authors << spec.authors.join(', ') 223: entry << format_text(authors, 68, 4) 224: 225: if spec.rubyforge_project and not spec.rubyforge_project.empty? then 226: rubyforge = "Rubyforge: http://rubyforge.org/projects/#{spec.rubyforge_project}" 227: entry << "\n" << format_text(rubyforge, 68, 4) 228: end 229: 230: if spec.homepage and not spec.homepage.empty? then 231: entry << "\n" << format_text("Homepage: #{spec.homepage}", 68, 4) 232: end 233: 234: if spec.license and not spec.license.empty? then 235: licenses = "License#{spec.licenses.length > 1 ? 's' : ''}: " 236: licenses << spec.licenses.join(', ') 237: entry << "\n" << format_text(licenses, 68, 4) 238: end 239: 240: if spec.loaded_from then 241: if matching_tuples.length == 1 then 242: loaded_from = File.dirname File.dirname(spec.loaded_from) 243: entry << "\n" << " Installed at: #{loaded_from}" 244: else 245: label = 'Installed at' 246: matching_tuples.each do |(_,version,_,s),| 247: loaded_from = File.dirname File.dirname(s.loaded_from) 248: entry << "\n" << " #{label} (#{version}): #{loaded_from}" 249: label = ' ' * label.length 250: end 251: end 252: end 253: 254: entry << "\n\n" << format_text(spec.summary, 68, 4) 255: end 256: output << entry 257: end 258: 259: say output.join(options[:details] ? "\n\n" : "\n") 260: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.