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 #5456 - Shekharrajak:5452_bundle_inject_options, r=colb…
Browse files Browse the repository at this point in the history
…y-swandale

`bundle inject` with source and group options

Fixes #5452

Eg

```
$ bundle inject "bootstrap" ">0" --source=https://rubygems.org --group=development
Fetching gem metadata from https://rubygems.org/............
Fetching version metadata from https://rubygems.org/..
Fetching dependency metadata from https://rubygems.org/.
Fetching gem metadata from https://rubygems.org/.............
Fetching version metadata from https://rubygems.org/..
Fetching dependency metadata from https://rubygems.org/.
Added to Gemfile:
  bootstrap (> 0), group => [:development], :source => 'https://rubygems.org'
```

In GemFile

```
gem 'bootstrap', '> 0', :group => [:development], :source => 'https://rubygems.org'
```

### Multiple group :

```
$ dbundle inject "bootstrap" ">0" --source=https://rubygems.org --group=development,production
Fetching gem metadata from https://rubygems.org/............

Added to Gemfile:
gem 'bootstrap', '> 0', :group => [:development, :production], :source => 'https://rubygems.org'

```
In gemfile

```
# Added at 2017-03-24 11:40:51 +0530 by shekharrajak:
gem 'bootstrap', '> 0', :group => [:development, :production], :source => 'https://rubygems.org'
```
  • Loading branch information
bundlerbot committed Apr 7, 2017
2 parents 7358d85 + 1754e50 commit 32fb832
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
6 changes: 5 additions & 1 deletion lib/bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,14 @@ def platform
end

desc "inject GEM VERSION", "Add the named gem, with version requirements, to the resolved Gemfile"
method_option "source", :type => :string, :banner =>
"Install gem from the given source"
method_option "group", :type => :string, :banner =>
"Install gem into a bundler group"
def inject(name, version)
SharedHelpers.major_deprecation "The `inject` command has been replaced by the `add` command"
require "bundler/cli/inject"
Inject.new(options, name, version).run
Inject.new(options.dup, name, version).run
end

desc "lock", "Creates a lockfile without installing"
Expand Down
10 changes: 8 additions & 2 deletions lib/bundler/cli/inject.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def initialize(options, name, version)
@options = options
@name = name
@version = version || last_version_number
@group = options[:group]
@group = options[:group].split(",") unless options[:group].nil?
@source = options[:source]
@gems = []
end
Expand All @@ -31,7 +31,13 @@ def run

if added.any?
Bundler.ui.confirm "Added to Gemfile:"
Bundler.ui.confirm added.map {|g| " #{g}" }.join("\n")
Bundler.ui.confirm(added.map do |d|
name = "'#{d.name}'"
requirement = ", '#{d.requirement}'"
group = ", :group => #{d.groups.inspect}" if d.groups != Array(:default)
source = ", :source => '#{d.source}'" unless d.source.nil?
%(gem #{name}#{requirement}#{group}#{source})
end.join("\n"))
else
Bundler.ui.confirm "All gems were already present in the Gemfile"
end
Expand Down
9 changes: 8 additions & 1 deletion lib/bundler/injector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ def new_gem_lines
@new_deps.map do |d|
name = "'#{d.name}'"
requirement = ", '#{d.requirement}'"
group = ", :group => #{d.groups.inspect}" if d.groups != Array(:default)
if d.groups != Array(:default)
group =
if d.groups.size == 1
", :group => #{d.groups.inspect}"
else
", :groups => #{d.groups.inspect}"
end
end
source = ", :source => '#{d.source}'" unless d.source.nil?
%(gem #{name}#{requirement}#{group}#{source})
end.join("\n")
Expand Down
25 changes: 25 additions & 0 deletions spec/commands/inject_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,31 @@
end
end

context "with source option" do
it "add gem with source option in gemfile" do
bundle "inject 'foo' '>0' --source file://#{gem_repo1}"
gemfile = bundled_app("Gemfile").read
str = "gem 'foo', '> 0', :source => 'file://#{gem_repo1}'"
expect(gemfile).to include str
end
end

context "with group option" do
it "add gem with group option in gemfile" do
bundle "inject 'rack-obama' '>0' --group=development"
gemfile = bundled_app("Gemfile").read
str = "gem 'rack-obama', '> 0', :group => [:development]"
expect(gemfile).to include str
end

it "add gem with multiple groups in gemfile" do
bundle "inject 'rack-obama' '>0' --group=development,test"
gemfile = bundled_app("Gemfile").read
str = "gem 'rack-obama', '> 0', :groups => [:development, :test]"
expect(gemfile).to include str
end
end

context "when frozen" do
before do
bundle "install"
Expand Down

0 comments on commit 32fb832

Please sign in to comment.