Debian Codegraph @

lib/bundler/cli/add.rb

raw
1 # frozen_string_literal: true
2
3 module Bundler
4 class CLI::Add
5 attr_reader :gems, :options, :version
6
7 def initialize(options, gems)
8 @gems = gems
9 @options = options
10 @options[:group] = options[:group].split(",").map(&:strip) unless options[:group].nil?
11 @version = options[:version].split(",").map(&:strip) unless options[:version].nil?
12 end
13
14 def run
15 validate_options!
16 inject_dependencies
17 perform_bundle_install unless options["skip-install"]
18 end
19
20 private
21
22 def perform_bundle_install
23 Installer.install(Bundler.root, Bundler.definition)
24 Bundler.load.cache if Bundler.app_cache.exist?
25 end
26
27 def inject_dependencies
28 dependencies = gems.map {|g| Bundler::Dependency.new(g, version, options) }
29
30 Injector.inject(dependencies,
31 conservative_versioning: options[:version].nil?, # Perform conservative versioning only when version is not specified
32 optimistic: options[:optimistic],
33 strict: options[:strict])
34 end
35
36 def validate_options!
37 raise InvalidOption, "You cannot specify `--strict` and `--optimistic` at the same time." if options[:strict] && options[:optimistic]
38
39 # raise error when no gems are specified
40 raise InvalidOption, "Please specify gems to add." if gems.empty?
41
42 version.to_a.each do |v|
43 raise InvalidOption, "Invalid gem requirement pattern '#{v}'" unless Gem::Requirement::PATTERN.match?(v.to_s)
44 end
45 end
46 end
47 end