Class | Gem::Commands::DependencyCommand |
In: |
lib/rubygems/commands/dependency_command.rb
|
Parent: | Gem::Command |
# File lib/rubygems/commands/dependency_command.rb, line 11 11: def initialize 12: super 'dependency', 13: 'Show the dependencies of an installed gem', 14: :version => Gem::Requirement.default, :domain => :local 15: 16: add_version_option 17: add_platform_option 18: 19: add_option('-R', '--[no-]reverse-dependencies', 20: 'Include reverse dependencies in the output') do 21: |value, options| 22: options[:reverse_dependencies] = value 23: end 24: 25: add_option('-p', '--pipe', 26: "Pipe Format (name --version ver)") do |value, options| 27: options[:pipe_format] = value 28: end 29: 30: add_local_remote_options 31: end
# File lib/rubygems/commands/dependency_command.rb, line 45 45: def execute 46: options[:args] << '' if options[:args].empty? 47: specs = {} 48: 49: source_indexes = Hash.new do |h, source_uri| 50: h[source_uri] = Gem::SourceIndex.new 51: end 52: 53: pattern = if options[:args].length == 1 and 54: options[:args].first =~ /\A\/(.*)\/(i)?\z/m then 55: flags = $2 ? Regexp::IGNORECASE : nil 56: Regexp.new $1, flags 57: else 58: /\A#{Regexp.union(*options[:args])}/ 59: end 60: 61: dependency = Gem::Dependency.new pattern, options[:version] 62: 63: if options[:reverse_dependencies] and remote? and not local? then 64: alert_error 'Only reverse dependencies for local gems are supported.' 65: terminate_interaction 1 66: end 67: 68: if local? then 69: Gem.source_index.search(dependency).each do |spec| 70: source_indexes[:local].add_spec spec 71: end 72: end 73: 74: if remote? and not options[:reverse_dependencies] then 75: fetcher = Gem::SpecFetcher.fetcher 76: 77: begin 78: fetcher.find_matching(dependency).each do |spec_tuple, source_uri| 79: spec = fetcher.fetch_spec spec_tuple, URI.parse(source_uri) 80: 81: source_indexes[source_uri].add_spec spec 82: end 83: rescue Gem::RemoteFetcher::FetchError => e 84: raise unless fetcher.warn_legacy e do 85: require 'rubygems/source_info_cache' 86: 87: specs = Gem::SourceInfoCache.search_with_source dependency, false 88: 89: specs.each do |spec, source_uri| 90: source_indexes[source_uri].add_spec spec 91: end 92: end 93: end 94: end 95: 96: if source_indexes.empty? then 97: patterns = options[:args].join ',' 98: say "No gems found matching #{patterns} (#{options[:version]})" if 99: Gem.configuration.verbose 100: 101: terminate_interaction 1 102: end 103: 104: specs = {} 105: 106: source_indexes.values.each do |source_index| 107: source_index.gems.each do |name, spec| 108: specs[spec.full_name] = [source_index, spec] 109: end 110: end 111: 112: reverse = Hash.new { |h, k| h[k] = [] } 113: 114: if options[:reverse_dependencies] then 115: specs.values.each do |_, spec| 116: reverse[spec.full_name] = find_reverse_dependencies spec 117: end 118: end 119: 120: if options[:pipe_format] then 121: specs.values.sort_by { |_, spec| spec }.each do |_, spec| 122: unless spec.dependencies.empty? 123: spec.dependencies.each do |dep| 124: say "#{dep.name} --version '#{dep.version_requirements}'" 125: end 126: end 127: end 128: else 129: response = '' 130: 131: specs.values.sort_by { |_, spec| spec }.each do |_, spec| 132: response << print_dependencies(spec) 133: unless reverse[spec.full_name].empty? then 134: response << " Used by\n" 135: reverse[spec.full_name].each do |sp, dep| 136: response << " #{sp} (#{dep})\n" 137: end 138: end 139: response << "\n" 140: end 141: 142: say response 143: end 144: end
# File lib/rubygems/commands/dependency_command.rb, line 175 175: def find_gems(name, source_index) 176: specs = {} 177: 178: spec_list = source_index.search name, options[:version] 179: 180: spec_list.each do |spec| 181: specs[spec.full_name] = [source_index, spec] 182: end 183: 184: specs 185: end
Retuns list of [specification, dep] that are satisfied by spec.
# File lib/rubygems/commands/dependency_command.rb, line 158 158: def find_reverse_dependencies(spec) 159: result = [] 160: 161: Gem.source_index.each do |name, sp| 162: sp.dependencies.each do |dep| 163: dep = Gem::Dependency.new(*dep) unless Gem::Dependency === dep 164: 165: if spec.name == dep.name and 166: dep.version_requirements.satisfied_by?(spec.version) then 167: result << [sp.full_name, dep] 168: end 169: end 170: end 171: 172: result 173: end
# File lib/rubygems/commands/dependency_command.rb, line 146 146: def print_dependencies(spec, level = 0) 147: response = '' 148: response << ' ' * level + "Gem #{spec.full_name}\n" 149: unless spec.dependencies.empty? then 150: spec.dependencies.each do |dep| 151: response << ' ' * level + " #{dep}\n" 152: end 153: end 154: response 155: end