Skip to content

Commit c933ecc

Browse files
simideivid-rodriguez
authored andcommitted
Merge pull request #6561 from technicalpickles/auto_install_bundler_setup
Add auto_install support to require "bundler/setup" (cherry picked from commit 9312624)
1 parent a2a89b7 commit c933ecc

File tree

4 files changed

+40
-21
lines changed

4 files changed

+40
-21
lines changed

bundler/lib/bundler.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module Bundler
4040
SUDO_MUTEX = Thread::Mutex.new
4141

4242
autoload :Checksum, File.expand_path("bundler/checksum", __dir__)
43+
autoload :CLI, File.expand_path("bundler/cli", __dir__)
4344
autoload :CIDetector, File.expand_path("bundler/ci_detector", __dir__)
4445
autoload :Definition, File.expand_path("bundler/definition", __dir__)
4546
autoload :Dependency, File.expand_path("bundler/dependency", __dir__)
@@ -165,6 +166,25 @@ def setup(*groups)
165166
end
166167
end
167168

169+
# Automatically install dependencies if Bundler.settings[:auto_install] exists.
170+
# This is set through config cmd `bundle config set --global auto_install 1`.
171+
#
172+
# Note that this method `nil`s out the global Definition object, so it
173+
# should be called first, before you instantiate anything like an
174+
# `Installer` that'll keep a reference to the old one instead.
175+
def auto_install
176+
return unless settings[:auto_install]
177+
178+
begin
179+
definition.specs
180+
rescue GemNotFound, GitError
181+
ui.info "Automatically installing missing gems."
182+
reset!
183+
CLI::Install.new({}).run
184+
reset!
185+
end
186+
end
187+
168188
# Setups Bundler environment (see Bundler.setup) if it is not already set,
169189
# and loads all gems from groups specified. Unlike ::setup, can be called
170190
# multiple times with different groups (if they were allowed by setup).

bundler/lib/bundler/cli.rb

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
module Bundler
66
class CLI < Thor
77
require_relative "cli/common"
8+
require_relative "cli/install"
89

910
package_name "Bundler"
1011

@@ -69,7 +70,7 @@ def initialize(*args)
6970
Bundler.settings.set_command_option_if_given :retry, options[:retry]
7071

7172
current_cmd = args.last[:current_command].name
72-
auto_install if AUTO_INSTALL_CMDS.include?(current_cmd)
73+
Bundler.auto_install if AUTO_INSTALL_CMDS.include?(current_cmd)
7374
rescue UnknownArgumentError => e
7475
raise InvalidOption, e.message
7576
ensure
@@ -737,26 +738,6 @@ def self.deprecated_ext_value?(arguments)
737738

738739
private
739740

740-
# Automatically invoke `bundle install` and resume if
741-
# Bundler.settings[:auto_install] exists. This is set through config cmd
742-
# `bundle config set --global auto_install 1`.
743-
#
744-
# Note that this method `nil`s out the global Definition object, so it
745-
# should be called first, before you instantiate anything like an
746-
# `Installer` that'll keep a reference to the old one instead.
747-
def auto_install
748-
return unless Bundler.settings[:auto_install]
749-
750-
begin
751-
Bundler.definition.specs
752-
rescue GemNotFound, GitError
753-
Bundler.ui.info "Automatically installing missing gems."
754-
Bundler.reset!
755-
invoke :install, []
756-
Bundler.reset!
757-
end
758-
end
759-
760741
def current_command
761742
_, _, config = @_initializer
762743
config[:current_command]

bundler/lib/bundler/setup.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
if Bundler::SharedHelpers.in_bundle?
66
require_relative "../bundler"
77

8+
# try to auto_install first before we get to the `Bundler.ui.silence`, so user knows what is happening
9+
Bundler.auto_install
10+
811
if STDOUT.tty? || ENV["BUNDLER_FORCE_TTY"]
912
begin
1013
Bundler.ui.silence { Bundler.setup }

bundler/spec/runtime/setup_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1599,4 +1599,19 @@ def require(path)
15991599
sys_exec "#{Gem.ruby} #{script}", raise_on_error: false
16001600
expect(out).to include("requiring foo used the monkeypatch")
16011601
end
1602+
1603+
it "performs an automatic bundle install" do
1604+
gemfile <<-G
1605+
source "#{file_uri_for(gem_repo1)}"
1606+
gem "rack", :group => :test
1607+
G
1608+
1609+
bundle "config set auto_install 1"
1610+
1611+
ruby <<-RUBY
1612+
require 'bundler/setup'
1613+
RUBY
1614+
expect(err).to be_empty
1615+
expect(out).to include("Installing rack 1.0.0")
1616+
end
16021617
end

0 commit comments

Comments
 (0)