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 #6556 - agrim123:agr-versions-gem-add, r=colby-swandale
Browse files Browse the repository at this point in the history
[bundle add] Add version prefix flexibility

### What was the end-user problem that led to this PR?

By default, on `bundle add` we use "pessimistic" way (~>) of versions. According to this [comment](https://github.com/bundler/bundler/issues/6553#issue-326023952) some users face problems.

### What was your diagnosis of the problem?

Adding flags to provide flexibility to change this declaration namely `optimistic` and `strict`

### What is your fix for the problem, implemented in this PR?

Adding flags to opt for other declarations.

### Why did you choose this fix out of the possible options?

Currently, its an experiment and I have added it to only `bundle add` but still the version locked in lockfile are "pessimistic". Need suggestions on this and on how to proceed.

Addresses #6553

(cherry picked from commit 43b4fa9)
  • Loading branch information
bundlerbot authored and lolwut committed Sep 21, 2018
1 parent 7572283 commit 58261be
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 3 deletions.
2 changes: 2 additions & 0 deletions lib/bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ def binstubs(*gems)
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
Expand Down
7 changes: 6 additions & 1 deletion lib/bundler/cli/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ def initialize(options, gem_name)
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?
Expand All @@ -18,7 +20,10 @@ def run
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
Expand Down
12 changes: 11 additions & 1 deletion lib/bundler/injector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,17 @@ def conservative_version(spec)
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)
Expand Down
8 changes: 7 additions & 1 deletion man/bundle-add.ronn
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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
32 changes: 32 additions & 0 deletions spec/commands/add_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,36 @@
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

0 comments on commit 58261be

Please sign in to comment.