| 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=DIR', '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 39
39: def execute
40: get_all_gem_names.each do |name|
41: path = get_path name, options[:version]
42:
43: if path then
44: basename = File.basename(path).sub(/\.gem$/, '')
45: target_dir = File.expand_path File.join(options[:target], basename)
46: FileUtils.mkdir_p target_dir
47: Gem::Installer.new(path, :unpack => true).unpack target_dir
48: say "Unpacked gem: '#{target_dir}'"
49: else
50: alert_error "Gem '#{name}' not installed."
51: end
52: end
53: 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 70
70: def get_path(gemname, version_req)
71: return gemname if gemname =~ /\.gem$/i
72:
73: specs = Gem::source_index.find_name gemname, version_req
74:
75: selected = specs.sort_by { |s| s.version }.last
76:
77: return nil if selected.nil?
78:
79: # We expect to find (basename).gem in the 'cache' directory.
80: # Furthermore, the name match must be exact (ignoring case).
81: if gemname =~ /^#{selected.name}$/i
82: filename = selected.full_name + '.gem'
83: path = nil
84:
85: Gem.path.find do |gem_dir|
86: path = File.join gem_dir, 'cache', filename
87: File.exist? path
88: end
89:
90: path
91: else
92: nil
93: end
94: end