Class: Gem::Commands::InstallCommand

Inherits:
Gem::Command show all
Includes:
InstallUpdateOptions, LocalRemoteOptions, VersionOption
Defined in:
lib/rubygems/commands/install_command.rb

Overview

Gem installer command line tool

See `gem help install`

Instance Attribute Summary collapse

Attributes inherited from Gem::Command

#command, #defaults, #options, #program_name, #summary

Instance Method Summary collapse

Methods included from InstallUpdateOptions

#add_install_update_options, #install_update_defaults_str, #install_update_options

Methods included from SecurityOption

#add_security_option

Methods included from LocalRemoteOptions

#accept_uri_http, #add_bulk_threshold_option, #add_clear_sources_option, #add_local_remote_options, #add_proxy_option, #add_source_option, #add_update_sources_option, #both?, #local?, #remote?

Methods included from VersionOption

#add_platform_option, #add_prerelease_option, #add_version_option

Methods inherited from Gem::Command

add_common_option, #add_extra_args, #add_option, add_specific_extra_args, #begins?, build_args, build_args=, #check_deprecated_options, common_options, #deprecate_option, #deprecated?, extra_args, extra_args=, #get_all_gem_names, #get_all_gem_names_and_versions, #get_one_gem_name, #get_one_optional_argument, #handle_options, #handles?, #invoke, #invoke_with_build_args, #merge_options, #remove_option, #show_help, #show_lookup_failure, specific_extra_args, specific_extra_args_hash, specific_extra_args_hash=, #when_invoked

Methods included from UserInteraction

#alert, #alert_error, #alert_warning, #ask, #ask_for_password, #ask_yes_no, #choose_from_list, #say, #terminate_interaction, #verbose

Methods included from DefaultUserInteraction

ui, #ui, #ui=, ui=, use_ui, #use_ui

Methods included from Text

#clean_text, #format_text, #levenshtein_distance, #min3, #truncate_text

Constructor Details

#initializeInstallCommand

Returns a new instance of InstallCommand.


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rubygems/commands/install_command.rb', line 21

def initialize
  defaults = Gem::DependencyInstaller::DEFAULT_OPTIONS.merge({
    :format_executable => false,
    :lock              => true,
    :suggest_alternate => true,
    :version           => Gem::Requirement.default,
    :without_groups    => [],
  })

  defaults.merge!(install_update_options)

  super 'install', 'Install a gem into the local repository', defaults

  add_install_update_options
  add_local_remote_options
  add_platform_option
  add_version_option
  add_prerelease_option "to be installed. (Only for listed gems)"

  @installed_specs = []
end

Instance Attribute Details

#installed_specsObject (readonly)

:nodoc:


15
16
17
# File 'lib/rubygems/commands/install_command.rb', line 15

def installed_specs
  @installed_specs
end

Instance Method Details

#argumentsObject

:nodoc:


43
44
45
# File 'lib/rubygems/commands/install_command.rb', line 43

def arguments # :nodoc:
  "GEMNAME       name of gem to install"
end

#check_install_dirObject

:nodoc:


136
137
138
139
140
141
# File 'lib/rubygems/commands/install_command.rb', line 136

def check_install_dir # :nodoc:
  if options[:install_dir] and options[:user_install]
    alert_error "Use --install-dir or --user-install but not both"
    terminate_interaction 1
  end
end

#check_versionObject

:nodoc:


143
144
145
146
147
148
149
150
# File 'lib/rubygems/commands/install_command.rb', line 143

def check_version # :nodoc:
  if options[:version] != Gem::Requirement.default and
       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.0.0'`"
    terminate_interaction 1
  end
end

#defaults_strObject

:nodoc:


47
48
49
50
51
# File 'lib/rubygems/commands/install_command.rb', line 47

def defaults_str # :nodoc:
  "--both --version '#{Gem::Requirement.default}' --no-force\n" +
  "--install-dir #{Gem.dir} --lock\n" +
  install_update_defaults_str
end

#descriptionObject

:nodoc:


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rubygems/commands/install_command.rb', line 53

def description # :nodoc:
  <<-EOF
The install command installs local or remote gem into a gem repository.

For gems with executables ruby installs a wrapper file into the executable
directory by default.  This can be overridden with the --no-wrappers option.
The wrapper allows you to choose among alternate gem versions using _version_.

For example `rake _0.7.3_ --version` will run rake version 0.7.3 if a newer
version is also installed.

Gem Dependency Files
====================

RubyGems can install a consistent set of gems across multiple environments
using `gem install -g` when a gem dependencies file (gem.deps.rb, Gemfile or
Isolate) is present.  If no explicit file is given RubyGems attempts to find
one in the current directory.

When the RUBYGEMS_GEMDEPS environment variable is set to a gem dependencies
file the gems from that file will be activated at startup time.  Set it to a
specific filename or to "-" to have RubyGems automatically discover the gem
dependencies file by walking up from the current directory.

NOTE: Enabling automatic discovery on multiuser systems can lead to
execution of arbitrary code when used from directories outside your control.

Extension Install Failures
==========================

If an extension fails to compile during gem installation the gem
specification is not written out, but the gem remains unpacked in the
repository.  You may need to specify the path to the library's headers and
libraries to continue.  You can do this by adding a -- between RubyGems'
options and the extension's build options:

$ gem install some_extension_gem
[build fails]
Gem files will remain installed in \\
/path/to/gems/some_extension_gem-1.0 for inspection.
Results logged to /path/to/gems/some_extension_gem-1.0/gem_make.out
$ gem install some_extension_gem -- --with-extension-lib=/path/to/lib
[build succeeds]
$ gem list some_extension_gem

*** LOCAL GEMS ***

some_extension_gem (1.0)
$

If you correct the compilation errors by editing the gem files you will need
to write the specification by hand.  For example:

$ gem install some_extension_gem
[build fails]
Gem files will remain installed in \\
/path/to/gems/some_extension_gem-1.0 for inspection.
Results logged to /path/to/gems/some_extension_gem-1.0/gem_make.out
$ [cd /path/to/gems/some_extension_gem-1.0]
$ [edit files or what-have-you and run make]
$ gem spec ../../cache/some_extension_gem-1.0.gem --ruby > \\
           ../../specifications/some_extension_gem-1.0.gemspec
$ gem list some_extension_gem

*** LOCAL GEMS ***

some_extension_gem (1.0)
$

Command Alias
==========================

You can use `i` command instead of `install`.

$ gem i GEMNAME

  EOF
end

#executeObject


152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rubygems/commands/install_command.rb', line 152

def execute
  if options.include? :gemdeps
    install_from_gemdeps
    return # not reached
  end

  @installed_specs = []

  ENV.delete 'GEM_PATH' if options[:install_dir].nil?

  check_install_dir
  check_version

  load_hooks

  exit_code = install_gems

  show_installed

  terminate_interaction exit_code
end

#install_from_gemdepsObject

:nodoc:


174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/rubygems/commands/install_command.rb', line 174

def install_from_gemdeps # :nodoc:
  require 'rubygems/request_set'
  rs = Gem::RequestSet.new

  specs = rs.install_from_gemdeps options do |req, inst|
    s = req.full_spec

    if inst
      say "Installing #{s.name} (#{s.version})"
    else
      say "Using #{s.name} (#{s.version})"
    end
  end

  @installed_specs = specs

  terminate_interaction
end

#install_gem(name, version) ⇒ Object

:nodoc:


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'lib/rubygems/commands/install_command.rb', line 193

def install_gem(name, version) # :nodoc:
  return if options[:conservative] and
    not Gem::Dependency.new(name, version).matching_specs.empty?

  req = Gem::Requirement.create(version)

  dinst = Gem::DependencyInstaller.new options

  request_set = dinst.resolve_dependencies name, req

  if options[:explain]
    say "Gems to install:"

    request_set.sorted_requests.each do |activation_request|
      say "  #{activation_request.full_name}"
    end
  else
    @installed_specs.concat request_set.install options
  end

  show_install_errors dinst.errors
end

#install_gemsObject

:nodoc:


216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/rubygems/commands/install_command.rb', line 216

def install_gems # :nodoc:
  exit_code = 0

  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 => e
      show_lookup_failure e.name, e.version, e.errors, suppress_suggestions

      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
  end

  exit_code
end

#load_hooksObject

Loads post-install hooks


248
249
250
251
252
253
254
255
# File 'lib/rubygems/commands/install_command.rb', line 248

def load_hooks # :nodoc:
  if options[:install_as_default]
    require 'rubygems/install_default_message'
  else
    require 'rubygems/install_message'
  end
  require 'rubygems/rdoc'
end

#show_install_errors(errors) ⇒ Object

:nodoc:


257
258
259
260
261
262
263
264
265
266
267
# File 'lib/rubygems/commands/install_command.rb', line 257

def show_install_errors(errors) # :nodoc:
  return unless errors

  errors.each do |x|
    return unless Gem::SourceFetchProblem === x

    msg = "Unable to pull data from '#{x.source.uri}': #{x.error.message}"

    alert_warning msg
  end
end

#show_installedObject

:nodoc:


269
270
271
272
273
274
# File 'lib/rubygems/commands/install_command.rb', line 269

def show_installed # :nodoc:
  return if @installed_specs.empty?

  gems = @installed_specs.length == 1 ? 'gem' : 'gems'
  say "#{@installed_specs.length} #{gems} installed"
end

#usageObject

:nodoc:


132
133
134
# File 'lib/rubygems/commands/install_command.rb', line 132

def usage # :nodoc:
  "#{program_name} GEMNAME [GEMNAME ...] [options] -- --build-flags"
end