| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
module Bundler |
| 4 |
class CLI::List |
| 5 |
def initialize(options) |
| 6 |
@options = options |
| 7 |
@without_group = options["without-group"].map(&:to_sym) |
| 8 |
@only_group = options["only-group"].map(&:to_sym) |
| 9 |
end |
| 10 |
|
| 11 |
def run |
| 12 |
raise InvalidOption, "The `--only-group` and `--without-group` options cannot be used together" if @only_group.any? && @without_group.any? |
| 13 |
|
| 14 |
raise InvalidOption, "The `--name-only` and `--paths` options cannot be used together" if @options["name-only"] && @options[:paths] |
| 15 |
|
| 16 |
specs = if @only_group.any? || @without_group.any? |
| 17 |
filtered_specs_by_groups |
| 18 |
else |
| 19 |
begin |
| 20 |
Bundler.load.specs |
| 21 |
rescue GemNotFound => e |
| 22 |
Bundler.ui.error e.message |
| 23 |
Bundler.ui.warn "Install missing gems with `bundle install`." |
| 24 |
exit 1 |
| 25 |
end |
| 26 |
end.reject {|s| s.name == "bundler" }.sort_by(&:name) |
| 27 |
|
| 28 |
return Bundler.ui.info "No gems in the Gemfile" if specs.empty? |
| 29 |
|
| 30 |
return specs.each {|s| Bundler.ui.info s.name } if @options["name-only"] |
| 31 |
return specs.each {|s| Bundler.ui.info s.full_gem_path } if @options["paths"] |
| 32 |
|
| 33 |
Bundler.ui.info "Gems included by the bundle:" |
| 34 |
|
| 35 |
specs.each {|s| Bundler.ui.info " * #{s.name} (#{s.version}#{s.git_version})" } |
| 36 |
|
| 37 |
Bundler.ui.info "Use `bundle info` to print more detailed information about a gem" |
| 38 |
end |
| 39 |
|
| 40 |
private |
| 41 |
|
| 42 |
def verify_group_exists(groups) |
| 43 |
(@without_group + @only_group).each do |group| |
| 44 |
raise InvalidOption, "`#{group}` group could not be found." unless groups.include?(group) |
| 45 |
end |
| 46 |
end |
| 47 |
|
| 48 |
def filtered_specs_by_groups |
| 49 |
definition = Bundler.definition |
| 50 |
groups = definition.groups |
| 51 |
|
| 52 |
verify_group_exists(groups) |
| 53 |
|
| 54 |
show_groups = |
| 55 |
if @without_group.any? |
| 56 |
groups.reject {|g| @without_group.include?(g) } |
| 57 |
elsif @only_group.any? |
| 58 |
groups.select {|g| @only_group.include?(g) } |
| 59 |
else |
| 60 |
groups |
| 61 |
end.map(&:to_sym) |
| 62 |
|
| 63 |
definition.specs_for(show_groups) |
| 64 |
end |
| 65 |
end |
| 66 |
end |