Class: Gem::ContentsCommand

Inherits:
Command
  • Object
show all
Includes:
CommandAids, VersionOption
Defined in:
lib/rubygems/gem_commands.rb

Instance Attribute Summary

Attributes inherited from Command

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

Instance Method Summary collapse

Methods included from VersionOption

#add_version_option

Methods included from CommandAids

#begins?, #get_all_gem_names, #get_one_gem_name, #get_one_optional_argument

Methods inherited from Command

add_common_option, #add_option, add_specific_extra_args, #arguments, common_options, #defaults_str, extra_args, extra_args=, #handles?, #invoke, #merge_options, #remove_option, #show_help, specific_extra_args, specific_extra_args_hash, #usage, #when_invoked

Methods included from DefaultUserInteraction

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

Constructor Details

#initializeContentsCommand

Returns a new instance of ContentsCommand.



1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
# File 'lib/rubygems/gem_commands.rb', line 1536

def initialize
  super(
    'contents',
    'Display the contents of the installed gems',
    { :list => true, :specdirs => [] })

  add_version_option('contents')

  add_option("-l","--list",'List the files inside a Gem') do |v,o|
    o[:list] = true
  end
  
  add_option('-s','--spec-dir a,b,c', Array, "Search for gems under specific paths") do |v,o|
    o[:specdirs] = v
  end
  
  add_option('-V','--verbose','Be verbose when showing status') do |v,o|
    o[:verbose] = v
  end
end

Instance Method Details

#execute(io = STDOUT) ⇒ Object



1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
# File 'lib/rubygems/gem_commands.rb', line 1557

def execute(io=STDOUT)
  if options[:list]
    version = options[:version] || "> 0.0.0"
    gem = get_one_gem_name
    
    s = options[:specdirs].map do |i|
      [i, File.join(i,"specifications")]
    end.flatten
    
    if s.empty?
      s = Gem::SourceIndex.installed_spec_directories
      path_kind = "default gem paths"
      system = true
    else
      path_kind = "specified path"
      system = false
    end

    si = Gem::SourceIndex.from_gems_in(*s)

    gem_spec = si.search(gem, version).last
    unless gem_spec
      io.puts "Unable to find gem '#{gem}' in #{path_kind}"
      if options[:verbose]
        io.puts "\nDirectories searched:"
        s.each do |p|
          io.puts p
        end
      end
      return
    end
    # show the list of files.
    gem_spec.files.each do |f|
      io.puts File.join(gem_spec.full_gem_path, f)
    end
  end
end