Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

Commit

Permalink
Auto merge of #6572 - agrim123:agr-bundle-list-options, r=hsbt
Browse files Browse the repository at this point in the history
[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 25bcb86)
  • Loading branch information
bundlerbot authored and lolwut committed Sep 21, 2018
1 parent 52b64be commit 7baafa9
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 8 deletions.
2 changes: 2 additions & 0 deletions lib/bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
45 changes: 39 additions & 6 deletions lib/bundler/cli/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 17 additions & 1 deletion man/bundle-list.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -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.
53 changes: 52 additions & 1 deletion spec/commands/list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 7baafa9

Please sign in to comment.