diff options
author | Homu <homu@barosl.com> | 2016-07-04 10:40:09 +0900 |
---|---|---|
committer | Homu <homu@barosl.com> | 2016-07-04 10:40:09 +0900 |
commit | 2439ac84cfd731aaee9ebaca284cb077cf601969 (patch) | |
tree | 496a1354c8008de8665e1a3efa96783039163cf5 /lib/bundler/cli | |
parent | 75aa757f7ae4863fcc78f585f32332ebff8f173a (diff) | |
parent | ecd55e56a6a6e87e4aab49b506bf4b5edab1adaa (diff) | |
download | bundler-2439ac84cfd731aaee9ebaca284cb077cf601969.tar.gz |
Auto merge of #4632 - b-ggs:bundle-add, r=RochesterinNYC
Add `bundle add` command to add gems into the Gemfile
Continuing the work done to implement `bundle add GEM` from bundler/bundler#4546
To do:
- [x] Cherry-pick commit from @ognevsky
- [x] Run specs
- [x] Use Bundler.definition to resolve `last_version_number`
- [x] Pattern `bundle add` implementation after `bundle inject`
- [x] Add option to specify added gem's `group`
- [x] Add option to specify added gem's `source`
- [x] Modify existing specs & add additional specs
@RochesterinNYC
---
Closes #4546
Diffstat (limited to 'lib/bundler/cli')
-rw-r--r-- | lib/bundler/cli/inject.rb | 30 |
1 files changed, 24 insertions, 6 deletions
diff --git a/lib/bundler/cli/inject.rb b/lib/bundler/cli/inject.rb index e319feb201..262514d587 100644 --- a/lib/bundler/cli/inject.rb +++ b/lib/bundler/cli/inject.rb @@ -1,26 +1,31 @@ # frozen_string_literal: true module Bundler class CLI::Inject - attr_reader :options, :name, :version, :gems + attr_reader :options, :name, :version, :group, :source, :gems def initialize(options, name, version, gems) @options = options @name = name - @version = version + @version = version || last_version_number + @group = options[:group] + @source = options[:source] @gems = gems end def run # The required arguments allow Thor to give useful feedback when the arguments # are incorrect. This adds those first two arguments onto the list as a whole. - gems.unshift(version).unshift(name) + gems.unshift(source).unshift(group).unshift(version).unshift(name) # Build an array of Dependency objects out of the arguments deps = [] - gems.each_slice(2) do |gem_name, gem_version| - deps << Bundler::Dependency.new(gem_name, gem_version) + gems.each_slice(4) do |gem_name, gem_version, gem_group, gem_source| + ops = Gem::Requirement::OPS.map {|key, _val| key } + has_op = ops.any? {|op| gem_version.start_with? op } + gem_version = "~> #{gem_version}" unless has_op + deps << Bundler::Dependency.new(gem_name, gem_version, "group" => gem_group, "source" => gem_source) end - added = Injector.inject(deps) + added = Injector.inject(deps, options) if added.any? Bundler.ui.confirm "Added to Gemfile:" @@ -29,5 +34,18 @@ module Bundler Bundler.ui.confirm "All injected gems were already present in the Gemfile" end end + + private + + def last_version_number + definition = Bundler.definition(true) + definition.resolve_remotely! + specs = definition.index[name].sort_by(&:version) + unless options[:pre] + specs.delete_if {|b| b.respond_to?(:version) && b.version.prerelease? } + end + spec = specs.last + spec.version.to_s + end end end |