class Bundler::Injector

def inject(gemfile_path, lockfile_path)

Returns:
  • (Array) -

Parameters:
  • lockfile_path (Pathname) -- The lockfile in which to inject the new dependency.
  • gemfile_path (Pathname) -- The Gemfile in which to inject the new dependency.
def inject(gemfile_path, lockfile_path)
  Bundler.definition.ensure_equivalent_gemfile_and_lockfile(true)
  # temporarily unfreeze
  Bundler.settings.temporary(deployment: false, frozen: false) do
    # evaluate the Gemfile we have now
    builder = Dsl.new
    builder.eval_gemfile(gemfile_path)
    # don't inject any gems that are already in the Gemfile
    @deps -= builder.dependencies
    # add new deps to the end of the in-memory Gemfile
    # Set conservative versioning to false because
    # we want to let the resolver resolve the version first
    builder.eval_gemfile(INJECTED_GEMS, build_gem_lines(false)) if @deps.any?
    # resolve to see if the new deps broke anything
    @definition = builder.to_definition(lockfile_path, {})
    @definition.resolve_remotely!
    # since nothing broke, we can add those gems to the gemfile
    append_to(gemfile_path, build_gem_lines(@options[:conservative_versioning])) if @deps.any?
    # since we resolved successfully, write out the lockfile
    @definition.lock
    # invalidate the cached Bundler.definition
    Bundler.reset_paths!
    # return an array of the deps that we added
    @deps
  end
end