2007-11-10 10:48:56 +03:00
|
|
|
#--
|
|
|
|
# Copyright 2006 by Chad Fowler, Rich Kilmer, Jim Weirich and others.
|
|
|
|
# All rights reserved.
|
|
|
|
# See LICENSE.txt for permissions.
|
|
|
|
#++
|
|
|
|
|
2011-01-19 03:08:49 +03:00
|
|
|
require 'rubygems/exceptions'
|
2012-11-29 10:52:18 +04:00
|
|
|
require 'rubygems/package'
|
2007-11-10 10:48:56 +03:00
|
|
|
require 'rubygems/ext'
|
2011-01-29 02:46:47 +03:00
|
|
|
require 'rubygems/user_interaction'
|
2013-02-22 02:05:09 +04:00
|
|
|
require 'fileutils'
|
2007-11-10 10:48:56 +03:00
|
|
|
|
|
|
|
##
|
2012-11-29 10:52:18 +04:00
|
|
|
# The installer installs the files contained in the .gem into the Gem.home.
|
2007-11-10 10:48:56 +03:00
|
|
|
#
|
|
|
|
# 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.
|
2009-06-10 01:38:59 +04:00
|
|
|
#
|
2011-01-20 00:23:04 +03:00
|
|
|
# The installer invokes pre and post install hooks. Hooks can be added either
|
2009-06-10 01:38:59 +04:00
|
|
|
# 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.
|
2008-09-25 14:13:50 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
class Gem::Installer
|
|
|
|
|
2009-06-10 01:38:59 +04:00
|
|
|
##
|
|
|
|
# Paths where env(1) might live. Some systems are broken and have it in
|
|
|
|
# /bin
|
|
|
|
|
|
|
|
ENV_PATHS = %w[/usr/bin/env /bin/env]
|
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
##
|
|
|
|
# Raised when there is an error while building extensions.
|
|
|
|
#
|
|
|
|
class ExtensionBuildError < Gem::InstallError; end
|
|
|
|
|
|
|
|
include Gem::UserInteraction
|
2007-12-20 11:39:12 +03:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
# DOC: Missing docs or :nodoc:.
|
2011-06-01 07:45:05 +04:00
|
|
|
attr_reader :gem
|
|
|
|
|
2008-09-25 14:13:50 +04:00
|
|
|
##
|
|
|
|
# The directory a gem's executables will be installed into
|
|
|
|
|
|
|
|
attr_reader :bin_dir
|
|
|
|
|
|
|
|
##
|
|
|
|
# The gem repository the gem will be installed into
|
|
|
|
|
|
|
|
attr_reader :gem_home
|
|
|
|
|
2011-01-20 00:23:04 +03:00
|
|
|
##
|
|
|
|
# The options passed when the Gem::Installer was instantiated.
|
|
|
|
|
|
|
|
attr_reader :options
|
|
|
|
|
2008-09-25 14:13:50 +04:00
|
|
|
@path_warning = false
|
|
|
|
|
2007-12-20 11:39:12 +03:00
|
|
|
class << self
|
|
|
|
|
2008-09-25 14:13:50 +04:00
|
|
|
##
|
|
|
|
# True if we've warned about PATH not including Gem.bindir
|
|
|
|
|
|
|
|
attr_accessor :path_warning
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
# DOC: Missing docs or :nodoc:.
|
2007-12-20 11:39:12 +03:00
|
|
|
attr_writer :exec_format
|
|
|
|
|
|
|
|
# Defaults to use Ruby's program prefix and suffix.
|
|
|
|
def exec_format
|
|
|
|
@exec_format ||= Gem.default_exec_format
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
##
|
|
|
|
# Constructs an Installer instance that will install the gem located at
|
|
|
|
# +gem+. +options+ is a Hash with the following keys:
|
|
|
|
#
|
2012-11-29 10:52:18 +04:00
|
|
|
# :bin_dir:: Where to put a bin wrapper if needed.
|
|
|
|
# :development:: Whether or not development dependencies should be installed.
|
2007-11-10 10:48:56 +03:00
|
|
|
# :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.
|
2013-09-14 12:59:02 +04:00
|
|
|
# :format_executable:: Format the executable the same as the Ruby executable.
|
|
|
|
# If your Ruby is ruby18, foo_exec will be installed as
|
2007-12-20 11:39:12 +03:00
|
|
|
# foo_exec18.
|
2012-11-29 10:52:18 +04:00
|
|
|
# :ignore_dependencies:: Don't raise if a dependency is missing.
|
|
|
|
# :install_dir:: The directory to install the gem into.
|
2007-11-10 10:48:56 +03:00
|
|
|
# :security_policy:: Use the specified security policy. See Gem::Security
|
2012-11-29 10:52:18 +04:00
|
|
|
# :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
|
2007-11-10 10:48:56 +03:00
|
|
|
# :wrappers:: Install wrappers if true, symlinks if false.
|
2012-11-29 10:52:18 +04:00
|
|
|
# :build_args:: An Array of arguments to pass to the extension builder
|
|
|
|
# process. If not set, then Gem::Command.build_args is used
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def initialize(gem, options={})
|
2011-01-19 03:08:49 +03:00
|
|
|
require 'fileutils'
|
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
@gem = gem
|
2011-01-20 00:23:04 +03:00
|
|
|
@options = options
|
2012-11-29 10:52:18 +04:00
|
|
|
@package = Gem::Package.new @gem
|
|
|
|
|
2011-01-20 00:23:04 +03:00
|
|
|
process_options
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
@package.security_policy = @security_policy
|
|
|
|
|
2008-10-26 02:58:43 +04:00
|
|
|
if options[:user_install] and not options[:unpack] then
|
2008-09-25 14:13:50 +04:00
|
|
|
@gem_home = Gem.user_dir
|
2013-02-19 03:15:49 +04:00
|
|
|
@bin_dir = Gem.bindir gem_home unless options[:bin_dir]
|
2011-01-20 00:23:04 +03:00
|
|
|
check_that_user_bin_dir_is_in_path
|
2008-06-26 06:06:00 +04:00
|
|
|
end
|
2011-06-01 07:45:05 +04:00
|
|
|
end
|
2008-06-26 06:06:00 +04:00
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
##
|
2012-11-29 10:52:18 +04:00
|
|
|
# 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.
|
2011-06-01 07:45:05 +04:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def check_executable_overwrite filename # :nodoc:
|
|
|
|
return if @force
|
|
|
|
|
2013-02-28 03:20:57 +04:00
|
|
|
generated_bin = File.join @bin_dir, formatted_program_filename(filename)
|
2012-11-29 10:52:18 +04:00
|
|
|
|
|
|
|
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
|
2013-09-14 12:59:02 +04:00
|
|
|
# to run a regexp against Ruby code.
|
2012-11-29 10:52:18 +04:00
|
|
|
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}"
|
2011-06-01 07:45:05 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2012-11-29 10:52:18 +04:00
|
|
|
# Lazy accessor for the spec's gem directory.
|
2011-06-01 07:45:05 +04:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
def gem_dir
|
|
|
|
@gem_dir ||= File.join(gem_home, "gems", spec.full_name)
|
2011-06-01 07:45:05 +04:00
|
|
|
end
|
2010-02-22 05:52:35 +03:00
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
##
|
|
|
|
# Lazy accessor for the installer's spec.
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
def spec
|
2012-11-29 10:52:18 +04:00
|
|
|
@spec ||= @package.spec
|
|
|
|
rescue Gem::Package::Error => e
|
|
|
|
raise Gem::InstallError, "invalid gem: #{e.message}"
|
2007-11-10 10:48:56 +03:00
|
|
|
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
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def install
|
2012-12-23 04:35:09 +04:00
|
|
|
pre_install_checks
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
run_pre_install_hooks
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2011-03-01 12:41:32 +03:00
|
|
|
# Completely remove any previous gem files
|
2012-12-23 04:35:09 +04:00
|
|
|
FileUtils.rm_rf gem_dir
|
2011-03-01 12:41:32 +03:00
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
FileUtils.mkdir_p gem_dir
|
2013-07-10 03:41:44 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
spec.loaded_from = spec_file
|
2013-07-10 03:41:44 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
if @options[:install_as_default]
|
|
|
|
extract_bin
|
|
|
|
write_default_spec
|
|
|
|
else
|
|
|
|
extract_files
|
2013-07-10 03:41:44 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
build_extensions
|
|
|
|
write_build_info_file
|
|
|
|
run_post_build_hooks
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
generate_bin
|
|
|
|
write_spec
|
|
|
|
write_cache_file
|
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
say spec.post_install_message unless spec.post_install_message.nil?
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
Gem::Specification.add_spec spec unless Gem::Specification.include? spec
|
2008-09-25 14:13:50 +04:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
run_post_install_hooks
|
|
|
|
|
|
|
|
spec
|
|
|
|
|
|
|
|
# TODO This rescue is in the wrong place. What is raising this exception?
|
2012-12-23 04:35:09 +04:00
|
|
|
# move this rescue to around the code that actually might raise it.
|
2012-11-29 10:52:18 +04:00
|
|
|
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:
|
2008-09-25 14:13:50 +04:00
|
|
|
Gem.post_install_hooks.each do |hook|
|
|
|
|
hook.call self
|
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# 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
|
2011-06-01 07:45:05 +04:00
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
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
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def ensure_dependency(spec, dependency)
|
|
|
|
unless installation_satisfies_dependency? dependency then
|
|
|
|
raise Gem::InstallError, "#{spec.name} requires #{dependency}"
|
|
|
|
end
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
2012-11-29 10:52:18 +04:00
|
|
|
# True if the gems in the system satisfy +dependency+.
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def installation_satisfies_dependency?(dependency)
|
2012-11-29 10:52:18 +04:00
|
|
|
return true if installed_specs.detect { |s| dependency.matches_spec? s }
|
|
|
|
return false if @only_install_dir
|
2011-06-01 07:45:05 +04:00
|
|
|
not dependency.matching_specs.empty?
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Unpacks the gem into the given directory.
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def unpack(directory)
|
|
|
|
@gem_dir = directory
|
|
|
|
extract_files
|
|
|
|
end
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
##
|
|
|
|
# The location of of the spec file that is installed.
|
|
|
|
#
|
|
|
|
|
|
|
|
def spec_file
|
|
|
|
File.join gem_home, "specifications", "#{spec.full_name}.gemspec"
|
|
|
|
end
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
|
|
|
# The location of of the default spec file for default gems.
|
|
|
|
#
|
|
|
|
|
|
|
|
def default_spec_file
|
|
|
|
File.join gem_home, "specifications/default", "#{spec.full_name}.gemspec"
|
|
|
|
end
|
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
##
|
2008-12-23 02:11:11 +03:00
|
|
|
# Writes the .gemspec specification (in Ruby) to the gem home's
|
|
|
|
# specifications directory.
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def write_spec
|
2013-07-09 02:41:03 +04:00
|
|
|
open spec_file, 'w' do |file|
|
2011-06-01 07:45:05 +04:00
|
|
|
file.puts spec.to_ruby_for_cache
|
2013-07-09 02:41:03 +04:00
|
|
|
file.fsync rescue nil # for filesystems without fsync(2)
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
end
|
2013-07-10 03:41:44 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
|
|
|
# Writes the full .gemspec specification (in Ruby) to the gem home's
|
|
|
|
# specifications/default directory.
|
|
|
|
|
|
|
|
def write_default_spec
|
|
|
|
File.open(default_spec_file, "w") do |file|
|
|
|
|
file.puts spec.to_ruby
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
##
|
|
|
|
# Creates windows .bat files for easy running of commands
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2010-04-22 12:24:42 +04:00
|
|
|
def generate_windows_script(filename, bindir)
|
2007-11-10 10:48:56 +03:00
|
|
|
if Gem.win_platform? then
|
|
|
|
script_name = filename + ".bat"
|
2007-12-20 11:39:12 +03:00
|
|
|
script_path = File.join bindir, File.basename(script_name)
|
|
|
|
File.open script_path, 'w' do |file|
|
2007-11-10 10:48:56 +03:00
|
|
|
file.puts windows_stub_script(bindir, filename)
|
|
|
|
end
|
2007-12-20 11:39:12 +03:00
|
|
|
|
|
|
|
say script_path if Gem.configuration.really_verbose
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
# DOC: Missing docs or :nodoc:.
|
2007-11-10 10:48:56 +03:00
|
|
|
def generate_bin
|
2011-06-01 07:45:05 +04:00
|
|
|
return if spec.executables.nil? or spec.executables.empty?
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
Dir.mkdir @bin_dir unless File.exist? @bin_dir
|
|
|
|
raise Gem::FilePermissionError.new(@bin_dir) unless File.writable? @bin_dir
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
spec.executables.each do |filename|
|
2007-11-10 10:48:56 +03:00
|
|
|
filename.untaint
|
2012-11-29 10:52:18 +04:00
|
|
|
bin_path = File.join gem_dir, spec.bindir, filename
|
2011-06-01 07:45:05 +04:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
unless File.exist? bin_path then
|
|
|
|
# TODO change this to a more useful warning
|
|
|
|
warn "#{bin_path} maybe `gem pristine #{spec.name}` will fix it?"
|
2011-06-01 07:45:05 +04:00
|
|
|
next
|
2011-03-01 12:41:32 +03:00
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
mode = File.stat(bin_path).mode | 0111
|
|
|
|
FileUtils.chmod mode, bin_path
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
check_executable_overwrite filename
|
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
if @wrappers then
|
2012-11-29 10:52:18 +04:00
|
|
|
generate_bin_script filename, @bin_dir
|
2007-11-10 10:48:56 +03:00
|
|
|
else
|
2012-11-29 10:52:18 +04:00
|
|
|
generate_bin_symlink filename, @bin_dir
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates the scripts to run the applications in the gem.
|
|
|
|
#--
|
|
|
|
# The Windows script is generated in addition to the regular one due to a
|
|
|
|
# bug or misfeature in the Windows shell's pipe. See
|
|
|
|
# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/193379
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def generate_bin_script(filename, bindir)
|
2007-12-20 11:39:12 +03:00
|
|
|
bin_script_path = File.join bindir, formatted_program_filename(filename)
|
|
|
|
|
2011-01-19 03:08:49 +03:00
|
|
|
FileUtils.rm_f bin_script_path # prior install may have been --no-wrappers
|
2007-12-20 11:39:12 +03:00
|
|
|
|
2011-03-01 12:41:32 +03:00
|
|
|
File.open bin_script_path, 'wb', 0755 do |file|
|
2011-01-19 03:08:49 +03:00
|
|
|
file.print app_script_text(filename)
|
|
|
|
end
|
2007-12-20 11:39:12 +03:00
|
|
|
|
2011-01-19 03:08:49 +03:00
|
|
|
say bin_script_path if Gem.configuration.really_verbose
|
2007-12-20 11:39:12 +03:00
|
|
|
|
2011-01-19 03:08:49 +03:00
|
|
|
generate_windows_script filename, bindir
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Creates the symlinks to run the applications in the gem. Moves
|
|
|
|
# the symlink if the gem being installed has a newer version.
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def generate_bin_symlink(filename, bindir)
|
2007-11-25 06:26:36 +03:00
|
|
|
if Gem.win_platform? then
|
|
|
|
alert_warning "Unable to use symlinks on Windows, installing wrapper"
|
2007-11-10 10:48:56 +03:00
|
|
|
generate_bin_script filename, bindir
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
src = File.join gem_dir, spec.bindir, filename
|
2007-12-20 11:39:12 +03:00
|
|
|
dst = File.join bindir, formatted_program_filename(filename)
|
2007-11-10 10:48:56 +03:00
|
|
|
|
|
|
|
if File.exist? dst then
|
|
|
|
if File.symlink? dst then
|
|
|
|
link = File.readlink(dst).split File::SEPARATOR
|
|
|
|
cur_version = Gem::Version.create(link[-3].sub(/^.*-/, ''))
|
2011-06-01 07:45:05 +04:00
|
|
|
return if spec.version < cur_version
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
File.unlink dst
|
|
|
|
end
|
|
|
|
|
2007-12-20 11:39:12 +03:00
|
|
|
FileUtils.symlink src, dst, :verbose => Gem.configuration.really_verbose
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Generates a #! line for +bin_file_name+'s wrapper copying arguments if
|
|
|
|
# necessary.
|
2012-11-29 10:52:18 +04:00
|
|
|
#
|
|
|
|
# If the :custom_shebang config is set, then it is used as a template
|
|
|
|
# for how to create the shebang used for to run a gem's executables.
|
|
|
|
#
|
|
|
|
# The template supports 4 expansions:
|
|
|
|
#
|
|
|
|
# $env the path to the unix env utility
|
|
|
|
# $ruby the path to the currently running ruby interpreter
|
|
|
|
# $exec the path to the gem's executable
|
|
|
|
# $name the name of the gem the executable is for
|
|
|
|
#
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def shebang(bin_file_name)
|
2009-03-15 06:01:39 +03:00
|
|
|
ruby_name = Gem::ConfigMap[:ruby_install_name] if @env_shebang
|
2012-11-29 10:52:18 +04:00
|
|
|
path = File.join gem_dir, spec.bindir, bin_file_name
|
2009-03-09 10:03:39 +03:00
|
|
|
first_line = File.open(path, "rb") {|file| file.gets}
|
2009-06-10 01:38:59 +04:00
|
|
|
|
2009-03-09 10:03:39 +03:00
|
|
|
if /\A#!/ =~ first_line then
|
|
|
|
# Preserve extra words on shebang line, like "-w". Thanks RPA.
|
2012-04-18 04:04:12 +04:00
|
|
|
shebang = first_line.sub(/\A\#!.*?ruby\S*((\s+\S+)+)/, "#!#{Gem.ruby}")
|
2009-03-15 06:01:39 +03:00
|
|
|
opts = $1
|
2009-03-09 10:03:39 +03:00
|
|
|
shebang.strip! # Avoid nasty ^M issues.
|
2009-03-15 06:01:39 +03:00
|
|
|
end
|
2009-06-10 01:38:59 +04:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
if which = Gem.configuration[:custom_shebang]
|
|
|
|
# replace bin_file_name with "ruby" to avoid endless loops
|
|
|
|
which = which.gsub(/ #{bin_file_name}$/," #{Gem::ConfigMap[:ruby_install_name]}")
|
|
|
|
|
|
|
|
which = which.gsub(/\$(\w+)/) do
|
|
|
|
case $1
|
|
|
|
when "env"
|
|
|
|
@env_path ||= ENV_PATHS.find {|env_path| File.executable? env_path }
|
|
|
|
when "ruby"
|
|
|
|
"#{Gem.ruby}#{opts}"
|
|
|
|
when "exec"
|
|
|
|
bin_file_name
|
|
|
|
when "name"
|
|
|
|
spec.name
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
"#!#{which}"
|
|
|
|
elsif not ruby_name then
|
2009-03-15 06:01:39 +03:00
|
|
|
"#!#{Gem.ruby}#{opts}"
|
2009-06-10 01:38:59 +04:00
|
|
|
elsif opts then
|
|
|
|
"#!/bin/sh\n'exec' #{ruby_name.dump} '-x' \"$0\" \"$@\"\n#{shebang}"
|
2007-11-10 10:48:56 +03:00
|
|
|
else
|
2009-03-09 10:03:39 +03:00
|
|
|
# Create a plain shebang line.
|
2009-06-10 01:38:59 +04:00
|
|
|
@env_path ||= ENV_PATHS.find {|env_path| File.executable? env_path }
|
2009-03-15 06:01:39 +03:00
|
|
|
"#!#{@env_path} #{ruby_name}"
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-23 04:35:09 +04:00
|
|
|
##
|
|
|
|
# Ensures the Gem::Specification written out for this gem is loadable upon
|
|
|
|
# installation.
|
|
|
|
|
|
|
|
def ensure_loadable_spec
|
|
|
|
ruby = spec.to_ruby_for_cache
|
2013-01-17 04:37:28 +04:00
|
|
|
ruby.untaint
|
2012-12-23 04:35:09 +04:00
|
|
|
|
|
|
|
begin
|
|
|
|
eval ruby
|
|
|
|
rescue StandardError, SyntaxError => e
|
|
|
|
raise Gem::InstallError,
|
|
|
|
"The specification for #{spec.full_name} is corrupt (#{e.class})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
# DOC: Missing docs or :nodoc:.
|
2011-01-20 00:23:04 +03:00
|
|
|
def ensure_required_ruby_version_met
|
2011-06-01 07:45:05 +04:00
|
|
|
if rrv = spec.required_ruby_version then
|
2011-01-20 00:23:04 +03:00
|
|
|
unless rrv.satisfied_by? Gem.ruby_version then
|
2011-06-01 07:45:05 +04:00
|
|
|
raise Gem::InstallError, "#{spec.name} requires Ruby version #{rrv}."
|
2011-01-20 00:23:04 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
# DOC: Missing docs or :nodoc:.
|
2011-01-20 00:23:04 +03:00
|
|
|
def ensure_required_rubygems_version_met
|
2011-06-01 07:45:05 +04:00
|
|
|
if rrgv = spec.required_rubygems_version then
|
2012-11-29 10:52:18 +04:00
|
|
|
unless rrgv.satisfied_by? Gem.rubygems_version then
|
2011-01-20 00:23:04 +03:00
|
|
|
raise Gem::InstallError,
|
2011-06-01 07:45:05 +04:00
|
|
|
"#{spec.name} requires RubyGems version #{rrgv}. " +
|
2011-01-20 00:23:04 +03:00
|
|
|
"Try 'gem update --system' to update RubyGems itself."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
# DOC: Missing docs or :nodoc:.
|
2011-01-20 00:23:04 +03:00
|
|
|
def ensure_dependencies_met
|
2011-06-01 07:45:05 +04:00
|
|
|
deps = spec.runtime_dependencies
|
|
|
|
deps |= spec.development_dependencies if @development
|
2011-01-20 00:23:04 +03:00
|
|
|
|
|
|
|
deps.each do |dep_gem|
|
2011-06-01 07:45:05 +04:00
|
|
|
ensure_dependency spec, dep_gem
|
2011-01-20 00:23:04 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
# DOC: Missing docs or :nodoc:.
|
2011-01-20 00:23:04 +03:00
|
|
|
def process_options
|
|
|
|
@options = {
|
|
|
|
:bin_dir => nil,
|
|
|
|
:env_shebang => false,
|
|
|
|
:force => false,
|
2012-11-29 10:52:18 +04:00
|
|
|
:only_install_dir => false
|
2011-02-01 06:11:34 +03:00
|
|
|
}.merge options
|
2011-01-20 00:23:04 +03:00
|
|
|
|
2011-02-01 06:11:34 +03:00
|
|
|
@env_shebang = options[:env_shebang]
|
|
|
|
@force = options[:force]
|
2013-09-14 12:59:02 +04:00
|
|
|
@install_dir = options[:install_dir]
|
|
|
|
@gem_home = options[:install_dir] || Gem.dir
|
2011-02-01 06:11:34 +03:00
|
|
|
@ignore_dependencies = options[:ignore_dependencies]
|
|
|
|
@format_executable = options[:format_executable]
|
|
|
|
@security_policy = options[:security_policy]
|
|
|
|
@wrappers = options[:wrappers]
|
2012-11-29 10:52:18 +04:00
|
|
|
@only_install_dir = options[:only_install_dir]
|
|
|
|
|
|
|
|
# If the user has asked for the gem to be installed in a directory that is
|
|
|
|
# the system gem directory, then use the system bin directory, else create
|
|
|
|
# (or use) a new bin dir under the gem_home.
|
|
|
|
@bin_dir = options[:bin_dir] || Gem.bindir(gem_home)
|
2011-02-01 06:11:34 +03:00
|
|
|
@development = options[:development]
|
2011-01-20 00:23:04 +03:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
@build_args = options[:build_args] || Gem::Command.build_args
|
2011-01-20 00:23:04 +03:00
|
|
|
end
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
# DOC: Missing docs or :nodoc:.
|
2011-01-20 00:23:04 +03:00
|
|
|
def check_that_user_bin_dir_is_in_path
|
2011-07-27 05:40:07 +04:00
|
|
|
user_bin_dir = @bin_dir || Gem.bindir(gem_home)
|
2013-02-20 02:51:38 +04:00
|
|
|
user_bin_dir = user_bin_dir.gsub(File::SEPARATOR, File::ALT_SEPARATOR) if
|
|
|
|
File::ALT_SEPARATOR
|
|
|
|
|
2012-09-28 12:29:21 +04:00
|
|
|
path = ENV['PATH']
|
|
|
|
if Gem.win_platform? then
|
|
|
|
path = path.downcase
|
|
|
|
user_bin_dir = user_bin_dir.downcase
|
|
|
|
end
|
|
|
|
|
|
|
|
unless path.split(File::PATH_SEPARATOR).include? user_bin_dir then
|
2011-01-20 00:23:04 +03:00
|
|
|
unless self.class.path_warning then
|
|
|
|
alert_warning "You don't have #{user_bin_dir} in your PATH,\n\t gem executables will not run."
|
|
|
|
self.class.path_warning = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
# DOC: Missing docs or :nodoc:.
|
2011-01-20 00:23:04 +03:00
|
|
|
def verify_gem_home(unpack = false)
|
2011-06-01 07:45:05 +04:00
|
|
|
FileUtils.mkdir_p gem_home
|
|
|
|
raise Gem::FilePermissionError, gem_home unless
|
|
|
|
unpack or File.writable?(gem_home)
|
2011-01-20 00:23:04 +03:00
|
|
|
end
|
|
|
|
|
2008-06-18 02:04:18 +04:00
|
|
|
##
|
2007-11-10 10:48:56 +03:00
|
|
|
# Return the text for an application file.
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def app_script_text(bin_file_name)
|
2011-06-01 07:45:05 +04:00
|
|
|
return <<-TEXT
|
2007-11-10 10:48:56 +03:00
|
|
|
#{shebang bin_file_name}
|
|
|
|
#
|
|
|
|
# This file was generated by RubyGems.
|
|
|
|
#
|
2011-06-01 07:45:05 +04:00
|
|
|
# The application '#{spec.name}' is installed as part of a gem, and
|
2007-11-10 10:48:56 +03:00
|
|
|
# this file is here to facilitate running it.
|
|
|
|
#
|
|
|
|
|
|
|
|
require 'rubygems'
|
|
|
|
|
|
|
|
version = "#{Gem::Requirement.default}"
|
|
|
|
|
2012-04-18 04:04:12 +04:00
|
|
|
if ARGV.first
|
|
|
|
str = ARGV.first
|
|
|
|
str = str.dup.force_encoding("BINARY") if str.respond_to? :force_encoding
|
|
|
|
if str =~ /\\A_(.*)_\\z/
|
|
|
|
version = $1
|
|
|
|
ARGV.shift
|
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
gem '#{spec.name}', version
|
|
|
|
load Gem.bin_path('#{spec.name}', '#{bin_file_name}', version)
|
2007-11-10 10:48:56 +03:00
|
|
|
TEXT
|
|
|
|
end
|
|
|
|
|
2008-06-18 02:04:18 +04:00
|
|
|
##
|
2013-09-14 12:59:02 +04:00
|
|
|
# return the stub script text used to launch the true Ruby script
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def windows_stub_script(bindir, bin_file_name)
|
2011-06-01 07:45:05 +04:00
|
|
|
ruby = File.basename(Gem.ruby).chomp('"')
|
|
|
|
return <<-TEXT
|
2007-11-10 10:48:56 +03:00
|
|
|
@ECHO OFF
|
|
|
|
IF NOT "%~f0" == "~f0" GOTO :WinNT
|
2011-06-01 07:45:05 +04:00
|
|
|
@"#{ruby}" "#{File.join(bindir, bin_file_name)}" %1 %2 %3 %4 %5 %6 %7 %8 %9
|
2007-11-10 10:48:56 +03:00
|
|
|
GOTO :EOF
|
|
|
|
:WinNT
|
2011-06-01 07:45:05 +04:00
|
|
|
@"#{ruby}" "%~dpn0" %*
|
2007-11-10 10:48:56 +03:00
|
|
|
TEXT
|
|
|
|
end
|
|
|
|
|
2008-06-18 02:04:18 +04:00
|
|
|
##
|
2007-11-10 10:48:56 +03:00
|
|
|
# Builds extensions. Valid types of extensions are extconf.rb files,
|
|
|
|
# configure scripts and rakefiles or mkrf_conf files.
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def build_extensions
|
2013-09-14 12:59:02 +04:00
|
|
|
builder = Gem::Ext::Builder.new spec, @build_args
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
builder.build_extensions
|
2012-11-29 10:52:18 +04:00
|
|
|
end
|
2007-11-10 10:48:56 +03:00
|
|
|
|
2012-11-29 10:52:18 +04:00
|
|
|
##
|
|
|
|
# Logs the build +output+ in +build_dir+, then raises ExtensionBuildError.
|
2013-09-14 12:59:02 +04:00
|
|
|
#
|
|
|
|
# TODO: Delete this for RubyGems 3. It remains for API compatibility
|
2012-11-29 10:52:18 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
def extension_build_error(build_dir, output, backtrace = nil) # :nodoc:
|
|
|
|
builder = Gem::Ext::Builder.new spec, @build_args
|
2013-09-13 23:58:57 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
builder.build_error build_dir, output, backtrace
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Reads the file index and extracts each file into the gem directory.
|
|
|
|
#
|
|
|
|
# Ensures that files can't be installed outside the gem directory.
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
def extract_files
|
2012-11-29 10:52:18 +04:00
|
|
|
@package.extract_files gem_dir
|
2007-12-20 11:39:12 +03:00
|
|
|
end
|
2013-07-10 03:41:44 +04:00
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
##
|
|
|
|
# Extracts only the bin/ files from the gem into the gem directory.
|
|
|
|
# This is used by default gems to allow a gem-aware stub to function
|
|
|
|
# without the full gem installed.
|
|
|
|
|
|
|
|
def extract_bin
|
|
|
|
@package.extract_files gem_dir, "bin/*"
|
|
|
|
end
|
|
|
|
|
2008-06-18 02:04:18 +04:00
|
|
|
##
|
2007-12-20 11:39:12 +03:00
|
|
|
# Prefix and suffix the program filename the same as ruby.
|
2008-06-18 02:04:18 +04:00
|
|
|
|
2007-12-20 11:39:12 +03:00
|
|
|
def formatted_program_filename(filename)
|
|
|
|
if @format_executable then
|
|
|
|
self.class.exec_format % File.basename(filename)
|
|
|
|
else
|
|
|
|
filename
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2011-06-01 07:45:05 +04:00
|
|
|
##
|
|
|
|
#
|
|
|
|
# Return the target directory where the gem is to be installed. This
|
|
|
|
# directory is not guaranteed to be populated.
|
|
|
|
#
|
|
|
|
|
|
|
|
def dir
|
|
|
|
gem_dir.to_s
|
|
|
|
end
|
2012-12-23 04:35:09 +04:00
|
|
|
|
|
|
|
##
|
|
|
|
# Performs various checks before installing the gem such as the install
|
2013-09-14 12:59:02 +04:00
|
|
|
# repository is writable and its directories exist, required Ruby and
|
2012-12-23 04:35:09 +04:00
|
|
|
# rubygems versions are met and that dependencies are installed.
|
|
|
|
#
|
|
|
|
# Version and dependency checks are skipped if this install is forced.
|
|
|
|
#
|
|
|
|
# The dependent check will be skipped this install is ignoring dependencies.
|
|
|
|
|
|
|
|
def pre_install_checks
|
|
|
|
verify_gem_home options[:unpack]
|
|
|
|
|
|
|
|
# If we're forcing the install then disable security unless the security
|
|
|
|
# policy says that we only install signed gems.
|
|
|
|
@security_policy = nil if
|
|
|
|
@force and @security_policy and not @security_policy.only_signed
|
|
|
|
|
|
|
|
ensure_loadable_spec
|
|
|
|
|
2013-09-14 12:59:02 +04:00
|
|
|
if options[:install_as_default]
|
|
|
|
Gem.ensure_default_gem_subdirectories gem_home
|
|
|
|
else
|
|
|
|
Gem.ensure_gem_subdirectories gem_home
|
|
|
|
end
|
2012-12-23 04:35:09 +04:00
|
|
|
|
|
|
|
return true if @force
|
|
|
|
|
|
|
|
ensure_required_ruby_version_met
|
|
|
|
ensure_required_rubygems_version_met
|
|
|
|
ensure_dependencies_met unless @ignore_dependencies
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Writes the file containing the arguments for building this gem's
|
|
|
|
# extensions.
|
|
|
|
|
|
|
|
def write_build_info_file
|
|
|
|
return if @build_args.empty?
|
|
|
|
|
2013-07-09 02:41:03 +04:00
|
|
|
build_info_dir = File.join gem_home, 'build_info'
|
|
|
|
|
|
|
|
FileUtils.mkdir_p build_info_dir
|
|
|
|
|
|
|
|
build_info_file = File.join build_info_dir, "#{spec.full_name}.info"
|
|
|
|
|
|
|
|
open build_info_file, 'w' do |io|
|
2012-12-23 04:35:09 +04:00
|
|
|
@build_args.each do |arg|
|
|
|
|
io.puts arg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
##
|
|
|
|
# Writes the .gem file to the cache directory
|
|
|
|
|
|
|
|
def write_cache_file
|
|
|
|
cache_file = File.join gem_home, 'cache', spec.file_name
|
|
|
|
|
|
|
|
FileUtils.cp @gem, cache_file unless File.exist? cache_file
|
|
|
|
end
|
|
|
|
|
2007-11-10 10:48:56 +03:00
|
|
|
end
|
|
|
|
|