2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/command'
|
|
|
|
require 'rubygems/installer'
|
|
|
|
require 'rubygems/version_option'
|
2017-10-07 21:32:18 -04:00
|
|
|
require 'rubygems/security_option'
|
2011-03-01 04:41:32 -05:00
|
|
|
require 'rubygems/remote_fetcher'
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
# forward-declare
|
|
|
|
|
|
|
|
module Gem::Security # :nodoc:
|
|
|
|
class Policy # :nodoc:
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
class Gem::Commands::UnpackCommand < Gem::Command
|
|
|
|
|
|
|
|
include Gem::VersionOption
|
2017-10-07 21:32:18 -04:00
|
|
|
include Gem::SecurityOption
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
add_security_option
|
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
|
2017-10-07 21:32:18 -04:00
|
|
|
security_policy = options[:security_policy]
|
|
|
|
|
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
|
2017-10-07 21:32:18 -04:00
|
|
|
spec, metadata = get_metadata path, security_policy
|
2011-05-31 23:45:05 -04:00
|
|
|
|
|
|
|
if metadata.nil? then
|
|
|
|
alert_error "--spec is unsupported on '#{name}' (old format gem)"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
spec_file = File.basename spec.spec_file
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
FileUtils.mkdir_p @options[:target] if @options[:target]
|
|
|
|
|
|
|
|
destination = begin
|
|
|
|
if @options[:target]
|
|
|
|
File.join @options[:target], spec_file
|
|
|
|
else
|
|
|
|
spec_file
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
File.open destination, 'w' do |io|
|
2011-05-31 23:45:05 -04:00
|
|
|
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
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
package = Gem::Package.new path, security_policy
|
2012-11-29 01:52:18 -05:00
|
|
|
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
|
|
|
|
2017-10-07 21:32:18 -04:00
|
|
|
def get_metadata path, security_policy = nil
|
|
|
|
format = Gem::Package.new path, security_policy
|
2011-05-31 23:45:05 -04:00
|
|
|
spec = format.spec
|
|
|
|
|
|
|
|
metadata = nil
|
|
|
|
|
2018-02-05 21:58:35 -05:00
|
|
|
File.open path, Gem.binary_mode do |io|
|
2011-05-31 23:45:05 -04:00
|
|
|
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
|
2018-05-17 21:39:13 -04:00
|
|
|
metadata = Gem::Util.gunzip entry.read
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return spec, metadata
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|