class Gem::Installer
安装程序将 gem 中包含的文件安装到 Gem.home 中。
Gem::Installer 负责将文件放置到文件系统的所有正确位置,包括将 gem 解压到其 gem 目录、在规格目录中安装 gemspec、在缓存目录中存储缓存的 gem,以及为可执行文件安装包装器或符号链接。
安装程序会调用安装前和安装后钩子。钩子可以通过已安装 gem 中的 rubygems_plugin.rb 文件或 rubygems/defaults/#{RUBY_ENGINE}.rb 或 rubygems/defaults/operating_system.rb 文件添加。有关详细信息,请参阅 Gem.pre_install 和 Gem.post_install。
Constants
- ENV_PATHS
-
env(1) 可能存在的路径。有些系统配置不当,将其放在 /bin 中
属性
覆盖可执行文件的格式。
这是一个 sprintf 格式,其中包含一个“%s”,将被可执行文件名替换。它基于 ruby 可执行文件名与“ruby”的差异。
gem 的可执行文件将被安装到的目录
gem 将被安装到的 gem 存储库
实例化 Gem::Installer 时传递的选项。
gem 包实例。
Public Class Methods
Source
# File lib/rubygems/installer.rb, line 85 def self.at(path, options = {}) security_policy = options[:security_policy] package = Gem::Package.new path, security_policy new package, options end
为位于 path 的 gem 文件构建安装程序对象
Source
# File lib/rubygems/installer.rb, line 77 def exec_format @exec_format ||= Gem.default_exec_format end
默认为使用 Ruby 的程序前缀和后缀。
Source
# File lib/rubygems/installer.rb, line 123 def self.for_spec(spec, options = {}) # FIXME: we should have a real Package class for this new FakePackage.new(spec), options end
为临时 gem(即我们实际上没有 .gem 文件,只有 spec)构建安装程序对象
Source
# File lib/rubygems/installer.rb, line 153 def initialize(package, options = {}) require "fileutils" @options = options @package = package process_options @package.dir_mode = options[:dir_mode] @package.prog_mode = options[:prog_mode] @package.data_mode = options[:data_mode] end
构造一个 Installer 实例,该实例将安装 package 处的 gem,package 可以是路径或 Gem::Package 的实例。options 是一个 Hash,包含以下键
- :bin_dir
-
如果需要,将 bin 包装器放置在此处。
- :development
-
是否安装开发依赖项。
- :env_shebang
-
在 bin 包装器中使用 /usr/bin/env。
- :force
-
覆盖所有版本检查和安全策略检查,除了签名 gem 专用策略。
- :format_executable
-
以与 Ruby 可执行文件相同的格式化可执行文件。如果你的 Ruby 是 ruby18,foo_exec 将安装为 foo_exec18。
- :ignore_dependencies
-
如果缺少依赖项,则不引发异常。
- :install_dir
-
安装 gem 的目录。
- :security_policy
-
使用指定的安全策略。请参阅
Gem::Security - :user_install
-
指示 gem 应解压到用户的个人 gem 目录。
- :only_install_dir
-
仅针对 install_dir 中的内容验证依赖项
- :wrappers
-
如果为 true,则安装包装器;如果为 false,则安装符号链接。
- :build_args
-
传递给扩展构建器进程的参数
Array。如果未设置,则使用Gem::Command.build_args - :post_install_message
-
如果为 true,则打印 gem 安装后消息
Public Instance Methods
Source
# File lib/rubygems/installer.rb, line 715 def app_script_text(bin_file_name) # NOTE: that the `load` lines cannot be indented, as old RG versions match # against the beginning of the line <<~TEXT #{shebang bin_file_name} # # This file was generated by RubyGems. # # The application '#{spec.name}' is installed as part of a gem, and # this file is here to facilitate running it. # require 'rubygems' #{gemdeps_load(spec.name)} version = "#{Gem::Requirement.default_prerelease}" str = ARGV.first if str str = str.b[/\\A_(.*)_\\z/, 1] if str and Gem::Version.correct?(str) #{explicit_version_requirement(spec.name)} ARGV.shift end end if Gem.respond_to?(:activate_and_load_bin_path) Gem.activate_and_load_bin_path('#{spec.name}', '#{bin_file_name}', version) else load Gem.activate_bin_path('#{spec.name}', '#{bin_file_name}', version) end TEXT end
返回应用程序文件的文本。
Source
# File lib/rubygems/installer.rb, line 806 def build_extensions builder = Gem::Ext::Builder.new spec, build_args, Gem.target_rbconfig, build_jobs builder.build_extensions end
构建扩展。扩展的有效类型包括 extconf.rb 文件、configure 脚本和 rakefiles 或 mkrf_conf 文件。
Source
# File lib/rubygems/installer.rb, line 389 def default_spec_dir dir = File.join(gem_home, "specifications", "default") FileUtils.mkdir_p dir dir end
Source
# File lib/rubygems/installer.rb, line 399 def default_spec_file File.join default_spec_dir, "#{spec.full_name}.gemspec" end
默认 gem 的默认 spec 文件的位置。
Source
# File lib/rubygems/installer.rb, line 847 def dir gem_dir.to_s end
返回 gem 要安装的目标目录。不保证该目录将被填充。
Source
# File lib/rubygems/installer.rb, line 364 def ensure_dependency(spec, dependency) unless installation_satisfies_dependency? dependency raise Gem::InstallError, "#{spec.name} requires #{dependency}" end true end
确保依赖项由 gem 的当前安装满足。如果不满足,则会引发异常。
- spec
- dependency
Source
# File lib/rubygems/installer.rb, line 595 def ensure_loadable_spec ruby = spec.to_ruby_for_cache begin eval ruby rescue StandardError, SyntaxError => e raise Gem::InstallError, "The specification for #{spec.full_name} is corrupt (#{e.class})" end end
确保为该 gem 写入的 Gem::Specification 在安装时是可加载的。
Source
# File lib/rubygems/installer.rb, line 757 def explicit_version_requirement(name) code = "version = str" return code unless name == "bundler" code += <<~TEXT ENV['BUNDLER_VERSION'] = str TEXT end
Source
# File lib/rubygems/installer.rb, line 826 def extract_bin @package.extract_files gem_dir, "#{spec.bindir}/*" end
仅将 gem 中的 bin/ 文件提取到 gem 目录。这由默认 gem 使用,以便 gem 感知的存根在没有完全安装 gem 的情况下也能正常工作。
Source
# File lib/rubygems/installer.rb, line 817 def extract_files @package.extract_files gem_dir end
读取文件索引并将每个文件提取到 gem 目录。
确保文件无法安装到 gem 目录之外。
Source
# File lib/rubygems/installer.rb, line 833 def formatted_program_filename(filename) if @format_executable self.class.exec_format % File.basename(filename) else filename end end
以与 ruby 相同的方式为程序文件名添加前缀和后缀。
Source
# File lib/rubygems/installer.rb, line 854 def gem @package.gem.path end
正在安装的 gem 的文件名。
Source
# File lib/rubygems/installer.rb, line 246 def gem_dir @gem_dir ||= File.join(gem_home, "gems", spec.full_name) end
spec 的 gem 目录的惰性访问器。
Source
# File lib/rubygems/installer.rb, line 748 def gemdeps_load(name) return "" if name == "bundler" <<~TEXT Gem.use_gemdeps TEXT end
Source
# File lib/rubygems/installer.rb, line 497 def generate_bin_script(filename, bindir) bin_script_path = File.join bindir, formatted_program_filename(filename) Gem.open_file_with_lock(bin_script_path) do require "fileutils" FileUtils.rm_f bin_script_path # prior install may have been --no-wrappers File.open(bin_script_path, "wb", 0o755) do |file| file.write app_script_text(filename) file.chmod(options[:prog_mode] || 0o755) end end verbose bin_script_path generate_windows_script filename, bindir end
创建运行 gem 中应用程序的脚本。
Source
# File lib/rubygems/installer.rb, line 519 def generate_bin_symlink(filename, bindir) src = File.join gem_dir, spec.bindir, filename dst = File.join bindir, formatted_program_filename(filename) if File.exist? dst if File.symlink? dst link = File.readlink(dst).split File::SEPARATOR cur_version = Gem::Version.create(link[-3].sub(/^.*-/, "")) return if spec.version < cur_version end File.unlink dst end FileUtils.symlink src, dst, verbose: Gem.configuration.really_verbose rescue NotImplementedError, SystemCallError alert_warning "Unable to use symlinks, installing wrapper" generate_bin_script filename, bindir end
创建运行 gem 中应用程序的符号链接。如果正在安装的 gem 版本较新,则会移动符号链接。
Source
# File lib/rubygems/installer.rb, line 427 def generate_windows_script(filename, bindir) if Gem.win_platform? script_name = formatted_program_filename(filename) + ".bat" script_path = File.join bindir, File.basename(script_name) File.open script_path, "w" do |file| file.puts windows_stub_script(bindir, filename) end verbose script_path end end
创建 Windows .bat 文件以便于运行命令
Source
# File lib/rubygems/installer.rb, line 268 def install pre_install_checks run_pre_install_hooks # Set loaded_from to ensure extension_dir is correct spec.loaded_from = spec_file # Completely remove any previous gem files FileUtils.rm_rf gem_dir FileUtils.rm_rf spec.extension_dir dir_mode = options[:dir_mode] FileUtils.mkdir_p gem_dir, mode: dir_mode && 0o755 extract_files build_extensions write_build_info_file run_post_build_hooks generate_bin generate_plugins write_spec write_cache_file File.chmod(dir_mode, gem_dir) if dir_mode say spec.post_install_message if options[:post_install_message] && !spec.post_install_message.nil? Gem::Specification.add_spec(spec) unless @install_dir load_plugin run_post_install_hooks spec rescue Errno::EACCES => e # Permission denied - /path/to/foo raise Gem::FilePermissionError, e.message.split(" - ").last end
安装 gem 并返回已安装 gem 的已加载 Gem::Specification。
gem 将以以下结构安装
@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
Source
# File lib/rubygems/installer.rb, line 374 def installation_satisfies_dependency?(dependency) return true if @options[:development] && dependency.type == :development return true if installed_specs.detect {|s| dependency.matches_spec? s } return false if @only_install_dir !dependency.matching_specs.empty? end
如果系统中的 gem 满足 dependency,则返回 true。
Source
# File lib/rubygems/installer.rb, line 344 def installed_specs @installed_specs ||= begin specs = [] Gem::Util.glob_files_in_dir("*.gemspec", File.join(gem_home, "specifications")).each do |path| spec = Gem::Specification.load path specs << spec if spec end specs end end
Source
# File lib/rubygems/installer.rb, line 867 def pre_install_checks verify_gem_home # The name and require_paths must be verified first, since it could contain # ruby code that would be eval'ed in #ensure_loadable_spec verify_spec ensure_loadable_spec Gem.ensure_gem_subdirectories gem_home return true if @force ensure_dependencies_met unless @ignore_dependencies true end
在安装 gem 之前执行各种检查,例如安装存储库是否可写且目录是否存在,是否满足所需的 Ruby 和 rubygems 版本,以及依赖项是否已安装。
如果强制安装,则跳过版本和依赖项检查。
如果安装正在忽略依赖项,则跳过依赖项检查。
Source
# File lib/rubygems/installer.rb, line 553 def shebang(bin_file_name) path = File.join gem_dir, spec.bindir, bin_file_name first_line = File.open(path, "rb", &:gets) || "" if first_line.start_with?("#!") # Preserve extra words on shebang line, like "-w". Thanks RPA. shebang = first_line.sub(/\A\#!.*?ruby\S*((\s+\S+)+)/, "#!#{Gem.ruby}") opts = $1 shebang.strip! # Avoid nasty ^M issues. end if which = Gem.configuration[:custom_shebang] # replace bin_file_name with "ruby" to avoid endless loops which = which.gsub(/ #{bin_file_name}$/," #{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 @env_shebang # Create a plain shebang line. @env_path ||= ENV_PATHS.find {|env_path| File.executable? env_path } "#!#{@env_path} #{ruby_install_name}" else "#{bash_prolog_script}#!#{Gem.ruby}#{opts}" end end
为 bin_file_name 的包装器生成一个 ! 行,必要时复制参数。
如果设置了 :custom_shebang 配置,则它将用作创建用于运行 gem 可执行文件的 shebang 的模板。
模板支持 4 种扩展
$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
Source
# File lib/rubygems/installer.rb, line 253 def spec @package.spec end
安装程序 spec 的惰性访问器。
Source
# File lib/rubygems/installer.rb, line 385 def spec_file File.join gem_home, "specifications", "#{spec.full_name}.gemspec" end
安装的 spec 文件的位置。
Source
# File lib/rubygems/installer.rb, line 682 def verify_spec unless Gem::Specification::VALID_NAME_PATTERN.match?(spec.name) raise Gem::InstallError, "#{spec} has an invalid name" end if spec.raw_require_paths.any? {|path| path =~ /\R/ } raise Gem::InstallError, "#{spec} has an invalid require_paths" end if spec.extensions.any? {|ext| ext =~ /\R/ } raise Gem::InstallError, "#{spec} has an invalid extensions" end if /\R/.match?(spec.platform.to_s) raise Gem::InstallError, "#{spec.platform} is an invalid platform" end unless /\A\d+\z/.match?(spec.specification_version.to_s) raise Gem::InstallError, "#{spec} has an invalid specification_version" end if spec.dependencies.any? {|dep| dep.type != :runtime && dep.type != :development } raise Gem::InstallError, "#{spec} has an invalid dependencies" end if spec.dependencies.any? {|dep| dep.name =~ /(?:\R|[<>])/ } raise Gem::InstallError, "#{spec} has an invalid dependencies" end end
Source
# File lib/rubygems/installer.rb, line 770 def windows_stub_script(bindir, bin_file_name) rb_topdir = RbConfig::TOPDIR || File.dirname(rb_config["bindir"]) # get ruby executable file name from RbConfig ruby_exe = "#{rb_config["RUBY_INSTALL_NAME"]}#{rb_config["EXEEXT"]}" ruby_exe = "ruby.exe" if ruby_exe.empty? if File.exist?(File.join(bindir, ruby_exe)) # stub & ruby.exe within same folder. Portable <<~TEXT @ECHO OFF @"%~dp0#{ruby_exe}" "%~dpn0" %* TEXT elsif bindir.downcase.start_with? rb_topdir.downcase # stub within ruby folder, but not standard bin. Portable require "pathname" from = Pathname.new bindir to = Pathname.new "#{rb_topdir}/bin" rel = to.relative_path_from from <<~TEXT @ECHO OFF @"%~dp0#{rel}/#{ruby_exe}" "%~dpn0" %* TEXT else # outside ruby folder, maybe -user-install or bundler. Portable, but ruby # is dependent on PATH <<~TEXT @ECHO OFF @#{ruby_exe} "%~dpn0" %* TEXT end end
返回用于启动真实 Ruby 脚本的存根脚本文本
Source
# File lib/rubygems/installer.rb, line 889 def write_build_info_file return if build_args.empty? build_info_dir = File.join gem_home, "build_info" dir_mode = options[:dir_mode] FileUtils.mkdir_p build_info_dir, mode: dir_mode && 0o755 build_info_file = File.join build_info_dir, "#{spec.full_name}.info" File.open build_info_file, "w" do |io| build_args.each do |arg| io.puts arg end end File.chmod(dir_mode, build_info_dir) if dir_mode end
写入包含此 gem 扩展构建参数的文件的内容。
Source
# File lib/rubygems/installer.rb, line 911 def write_cache_file cache_file = File.join gem_home, "cache", spec.file_name @package.copy_to cache_file end
将 .gem 文件写入缓存目录
Source
# File lib/rubygems/installer.rb, line 420 def write_default_spec Gem.write_binary(default_spec_file, spec.to_ruby) end
将完整的 .gemspec 规范(以 Ruby 编写)写入 gem home 的 specifications/default 目录。
与 write_spec 相比,此方法保留了文件列表,因此 'gem contents` 命令可以正常工作。
Source
# File lib/rubygems/installer.rb, line 407 def write_spec spec.installed_by_version = Gem.rubygems_version Gem.write_binary(spec_file, spec.to_ruby_for_cache) end
将 .gemspec 规范(以 Ruby 编写)写入 gem home 的 specifications 目录。
私有实例方法
Source
# File lib/rubygems/installer.rb, line 961 def bash_prolog_script if load_relative_enabled? <<~EOS #!/bin/sh # -*- ruby -*- _=_\\ =begin bindir="${0%/*}" ruby="$bindir/#{ruby_install_name}" if [ ! -f "$ruby" ]; then ruby="#{ruby_install_name}" fi exec "$ruby" "-x" "$0" "$@" =end EOS else "" end end
Source
# File lib/rubygems/installer.rb, line 938 def build_args @build_args ||= begin require_relative "command" Gem::Command.build_args end end
Source
# File lib/rubygems/installer.rb, line 945 def build_jobs @build_jobs ||= Etc.nprocessors + 1 end
Source
# File lib/rubygems/installer.rb, line 981 def load_plugin specs = Gem::Specification.find_all_by_name(spec.name) # If old version already exists, this plugin isn't loaded # immediately. It's for avoiding a case that multiple versions # are loaded at the same time. return unless specs.size == 1 plugin_files = spec.plugins.map do |plugin| File.join(@plugins_dir, "#{spec.name}_plugin#{File.extname(plugin)}") end Gem.load_plugin_files(plugin_files) end
Source
# File lib/rubygems/installer.rb, line 957 def load_relative_enabled? rb_config["LIBRUBY_RELATIVE"] == "yes" end
Source
# File lib/rubygems/installer.rb, line 949 def rb_config Gem.target_rbconfig end
Source
# File lib/rubygems/installer.rb, line 953 def ruby_install_name rb_config["ruby_install_name"] end
Source
# File lib/rubygems/installer.rb, line 925 def user_install_dir # never install to user home in --build-root mode return unless @build_root.nil? # Please note that @user_install might have three states: # * `true`: `--user-install` # * `false`: `--no-user-install` and # * `nil`: option was not specified if @user_install || (@user_install.nil? && Gem.default_user_install) Gem.user_dir end end