Module: Bundler::Multilock

Defined in:
lib/bundler/multilock.rb,
lib/bundler/multilock/cache.rb,
lib/bundler/multilock/check.rb,
lib/bundler/multilock/ext/dsl.rb,
lib/bundler/multilock/version.rb,
lib/bundler/multilock/ext/plugin.rb,
lib/bundler/multilock/ext/source.rb,
lib/bundler/multilock/ui/capture.rb,
lib/bundler/multilock/ext/bundler.rb,
lib/bundler/multilock/ext/definition.rb,
lib/bundler/multilock/ext/plugin/dsl.rb,
lib/bundler/multilock/ext/source_list.rb,
lib/bundler/multilock/ext/shared_helpers.rb,
lib/bundler/multilock/lockfile_generator.rb

Defined Under Namespace

Modules: Ext, UI Classes: Cache, Check, LockfileGenerator

Constant Summary collapse

VERSION =
"1.3.1"

Class Method Summary collapse

Class Method Details

.add_lockfile(lockfile = nil, builder:, gemfile: nil, active: nil, default: nil, parent: nil, allow_mismatched_dependencies: nil, enforce_pinned_additional_dependencies: false) { ... } ⇒ true, false

Returns if the lockfile is the active lockfile.

Parameters:

  • lockfile (String) (defaults to: nil)

    The lockfile path (defaults to Gemfile.lock)

  • builder (Dsl)

    The Bundler DSL

  • gemfile (String, nil) (defaults to: nil)

    The Gemfile for this lockfile (defaults to Gemfile)

  • active (Boolean) (defaults to: nil)

    If this lockfile should be the default (instead of Gemfile.lock) BUNDLE_LOCKFILE will still override a lockfile tagged as active

  • parent (String) (defaults to: nil)

    The parent lockfile to sync dependencies from. Also used for comparing enforce_pinned_additional_dependencies against.

  • enforce_pinned_additional_dependencies (true, false) (defaults to: false)

    If dependencies are present in this lockfile that are not present in the default lockfile, enforce that they are pinned.

Yields:

  • Block executed only when this lockfile is active.

Returns:

  • (true, false)

    if the lockfile is the active lockfile

Raises:

  • (ArgumentError)


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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/bundler/multilock.rb', line 36

def add_lockfile(lockfile = nil,
                 builder:,
                 gemfile: nil,
                 active: nil,
                 default: nil,
                 parent: nil,
                 allow_mismatched_dependencies: nil,
                 enforce_pinned_additional_dependencies: false,
                 &block)
  # backcompat
  active = default if active.nil?
  Bundler.ui.warn("lockfile(default:) is deprecated. Use lockfile(active:) instead.") if default
  unless allow_mismatched_dependencies.nil?
    Bundler.ui.warn("lockfile(allow_mismatched_dependencies:) is deprecated.")
  end

  active = true if active.nil? && lockfile_definitions.empty? && lockfile.nil? && gemfile.nil?

  # if a gemfile was provided, but not a lockfile, infer the default lockfile for that gemfile
  lockfile ||= "#{gemfile}.lock" if gemfile
  # allow short-form lockfile names
  lockfile = expand_lockfile(lockfile)

  raise ArgumentError, "Lockfile #{lockfile} is already defined" if lockfile_definitions.key?(lockfile)

  env_lockfile = lockfile if active && ENV["BUNDLE_LOCKFILE"] == "active"
  env_lockfile ||= ENV["BUNDLE_LOCKFILE"]&.then { |l| expand_lockfile(l) }
  active = env_lockfile == lockfile if env_lockfile

  if active && (old_active = lockfile_definitions.each_value.find { |definition| definition[:active] })
    raise ArgumentError, "Only one lockfile (#{old_active[:lockfile]}) can be flagged as active"
  end

  parent = expand_lockfile(parent)
  if parent != Bundler.default_lockfile(force_original: true) &&
     !lockfile_definitions.key?(parent) &&
     !parent.exist?
    raise ArgumentError, "Parent lockfile #{parent} is not defined"
  end

  lockfile_definitions[lockfile] = (lockfile_def = {
    gemfile: (gemfile && Bundler.root.join(gemfile).expand_path) || Bundler.default_gemfile,
    lockfile: lockfile,
    active: active,
    prepare: block,
    parent: parent,
    enforce_pinned_additional_dependencies: enforce_pinned_additional_dependencies
  })

  if (defined?(CLI::Check) ||
      defined?(CLI::Install) ||
      defined?(CLI::Lock) ||
      defined?(CLI::Update)) &&
     !defined?(CLI::Cache) && !env_lockfile
    # always use Gemfile.lock for `bundle check`, `bundle install`,
    # `bundle lock`, and `bundle update`. `bundle cache` delegates to
    # `bundle install`, but we want that to run as normal.
    # If they're using BUNDLE_LOCKFILE, then they really do want to
    # use a particular lockfile, and it overrides whatever they
    # dynamically set in their gemfile
    active = lockfile == Bundler.default_lockfile(force_original: true)
  end

  if active
    block&.call
    Bundler.default_lockfile = lockfile

    # we started evaluating the project's primary gemfile, but got told to use a lockfile
    # associated with a different Gemfile. so we need to evaluate that Gemfile instead
    if lockfile_def[:gemfile] != Bundler.default_gemfile
      # share a cache between all lockfiles
      Bundler.cache_root = Bundler.root
      ENV["BUNDLE_GEMFILE"] = lockfile_def[:gemfile].to_s
      Bundler.root = Bundler.default_gemfile.dirname
      Bundler.default_lockfile = lockfile

      builder.eval_gemfile(Bundler.default_gemfile)

      return false
    end
  end
  true
end