From 7baafa9c44200b6e536a501e4b359f8908268886 Mon Sep 17 00:00:00 2001 From: The Bundler Bot Date: Wed, 27 Jun 2018 15:29:51 +0000 Subject: [PATCH] Auto merge of #6572 - agrim123:agr-bundle-list-options, r=hsbt [bundle list] add `--without-group` and `--only-group` Listing gems according to groups, either excluding a particular or from a specific group. Usage: ```bash bundle list --without-group dev ``` ```bash bundle list --only-group dev ``` Addresses #6564 (cherry picked from commit 25bcb86eb3e0fe8dd18a7b42728d4eda9554beec) --- lib/bundler/cli.rb | 2 ++ lib/bundler/cli/list.rb | 45 +++++++++++++++++++++++++++----- man/bundle-list.ronn | 18 ++++++++++++- spec/commands/list_spec.rb | 53 +++++++++++++++++++++++++++++++++++++- 4 files changed, 110 insertions(+), 8 deletions(-) diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index e91de990147..394118b65f8 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -287,6 +287,8 @@ def show(gem_name = nil) if Bundler.feature_flag.list_command? desc "list", "List all gems in the bundle" method_option "name-only", :type => :boolean, :banner => "print only the gem names" + method_option "only-group", :type => :string, :banner => "print gems from a particular group" + method_option "without-group", :type => :string, :banner => "print all gems expect from a group" method_option "paths", :type => :boolean, :banner => "print the path to each gem in the bundle" def list require "bundler/cli/list" diff --git a/lib/bundler/cli/list.rb b/lib/bundler/cli/list.rb index c92f05df07e..d1799196e78 100644 --- a/lib/bundler/cli/list.rb +++ b/lib/bundler/cli/list.rb @@ -7,19 +7,52 @@ def initialize(options) end def run - specs = Bundler.load.specs.reject {|s| s.name == "bundler" }.sort_by(&:name) + raise InvalidOption, "The `--only-group` and `--without-group` options cannot be used together" if @options["only-group"] && @options["without-group"] + + raise InvalidOption, "The `--name-only` and `--paths` options cannot be used together" if @options["name-only"] && @options[:paths] + + specs = if @options["only-group"] || @options["without-group"] + filtered_specs_by_groups + else + Bundler.load.specs + end.reject {|s| s.name == "bundler" }.sort_by(&:name) + + return Bundler.ui.info "No gems in the Gemfile" if specs.empty? - raise InvalidOption, "The `--name-only` and `--paths` options cannot be used together" if @options["name-only"] && @options["paths"] return specs.each {|s| Bundler.ui.info s.name } if @options["name-only"] return specs.each {|s| Bundler.ui.info s.full_gem_path } if @options["paths"] - return Bundler.ui.info "No gems in the Gemfile" if specs.empty? Bundler.ui.info "Gems included by the bundle:" - specs.each do |s| - Bundler.ui.info " * #{s.name} (#{s.version}#{s.git_version})" - end + + specs.each {|s| Bundler.ui.info " * #{s.name} (#{s.version}#{s.git_version})" } Bundler.ui.info "Use `bundle info` to print more detailed information about a gem" end + + private + + def verify_group_exists(groups) + raise InvalidOption, "`#{@options["without-group"]}` group could not be found." if @options["without-group"] && !groups.include?(@options["without-group"].to_sym) + + raise InvalidOption, "`#{@options["only-group"]}` group could not be found." if @options["only-group"] && !groups.include?(@options["only-group"].to_sym) + end + + def filtered_specs_by_groups + definition = Bundler.definition + groups = definition.groups + + verify_group_exists(groups) + + show_groups = + if @options["without-group"] + groups.reject {|g| g == @options["without-group"].to_sym } + elsif @options["only-group"] + groups.select {|g| g == @options["only-group"].to_sym } + else + groups + end.map(&:to_sym) + + definition.specs_for(show_groups) + end end end diff --git a/man/bundle-list.ronn b/man/bundle-list.ronn index b7a9d3f7861..120cf5e3075 100644 --- a/man/bundle-list.ronn +++ b/man/bundle-list.ronn @@ -3,15 +3,31 @@ bundle-list(1) -- List all the gems in the bundle ## SYNOPSIS -`bundle list` [--name-only] +`bundle list` [--name-only] [--paths] [--without-group=GROUP] [--only-group=GROUP] ## DESCRIPTION Prints a list of all the gems in the bundle including their version. +Example: + +bundle list --name-only + +bundle list --paths + +bundle list --without-group test + +bundle list --only-group dev + +bundle list --only-group dev --paths + ## OPTIONS * `--name-only`: Print only the name of each gem. * `--paths`: Print the path to each gem in the bundle. +* `--without-group`: + Print all gems expect from a group. +* `--only-group`: + Print gems from a particular group. diff --git a/spec/commands/list_spec.rb b/spec/commands/list_spec.rb index 4ebe934ca7b..5305176c655 100644 --- a/spec/commands/list_spec.rb +++ b/spec/commands/list_spec.rb @@ -4,21 +4,72 @@ before do install_gemfile <<-G source "file://#{gem_repo1}" + gem "rack" + gem "rspec", :group => [:test] G end context "with name-only and paths option" do it "raises an error" do bundle "list --name-only --paths" + expect(out).to eq "The `--name-only` and `--paths` options cannot be used together" end end + context "with without-group and only-group option" do + it "raises an error" do + bundle "list --without-group dev --only-group test" + + expect(out).to eq "The `--only-group` and `--without-group` options cannot be used together" + end + end + + describe "with without-group option" do + context "when group is present" do + it "prints the gems not in the specified group" do + bundle! "list --without-group test" + + expect(out).to include(" * rack (1.0.0)") + expect(out).not_to include(" * rspec (1.2.7)") + end + end + + context "when group is not found" do + it "raises an error" do + bundle "list --without-group random" + + expect(out).to eq "`random` group could not be found." + end + end + end + + describe "with only-group option" do + context "when group is present" do + it "prints the gems in the specified group" do + bundle! "list --only-group default" + + expect(out).to include(" * rack (1.0.0)") + expect(out).not_to include(" * rspec (1.2.7)") + end + end + + context "when group is not found" do + it "raises an error" do + bundle "list --only-group random" + + expect(out).to eq "`random` group could not be found." + end + end + end + context "with name-only option" do it "prints only the name of the gems in the bundle" do bundle "list --name-only" - expect(out).to eq "rack" + + expect(out).to include("rack") + expect(out).to include("rspec") end end