diff options
Diffstat (limited to 'lib/bundler/cli/add.rb')
| -rw-r--r-- | lib/bundler/cli/add.rb | 57 |
1 files changed, 42 insertions, 15 deletions
diff --git a/lib/bundler/cli/add.rb b/lib/bundler/cli/add.rb index 9709e71be0..20f76b59d1 100644 --- a/lib/bundler/cli/add.rb +++ b/lib/bundler/cli/add.rb @@ -2,34 +2,61 @@ module Bundler class CLI::Add + attr_reader :gems, :options, :version + def initialize(options, gems) @gems = gems @options = options - @options[:group] = @options[:group].split(",").map(&:strip) if !@options[:group].nil? && !@options[:group].empty? + @options[:group] = options[:group].split(",").map(&:strip) unless options[:group].nil? + @version = options[:version].split(",").map(&:strip) unless options[:version].nil? end def run - raise InvalidOption, "You can not specify `--strict` and `--optimistic` at the same time." if @options[:strict] && @options[:optimistic] + Bundler.ui.level = "warn" if options[:quiet] - # raise error when no gems are specified - raise InvalidOption, "Please specify gems to add." if @gems.empty? + Bundler::CLI::Common.validate_cooldown!(options[:cooldown]) + Bundler.settings.set_command_option_if_given :cooldown, options[:cooldown] - version = @options[:version].nil? ? nil : @options[:version].split(",").map(&:strip) + validate_options! + inject_dependencies + perform_bundle_install unless options["skip-install"] + end - unless version.nil? - version.each do |v| - raise InvalidOption, "Invalid gem requirement pattern '#{v}'" unless Gem::Requirement::PATTERN =~ v.to_s - end - end + private - dependencies = @gems.map {|g| Bundler::Dependency.new(g, version, @options) } + def perform_bundle_install + Installer.install(Bundler.root, Bundler.definition) + Bundler.load.cache if Bundler.app_cache.exist? + end + + def inject_dependencies + dependencies = gems.map {|g| Bundler::Dependency.new(g, version, options) } Injector.inject(dependencies, - :conservative_versioning => @options[:version].nil?, # Perform conservative versioning only when version is not specified - :optimistic => @options[:optimistic], - :strict => @options[:strict]) + conservative_versioning: options[:version].nil?, # Perform conservative versioning only when version is not specified + pessimistic: options[:pessimistic], + strict: options[:strict]) + end + + def validate_options! + raise InvalidOption, "You cannot specify `--git` and `--github` at the same time." if options["git"] && options["github"] - Installer.install(Bundler.root, Bundler.definition) unless @options["skip-install"] + unless options["git"] || options["github"] + raise InvalidOption, "You cannot specify `--branch` unless `--git` or `--github` is specified." if options["branch"] + + raise InvalidOption, "You cannot specify `--ref` unless `--git` or `--github` is specified." if options["ref"] + end + + raise InvalidOption, "You cannot specify `--branch` and `--ref` at the same time." if options["branch"] && options["ref"] + + raise InvalidOption, "You cannot specify `--strict` and `--pessimistic` at the same time." if options[:strict] && options[:pessimistic] + + # raise error when no gems are specified + raise InvalidOption, "Please specify gems to add." if gems.empty? + + version.to_a.each do |v| + raise InvalidOption, "Invalid gem requirement pattern '#{v}'" unless Gem::Requirement::PATTERN.match?(v.to_s) + end end end end |
