Class: Bundler::Injector

Inherits:
Object
  • Object
show all
Defined in:
lib/bundler/injector.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(new_deps, options = {}) ⇒ Injector

Returns a new instance of Injector



10
11
12
13
# File 'lib/bundler/injector.rb', line 10

def initialize(new_deps, options = {})
  @new_deps = new_deps
  @options = options
end

Class Method Details

.inject(new_deps, options = {}) ⇒ Object



5
6
7
8
# File 'lib/bundler/injector.rb', line 5

def self.inject(new_deps, options = {})
  injector = new(new_deps, options)
  injector.inject(Bundler.default_gemfile, Bundler.default_lockfile)
end

Instance Method Details

#inject(gemfile_path, lockfile_path) ⇒ Object



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
# File 'lib/bundler/injector.rb', line 15

def inject(gemfile_path, lockfile_path)
  if Bundler.frozen?
    # ensure the lock and Gemfile are synced
    Bundler.definition.ensure_equivalent_gemfile_and_lockfile(true)
  end

  # 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
    @new_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 @new_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 @new_deps.any?

    # since we resolved successfully, write out the lockfile
    @definition.lock(Bundler.default_lockfile)

    # invalidate the cached Bundler.definition
    Bundler.reset_paths!

    # return an array of the deps that we added
    @new_deps
  end
end