Class Gem::Commands::UnpackCommand
In: lib/rubygems/commands/unpack_command.rb
Parent: Gem::Command

Methods

download   execute   find_in_cache   get_path   new  

Included Modules

Gem::VersionOption

Public Class methods

[Source]

    # File lib/rubygems/commands/unpack_command.rb, line 9
 9:   def initialize
10:     require 'fileutils'
11: 
12:     super 'unpack', 'Unpack an installed gem to the current directory',
13:           :version => Gem::Requirement.default,
14:           :target  => Dir.pwd
15: 
16:     add_option('--target=DIR',
17:                'target directory for unpacking') do |value, options|
18:       options[:target] = value
19:     end
20: 
21:     add_version_option
22:   end

Public Instance methods

[Source]

    # File lib/rubygems/commands/unpack_command.rb, line 36
36:   def download dependency
37:     found = Gem::SpecFetcher.fetcher.fetch dependency
38: 
39:     return if found.empty?
40: 
41:     spec, source_uri = found.first
42: 
43:     Gem::RemoteFetcher.fetcher.download spec, source_uri
44:   end

[Source]

    # File lib/rubygems/commands/unpack_command.rb, line 51
51:   def execute
52:     get_all_gem_names.each do |name|
53:       dependency = Gem::Dependency.new name, options[:version]
54:       path = get_path dependency
55: 
56:       if path then
57:         basename = File.basename path, '.gem'
58:         target_dir = File.expand_path basename, options[:target]
59:         FileUtils.mkdir_p target_dir
60:         Gem::Installer.new(path, :unpack => true).unpack target_dir
61:         say "Unpacked gem: '#{target_dir}'"
62:       else
63:         alert_error "Gem '#{name}' not installed."
64:       end
65:     end
66:   end

Find cached filename in Gem.path. Returns nil if the file cannot be found.

[Source]

    # File lib/rubygems/commands/unpack_command.rb, line 75
75:   def find_in_cache(filename)
76:     Gem.path.each do |gem_dir|
77:       this_path = File.join gem_dir, 'cache', filename
78:       return this_path if File.exist? this_path
79:     end
80: 
81:     return nil
82:   end

Return the full path to the cached gem file matching the given name and version requirement. Returns ‘nil’ if no match.

Example:

  get_path 'rake', '> 0.4' # "/usr/lib/ruby/gems/1.8/cache/rake-0.4.2.gem"
  get_path 'rake', '< 0.1' # nil
  get_path 'rak'           # nil (exact name required)

[Source]

     # File lib/rubygems/commands/unpack_command.rb, line 101
101:   def get_path dependency
102:     return dependency.name if dependency.name =~ /\.gem$/i
103: 
104:     specs = Gem.source_index.search dependency
105: 
106:     selected = specs.sort_by { |s| s.version }.last
107: 
108:     return download(dependency) if selected.nil?
109: 
110:     return unless dependency.name =~ /^#{selected.name}$/i
111: 
112:     # We expect to find (basename).gem in the 'cache' directory.  Furthermore,
113:     # the name match must be exact (ignoring case).
114:     
115:     path = find_in_cache(selected.file_name)
116:     return download(dependency) unless path
117: 
118:     path
119:   end

[Validate]