#--
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
# All rights reserved.
# See LICENSE.txt for permissions.
#++
require 'rubygems/command'
require 'rubygems/exceptions'
require 'rubygems/package'
require 'rubygems/ext'
require 'rubygems/user_interaction'
require 'fileutils'
##
# The installer installs the files contained in the .gem into the Gem.home.
#
# Gem::Installer does the work of putting files in all the right places on the
# filesystem including unpacking the gem into its gem dir, installing the
# gemspec in the specifications dir, storing the cached gem in the cache dir,
# and installing either wrappers or symlinks for executables.
#
# The installer invokes pre and post install hooks. Hooks can be added either
# through a rubygems_plugin.rb file in an installed gem or via a
# rubygems/defaults/#{RUBY_ENGINE}.rb or rubygems/defaults/operating_system.rb
# file. See Gem.pre_install and Gem.post_install for details.
class Gem::Installer
##
# Paths where env(1) might live. Some systems are broken and have it in
# /bin
ENV_PATHS = %w[/usr/bin/env /bin/env]
##
# Deprecated in favor of Gem::Ext::BuildError
ExtensionBuildError = Gem::Ext::BuildError # :nodoc:
include Gem::UserInteraction
##
# Filename of the gem being installed.
attr_reader :gem
##
# The directory a gem's executables will be installed into
attr_reader :bin_dir
attr_reader :build_root # :nodoc:
##
# The gem repository the gem will be installed into
attr_reader :gem_home
##
# The options passed when the Gem::Installer was instantiated.
attr_reader :options
##
# Sets the specification for .gem-less installs.
attr_writer :spec
@path_warning = false
@install_lock = Mutex.new
class << self
##
# True if we've warned about PATH not including Gem.bindir
attr_accessor :path_warning
##
# Certain aspects of the install process are not thread-safe. This lock is
# used to allow multiple threads to install Gems at the same time.
attr_reader :install_lock
##
# Overrides the executable format.
#
# This is a sprintf format with a "%s" which will be replaced with the
# executable name. It is based off the ruby executable name's difference
# from "ruby".
attr_writer :exec_format
# Defaults to use Ruby's program prefix and suffix.
def exec_format
@exec_format ||= Gem.default_exec_format
end
end
##
# Constructs an Installer instance that will install the gem located at
# +gem+. +options+ is a Hash with the following keys:
#
# :bin_dir:: Where to put a bin wrapper if needed.
# :development:: Whether or not development dependencies should be installed.
# :env_shebang:: Use /usr/bin/env in bin wrappers.
# :force:: Overrides all version checks and security policy checks, except
# for a signed-gems-only policy.
# :format_executable:: Format the executable the same as the Ruby executable.
# If your Ruby is ruby18, foo_exec will be installed as
# foo_exec18.
# :ignore_dependencies:: Don't raise if a dependency is missing.
# :install_dir:: The directory to install the gem into.
# :security_policy:: Use the specified security policy. See Gem::Security
# :user_install:: Indicate that the gem should be unpacked into the users
# personal gem directory.
# :only_install_dir:: Only validate dependencies against what is in the
# install_dir
# :wrappers:: Install wrappers if true, symlinks if false.
# :build_args:: An Array of arguments to pass to the extension builder
# process. If not set, then Gem::Command.build_args is used
def initialize(gem, options={})
require 'fileutils'
@gem = gem
@options = options
@package = Gem::Package.new @gem
process_options
@package.security_policy = @security_policy
if options[:user_install] and not options[:unpack] then
@gem_home = Gem.user_dir
@bin_dir = Gem.bindir gem_home unless options[:bin_dir]
check_that_user_bin_dir_is_in_path
end
end
##
# Checks if +filename+ exists in +@bin_dir+.
#
# If +@force+ is set +filename+ is overwritten.
#
# If +filename+ exists and is a RubyGems wrapper for different gem the user
# is consulted.
#
# If +filename+ exists and +@bin_dir+ is Gem.default_bindir (/usr/local) the
# user is consulted.
#
# Otherwise +filename+ is overwritten.
def check_executable_overwrite filename # :nodoc:
return if @force
generated_bin = File.join @bin_dir, formatted_program_filename(filename)
return unless File.exist? generated_bin
ruby_executable = false
existing = nil
open generated_bin, 'rb' do |io|
next unless io.gets =~ /^#!/ # shebang
io.gets # blankline
# TODO detect a specially formatted comment instead of trying
# to run a regexp against Ruby code.
next unless io.gets =~ /This file was generated by RubyGems/
ruby_executable = true
existing = io.read.slice(/^gem (['"])(.*?)(\1),/, 2)
end
return if spec.name == existing
# somebody has written to RubyGems' directory, overwrite, too bad
return if Gem.default_bindir != @bin_dir and not ruby_executable
question = "#{spec.name}'s executable \"#{filename}\" conflicts with "
if ruby_executable then
question << existing
return if ask_yes_no "#{question}\nOverwrite the executable?", false
conflict = "installed executable from #{existing}"
else
question << generated_bin
return if ask_yes_no "#{question}\nOverwrite the executable?", false
conflict = generated_bin
end
raise Gem::InstallError,
"\"#{filename}\" from #{spec.name} conflicts with #{conflict}"
end
##
# Lazy accessor for the spec's gem directory.
def gem_dir
@gem_dir ||= File.join(gem_home, "gems", spec.full_name)
end
##
# Lazy accessor for the installer's spec.
def spec
@spec ||= @package.spec
rescue Gem::Package::Error => e
raise Gem::InstallError, "invalid gem: #{e.message}"
end
##
# Installs the gem and returns a loaded Gem::Specification for the installed
# gem.
#
# The gem will be installed with the following structure:
#
# @gem_home/
# cache/<gem-version>.gem #=> a cached copy of the installed gem
# gems/<gem-version>/... #=> extracted files
# specifications/<gem-version>.gemspec #=> the Gem::Specification
def install
pre_install_checks
FileUtils.rm_f File.join gem_home, 'specifications', @spec.spec_name
run_pre_install_hooks
# Completely remove any previous gem files
FileUtils.rm_rf gem_dir
FileUtils.mkdir_p gem_dir
spec.loaded_from = spec_file
if @options[:install_as_default]
extract_bin
write_default_spec
else
extract_files
build_extensions
write_build_info_file
run_post_build_hooks
generate_bin
write_spec
write_cache_file
end
say spec.post_install_message unless spec.post_install_message.nil?
Gem::Installer.install_lock.synchronize { Gem::Specification.add_spec spec }
run_post_install_hooks
spec
# TODO This rescue is in the wrong place. What is raising this exception?
# move this rescue to around the code that actually might raise it.
rescue Zlib::GzipFile::Error
raise Gem::InstallError, "gzip error installing #{gem}"
end
def run_pre_install_hooks # :nodoc:
Gem.pre_install_hooks.each do |hook|
if hook.call(self) == false then
location = " at #{$1}" if hook.inspect =~ /@(.*:\d+)/
message = "pre-install hook#{location} failed for #{spec.full_name}"
raise Gem::InstallError, message
end
end
end
def run_post_build_hooks # :nodoc:
Gem.post_build_hooks.each do |hook|
if hook.call(self) == false then
FileUtils.rm_rf gem_dir
location = " at #{$1}" if hook.inspect =~ /@(.*:\d+)/
message = "post-build hook#{location} failed for #{spec.full_name}"
raise Gem::InstallError, message
end
end
end
def run_post_install_hooks # :nodoc:
Gem.post_install_hooks.each do |hook|
hook.call self
end
end
##
#
# Return an Array of Specifications contained within the gem_home
# we'll be installing into.
def installed_specs
@specs ||= begin
specs = []
Dir[File.join(gem_home, "specifications", "*.gemspec")].each do |path|
spec = Gem::Specification.load path.untaint
specs << spec if spec
end
specs
end
end
##
# Ensure that the dependency is satisfied by the current installation of
# gem. If it is not an exception is raised.
#
# spec :: Gem::Specification
# dependency :: Gem::Dependency
def ensure_dependency(spec, dependency)
unless installation_satisfies_dependency? dependency then
raise Gem::InstallError, "#{spec.name} requires #{dependency}"
end
true
end
##
# True if the gems in the system satisfy +dependency+.
def installation_satisfies_dependency?(dependency)
return true if @options[:development] and dependency.type == :development
return true if installed_specs.detect { |s| dependency.matches_spec? s }
return false if @only_install_dir
not dependency.matching_specs.empty?
end
##
Loading ...