PHP 8.2.31
Preview: self_manager.rb Size: 5.92 KB
//proc/thread-self/root/opt/alt/ruby40/share/gems/gems/bundler-4.0.10/lib/bundler/self_manager.rb

# frozen_string_literal: true

module Bundler
  #
  # This class handles installing and switching to the version of bundler needed
  # by an application.
  #
  class SelfManager
    def restart_with_locked_bundler_if_needed
      restart_version = find_restart_version
      return unless restart_version && installed?(restart_version)

      restart_with(restart_version)
    end

    def install_locked_bundler_and_restart_with_it_if_needed
      restart_version = find_restart_version
      return unless restart_version

      if restart_version == lockfile_version
        Bundler.ui.info \
          "Bundler #{current_version} is running, but your lockfile was generated with #{lockfile_version}. " \
          "Installing Bundler #{lockfile_version} and restarting using that version."
      else
        Bundler.ui.info \
          "Bundler #{current_version} is running, but your configuration was #{restart_version}. " \
          "Installing Bundler #{restart_version} and restarting using that version."
      end

      install_and_restart_with(restart_version)
    end

    def update_bundler_and_restart_with_it_if_needed(target)
      spec = resolve_update_version_from(target)
      return unless spec

      version = spec.version

      Bundler.ui.info "Updating bundler to #{version}."

      install(spec) unless installed?(version)

      restart_with(version)
    end

    private

    def install_and_restart_with(version)
      requirement = Gem::Requirement.new(version)
      spec = find_latest_matching_spec(requirement)

      if spec.nil?
        Bundler.ui.warn "Your lockfile is locked to a version of bundler (#{lockfile_version}) that doesn't exist at https://rubygems.org/. Going on using #{current_version}"
        return
      end

      install(spec)
    rescue StandardError => e
      Bundler.ui.trace e
      Bundler.ui.warn "There was an error installing the locked bundler version (#{lockfile_version}), rerun with the `--verbose` flag for more details. Going on using bundler #{current_version}."
    else
      restart_with(version)
    end

    def install(spec)
      spec.source.download(spec)
      spec.source.install(spec)
    end

    def restart_with(version)
      configured_gem_home = ENV["GEM_HOME"]
      configured_orig_gem_home = ENV["BUNDLER_ORIG_GEM_HOME"]
      configured_gem_path = ENV["GEM_PATH"]
      configured_orig_gem_path = ENV["BUNDLER_ORIG_GEM_PATH"]

      argv0 = File.exist?($PROGRAM_NAME) ? $PROGRAM_NAME : Process.argv0
      cmd = [argv0, *ARGV]
      cmd.unshift(Gem.ruby) unless File.executable?(argv0)

      Bundler.with_original_env do
        Kernel.exec(
          {
            "GEM_HOME" => configured_gem_home,
            "BUNDLER_ORIG_GEM_HOME" => configured_orig_gem_home,
            "GEM_PATH" => configured_gem_path,
            "BUNDLER_ORIG_GEM_PATH" => configured_orig_gem_path,
            "BUNDLER_VERSION" => version.to_s,
          },
          *cmd
        )
      end
    end

    def needs_switching?(restart_version)
      autoswitching_applies? &&
        released?(restart_version) &&
        !running?(restart_version)
    end

    def autoswitching_applies?
      (ENV["BUNDLER_VERSION"].nil? || ENV["BUNDLER_VERSION"].empty?) &&
        ruby_can_restart_with_same_arguments? &&
        lockfile_version
    end

    def resolve_update_version_from(target)
      requirement = Gem::Requirement.new(target)
      update_candidate = find_latest_matching_spec(requirement)

      if update_candidate.nil?
        raise InvalidOption, "The `bundle update --bundler` target version (#{target}) does not exist"
      end

      resolved_version = update_candidate.version
      needs_update = requirement.specific? ? !running?(resolved_version) : running_older_than?(resolved_version)

      return unless needs_update

      update_candidate
    end

    def local_specs
      @local_specs ||= Bundler::Source::Rubygems.new("allow_local" => true).specs.select {|spec| spec.name == "bundler" }
    end

    def remote_specs
      @remote_specs ||= begin
        source = Bundler::Source::Rubygems.new("remotes" => "https://rubygems.org")
        source.remote!
        source.add_dependency_names("bundler")
        source.specs.select(&:matches_current_metadata?)
      end
    end

    def find_latest_matching_spec(requirement)
      Bundler.configure
      local_result = find_latest_matching_spec_from_collection(local_specs, requirement)
      return local_result if local_result && requirement.specific?

      remote_result = find_latest_matching_spec_from_collection(remote_specs, requirement)
      return remote_result if local_result.nil?

      [local_result, remote_result].max
    end

    def find_latest_matching_spec_from_collection(specs, requirement)
      specs.sort.reverse_each.find {|spec| requirement.satisfied_by?(spec.version) }
    end

    def running?(version)
      version == current_version
    end

    def running_older_than?(version)
      current_version < version
    end

    def released?(version)
      !version.to_s.end_with?(".dev")
    end

    def ruby_can_restart_with_same_arguments?
      $PROGRAM_NAME != "-e"
    end

    def installed?(restart_version)
      Bundler.configure

      Bundler.rubygems.find_bundler(restart_version.to_s)
    end

    def current_version
      @current_version ||= Bundler.gem_version
    end

    def lockfile_version
      return @lockfile_version if defined?(@lockfile_version)

      parsed_version = Bundler::LockfileParser.bundled_with
      @lockfile_version = parsed_version ? Gem::Version.new(parsed_version) : nil
    rescue ArgumentError
      @lockfile_version = nil
    end

    def find_restart_version
      return unless SharedHelpers.in_bundle?

      configured_version = Bundler.settings[:version]
      return if configured_version == "system"

      restart_version = configured_version == "lockfile" ? lockfile_version : Gem::Version.new(configured_version)
      return unless needs_switching?(restart_version)

      restart_version
    end
  end
end

Directory Contents

Dirs: 12 × Files: 74

Name Size Perms Modified Actions
cli DIR
- drwxr-xr-x 2026-06-09 07:03:24
Edit Download
- drwxr-xr-x 2026-06-09 07:03:24
Edit Download
fetcher DIR
- drwxr-xr-x 2026-06-09 07:03:24
Edit Download
installer DIR
- drwxr-xr-x 2026-06-09 07:03:24
Edit Download
man DIR
- drwxr-xr-x 2026-06-09 07:03:24
Edit Download
plugin DIR
- drwxr-xr-x 2026-06-09 07:03:24
Edit Download
resolver DIR
- drwxr-xr-x 2026-06-09 07:03:24
Edit Download
settings DIR
- drwxr-xr-x 2026-06-09 07:03:24
Edit Download
source DIR
- drwxr-xr-x 2026-06-09 07:03:24
Edit Download
templates DIR
- drwxr-xr-x 2026-06-09 07:03:24
Edit Download
ui DIR
- drwxr-xr-x 2026-06-09 07:03:24
Edit Download
vendor DIR
- drwxr-xr-x 2026-06-09 07:03:24
Edit Download
1.24 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
189 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
7.26 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
3.72 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
40.13 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
2.97 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
311 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
3.07 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
42.51 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
3.14 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
272 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
876 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
2.16 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
22.11 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
4.49 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
4.92 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
1.44 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
8.35 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
522 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
12.00 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
544 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
3.80 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
6.88 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
138 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
5.06 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
4.79 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
9.97 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
3.66 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
9.24 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
8.88 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
2.42 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
9.30 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
769 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
1.48 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
863 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
1.40 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
5.77 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
12.17 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
554 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
3.92 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
19.17 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
2.44 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
14.11 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
4.61 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
12.26 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
2.60 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
4.57 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
10.66 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
597 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
5.92 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
14.86 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
1.36 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
12.06 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
3.06 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
6.09 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
2.13 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
9.55 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
3.48 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
255 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
1.29 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
715 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
101 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
735 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
197 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
99 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
387 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
180 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
209 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
93 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
496 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
500 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
133 B lrw-r--r-- 2026-05-21 12:15:22
Edit Download
3.10 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download
2.42 KB lrw-r--r-- 2026-05-21 12:15:22
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).