File: bundle

package info (click to toggle)
ruby-pry-byebug 3.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye
  • size: 324 kB
  • sloc: ruby: 1,171; makefile: 4
file content (62 lines) | stat: -rwxr-xr-x 1,449 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env ruby
# frozen_string_literal: true

require "rubygems"

m = Module.new do
  extend self

  def invoked_as_script?
    File.expand_path($PROGRAM_NAME) == File.expand_path(__FILE__)
  end

  def cli_arg_version
    return unless invoked_as_script? # don't want to hijack other binstubs
    return unless "update".start_with?(ARGV.first || " ") # must be running `bundle update`

    bundler_version = nil
    update_index = nil
    ARGV.each_with_index do |a, i|
      bundler_version = a if update_index && update_index.succ == i && a =~ Gem::Version::ANCHORED_VERSION_PATTERN
      next unless a =~ /\A--bundler(?:[= ](#{Gem::Version::VERSION_PATTERN}))?\z/

      bundler_version = Regexp.last_match(1) || ">= 0.a"
      update_index = i
    end
    bundler_version
  end

  def gemfile
    File.expand_path("../Gemfile", __dir__)
  end

  def lockfile
    "#{gemfile}.lock"
  end

  def lockfile_version
    lockfile_contents = File.read(lockfile)

    regexp = /\n\nBUNDLED WITH\n\s{2,}(#{Gem::Version::VERSION_PATTERN})\n/

    regexp.match(lockfile_contents)[1]
  end

  def bundler_version
    @bundler_version ||= cli_arg_version || lockfile_version
  end

  def load_bundler!
    ENV["BUNDLE_GEMFILE"] ||= gemfile

    activate_bundler(bundler_version)
  end

  def activate_bundler(bundler_version)
    gem "bundler", bundler_version
  end
end

m.load_bundler!

load Gem.bin_path("bundler", "bundle") if m.invoked_as_script?