[rubygems/rubygems] Use a constant empty tar header to avoid extra allocations
[ruby.git] / lib / bundler / cli / update.rb
blob985e8db05191b5c46d4d72f0dc9098f7338dbcb8
1 # frozen_string_literal: true
3 module Bundler
4   class CLI::Update
5     attr_reader :options, :gems
6     def initialize(options, gems)
7       @options = options
8       @gems = gems
9     end
11     def run
12       Bundler.ui.level = "warn" if options[:quiet]
14       update_bundler = options[:bundler]
16       Bundler.self_manager.update_bundler_and_restart_with_it_if_needed(update_bundler) if update_bundler
18       Plugin.gemfile_install(Bundler.default_gemfile) if Bundler.feature_flag.plugins?
20       sources = Array(options[:source])
21       groups  = Array(options[:group]).map(&:to_sym)
23       full_update = gems.empty? && sources.empty? && groups.empty? && !options[:ruby] && !update_bundler
25       if full_update && !options[:all]
26         if Bundler.feature_flag.update_requires_all_flag?
27           raise InvalidOption, "To update everything, pass the `--all` flag."
28         end
29         SharedHelpers.major_deprecation 3, "Pass --all to `bundle update` to update everything"
30       elsif !full_update && options[:all]
31         raise InvalidOption, "Cannot specify --all along with specific options."
32       end
34       conservative = options[:conservative]
36       if full_update
37         if conservative
38           Bundler.definition(conservative: conservative)
39         else
40           Bundler.definition(true)
41         end
42       else
43         unless Bundler.default_lockfile.exist?
44           raise GemfileLockNotFound, "This Bundle hasn't been installed yet. " \
45             "Run `bundle install` to update and install the bundled gems."
46         end
47         Bundler::CLI::Common.ensure_all_gems_in_lockfile!(gems)
49         if groups.any?
50           deps = Bundler.definition.dependencies.select {|d| (d.groups & groups).any? }
51           gems.concat(deps.map(&:name))
52         end
54         Bundler.definition(gems: gems, sources: sources, ruby: options[:ruby],
55                            conservative: conservative,
56                            bundler: update_bundler)
57       end
59       Bundler::CLI::Common.configure_gem_version_promoter(Bundler.definition, options)
61       Bundler::Fetcher.disable_endpoint = options["full-index"]
63       opts = options.dup
64       opts["update"] = true
65       opts["local"] = options[:local]
66       opts["force"] = options[:redownload]
68       Bundler.settings.set_command_option_if_given :jobs, opts["jobs"]
70       Bundler.definition.validate_runtime!
72       if locked_gems = Bundler.definition.locked_gems
73         previous_locked_info = locked_gems.specs.reduce({}) do |h, s|
74           h[s.name] = { spec: s, version: s.version, source: s.source.identifier }
75           h
76         end
77       end
79       installer = Installer.install Bundler.root, Bundler.definition, opts
80       Bundler.load.cache if Bundler.app_cache.exist?
82       if CLI::Common.clean_after_install?
83         require_relative "clean"
84         Bundler::CLI::Clean.new(options).run
85       end
87       if locked_gems
88         gems.each do |name|
89           locked_info = previous_locked_info[name]
90           next unless locked_info
92           locked_spec = locked_info[:spec]
93           new_spec = Bundler.definition.specs[name].first
94           unless new_spec
95             unless locked_spec.match_platform(Bundler.local_platform)
96               Bundler.ui.warn "Bundler attempted to update #{name} but it was not considered because it is for a different platform from the current one"
97             end
99             next
100           end
102           locked_source = locked_info[:source]
103           new_source = new_spec.source.identifier
104           next if locked_source != new_source
106           new_version = new_spec.version
107           locked_version = locked_info[:version]
108           if new_version < locked_version
109             Bundler.ui.warn "Note: #{name} version regressed from #{locked_version} to #{new_version}"
110           elsif new_version == locked_version
111             Bundler.ui.warn "Bundler attempted to update #{name} but its version stayed the same"
112           end
113         end
114       end
116       Bundler.ui.confirm "Bundle updated!"
117       Bundler::CLI::Common.output_without_groups_message(:update)
118       Bundler::CLI::Common.output_post_install_messages installer.post_install_messages
120       Bundler::CLI::Common.output_fund_metadata_summary
121     end
122   end