2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
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:
|
2014-09-13 23:30:02 -04:00
|
|
|
"REGEXP show dependencies for gems whose names start with REGEXP"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def defaults_str # :nodoc:
|
|
|
|
"--local --version '#{Gem::Requirement.default}' --no-reverse-dependencies"
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def description # :nodoc:
|
|
|
|
<<-EOF
|
|
|
|
The dependency commands lists which other gems a given gem depends on. For
|
|
|
|
local gems only the reverse dependencies can be shown (which gems depend on
|
|
|
|
the named gem).
|
|
|
|
|
|
|
|
The dependency list can be displayed in a format suitable for piping for
|
|
|
|
use with other commands.
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def usage # :nodoc:
|
2014-09-13 23:30:02 -04:00
|
|
|
"#{program_name} REGEXP"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def fetch_remote_specs(dependency) # :nodoc:
|
2013-09-14 04:59:02 -04:00
|
|
|
fetcher = Gem::SpecFetcher.fetcher
|
|
|
|
|
|
|
|
ss, = fetcher.spec_for_dependency dependency
|
|
|
|
|
2020-06-10 13:46:05 -04:00
|
|
|
ss.map {|spec, _| spec }
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def fetch_specs(name_pattern, dependency) # :nodoc:
|
2013-09-14 04:59:02 -04:00
|
|
|
specs = []
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
if local?
|
2020-06-10 13:46:05 -04:00
|
|
|
specs.concat Gem::Specification.stubs.find_all {|spec|
|
2015-07-01 17:50:14 -04:00
|
|
|
name_pattern =~ spec.name and
|
|
|
|
dependency.requirement.satisfied_by? spec.version
|
|
|
|
}.map(&:to_spec)
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
specs.concat fetch_remote_specs dependency if remote?
|
|
|
|
|
|
|
|
ensure_specs specs
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
specs.uniq.sort
|
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def gem_dependency(pattern, version, prerelease) # :nodoc:
|
2019-02-14 07:59:03 -05:00
|
|
|
dependency = Gem::Deprecate.skip_during do
|
2013-09-14 04:59:02 -04:00
|
|
|
Gem::Dependency.new pattern, version
|
2019-02-14 07:59:03 -05:00
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
dependency.prerelease = prerelease
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
dependency
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def display_pipe(specs) # :nodoc:
|
2013-09-14 04:59:02 -04:00
|
|
|
specs.each do |spec|
|
2018-11-21 05:20:47 -05:00
|
|
|
unless spec.dependencies.empty?
|
2020-06-10 13:46:05 -04:00
|
|
|
spec.dependencies.sort_by {|dep| dep.name }.each do |dep|
|
2013-09-14 04:59:02 -04:00
|
|
|
say "#{dep.name} --version '#{dep.requirement}'"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-06-17 18:04:18 -04:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def display_readable(specs, reverse) # :nodoc:
|
2016-02-01 07:43:26 -05:00
|
|
|
response = String.new
|
2012-11-29 01:52:18 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
specs.each do |spec|
|
|
|
|
response << print_dependencies(spec)
|
2018-11-21 05:20:47 -05:00
|
|
|
unless reverse[spec.full_name].empty?
|
2013-09-14 04:59:02 -04:00
|
|
|
response << " Used by\n"
|
|
|
|
reverse[spec.full_name].each do |sp, dep|
|
|
|
|
response << " #{sp} (#{dep})\n"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
response << "\n"
|
2008-06-17 18:04:18 -04:00
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
say response
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def execute
|
|
|
|
ensure_local_only_reverse_dependencies
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
pattern = name_pattern options[:args]
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
dependency =
|
2015-07-01 17:50:14 -04:00
|
|
|
gem_dependency pattern, options[:version], options[:prerelease]
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
specs = fetch_specs pattern, dependency
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
reverse = reverse_dependencies specs
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if options[:pipe_format]
|
2013-09-14 04:59:02 -04:00
|
|
|
display_pipe specs
|
2007-11-10 02:48:56 -05:00
|
|
|
else
|
2013-09-14 04:59:02 -04:00
|
|
|
display_readable specs, reverse
|
|
|
|
end
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def ensure_local_only_reverse_dependencies # :nodoc:
|
2018-11-21 05:20:47 -05:00
|
|
|
if options[:reverse_dependencies] and remote? and not local?
|
2013-09-14 04:59:02 -04:00
|
|
|
alert_error 'Only reverse dependencies for local gems are supported.'
|
|
|
|
terminate_interaction 1
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def ensure_specs(specs) # :nodoc:
|
2013-09-14 04:59:02 -04:00
|
|
|
return unless specs.empty?
|
|
|
|
|
|
|
|
patterns = options[:args].join ','
|
|
|
|
say "No gems found matching #{patterns} (#{options[:version]})" if
|
|
|
|
Gem.configuration.verbose
|
|
|
|
|
|
|
|
terminate_interaction 1
|
|
|
|
end
|
|
|
|
|
|
|
|
def print_dependencies(spec, level = 0) # :nodoc:
|
2016-02-01 07:43:26 -05:00
|
|
|
response = String.new
|
2007-11-10 02:48:56 -05:00
|
|
|
response << ' ' * level + "Gem #{spec.full_name}\n"
|
2018-11-21 05:20:47 -05:00
|
|
|
unless spec.dependencies.empty?
|
2020-06-10 13:46:05 -04: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
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def remote_specs(dependency) # :nodoc:
|
2013-09-14 04:59:02 -04:00
|
|
|
fetcher = Gem::SpecFetcher.fetcher
|
|
|
|
|
|
|
|
ss, _ = fetcher.spec_for_dependency dependency
|
|
|
|
|
2020-06-10 13:46:05 -04:00
|
|
|
ss.map {|s,o| s }
|
2013-09-14 04:59:02 -04:00
|
|
|
end
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def reverse_dependencies(specs) # :nodoc:
|
2020-06-10 13:46:05 -04:00
|
|
|
reverse = Hash.new {|h, k| h[k] = [] }
|
2013-09-14 04:59:02 -04:00
|
|
|
|
|
|
|
return reverse unless options[:reverse_dependencies]
|
|
|
|
|
|
|
|
specs.each do |spec|
|
|
|
|
reverse[spec.full_name] = find_reverse_dependencies spec
|
|
|
|
end
|
|
|
|
|
|
|
|
reverse
|
|
|
|
end
|
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
##
|
|
|
|
# Returns an Array of [specification, dep] that are satisfied by +spec+.
|
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def find_reverse_dependencies(spec) # :nodoc:
|
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
|
2018-11-21 05:20:47 -05:00
|
|
|
dep.requirement.satisfied_by?(spec.version)
|
2007-11-10 02:48:56 -05:00
|
|
|
result << [sp.full_name, dep]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
private
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
def name_pattern(args)
|
2015-07-01 17:50:14 -04:00
|
|
|
args << '' if args.empty?
|
|
|
|
|
2019-04-26 07:26:21 -04:00
|
|
|
if args.length == 1 and args.first =~ /\A(.*)(i)?\z/m
|
2015-07-01 17:50:14 -04:00
|
|
|
flags = $2 ? Regexp::IGNORECASE : nil
|
|
|
|
Regexp.new $1, flags
|
|
|
|
else
|
|
|
|
/\A#{Regexp.union(*args)}/
|
|
|
|
end
|
|
|
|
end
|
2019-02-14 07:59:03 -05:00
|
|
|
|
2015-07-01 17:50:14 -04:00
|
|
|
end
|