diff options
-rw-r--r-- | lib/bundler/cli.rb | 2 | ||||
-rw-r--r-- | lib/bundler/cli/add.rb | 7 | ||||
-rw-r--r-- | lib/bundler/injector.rb | 12 | ||||
-rw-r--r-- | man/bundle-add.ronn | 8 | ||||
-rw-r--r-- | spec/commands/add_spec.rb | 32 |
5 files changed, 58 insertions, 3 deletions
diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index f3340f7cf7..926a748231 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -335,6 +335,8 @@ module Bundler method_option "source", :aliases => "-s", :type => :string method_option "skip-install", :type => :boolean, :banner => "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) require "bundler/cli/add" Add.new(options.dup, gem_name).run diff --git a/lib/bundler/cli/add.rb b/lib/bundler/cli/add.rb index e1a662161e..6adccb13d0 100644 --- a/lib/bundler/cli/add.rb +++ b/lib/bundler/cli/add.rb @@ -9,6 +9,8 @@ module Bundler end def run + raise InvalidOption, "You can not specify `--strict` and `--optimistic` at the same time." if @options[:strict] && @options[:optimistic] + version = @options[:version].nil? ? nil : @options[:version].split(",").map(&:strip) unless version.nil? @@ -18,7 +20,10 @@ module Bundler end dependency = Bundler::Dependency.new(@gem_name, version, @options) - Injector.inject([dependency], :conservative_versioning => @options[:version].nil?) # Perform conservative versioning only when version is not specified + Injector.inject([dependency], + :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 9c67a80777..b62279b94c 100644 --- a/lib/bundler/injector.rb +++ b/lib/bundler/injector.rb @@ -61,7 +61,17 @@ module Bundler seg_end_index = version >= Gem::Version.new("1.0") ? 1 : 2 prerelease_suffix = version.to_s.gsub(version.release.to_s, "") if version.prerelease? - "~> #{segments[0..seg_end_index].join(".")}#{prerelease_suffix}" + "#{version_prefix}#{segments[0..seg_end_index].join(".")}#{prerelease_suffix}" + end + + def version_prefix + if @options[:strict] + "= " + elsif @options[:optimistic] + ">= " + else + "~> " + end end def build_gem_lines(conservative_versioning) diff --git a/man/bundle-add.ronn b/man/bundle-add.ronn index 91eb1d7188..1e2d732ec6 100644 --- a/man/bundle-add.ronn +++ b/man/bundle-add.ronn @@ -3,7 +3,7 @@ bundle-add(1) -- Add gem to the Gemfile and run bundle install ## SYNOPSIS -`bundle add` <GEM_NAME> [--group=GROUP] [--version=VERSION] [--source=SOURCE] [--skip-install] +`bundle add` <GEM_NAME> [--group=GROUP] [--version=VERSION] [--source=SOURCE] [--skip-install] [--strict] [--optimistic] ## DESCRIPTION Adds the named gem to the Gemfile and run `bundle install`. `bundle install` can be avoided by using the flag `--skip-install`. @@ -32,3 +32,9 @@ bundle add rails --group "development, test" * `--skip-install`: Adds the gem to the Gemfile but does not install it. + +* `--optimistic`: + Adds optimistic declaration of version + +* `--strict`: + Adds strict declaration of version diff --git a/spec/commands/add_spec.rb b/spec/commands/add_spec.rb index 7299938caa..bf3b4e1759 100644 --- a/spec/commands/add_spec.rb +++ b/spec/commands/add_spec.rb @@ -116,4 +116,36 @@ RSpec.describe "bundle add" do bundle "add 'baz' --source='file://does/not/exist'" expect(out).to include("Could not fetch specs from file://does/not/exist/") end + + describe "with --optimistic" do + it "adds optimistic version" do + bundle! "add 'foo' --optimistic" + expect(bundled_app("Gemfile").read).to include %(gem "foo", ">= 2.0") + expect(the_bundle).to include_gems "foo 2.0" + end + end + + describe "with --strict option" do + it "adds strict version" do + bundle! "add 'foo' --strict" + expect(bundled_app("Gemfile").read).to include %(gem "foo", "= 2.0") + expect(the_bundle).to include_gems "foo 2.0" + end + end + + describe "with no option" do + it "adds pessimistic version" do + bundle! "add 'foo'" + expect(bundled_app("Gemfile").read).to include %(gem "foo", "~> 2.0") + expect(the_bundle).to include_gems "foo 2.0" + end + end + + describe "with --optimistic and --strict" do + it "throws error" do + bundle "add 'foo' --strict --optimistic" + + expect(out).to include("You can not specify `--strict` and `--optimistic` at the same time") + end + end end |