diff options
Diffstat (limited to 'lib/rubygems/commands/install_command.rb')
| -rw-r--r-- | lib/rubygems/commands/install_command.rb | 165 |
1 files changed, 65 insertions, 100 deletions
diff --git a/lib/rubygems/commands/install_command.rb b/lib/rubygems/commands/install_command.rb index 3a7d50517f..6d3beec0b4 100644 --- a/lib/rubygems/commands/install_command.rb +++ b/lib/rubygems/commands/install_command.rb @@ -1,10 +1,12 @@ # frozen_string_literal: true -require 'rubygems/command' -require 'rubygems/install_update_options' -require 'rubygems/dependency_installer' -require 'rubygems/local_remote_options' -require 'rubygems/validator' -require 'rubygems/version_option' + +require_relative "../command" +require_relative "../install_update_options" +require_relative "../dependency_installer" +require_relative "../local_remote_options" +require_relative "../validator" +require_relative "../version_option" +require_relative "../update_suggestion" ## # Gem installer command line tool @@ -12,23 +14,25 @@ require 'rubygems/version_option' # See `gem help install` class Gem::Commands::InstallCommand < Gem::Command - attr_reader :installed_specs # :nodoc: include Gem::VersionOption include Gem::LocalRemoteOptions include Gem::InstallUpdateOptions + include Gem::UpdateSuggestion def initialize defaults = Gem::DependencyInstaller::DEFAULT_OPTIONS.merge({ - :format_executable => false, - :lock => true, - :suggest_alternate => true, - :version => Gem::Requirement.default, - :without_groups => [], + format_executable: false, + lock: true, + suggest_alternate: true, + version: Gem::Requirement.default, + without_groups: [], }) - super 'install', 'Install a gem into the local repository', defaults + defaults.merge!(install_update_options) + + super "install", "Install a gem into the local repository", defaults add_install_update_options add_local_remote_options @@ -44,8 +48,9 @@ class Gem::Commands::InstallCommand < Gem::Command end def defaults_str # :nodoc: - "--both --version '#{Gem::Requirement.default}' --document --no-force\n" + - "--install-dir #{Gem.dir} --lock" + "--both --version '#{Gem::Requirement.default}' --no-force\n" \ + "--install-dir #{Gem.dir} --lock\n" + + install_update_defaults_str end def description # :nodoc: @@ -117,40 +122,39 @@ to write the specification by hand. For example: some_extension_gem (1.0) $ +Command Alias +========================== + +You can use `i` command instead of `install`. + + $ gem i GEMNAME + EOF end def usage # :nodoc: - "#{program_name} GEMNAME [GEMNAME ...] [options] -- --build-flags" - end - - def check_install_dir # :nodoc: - if options[:install_dir] and options[:user_install] then - alert_error "Use --install-dir or --user-install but not both" - terminate_interaction 1 - end + "#{program_name} [options] GEMNAME [GEMNAME ...] -- --build-flags" end def check_version # :nodoc: - if options[:version] != Gem::Requirement.default and - get_all_gem_names.size > 1 then - alert_error "Can't use --version w/ multiple gems. Use name:ver instead." + if options[:version] != Gem::Requirement.default && + get_all_gem_names.size > 1 + alert_error "Can't use --version with multiple gems. You can specify multiple gems with" \ + " version requirements using `gem install 'my_gem:1.0.0' 'my_other_gem:>=2'`" terminate_interaction 1 end end def execute - - if options.include? :gemdeps then + if options.include? :gemdeps install_from_gemdeps return # not reached end @installed_specs = [] - ENV.delete 'GEM_PATH' if options[:install_dir].nil? and RUBY_VERSION > '1.9' + ENV.delete "GEM_PATH" if options[:install_dir].nil? - check_install_dir check_version load_hooks @@ -159,11 +163,13 @@ to write the specification by hand. For example: show_installed + say update_suggestion if eligible_for_update? + terminate_interaction exit_code end def install_from_gemdeps # :nodoc: - require 'rubygems/request_set' + require_relative "../request_set" rs = Gem::RequestSet.new specs = rs.install_from_gemdeps options do |req, inst| @@ -181,68 +187,27 @@ to write the specification by hand. For example: terminate_interaction end - def install_gem name, version # :nodoc: - return if options[:conservative] and - not Gem::Dependency.new(name, version).matching_specs.empty? + def install_gem(name, version) # :nodoc: + return if options[:conservative] && + !Gem::Dependency.new(name, version).matching_specs.empty? req = Gem::Requirement.create(version) - if options[:ignore_dependencies] then - install_gem_without_dependencies name, req - else - inst = Gem::DependencyInstaller.new options - request_set = inst.resolve_dependencies name, req - - if options[:explain] - puts "Gems to install:" - - request_set.sorted_requests.each do |s| - puts " #{s.full_name}" - end - - return - else - @installed_specs.concat request_set.install options - end + dinst = Gem::DependencyInstaller.new options - show_install_errors inst.errors - end - end + request_set = dinst.resolve_dependencies name, req - def install_gem_without_dependencies name, req # :nodoc: - gem = nil + if options[:explain] + say "Gems to install:" - if local? then - if name =~ /\.gem$/ and File.file? name then - source = Gem::Source::SpecificFile.new name - spec = source.spec - else - source = Gem::Source::Local.new - spec = source.find_gem name, req + request_set.sorted_requests.each do |activation_request| + say " #{activation_request.full_name}" end - gem = source.download spec if spec - end - - if remote? and not gem then - dependency = Gem::Dependency.new name, req - dependency.prerelease = options[:prerelease] - - fetcher = Gem::RemoteFetcher.fetcher - gem = fetcher.download_to_cache dependency + else + @installed_specs.concat request_set.install options end - inst = Gem::Installer.at gem, options - inst.install - - require 'rubygems/dependency_installer' - dinst = Gem::DependencyInstaller.new options - dinst.installed_gems.replace [inst.spec] - - Gem.done_installing_hooks.each do |hook| - hook.call dinst, [inst.spec] - end unless Gem.done_installing_hooks.empty? - - @installed_specs.push(inst.spec) + show_install_errors dinst.errors end def install_gems # :nodoc: @@ -250,16 +215,21 @@ to write the specification by hand. For example: get_all_gem_names_and_versions.each do |gem_name, gem_version| gem_version ||= options[:version] + domain = options[:domain] + domain = :local unless options[:suggest_alternate] + suppress_suggestions = (domain == :local) begin install_gem gem_name, gem_version rescue Gem::InstallError => e alert_error "Error installing #{gem_name}:\n\t#{e.message}" exit_code |= 1 - rescue Gem::GemNotFoundException, Gem::UnsatisfiableDependencyError => e - domain = options[:domain] - domain = :local unless options[:suggest_alternate] - show_lookup_failure e.name, e.version, e.errors, domain + rescue Gem::DependencyResolutionError => e + alert_error "Error installing #{gem_name}:\n\t#{e.message}" + exit_code |= 2 + rescue Gem::UnsatisfiableDependencyError => e + show_lookup_failure e.name, e.version, e.errors, suppress_suggestions, + "'#{gem_name}' (#{gem_version})" exit_code |= 2 end @@ -272,21 +242,18 @@ to write the specification by hand. For example: # Loads post-install hooks def load_hooks # :nodoc: - if options[:install_as_default] - require 'rubygems/install_default_message' - else - require 'rubygems/install_message' - end - require 'rubygems/rdoc' + require_relative "../install_message" + require_relative "../rdoc" end - def show_install_errors errors # :nodoc: + def show_install_errors(errors) # :nodoc: return unless errors errors.each do |x| - return unless Gem::SourceFetchProblem === x + next unless Gem::SourceFetchProblem === x - msg = "Unable to pull data from '#{x.source.uri}': #{x.error.message}" + require_relative "../uri" + msg = "Unable to pull data from '#{Gem::Uri.redact(x.source.uri)}': #{x.error.message}" alert_warning msg end @@ -295,9 +262,7 @@ to write the specification by hand. For example: def show_installed # :nodoc: return if @installed_specs.empty? - gems = @installed_specs.length == 1 ? 'gem' : 'gems' + gems = @installed_specs.length == 1 ? "gem" : "gems" say "#{@installed_specs.length} #{gems} installed" end - end - |
