2016-02-01 07:43:26 -05:00
|
|
|
# frozen_string_literal: true
|
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,
|
2013-09-18 17:29:41 -04:00
|
|
|
:extensions_set => false,
|
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
|
|
|
|
|
2015-02-27 08:00:45 -05:00
|
|
|
add_option('--skip=gem_name',
|
|
|
|
'used on --all, skip if name == gem_name') do |value, options|
|
2018-05-30 09:01:35 -04:00
|
|
|
options[:skip] ||= []
|
|
|
|
options[:skip] << value
|
2015-02-27 08:00:45 -05:00
|
|
|
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|
|
2013-09-18 17:29:41 -04:00
|
|
|
options[:extensions_set] = true
|
|
|
|
options[:extensions] = value
|
2011-05-31 23:45:05 -04:00
|
|
|
end
|
|
|
|
|
2012-11-29 01:52:18 -05:00
|
|
|
add_option('--only-executables',
|
|
|
|
'Only restore executables') do |value, options|
|
|
|
|
options[:only_executables] = value
|
|
|
|
end
|
|
|
|
|
2020-01-31 21:14:04 -05:00
|
|
|
add_option('--only-plugins',
|
|
|
|
'Only restore plugins') do |value, options|
|
|
|
|
options[:only_plugins] = value
|
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
add_option('-E', '--[no-]env-shebang',
|
|
|
|
'Rewrite executables with a shebang',
|
|
|
|
'of /usr/bin/env') do |value, options|
|
|
|
|
options[:env_shebang] = value
|
|
|
|
end
|
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
add_option('-n', '--bindir DIR',
|
|
|
|
'Directory where executables are',
|
|
|
|
'located') do |value, options|
|
|
|
|
options[:bin_dir] = File.expand_path(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.
|
2013-09-18 17:29:41 -04:00
|
|
|
|
|
|
|
If --extensions is given (but not --all or gem names) only gems with
|
|
|
|
extensions will be restored.
|
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
|
2018-11-21 05:20:47 -05:00
|
|
|
specs = if options[:all]
|
2011-05-31 23:45:05 -04:00
|
|
|
Gem::Specification.map
|
2013-09-18 17:29:41 -04:00
|
|
|
|
|
|
|
# `--extensions` must be explicitly given to pristine only gems
|
|
|
|
# with extensions.
|
|
|
|
elsif options[:extensions_set] and
|
2018-11-21 05:20:47 -05:00
|
|
|
options[:extensions] and options[:args].empty?
|
2013-09-18 17:29:41 -04:00
|
|
|
Gem::Specification.select do |spec|
|
|
|
|
spec.extensions and not spec.extensions.empty?
|
|
|
|
end
|
2007-11-10 02:48:56 -05:00
|
|
|
else
|
2016-04-06 02:01:14 -04:00
|
|
|
get_all_gem_names.sort.map do |gem_name|
|
|
|
|
Gem::Specification.find_all_by_name(gem_name, options[:version]).reverse
|
2011-05-31 23:45:05 -04:00
|
|
|
end.flatten
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2019-06-01 05:45:11 -04:00
|
|
|
specs = specs.select{|spec| RUBY_ENGINE == spec.platform || Gem::Platform.local === spec.platform || spec.platform == Gem::Platform::RUBY }
|
2019-03-07 06:24:48 -05:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if specs.to_a.empty?
|
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
|
|
|
|
|
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
|
|
|
|
|
2018-05-30 09:01:35 -04:00
|
|
|
if options.has_key? :skip
|
|
|
|
if options[:skip].include? spec.name
|
|
|
|
say "Skipped #{spec.full_name}, it was given through options"
|
|
|
|
next
|
|
|
|
end
|
2015-02-27 08:00:45 -05:00
|
|
|
end
|
|
|
|
|
2020-01-31 21:14:04 -05:00
|
|
|
unless spec.extensions.empty? or options[:extensions] or options[:only_executables] or options[:only_plugins]
|
2011-05-31 23:45:05 -04:00
|
|
|
say "Skipped #{spec.full_name}, it needs to compile an extension"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
gem = spec.cache_file
|
|
|
|
|
2020-01-31 21:14:04 -05:00
|
|
|
unless File.exist? gem or options[:only_executables] or options[:only_plugins]
|
2011-05-31 23:45:05 -04:00
|
|
|
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..."
|
2014-12-06 19:53:01 -05:00
|
|
|
|
2011-03-01 04:41:32 -05:00
|
|
|
dep = Gem::Dependency.new spec.name, spec.version
|
2014-12-06 19:53:01 -05:00
|
|
|
found, _ = Gem::SpecFetcher.fetcher.spec_for_dependency dep
|
|
|
|
|
|
|
|
if found.empty?
|
|
|
|
say "Skipped #{spec.full_name}, it was not found from cache and remote sources"
|
|
|
|
next
|
|
|
|
end
|
|
|
|
|
|
|
|
spec_candidate, source = found.first
|
|
|
|
Gem::RemoteFetcher.fetcher.download spec_candidate, source.uri.to_s, spec.base_dir
|
2007-11-10 02:48:56 -05:00
|
|
|
end
|
|
|
|
|
2013-09-14 04:59:02 -04:00
|
|
|
env_shebang =
|
2018-11-21 05:20:47 -05:00
|
|
|
if options.include? :env_shebang
|
2013-09-14 04:59:02 -04:00
|
|
|
options[:env_shebang]
|
|
|
|
else
|
|
|
|
install_defaults = Gem::ConfigFile::PLATFORM_DEFAULTS['install']
|
|
|
|
install_defaults.to_s['--env-shebang']
|
|
|
|
end
|
2012-08-21 00:50:18 -04:00
|
|
|
|
2018-08-27 06:05:04 -04:00
|
|
|
bin_dir = options[:bin_dir] if options[:bin_dir]
|
|
|
|
|
2017-10-07 21:32:22 -04:00
|
|
|
installer_options = {
|
2017-10-07 21:32:18 -04:00
|
|
|
:wrappers => true,
|
|
|
|
:force => true,
|
|
|
|
:install_dir => spec.base_dir,
|
|
|
|
:env_shebang => env_shebang,
|
|
|
|
:build_args => spec.build_args,
|
2018-08-27 06:05:04 -04:00
|
|
|
:bin_dir => bin_dir
|
2017-10-07 21:32:18 -04:00
|
|
|
}
|
2017-10-07 21:32:22 -04:00
|
|
|
|
2018-11-21 05:20:47 -05:00
|
|
|
if options[:only_executables]
|
2017-10-07 21:32:18 -04:00
|
|
|
installer = Gem::Installer.for_spec(spec, installer_options)
|
2012-11-29 01:52:18 -05:00
|
|
|
installer.generate_bin
|
2020-01-31 21:14:04 -05:00
|
|
|
elsif options[:only_plugins]
|
|
|
|
installer = Gem::Installer.for_spec(spec)
|
|
|
|
installer.generate_plugins
|
2012-11-29 01:52:18 -05:00
|
|
|
else
|
2017-10-07 21:32:18 -04:00
|
|
|
installer = Gem::Installer.at(gem, installer_options)
|
2012-11-29 01:52:18 -05:00
|
|
|
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
|