2018-11-02 19:07:56 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module Bundler
|
|
|
|
class CLI::Add
|
2019-06-01 05:49:40 -04:00
|
|
|
attr_reader :gems, :options, :version
|
|
|
|
|
2018-11-02 19:07:56 -04:00
|
|
|
def initialize(options, gems)
|
|
|
|
@gems = gems
|
|
|
|
@options = options
|
2019-06-01 05:49:40 -04:00
|
|
|
@options[:group] = options[:group].split(",").map(&:strip) unless options[:group].nil?
|
|
|
|
@version = options[:version].split(",").map(&:strip) unless options[:version].nil?
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def run
|
2019-06-01 05:49:40 -04:00
|
|
|
validate_options!
|
|
|
|
inject_dependencies
|
|
|
|
perform_bundle_install unless options["skip-install"]
|
|
|
|
end
|
2018-11-02 19:07:56 -04:00
|
|
|
|
2019-06-01 05:49:40 -04:00
|
|
|
private
|
2018-11-02 19:07:56 -04:00
|
|
|
|
2019-06-01 05:49:40 -04:00
|
|
|
def perform_bundle_install
|
|
|
|
Installer.install(Bundler.root, Bundler.definition)
|
2019-11-11 03:57:45 -05:00
|
|
|
Bundler.load.cache if Bundler.app_cache.exist?
|
2019-06-01 05:49:40 -04:00
|
|
|
end
|
2018-11-02 19:07:56 -04:00
|
|
|
|
2019-06-01 05:49:40 -04:00
|
|
|
def inject_dependencies
|
|
|
|
dependencies = gems.map {|g| Bundler::Dependency.new(g, version, options) }
|
2018-11-02 19:07:56 -04:00
|
|
|
|
|
|
|
Injector.inject(dependencies,
|
2019-06-01 05:49:40 -04:00
|
|
|
:conservative_versioning => options[:version].nil?, # Perform conservative versioning only when version is not specified
|
|
|
|
:optimistic => options[:optimistic],
|
|
|
|
:strict => options[:strict])
|
|
|
|
end
|
|
|
|
|
|
|
|
def validate_options!
|
|
|
|
raise InvalidOption, "You can not specify `--strict` and `--optimistic` at the same time." if options[:strict] && options[:optimistic]
|
2018-11-02 19:07:56 -04:00
|
|
|
|
2019-06-01 05:49:40 -04:00
|
|
|
# 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 =~ v.to_s
|
|
|
|
end
|
2018-11-02 19:07:56 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|