| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
module Bundler |
| 4 |
class CLI::Pristine |
| 5 |
def initialize(gems) |
| 6 |
@gems = gems |
| 7 |
end |
| 8 |
|
| 9 |
def run |
| 10 |
CLI::Common.ensure_all_gems_in_lockfile!(@gems) |
| 11 |
definition = Bundler.definition |
| 12 |
definition.validate_runtime! |
| 13 |
installer = Bundler::Installer.new(Bundler.root, definition) |
| 14 |
|
| 15 |
ProcessLock.lock do |
| 16 |
installed_specs = definition.specs.reject do |spec| |
| 17 |
next if spec.name == "bundler" # Source::Rubygems doesn't install bundler |
| 18 |
next if [email protected]? && [email protected]?(spec.name) |
| 19 |
|
| 20 |
gem_name = "#{spec.name} (#{spec.version}#{spec.git_version})" |
| 21 |
gem_name += " (#{spec.platform})" if !spec.platform.nil? && spec.platform != Gem::Platform::RUBY |
| 22 |
|
| 23 |
case source = spec.source |
| 24 |
when Source::Rubygems |
| 25 |
cached_gem = spec.cache_file |
| 26 |
unless File.exist?(cached_gem) |
| 27 |
Bundler.ui.error("Failed to pristine #{gem_name}. Cached gem #{cached_gem} does not exist.") |
| 28 |
next |
| 29 |
end |
| 30 |
|
| 31 |
FileUtils.rm_rf spec.full_gem_path |
| 32 |
when Source::Git |
| 33 |
if source.local? |
| 34 |
Bundler.ui.warn("Cannot pristine #{gem_name}. Gem is locally overridden.") |
| 35 |
next |
| 36 |
end |
| 37 |
|
| 38 |
source.remote! |
| 39 |
if extension_cache_path = source.extension_cache_path(spec) |
| 40 |
FileUtils.rm_rf extension_cache_path |
| 41 |
end |
| 42 |
FileUtils.rm_rf spec.extension_dir |
| 43 |
FileUtils.rm_rf spec.full_gem_path |
| 44 |
else |
| 45 |
Bundler.ui.warn("Cannot pristine #{gem_name}. Gem is sourced from local path.") |
| 46 |
next |
| 47 |
end |
| 48 |
|
| 49 |
true |
| 50 |
end.map(&:name) |
| 51 |
|
| 52 |
jobs = installer.send(:installation_parallelization, {}) |
| 53 |
pristine_count = definition.specs.count - installed_specs.count |
| 54 |
# allow a pristining a single gem to skip the parallel worker |
| 55 |
jobs = [jobs, pristine_count].min |
| 56 |
ParallelInstaller.call(installer, definition.specs, jobs, false, true, skip: installed_specs) |
| 57 |
end |
| 58 |
end |
| 59 |
end |
| 60 |
end |