2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/command'
|
|
|
|
require 'rubygems/installer'
|
|
|
|
require 'rubygems/version_option'
|
2011-03-01 04:41:32 -05:00
|
|
|
require 'rubygems/remote_fetcher'
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
class Gem::Commands::UnpackCommand < Gem::Command
|
|
|
|
|
|
|
|
include Gem::VersionOption
|
|
|
|
|
|
|
|
def initialize
|
2011-01-18 19:08:49 -05:00
|
|
|
require 'fileutils'
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
super 'unpack', 'Unpack an installed gem to the current directory',
|
2007-12-20 03:39:12 -05:00
|
|
|
:version => Gem::Requirement.default,
|
|
|
|
:target => Dir.pwd
|
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
add_option('--target=DIR',
|
|
|
|
'target directory for unpacking') do |value, options|
|
2007-12-20 03:39:12 -05:00
|
|
|
options[:target] = value
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
add_option('--spec', 'unpack the gem specification') do |value, options|
|
|
|
|
options[:spec] = true
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
add_version_option
|
|
|
|
end
|
|
|
|
|
|
|
|
def arguments # :nodoc:
|
|
|
|
"GEMNAME name of gem to unpack"
|
|
|
|
end
|
|
|
|
|
|
|
|
def defaults_str # :nodoc:
|
|
|
|
"--version '#{Gem::Requirement.default}'"
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
def description
|
|
|
|
<<-EOF
|
|
|
|
The unpack command allows you to examine the contents of a gem or modify
|
|
|
|
them to help diagnose a bug.
|
|
|
|
|
|
|
|
You can add the contents of the unpacked gem to the load path using the
|
|
|
|
RUBYLIB environment variable or -I:
|
|
|
|
|
|
|
|
$ gem unpack my_gem
|
|
|
|
Unpacked gem: '.../my_gem-1.0'
|
|
|
|
[edit my_gem-1.0/lib/my_gem.rb]
|
|
|
|
$ ruby -Imy_gem-1.0/lib -S other_program
|
|
|
|
|
|
|
|
You can repackage an unpacked gem using the build command. See the build
|
|
|
|
command help for an example.
|
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def usage # :nodoc:
|
|
|
|
"#{program_name} GEMNAME"
|
|
|
|
end
|
|
|
|
|
|
|
|
#--
|
|
|
|
# TODO: allow, e.g., 'gem unpack rake-0.3.1'. Find a general solution for
|
|
|
|
# this, so that it works for uninstall as well. (And check other commands
|
|
|
|
# at the same time.)
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
def execute
|
2009-06-09 17:38:59 -04:00
|
|
|
get_all_gem_names.each do |name|
|
2010-04-22 04:24:42 -04:00
|
|
|
dependency = Gem::Dependency.new name, options[:version]
|
|
|
|
path = get_path dependency
|
2009-06-09 17:38:59 -04:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
unless path then
|
|
|
|
alert_error "Gem '#{name}' not installed nor fetchable."
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
if @options[:spec] then
|
|
|
|
spec, metadata = get_metadata path
|
|
|
|
|
|
|
|
if metadata.nil? then
|
|
|
|
alert_error "--spec is unsupported on '#{name}' (old format gem)"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
spec_file = File.basename spec.spec_file
|
|
|
|
|
|
|
|
open spec_file, 'w' do |io|
|
|
|
|
io.write metadata
|
|
|
|
end
|
|
|
|
else
|
2010-04-22 04:24:42 -04:00
|
|
|
basename = File.basename path, '.gem'
|
|
|
|
target_dir = File.expand_path basename, options[:target]
|
2012-11-29 01:52:18 -05:00
|
|
|
|
|
|
|
package = Gem::Package.new path
|
|
|
|
package.extract_files target_dir
|
|
|
|
|
2009-06-09 17:38:59 -04:00
|
|
|
say "Unpacked gem: '#{target_dir}'"
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-01-31 22:11:34 -05:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Find cached filename in Gem.path. Returns nil if the file cannot be found.
|
|
|
|
#
|
|
|
|
#--
|
|
|
|
# TODO: see comments in get_path() about general service.
|
|
|
|
|
|
|
|
def find_in_cache(filename)
|
2011-03-01 04:41:32 -05:00
|
|
|
Gem.path.each do |path|
|
2011-05-31 23:45:05 -04:00
|
|
|
this_path = File.join(path, "cache", filename)
|
2011-01-31 22:11:34 -05:00
|
|
|
return this_path if File.exist? this_path
|
|
|
|
end
|
|
|
|
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
##
|
2007-11-10 02:48:56 -05:00
|
|
|
# Return the full path to the cached gem file matching the given
|
|
|
|
# name and version requirement. Returns 'nil' if no match.
|
|
|
|
#
|
|
|
|
# Example:
|
|
|
|
#
|
2010-04-22 04:24:42 -04:00
|
|
|
# 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)
|
2007-11-10 02:48:56 -05:00
|
|
|
#--
|
|
|
|
# TODO: This should be refactored so that it's a general service. I don't
|
|
|
|
# think any of our existing classes are the right place though. Just maybe
|
|
|
|
# 'Cache'?
|
|
|
|
#
|
|
|
|
# TODO: It just uses Gem.dir for now. What's an easy way to get the list of
|
|
|
|
# source directories?
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
def get_path dependency
|
|
|
|
return dependency.name if dependency.name =~ /\.gem$/i
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
specs = dependency.matching_specs
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2013-09-24 20:53:19 -04:00
|
|
|
selected = specs.max_by { |s| s.version }
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
return Gem::RemoteFetcher.fetcher.download_to_cache(dependency) unless
|
|
|
|
selected
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
return unless dependency.name =~ /^#{selected.name}$/i
|
2008-03-31 18:40:06 -04:00
|
|
|
|
2010-04-22 04:24:42 -04:00
|
|
|
# We expect to find (basename).gem in the 'cache' directory. Furthermore,
|
|
|
|
# the name match must be exact (ignoring case).
|
2011-03-01 04:41:32 -05:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
path = find_in_cache File.basename selected.cache_file
|
2011-03-01 04:41:32 -05:00
|
|
|
|
|
|
|
return Gem::RemoteFetcher.fetcher.download_to_cache(dependency) unless path
|
2010-04-22 04:24:42 -04:00
|
|
|
|
|
|
|
path
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
##
|
|
|
|
# Extracts the Gem::Specification and raw metadata from the .gem file at
|
|
|
|
# +path+.
|
2012-11-29 01:52:18 -05:00
|
|
|
#--
|
|
|
|
# TODO move to Gem::Package as #raw_spec or something
|
2011-05-31 23:45:05 -04:00
|
|
|
|
|
|
|
def get_metadata path
|
2012-11-29 01:52:18 -05:00
|
|
|
format = Gem::Package.new path
|
2011-05-31 23:45:05 -04:00
|
|
|
spec = format.spec
|
|
|
|
|
|
|
|
metadata = nil
|
|
|
|
|
|
|
|
open path, Gem.binary_mode do |io|
|
|
|
|
tar = Gem::Package::TarReader.new io
|
|
|
|
tar.each_entry do |entry|
|
|
|
|
case entry.full_name
|
|
|
|
when 'metadata' then
|
|
|
|
metadata = entry.read
|
|
|
|
when 'metadata.gz' then
|
|
|
|
metadata = Gem.gunzip entry.read
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return spec, metadata
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|