Class: Gem::Commands::DependencyCommand

Inherits:
Gem::Command show all
Includes:
LocalRemoteOptions, VersionOption
Defined in:
lib/rubygems/commands/dependency_command.rb

Instance Attribute Summary

Attributes inherited from Gem::Command

#command, #defaults, #options, #program_name, #summary

Instance Method Summary collapse

Methods included from VersionOption

#add_platform_option, #add_prerelease_option, #add_version_option, #get_platform_from_requirements

Methods included from LocalRemoteOptions

#accept_uri_http, #add_bulk_threshold_option, #add_clear_sources_option, #add_local_remote_options, #add_proxy_option, #add_source_option, #add_update_sources_option, #both?, #local?, #remote?

Methods inherited from Gem::Command

add_common_option, #add_extra_args, #add_option, add_specific_extra_args, #begins?, build_args, build_args=, #check_deprecated_options, common_options, #deprecate_option, #deprecated?, extra_args, extra_args=, #extract_gem_name_and_version, #get_all_gem_names, #get_all_gem_names_and_versions, #get_one_gem_name, #get_one_optional_argument, #handle_options, #handles?, #invoke, #invoke_with_build_args, #merge_options, #remove_option, #show_help, #show_lookup_failure, specific_extra_args, specific_extra_args_hash, #when_invoked

Methods included from UserInteraction

#alert, #alert_error, #alert_warning, #ask, #ask_for_password, #ask_yes_no, #choose_from_list, #say, #terminate_interaction, #verbose

Methods included from DefaultUserInteraction

ui, #ui, ui=, #ui=, use_ui, #use_ui

Methods included from Text

#clean_text, #format_text, #levenshtein_distance, #min3, #truncate_text

Constructor Details

#initializeDependencyCommand

Returns a new instance of DependencyCommand.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/rubygems/commands/dependency_command.rb', line 11

def initialize
  super "dependency",
        "Show the dependencies of an installed gem",
        version: Gem::Requirement.default, domain: :local

  add_version_option
  add_platform_option
  add_prerelease_option

  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

Instance Method Details

#argumentsObject

:nodoc:



33
34
35
# File 'lib/rubygems/commands/dependency_command.rb', line 33

def arguments # :nodoc:
  "REGEXP        show dependencies for gems whose names start with REGEXP"
end

#defaults_strObject

:nodoc:



37
38
39
# File 'lib/rubygems/commands/dependency_command.rb', line 37

def defaults_str # :nodoc:
  "--local --version '#{Gem::Requirement.default}' --no-reverse-dependencies"
end

#descriptionObject

:nodoc:



41
42
43
44
45
46
47
48
49
50
# File 'lib/rubygems/commands/dependency_command.rb', line 41

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

#display_pipe(specs) ⇒ Object

:nodoc:



91
92
93
94
95
96
97
98
# File 'lib/rubygems/commands/dependency_command.rb', line 91

def display_pipe(specs) # :nodoc:
  specs.each do |spec|
    next if spec.dependencies.empty?
    spec.dependencies.sort_by(&:name).each do |dep|
      say "#{dep.name} --version '#{dep.requirement}'"
    end
  end
end

#display_readable(specs, reverse) ⇒ Object

:nodoc:



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rubygems/commands/dependency_command.rb', line 100

def display_readable(specs, reverse) # :nodoc:
  response = String.new

  specs.each do |spec|
    response << print_dependencies(spec)
    unless reverse[spec.full_name].empty?
      response << "  Used by\n"
      reverse[spec.full_name].each do |sp, dep|
        response << "    #{sp} (#{dep})\n"
      end
    end
    response << "\n"
  end

  say response
end

#ensure_local_only_reverse_dependenciesObject

:nodoc:



134
135
136
137
138
139
# File 'lib/rubygems/commands/dependency_command.rb', line 134

def ensure_local_only_reverse_dependencies # :nodoc:
  if options[:reverse_dependencies] && remote? && !local?
    alert_error "Only reverse dependencies for local gems are supported."
    terminate_interaction 1
  end
end

#ensure_specs(specs) ⇒ Object

:nodoc:



141
142
143
144
145
146
147
148
149
# File 'lib/rubygems/commands/dependency_command.rb', line 141

def ensure_specs(specs) # :nodoc:
  return unless specs.empty?

  patterns = options[:args].join ","
  say "No gems found matching #{patterns} (#{options[:version]})" if
    Gem.configuration.verbose

  terminate_interaction 1
end

#executeObject



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/rubygems/commands/dependency_command.rb', line 117

def execute
  ensure_local_only_reverse_dependencies

  pattern = name_pattern options[:args]
  requirement = Gem::Requirement.new options[:version]

  specs = fetch_specs pattern, requirement, options[:prerelease]

  reverse = reverse_dependencies specs

  if options[:pipe_format]
    display_pipe specs
  else
    display_readable specs, reverse
  end
end

#fetch_remote_specs(name, requirement, prerelease) ⇒ Object

:nodoc:



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/rubygems/commands/dependency_command.rb', line 56

def fetch_remote_specs(name, requirement, prerelease) # :nodoc:
  fetcher = Gem::SpecFetcher.fetcher

  specs_type = prerelease ? :complete : :released

  ss = if name.nil?
    fetcher.detect(specs_type) { true }
  else
    fetcher.detect(specs_type) do |name_tuple|
      name === name_tuple.name && requirement.satisfied_by?(name_tuple.version)
    end
  end

  ss.map {|tuple, source| source.fetch_spec(tuple) }
end

#fetch_specs(name_pattern, requirement, prerelease) ⇒ Object

:nodoc:



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rubygems/commands/dependency_command.rb', line 72

def fetch_specs(name_pattern, requirement, prerelease) # :nodoc:
  specs = []

  if local?
    specs.concat Gem::Specification.stubs.find_all {|spec|
      name_matches = name_pattern ? name_pattern =~ spec.name : true
      version_matches = requirement.satisfied_by?(spec.version)

      name_matches && version_matches
    }.map(&:to_spec)
  end

  specs.concat fetch_remote_specs name_pattern, requirement, prerelease if remote?

  ensure_specs specs

  specs.uniq.sort
end

#find_reverse_dependencies(spec) ⇒ Object

Returns an Array of [specification, dep] that are satisfied by spec.



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rubygems/commands/dependency_command.rb', line 177

def find_reverse_dependencies(spec) # :nodoc:
  result = []

  Gem::Specification.each do |sp|
    sp.dependencies.each do |dep|
      dep = Gem::Dependency.new(*dep) unless Gem::Dependency === dep

      if spec.name == dep.name &&
         dep.requirement.satisfied_by?(spec.version)
        result << [sp.full_name, dep]
      end
    end
  end

  result
end

:nodoc:



151
152
153
154
155
156
157
158
159
160
# File 'lib/rubygems/commands/dependency_command.rb', line 151

def print_dependencies(spec, level = 0) # :nodoc:
  response = String.new
  response << "  " * level + "Gem #{spec.full_name}\n"
  unless spec.dependencies.empty?
    spec.dependencies.sort_by(&:name).each do |dep|
      response << "  " * level + "  #{dep}\n"
    end
  end
  response
end

#reverse_dependencies(specs) ⇒ Object

:nodoc:



162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rubygems/commands/dependency_command.rb', line 162

def reverse_dependencies(specs) # :nodoc:
  reverse = Hash.new {|h, k| h[k] = [] }

  return reverse unless options[:reverse_dependencies]

  specs.each do |spec|
    reverse[spec.full_name] = find_reverse_dependencies spec
  end

  reverse
end

#usageObject

:nodoc:



52
53
54
# File 'lib/rubygems/commands/dependency_command.rb', line 52

def usage # :nodoc:
  "#{program_name} REGEXP"
end