Class: LibGems::Commands::DependencyCommand
- Inherits:
-
LibGems::Command
- Object
- LibGems::Command
- LibGems::Commands::DependencyCommand
- Includes:
- LocalRemoteOptions, VersionOption
- Defined in:
- lib/libgems/commands/dependency_command.rb
Instance Attribute Summary
Attributes inherited from LibGems::Command
#command, #defaults, #options, #program_name, #summary
Instance Method Summary collapse
-
#arguments ⇒ Object
:nodoc:.
-
#defaults_str ⇒ Object
:nodoc:.
- #execute ⇒ Object
- #find_gems(name, source_index) ⇒ Object
-
#find_reverse_dependencies(spec) ⇒ Object
Returns an Array of [specification, dep] that are satisfied by
spec
. -
#initialize ⇒ DependencyCommand
constructor
A new instance of DependencyCommand.
- #print_dependencies(spec, level = 0) ⇒ Object
-
#usage ⇒ Object
:nodoc:.
Methods included from VersionOption
#add_platform_option, #add_prerelease_option, #add_version_option
Methods included from LocalRemoteOptions
#accept_uri_http, #add_bulk_threshold_option, #add_local_remote_options, #add_proxy_option, #add_source_option, #add_update_sources_option, #both?, #local?, #remote?
Methods inherited from LibGems::Command
add_common_option, #add_extra_args, #add_option, add_specific_extra_args, #begins?, build_args, build_args=, common_options, #description, extra_args, extra_args=, #get_all_gem_names, #get_one_gem_name, #get_one_optional_argument, #handle_options, #handles?, #invoke, #merge_options, #remove_option, #show_help, #show_lookup_failure, specific_extra_args, specific_extra_args_hash, #when_invoked
Methods included from UserInteraction
Methods included from DefaultUserInteraction
ui, #ui, ui=, #ui=, use_ui, #use_ui
Constructor Details
#initialize ⇒ DependencyCommand
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 32 |
# File 'lib/libgems/commands/dependency_command.rb', line 11 def initialize super 'dependency', 'Show the dependencies of an installed gem', :version => LibGems::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, | [:reverse_dependencies] = value end add_option('-p', '--pipe', "Pipe Format (name --version ver)") do |value, | [:pipe_format] = value end end |
Instance Method Details
#arguments ⇒ Object
:nodoc:
34 35 36 |
# File 'lib/libgems/commands/dependency_command.rb', line 34 def arguments # :nodoc: "GEMNAME name of gem to show dependencies for" end |
#defaults_str ⇒ Object
:nodoc:
38 39 40 |
# File 'lib/libgems/commands/dependency_command.rb', line 38 def defaults_str # :nodoc: "--local --version '#{LibGems::Requirement.default}' --no-reverse-dependencies" end |
#execute ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/libgems/commands/dependency_command.rb', line 46 def execute [:args] << '' if [:args].empty? specs = {} source_indexes = Hash.new do |h, source_uri| h[source_uri] = LibGems::SourceIndex.new end pattern = if [:args].length == 1 and [:args].first =~ /\A\/(.*)\/(i)?\z/m then flags = $2 ? Regexp::IGNORECASE : nil Regexp.new $1, flags else /\A#{Regexp.union(*[:args])}/ end dependency = LibGems::Dependency.new pattern, [:version] dependency.prerelease = [:prerelease] if [:reverse_dependencies] and remote? and not local? then alert_error 'Only reverse dependencies for local gems are supported.' terminate_interaction 1 end if local? then LibGems.source_index.search(dependency).each do |spec| source_indexes[:local].add_spec spec end end if remote? and not [:reverse_dependencies] then fetcher = LibGems::SpecFetcher.fetcher begin specs_and_sources = fetcher.find_matching(dependency, false, true, dependency.prerelease?) specs_and_sources.each do |spec_tuple, source_uri| spec = fetcher.fetch_spec spec_tuple, URI.parse(source_uri) source_indexes[source_uri].add_spec spec end rescue LibGems::RemoteFetcher::FetchError => e raise unless fetcher.warn_legacy e do require 'libgems/source_info_cache' specs = LibGems::SourceInfoCache.search_with_source dependency, false specs.each do |spec, source_uri| source_indexes[source_uri].add_spec spec end end end end if source_indexes.empty? then patterns = [:args].join ',' say "No gems found matching #{patterns} (#{[:version]})" if LibGems.configuration.verbose terminate_interaction 1 end specs = {} source_indexes.values.each do |source_index| source_index.gems.each do |name, spec| specs[spec.full_name] = [source_index, spec] end end reverse = Hash.new { |h, k| h[k] = [] } if [:reverse_dependencies] then specs.values.each do |_, spec| reverse[spec.full_name] = find_reverse_dependencies spec end end if [:pipe_format] then specs.values.sort_by { |_, spec| spec }.each do |_, spec| unless spec.dependencies.empty? spec.dependencies.sort_by { |dep| dep.name }.each do |dep| say "#{dep.name} --version '#{dep.requirement}'" end end end else response = '' specs.values.sort_by { |_, spec| spec }.each do |_, spec| 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 |
#find_gems(name, source_index) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/libgems/commands/dependency_command.rb', line 182 def find_gems(name, source_index) specs = {} spec_list = source_index.search name, [:version] spec_list.each do |spec| specs[spec.full_name] = [source_index, spec] end specs end |
#find_reverse_dependencies(spec) ⇒ Object
Returns an Array of [specification, dep] that are satisfied by spec
.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/libgems/commands/dependency_command.rb', line 165 def find_reverse_dependencies(spec) result = [] LibGems.source_index.each do |name, sp| sp.dependencies.each do |dep| dep = LibGems::Dependency.new(*dep) unless LibGems::Dependency === dep if spec.name == dep.name and dep.requirement.satisfied_by?(spec.version) then result << [sp.full_name, dep] end end end result end |
#print_dependencies(spec, level = 0) ⇒ Object
151 152 153 154 155 156 157 158 159 160 |
# File 'lib/libgems/commands/dependency_command.rb', line 151 def print_dependencies(spec, level = 0) response = '' response << ' ' * level + "Gem #{spec.full_name}\n" unless spec.dependencies.empty? then spec.dependencies.sort_by { |dep| dep.name }.each do |dep| response << ' ' * level + " #{dep}\n" end end response end |
#usage ⇒ Object
:nodoc:
42 43 44 |
# File 'lib/libgems/commands/dependency_command.rb', line 42 def usage # :nodoc: "#{program_name} GEMNAME" end |