Class: Bundler::Injector

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

Constant Summary collapse

INJECTED_GEMS =
"injected gems"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Injector.



17
18
19
20
# File 'lib/bundler/injector.rb', line 17

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

Class Method Details

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



7
8
9
10
# File 'lib/bundler/injector.rb', line 7

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

.remove(gems, options = {}) ⇒ Object



12
13
14
15
# File 'lib/bundler/injector.rb', line 12

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

Instance Method Details

#inject(gemfile_path, lockfile_path) ⇒ Array

Parameters:

  • gemfile_path (Pathname)

    The Gemfile in which to inject the new dependency.

  • lockfile_path (Pathname)

    The lockfile in which to inject the new dependency.

Returns:

  • (Array)


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

def inject(gemfile_path, lockfile_path)
  if Bundler.frozen_bundle?
    # 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
    @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

#remove(gemfile_path, lockfile_path) ⇒ Array

Parameters:

  • gemfile_path (Pathname)

    The Gemfile from which to remove dependencies.

  • lockfile_path (Pathname)

    The lockfile from which to remove dependencies.

Returns:

  • (Array)


66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/bundler/injector.rb', line 66

def remove(gemfile_path, lockfile_path)
  # remove gems from each gemfiles we have
  Bundler.definition.gemfiles.each do |path|
    deps = remove_deps(path)

    show_warning("No gems were removed from the gemfile.") if deps.empty?

    deps.each {|dep| Bundler.ui.confirm "#{SharedHelpers.pretty_dependency(dep)} was removed." }
  end

  # Invalidate the cached Bundler.definition.
  # This prevents e.g. `bundle remove ...` from using outdated information.
  Bundler.reset_paths!
end