2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/command'
|
2012-11-29 01:52:18 -05:00
|
|
|
require 'rubygems/package'
|
2007-11-10 02:48:56 -05:00
|
|
|
require 'rubygems/installer'
|
|
|
|
require 'rubygems/version_option'
|
|
|
|
|
|
|
|
class Gem::Commands::PristineCommand < Gem::Command
|
|
|
|
|
|
|
|
include Gem::VersionOption
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super 'pristine',
|
|
|
|
'Restores installed gems to pristine condition from files located in the gem cache',
|
2013-07-08 18:41:03 -04:00
|
|
|
:version => Gem::Requirement.default,
|
|
|
|
:extensions => true,
|
2011-05-31 23:45:05 -04:00
|
|
|
:all => false
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
add_option('--all',
|
|
|
|
'Restore all installed gems to pristine',
|
|
|
|
'condition') do |value, options|
|
|
|
|
options[:all] = value
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
add_option('--[no-]extensions',
|
2013-08-26 16:24:51 -04:00
|
|
|
'Restore gems with extensions',
|
|
|
|
'in addition to regular gems') do |value, options|
|
2011-05-31 23:45:05 -04:00
|
|
|
options[:extensions] = value
|
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
add_option('--only-executables',
|
|
|
|
'Only restore executables') do |value, options|
|
|
|
|
options[:only_executables] = value
|
|
|
|
end
|
|
|
|
|
2007-11-10 02:48:56 -05:00
|
|
|
add_version_option('restore to', 'pristine condition')
|
|
|
|
end
|
|
|
|
|
|
|
|
def arguments # :nodoc:
|
|
|
|
"GEMNAME gem to restore to pristine condition (unless --all)"
|
|
|
|
end
|
|
|
|
|
|
|
|
def defaults_str # :nodoc:
|
2013-07-08 18:41:03 -04:00
|
|
|
'--extensions'
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def description # :nodoc:
|
|
|
|
<<-EOF
|
2013-08-26 16:24:51 -04:00
|
|
|
The pristine command compares an installed gem with the contents of its
|
|
|
|
cached .gem file and restores any files that don't match the cached .gem's
|
|
|
|
copy.
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
If you have made modifications to an installed gem, the pristine command
|
|
|
|
will revert them. All extensions are rebuilt and all bin stubs for the gem
|
|
|
|
are regenerated after checking for modifications.
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
If the cached gem cannot be found it will be downloaded.
|
2011-05-31 23:45:05 -04:00
|
|
|
|
2013-08-26 16:24:51 -04:00
|
|
|
If --no-extensions is provided pristine will not attempt to restore a gem
|
|
|
|
with an extension.
|
2007-11-10 02:48:56 -05:00
|
|
|
EOF
|
|
|
|
end
|
|
|
|
|
|
|
|
def usage # :nodoc:
|
2013-08-26 16:24:51 -04:00
|
|
|
"#{program_name} [GEMNAME ...]"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
specs = if options[:all] then
|
2011-05-31 23:45:05 -04:00
|
|
|
Gem::Specification.map
|
2007-11-10 02:48:56 -05:00
|
|
|
else
|
2011-05-31 23:45:05 -04:00
|
|
|
get_all_gem_names.map do |gem_name|
|
|
|
|
Gem::Specification.find_all_by_name gem_name, options[:version]
|
|
|
|
end.flatten
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
if specs.to_a.empty? then
|
2007-11-10 02:48:56 -05:00
|
|
|
raise Gem::Exception,
|
2011-05-31 23:45:05 -04:00
|
|
|
"Failed to find gems #{options[:args]} #{options[:version]}"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
install_dir = Gem.dir # TODO use installer option
|
|
|
|
|
|
|
|
raise Gem::FilePermissionError.new(install_dir) unless
|
|
|
|
File.writable?(install_dir)
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
say "Restoring gems to pristine condition..."
|
2007-11-10 02:48:56 -05:00
|
|
|
|
|
|
|
specs.each do |spec|
|
2012-11-29 01:52:18 -05:00
|
|
|
if spec.default_gem?
|
|
|
|
say "Skipped #{spec.full_name}, it is a default gem"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
unless spec.extensions.empty? or options[:extensions] then
|
|
|
|
say "Skipped #{spec.full_name}, it needs to compile an extension"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
gem = spec.cache_file
|
|
|
|
|
|
|
|
unless File.exist? gem then
|
|
|
|
require 'rubygems/remote_fetcher'
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
say "Cached gem for #{spec.full_name} not found, attempting to fetch..."
|
|
|
|
dep = Gem::Dependency.new spec.name, spec.version
|
|
|
|
Gem::RemoteFetcher.fetcher.download_to_cache dep
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2013-09-13 15:58:57 -04:00
|
|
|
# TODO use installer options
|
|
|
|
install_defaults = Gem::ConfigFile::PLATFORM_DEFAULTS['install']
|
|
|
|
installer_env_shebang = install_defaults.to_s['--env-shebang']
|
2012-08-21 00:50:18 -04:00
|
|
|
|
2011-05-31 23:45:05 -04:00
|
|
|
installer = Gem::Installer.new(gem,
|
|
|
|
:wrappers => true,
|
|
|
|
:force => true,
|
2012-04-17 20:04:12 -04:00
|
|
|
:install_dir => spec.base_dir,
|
2013-09-13 15:58:57 -04:00
|
|
|
:env_shebang => installer_env_shebang,
|
2012-11-29 01:52:18 -05:00
|
|
|
:build_args => spec.build_args)
|
|
|
|
if options[:only_executables] then
|
|
|
|
installer.generate_bin
|
|
|
|
else
|
|
|
|
installer.install
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
|
2008-06-17 18:04:18 -04:00
|
|
|
say "Restored #{spec.full_name}"
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|