Skip to content

Commit d1634c7

Browse files
committed
Print help summary when the default command fail
As mentioned in #9124, the intent for changing the default command was to be more welcoming. I think we can acheive that by attempting to install, but to print that same help message if there is no Gemfile. That should address both concerns.
1 parent 9833263 commit d1634c7

File tree

4 files changed

+25
-15
lines changed

4 files changed

+25
-15
lines changed

bundler/lib/bundler/cli.rb

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,18 @@ def cli_help
120120
self.class.send(:class_options_help, shell)
121121
end
122122

123+
desc "install_or_cli_help", "Tries to run bundle install but prints a summary of bundler commands if there is no Gemfile", hide: true
124+
def install_or_cli_help
125+
invoke_other_command("install")
126+
rescue GemfileNotFound => error
127+
Bundler.ui.error error.message, wrap: true
128+
invoke_other_command("cli_help")
129+
end
130+
123131
def self.default_command(meth = nil)
124132
return super if meth
125133

126-
Bundler.settings[:default_cli_command] || "install"
134+
Bundler.settings[:default_cli_command] || "install_or_cli_help"
127135
end
128136

129137
class_option "no-color", type: :boolean, desc: "Disable colorization in output"
@@ -713,6 +721,19 @@ def current_command
713721
config[:current_command]
714722
end
715723

724+
def invoke_other_command(name)
725+
_, _, config = @_initializer
726+
original_command = config[:current_command]
727+
command = self.class.all_commands[name]
728+
config[:current_command] = command
729+
send(name)
730+
ensure
731+
config[:current_command] = original_command
732+
end
733+
734+
def current_command=(command)
735+
end
736+
716737
def print_command
717738
return unless Bundler.ui.debug?
718739
cmd = current_command

bundler/lib/bundler/vendor/thor/lib/thor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ def normalize_command_name(meth) #:nodoc:
625625
# alias name.
626626
def find_command_possibilities(meth)
627627
len = meth.to_s.length
628-
possibilities = all_commands.merge(map).keys.select { |n| meth == n[0, len] }.sort
628+
possibilities = all_commands.reject { |_k, c| c.hidden? }.merge(map).keys.select { |n| meth == n[0, len] }.sort
629629
unique_possibilities = possibilities.map { |k| map[k] || k }.uniq
630630

631631
if possibilities.include?(meth)

bundler/spec/bundler/cli_spec.rb

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,16 @@ def out_with_macos_man_workaround
8787
end
8888

8989
context "with no arguments" do
90-
it "installs by default" do
90+
it "tries to installs by default but print help on missing Gemfile" do
9191
bundle "", raise_on_error: false
9292
expect(err).to include("Could not locate Gemfile")
93-
end
9493

95-
it "prints a concise help message when default_cli_command set to cli_help" do
96-
bundle "config set default_cli_command cli_help"
97-
bundle ""
98-
expect(err).to be_empty
9994
expect(out).to include("Bundler version #{Bundler::VERSION}").
10095
and include("\n\nBundler commands:\n\n").
10196
and include("\n\n Primary commands:\n").
10297
and include("\n\n Utilities:\n").
10398
and include("\n\nOptions:\n")
10499
end
105-
106-
it "runs bundle install when default_cli_command set to install" do
107-
bundle "config set default_cli_command install"
108-
bundle "", raise_on_error: false
109-
expect(err).to include("Could not locate Gemfile")
110-
end
111100
end
112101

113102
context "when ENV['BUNDLE_GEMFILE'] is set to an empty string" do

bundler/spec/other/cli_dispatch_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@
1515

1616
it "print a friendly error when ambiguous" do
1717
bundle "in", raise_on_error: false
18-
expect(err).to eq("Ambiguous command in matches [info, init, inject, install]")
18+
expect(err).to eq("Ambiguous command in matches [info, init, install]")
1919
end
2020
end

0 commit comments

Comments
 (0)