diff --git a/lib/bundler/cli.rb b/lib/bundler/cli.rb index 7c8a1a0fc60..61c5427c909 100644 --- a/lib/bundler/cli.rb +++ b/lib/bundler/cli.rb @@ -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" diff --git a/lib/bundler/cli/inject.rb b/lib/bundler/cli/inject.rb index cf35e4985b4..b17292643f5 100644 --- a/lib/bundler/cli/inject.rb +++ b/lib/bundler/cli/inject.rb @@ -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 @@ -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 diff --git a/lib/bundler/injector.rb b/lib/bundler/injector.rb index 787d95fd9ca..7c78486dacd 100644 --- a/lib/bundler/injector.rb +++ b/lib/bundler/injector.rb @@ -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") diff --git a/spec/commands/inject_spec.rb b/spec/commands/inject_spec.rb index dd5e22498b4..5c711b32a0b 100644 --- a/spec/commands/inject_spec.rb +++ b/spec/commands/inject_spec.rb @@ -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"