| 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: if options[:reverse_dependencies] and remote? and not local? then
47: alert_error 'Only reverse dependencies for local gems are supported.'
48: terminate_interaction 1
49: end
50:
51: options[:args] << '' if options[:args].empty?
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: # TODO: deprecate for real damnit
62: dependency = Gem::Deprecate.skip_during {
63: Gem::Dependency.new pattern, options[:version]
64: }
65: dependency.prerelease = options[:prerelease]
66:
67: specs = []
68:
69: specs.concat dependency.matching_specs if local?
70:
71: if remote? and not options[:reverse_dependencies] then
72: fetcher = Gem::SpecFetcher.fetcher
73:
74: # REFACTOR: fetcher.find_specs_matching => specs
75: specs_and_sources = fetcher.find_matching(dependency,
76: dependency.specific?, true,
77: dependency.prerelease?)
78:
79: specs.concat specs_and_sources.map { |spec_tuple, source_uri|
80: fetcher.fetch_spec spec_tuple, URI.parse(source_uri)
81: }
82: end
83:
84: if specs.empty? then
85: patterns = options[:args].join ','
86: say "No gems found matching #{patterns} (#{options[:version]})" if
87: Gem.configuration.verbose
88:
89: terminate_interaction 1
90: end
91:
92: specs = specs.uniq.sort
93:
94: reverse = Hash.new { |h, k| h[k] = [] }
95:
96: if options[:reverse_dependencies] then
97: specs.each do |spec|
98: reverse[spec.full_name] = find_reverse_dependencies spec
99: end
100: end
101:
102: if options[:pipe_format] then
103: specs.each do |spec|
104: unless spec.dependencies.empty?
105: spec.dependencies.sort_by { |dep| dep.name }.each do |dep|
106: say "#{dep.name} --version '#{dep.requirement}'"
107: end
108: end
109: end
110: else
111: response = ''
112:
113: specs.each do |spec|
114: response << print_dependencies(spec)
115: unless reverse[spec.full_name].empty? then
116: response << " Used by\n"
117: reverse[spec.full_name].each do |sp, dep|
118: response << " #{sp} (#{dep})\n"
119: end
120: end
121: response << "\n"
122: end
123:
124: say response
125: end
126: end
Returns an Array of [specification, dep] that are satisfied by spec.
# File lib/rubygems/commands/dependency_command.rb, line 142
142: def find_reverse_dependencies(spec)
143: result = []
144:
145: Gem::Specification.each do |sp|
146: sp.dependencies.each do |dep|
147: dep = Gem::Dependency.new(*dep) unless Gem::Dependency === dep
148:
149: if spec.name == dep.name and
150: dep.requirement.satisfied_by?(spec.version) then
151: result << [sp.full_name, dep]
152: end
153: end
154: end
155:
156: result
157: end
# File lib/rubygems/commands/dependency_command.rb, line 128
128: def print_dependencies(spec, level = 0)
129: response = ''
130: response << ' ' * level + "Gem #{spec.full_name}\n"
131: unless spec.dependencies.empty? then
132: spec.dependencies.sort_by { |dep| dep.name }.each do |dep|
133: response << ' ' * level + " #{dep}\n"
134: end
135: end
136: response
137: end