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 #6547 - agrim123:agr-add-mutiple-gems-names, r=segiddins
Browse files Browse the repository at this point in the history
Add mutiple gems by names

Add support for mutiple gems by names only

```bash
bundle add rack rails
```
Concerns:
- All the options/switches used would be applied to the all gems specified and a warning is displayed for the time being.

Closes #6543

(cherry picked from commit c48cca8)
  • Loading branch information
bundlerbot authored and lolwut committed Sep 21, 2018
1 parent 58261be commit 52b64be
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions lib/bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,9 @@ def binstubs(*gems)
"Adds gem to the Gemfile but does not install it"
method_option "optimistic", :type => :boolean, :banner => "Adds optimistic declaration of version to gem"
method_option "strict", :type => :boolean, :banner => "Adds strict declaration of version to gem"
def add(gem_name)
def add(*gems)
require "bundler/cli/add"
Add.new(options.dup, gem_name).run
Add.new(options.dup, gems).run
end

desc "outdated GEM [OPTIONS]", "List installed gems with newer versions available"
Expand Down
13 changes: 9 additions & 4 deletions lib/bundler/cli/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,33 @@

module Bundler
class CLI::Add
def initialize(options, gem_name)
@gem_name = gem_name
def initialize(options, gems)
@gems = gems
@options = options
@options[:group] = @options[:group].split(",").map(&:strip) if !@options[:group].nil? && !@options[:group].empty?
end

def run
raise InvalidOption, "You can not specify `--strict` and `--optimistic` at the same time." if @options[:strict] && @options[:optimistic]

# raise error when no gems are specified
raise InvalidOption, "Please specify gems to add." if @gems.empty?

version = @options[:version].nil? ? nil : @options[:version].split(",").map(&:strip)

unless version.nil?
version.each do |v|
raise InvalidOption, "Invalid gem requirement pattern '#{v}'" unless Gem::Requirement::PATTERN =~ v.to_s
end
end
dependency = Bundler::Dependency.new(@gem_name, version, @options)

Injector.inject([dependency],
dependencies = @gems.map {|g| Bundler::Dependency.new(g, version, @options) }

Injector.inject(dependencies,
:conservative_versioning => @options[:version].nil?, # Perform conservative versioning only when version is not specified
:optimistic => @options[:optimistic],
:strict => @options[:strict])

Installer.install(Bundler.root, Bundler.definition) unless @options["skip-install"]
end
end
Expand Down
3 changes: 2 additions & 1 deletion lib/bundler/injector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ def inject(gemfile_path, lockfile_path)
@new_deps -= builder.dependencies

# add new deps to the end of the in-memory Gemfile
# Set conservative versioning to false because we want to let the resolver resolve the version first
# Set conservative versioning to false because
# we want to let the resolver resolve the version first
builder.eval_gemfile("injected gems", build_gem_lines(false)) if @new_deps.any?

# resolve to see if the new deps broke anything
Expand Down
24 changes: 24 additions & 0 deletions spec/commands/add_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@
G
end

context "when no gems are specified" do
it "shows error" do
bundle "add"

expect(last_command.bundler_err).to include("Please specify gems to add")
end
end

describe "without version specified" do
it "version requirement becomes ~> major.minor.patch when resolved version is < 1.0" do
bundle "add 'bar'"
Expand Down Expand Up @@ -148,4 +156,20 @@
expect(out).to include("You can not specify `--strict` and `--optimistic` at the same time")
end
end

context "multiple gems" do
it "adds multiple gems to gemfile" do
bundle! "add bar baz"

expect(bundled_app("Gemfile").read).to match(/gem "bar", "~> 0.12.3"/)
expect(bundled_app("Gemfile").read).to match(/gem "baz", "~> 1.2"/)
end

it "throws error if any of the specified gems are present in the gemfile with different version" do
bundle "add weakling bar"

expect(out).to include("You cannot specify the same gem twice with different version requirements")
expect(out).to include("You specified: weakling (~> 0.0.1) and weakling (>= 0).")
end
end
end

0 comments on commit 52b64be

Please sign in to comment.