diff options
-rw-r--r-- | lib/bundler/cli/plugin.rb | 5 | ||||
-rw-r--r-- | lib/bundler/plugin.rb | 20 | ||||
-rw-r--r-- | lib/bundler/plugin/index.rb | 8 | ||||
-rw-r--r-- | spec/bundler/plugin_spec.rb | 23 | ||||
-rw-r--r-- | spec/plugins/list_spec.rb | 60 |
5 files changed, 116 insertions, 0 deletions
diff --git a/lib/bundler/cli/plugin.rb b/lib/bundler/cli/plugin.rb index 5488a9f28d..92d4653bda 100644 --- a/lib/bundler/cli/plugin.rb +++ b/lib/bundler/cli/plugin.rb @@ -20,5 +20,10 @@ module Bundler def install(*plugins) Bundler::Plugin.install(plugins, options) end + + desc "list", "List the installed plugins and available commands" + def list + Bundler::Plugin.list + end end end diff --git a/lib/bundler/plugin.rb b/lib/bundler/plugin.rb index 7db159c593..f7e6093fa5 100644 --- a/lib/bundler/plugin.rb +++ b/lib/bundler/plugin.rb @@ -46,6 +46,26 @@ module Bundler Bundler.ui.error "Failed to install plugin #{name}: #{e.message}\n #{e.backtrace.join("\n ")}" end + # List installed plugins and commands + # + def list + installed_plugins = index.installed_plugins + if installed_plugins.any? + output = String.new + installed_plugins.each do |plugin| + output << "#{plugin}\n" + output << "-----\n" + index.plugin_commands(plugin).each do |command| + output << " #{command}\n" + end + output << "\n" + end + else + output = "No plugins installed" + end + Bundler.ui.info output + end + # Evaluates the Gemfile with a limited DSL and installs the plugins # specified by plugin method # diff --git a/lib/bundler/plugin/index.rb b/lib/bundler/plugin/index.rb index 8dde072f16..f5c2baf431 100644 --- a/lib/bundler/plugin/index.rb +++ b/lib/bundler/plugin/index.rb @@ -95,6 +95,14 @@ module Bundler @plugin_paths[name] end + def installed_plugins + @plugin_paths.keys + end + + def plugin_commands(plugin) + @commands.find_all {|_, n| n == plugin }.map(&:first) + end + def source?(source) @sources.key? source end diff --git a/spec/bundler/plugin_spec.rb b/spec/bundler/plugin_spec.rb index 9796b580a3..68a7e32ad1 100644 --- a/spec/bundler/plugin_spec.rb +++ b/spec/bundler/plugin_spec.rb @@ -32,6 +32,29 @@ RSpec.describe Bundler::Plugin do allow(index).to receive(:register_plugin) end + describe "list command" do + context "when no plugins are installed" do + before { allow(index).to receive(:installed_plugins) { [] } } + it "outputs no plugins installed" do + expect(Bundler.ui).to receive(:info).with("No plugins installed") + subject.list + end + end + + context "with installed plugins" do + before do + allow(index).to receive(:installed_plugins) { %w[plug1 plug2] } + allow(index).to receive(:plugin_commands).with("plug1") { %w[c11 c12] } + allow(index).to receive(:plugin_commands).with("plug2") { %w[c21 c22] } + end + it "list plugins followed by commands" do + expected_output = "plug1\n-----\n c11\n c12\n\nplug2\n-----\n c21\n c22\n\n" + expect(Bundler.ui).to receive(:info).with(expected_output) + subject.list + end + end + end + describe "install command" do let(:opts) { { "version" => "~> 1.0", "source" => "foo" } } diff --git a/spec/plugins/list_spec.rb b/spec/plugins/list_spec.rb new file mode 100644 index 0000000000..7dc9d10c4b --- /dev/null +++ b/spec/plugins/list_spec.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +RSpec.describe "bundler plugin list" do + before do + build_repo2 do + build_plugin "foo" do |s| + s.write "plugins.rb", <<-RUBY + class Foo < Bundler::Plugin::API + command "shout" + + def exec(command, args) + puts "Foo shout" + end + end + RUBY + end + build_plugin "bar" do |s| + s.write "plugins.rb", <<-RUBY + class Bar < Bundler::Plugin::API + command "scream" + + def exec(command, args) + puts "Bar scream" + end + end + RUBY + end + end + end + + context "no plugins installed" do + it "shows proper no plugins installed message" do + bundle "plugin list" + + expect(out).to include("No plugins installed") + end + end + + context "single plugin installed" do + it "shows plugin name with commands list" do + bundle "plugin install foo --source file://#{gem_repo2}" + plugin_should_be_installed("foo") + bundle "plugin list" + + expected_output = "foo\n-----\n shout" + expect(out).to include(expected_output) + end + end + + context "multiple plugins installed" do + it "shows plugin names with commands list" do + bundle "plugin install foo bar --source file://#{gem_repo2}" + plugin_should_be_installed("foo", "bar") + bundle "plugin list" + + expected_output = "foo\n-----\n shout\n\nbar\n-----\n scream" + expect(out).to include(expected_output) + end + end +end |