diff options
author | The Bundler Bot <bot@bundler.io> | 2018-06-15 07:20:18 +0000 |
---|---|---|
committer | Colby Swandale <me@colby.fyi> | 2018-10-05 16:15:43 +1000 |
commit | a5867b633a9c7e5755266618178e6ee25ce6aa16 (patch) | |
tree | de84e969d95a593261da2395019d0062edc75f9a | |
parent | 547ce9fd5f13ca6254e439fe886c08a589eb19eb (diff) | |
download | bundler-a5867b633a9c7e5755266618178e6ee25ce6aa16.tar.gz |
Auto merge of #6547 - agrim123:agr-add-mutiple-gems-names, r=segiddins
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 c48cca89bbbe8deeed985135a3ca8ffe219efb44)
-rw-r--r-- | lib/bundler/cli.rb | 4 | ||||
-rw-r--r-- | lib/bundler/cli/add.rb | 13 | ||||
-rw-r--r-- | lib/bundler/injector.rb | 3 | ||||
-rw-r--r-- | spec/commands/add_spec.rb | 24 |
4 files changed, 37 insertions, 7 deletions
diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index d5247e55d9..8ebef850fc 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -335,9 +335,9 @@ module Bundler "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" diff --git a/lib/bundler/cli/add.rb b/lib/bundler/cli/add.rb index 6adccb13d0..9709e71be0 100644 --- a/lib/bundler/cli/add.rb +++ b/lib/bundler/cli/add.rb @@ -2,8 +2,8 @@ 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 @@ -11,6 +11,9 @@ module Bundler 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? @@ -18,12 +21,14 @@ module Bundler 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 diff --git a/lib/bundler/injector.rb b/lib/bundler/injector.rb index 556429ea2a..fd7db8762b 100644 --- a/lib/bundler/injector.rb +++ b/lib/bundler/injector.rb @@ -31,7 +31,8 @@ module Bundler @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 diff --git a/spec/commands/add_spec.rb b/spec/commands/add_spec.rb index 9b767f6143..20ce89d31c 100644 --- a/spec/commands/add_spec.rb +++ b/spec/commands/add_spec.rb @@ -17,6 +17,14 @@ RSpec.describe "bundle add" do 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'" @@ -148,4 +156,20 @@ RSpec.describe "bundle add" do 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 |