| Class | Gem::Commands::UnpackCommand |
| In: |
lib/rubygems/commands/unpack_command.rb
|
| Parent: | Gem::Command |
# File lib/rubygems/commands/unpack_command.rb, line 10
10: def initialize
11: super 'unpack', 'Unpack an installed gem to the current directory',
12: :version => Gem::Requirement.default,
13: :target => Dir.pwd
14:
15: add_option('--target', 'target directory for unpacking') do |value, options|
16: options[:target] = value
17: end
18:
19: add_version_option
20: end
# File lib/rubygems/commands/unpack_command.rb, line 38
38: def execute
39: gemname = get_one_gem_name
40: path = get_path(gemname, options[:version])
41:
42: if path then
43: basename = File.basename(path).sub(/\.gem$/, '')
44: target_dir = File.expand_path File.join(options[:target], basename)
45: FileUtils.mkdir_p target_dir
46: Gem::Installer.new(path).unpack target_dir
47: say "Unpacked gem: '#{target_dir}'"
48: else
49: alert_error "Gem '#{gemname}' not installed."
50: end
51: 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)
# File lib/rubygems/commands/unpack_command.rb, line 68
68: def get_path(gemname, version_req)
69: return gemname if gemname =~ /\.gem$/i
70:
71: specs = Gem::source_index.search(/\A#{gemname}\z/, version_req)
72:
73: selected = specs.sort_by { |s| s.version }.last
74:
75: return nil if selected.nil?
76:
77: # We expect to find (basename).gem in the 'cache' directory.
78: # Furthermore, the name match must be exact (ignoring case).
79: if gemname =~ /^#{selected.name}$/i
80: filename = selected.full_name + '.gem'
81: path = nil
82:
83: Gem.path.find do |gem_dir|
84: path = File.join gem_dir, 'cache', filename
85: File.exist? path
86: end
87:
88: path
89: else
90: nil
91: end
92: end