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