Class | Gem::Commands::DependencyCommand |
In: |
lib/rubygems/commands/dependency_command.rb
|
Parent: | Gem::Command |
# File lib/rubygems/commands/dependency_command.rb, line 10 10: def initialize 11: super 'dependency', 12: 'Show the dependencies of an installed gem', 13: :version => Gem::Requirement.default, :domain => :local 14: 15: add_version_option 16: add_platform_option 17: add_prerelease_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: dependency.prerelease = options[:prerelease] 63: 64: if options[:reverse_dependencies] and remote? and not local? then 65: alert_error 'Only reverse dependencies for local gems are supported.' 66: terminate_interaction 1 67: end 68: 69: if local? then 70: Gem.source_index.search(dependency).each do |spec| 71: source_indexes[:local].add_spec spec 72: end 73: end 74: 75: if remote? and not options[:reverse_dependencies] then 76: fetcher = Gem::SpecFetcher.fetcher 77: 78: specs_and_sources = fetcher.find_matching(dependency, false, true, 79: dependency.prerelease?) 80: 81: specs_and_sources.each do |spec_tuple, source_uri| 82: spec = fetcher.fetch_spec spec_tuple, URI.parse(source_uri) 83: 84: source_indexes[source_uri].add_spec spec 85: end 86: end 87: 88: if source_indexes.empty? then 89: patterns = options[:args].join ',' 90: say "No gems found matching #{patterns} (#{options[:version]})" if 91: Gem.configuration.verbose 92: 93: terminate_interaction 1 94: end 95: 96: specs = {} 97: 98: source_indexes.values.each do |source_index| 99: source_index.gems.each do |name, spec| 100: specs[spec.full_name] = [source_index, spec] 101: end 102: end 103: 104: reverse = Hash.new { |h, k| h[k] = [] } 105: 106: if options[:reverse_dependencies] then 107: specs.values.each do |_, spec| 108: reverse[spec.full_name] = find_reverse_dependencies spec 109: end 110: end 111: 112: if options[:pipe_format] then 113: specs.values.sort_by { |_, spec| spec }.each do |_, spec| 114: unless spec.dependencies.empty? 115: spec.dependencies.sort_by { |dep| dep.name }.each do |dep| 116: say "#{dep.name} --version '#{dep.requirement}'" 117: end 118: end 119: end 120: else 121: response = '' 122: 123: specs.values.sort_by { |_, spec| spec }.each do |_, spec| 124: response << print_dependencies(spec) 125: unless reverse[spec.full_name].empty? then 126: response << " Used by\n" 127: reverse[spec.full_name].each do |sp, dep| 128: response << " #{sp} (#{dep})\n" 129: end 130: end 131: response << "\n" 132: end 133: 134: say response 135: end 136: end
# File lib/rubygems/commands/dependency_command.rb, line 169 169: def find_gems(name, source_index) 170: specs = {} 171: 172: spec_list = source_index.search name, options[:version] 173: 174: spec_list.each do |spec| 175: specs[spec.full_name] = [source_index, spec] 176: end 177: 178: specs 179: end
Returns an Array of [specification, dep] that are satisfied by spec.
# File lib/rubygems/commands/dependency_command.rb, line 152 152: def find_reverse_dependencies(spec) 153: result = [] 154: 155: Gem.source_index.each do |name, sp| 156: sp.dependencies.each do |dep| 157: dep = Gem::Dependency.new(*dep) unless Gem::Dependency === dep 158: 159: if spec.name == dep.name and 160: dep.requirement.satisfied_by?(spec.version) then 161: result << [sp.full_name, dep] 162: end 163: end 164: end 165: 166: result 167: end
# File lib/rubygems/commands/dependency_command.rb, line 138 138: def print_dependencies(spec, level = 0) 139: response = '' 140: response << ' ' * level + "Gem #{spec.full_name}\n" 141: unless spec.dependencies.empty? then 142: spec.dependencies.sort_by { |dep| dep.name }.each do |dep| 143: response << ' ' * level + " #{dep}\n" 144: end 145: end 146: response 147: end