2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/command'
|
|
|
|
require 'rubygems/local_remote_options'
|
|
|
|
require 'rubygems/version_option'
|
|
|
|
|
|
|
|
class Gem::Commands::DependencyCommand < Gem::Command
|
|
|
|
|
|
|
|
include Gem::LocalRemoteOptions
|
|
|
|
include Gem::VersionOption
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super 'dependency',
|
|
|
|
'Show the dependencies of an installed gem',
|
|
|
|
:version => Gem::Requirement.default, :domain => :local
|
|
|
|
|
|
|
|
add_version_option
|
|
|
|
add_platform_option
|
2010-02-21 21:52:35 -05:00
|
|
|
add_prerelease_option
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
add_option('-R', '--[no-]reverse-dependencies',
|
|
|
|
'Include reverse dependencies in the output') do
|
|
|
|
|value, options|
|
|
|
|
options[:reverse_dependencies] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
add_option('-p', '--pipe',
|
|
|
|
"Pipe Format (name --version ver)") do |value, options|
|
|
|
|
options[:pipe_format] = value
|
|
|
|
end
|
|
|
|
|
|
|
|
add_local_remote_options
|
|
|
|
end
|
|
|
|
|
|
|
|
def arguments # :nodoc:
|
|
|
|
"GEMNAME name of gem to show dependencies for"
|
|
|
|
end
|
|
|
|
|
|
|
|
def defaults_str # :nodoc:
|
|
|
|
"--local --version '#{Gem::Requirement.default}' --no-reverse-dependencies"
|
|
|
|
end
|
|
|
|
|
|
|
|
def usage # :nodoc:
|
|
|
|
"#{program_name} GEMNAME"
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2011-05-31 23:45:05 -04:00
|
|
|
if options[:reverse_dependencies] and remote? and not local? then
|
|
|
|
alert_error 'Only reverse dependencies for local gems are supported.'
|
|
|
|
terminate_interaction 1
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
options[:args] << '' if options[:args].empty?
|
|
|
|
|
2008-06-19 14:15:26 -04:00
|
|
|
pattern = if options[:args].length == 1 and
|
|
|
|
options[:args].first =~ /\A\/(.*)\/(i)?\z/m then
|
|
|
|
flags = $2 ? Regexp::IGNORECASE : nil
|
|
|
|
Regexp.new $1, flags
|
|
|
|
else
|
|
|
|
/\A#{Regexp.union(*options[:args])}/
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
# TODO: deprecate for real damnit
|
2011-10-03 18:15:47 -04:00
|
|
|
dependency = Gem::Deprecate.skip_during {
|
2011-05-31 23:45:05 -04:00
|
|
|
Gem::Dependency.new pattern, options[:version]
|
|
|
|
}
|
2010-02-21 21:52:35 -05:00
|
|
|
dependency.prerelease = options[:prerelease]
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
specs = []
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
specs.concat dependency.matching_specs if local?
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
if remote? and not options[:reverse_dependencies] then
|
|
|
|
fetcher = Gem::SpecFetcher.fetcher
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
ss, _ = fetcher.spec_for_dependency dependency
|
|
|
|
|
|
|
|
ss.each { |s,o| specs << s }
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
if specs.empty? then
|
2008-06-17 18:04:18 -04:00
|
|
|
patterns = options[:args].join ','
|
|
|
|
say "No gems found matching #{patterns} (#{options[:version]})" if
|
|
|
|
Gem.configuration.verbose
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
terminate_interaction 1
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
specs = specs.uniq.sort
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
reverse = Hash.new { |h, k| h[k] = [] }
|
|
|
|
|
|
|
|
if options[:reverse_dependencies] then
|
2011-05-31 23:45:05 -04:00
|
|
|
specs.each do |spec|
|
2008-06-17 18:04:18 -04:00
|
|
|
reverse[spec.full_name] = find_reverse_dependencies spec
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if options[:pipe_format] then
|
2011-05-31 23:45:05 -04:00
|
|
|
specs.each do |spec|
|
2007-11-10 02:48:56 -05:00
|
|
|
unless spec.dependencies.empty?
|
2010-02-21 21:52:35 -05:00
|
|
|
spec.dependencies.sort_by { |dep| dep.name }.each do |dep|
|
|
|
|
say "#{dep.name} --version '#{dep.requirement}'"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
response = ''
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
specs.each do |spec|
|
2007-11-10 02:48:56 -05:00
|
|
|
response << print_dependencies(spec)
|
|
|
|
unless reverse[spec.full_name].empty? then
|
|
|
|
response << " Used by\n"
|
|
|
|
reverse[spec.full_name].each do |sp, dep|
|
|
|
|
response << " #{sp} (#{dep})\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
response << "\n"
|
|
|
|
end
|
|
|
|
|
|
|
|
say response
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def print_dependencies(spec, level = 0)
|
|
|
|
response = ''
|
|
|
|
response << ' ' * level + "Gem #{spec.full_name}\n"
|
|
|
|
unless spec.dependencies.empty? then
|
2010-02-21 21:52:35 -05:00
|
|
|
spec.dependencies.sort_by { |dep| dep.name }.each do |dep|
|
2007-11-10 02:48:56 -05:00
|
|
|
response << ' ' * level + " #{dep}\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
##
|
|
|
|
# Returns an Array of [specification, dep] that are satisfied by +spec+.
|
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
def find_reverse_dependencies(spec)
|
2007-11-10 02:48:56 -05:00
|
|
|
result = []
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
Gem::Specification.each do |sp|
|
2007-11-10 02:48:56 -05:00
|
|
|
sp.dependencies.each do |dep|
|
|
|
|
dep = Gem::Dependency.new(*dep) unless Gem::Dependency === dep
|
|
|
|
|
|
|
|
if spec.name == dep.name and
|
2010-02-21 21:52:35 -05:00
|
|
|
dep.requirement.satisfied_by?(spec.version) then
|
2007-11-10 02:48:56 -05:00
|
|
|
result << [sp.full_name, dep]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|