diff options
-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 a0d6507afe..86206660b3 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -337,9 +337,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 b62279b94c..08f2ba90fd 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 bf3b4e1759..5d7e5a30a6 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 |