Skip to content
This repository has been archived by the owner on Apr 14, 2021. It is now read-only.

bundle inject with source and group options #5456

Merged
merged 8 commits into from
Apr 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/bundler/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,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 =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe "install gem from the given source" and "Install gem into a bundler group"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review @colby-swandale . I have edited the PR according to to your comments.

"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}"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's just one group, we shouldn't need to put it in an array

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please also add a test for including multiple groups

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for review I have added the testcase for multiple groups in this commit

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