Class: LibGems::Installer
- Inherits:
-
Object
- Object
- LibGems::Installer
- Includes:
- RequirePathsBuilder, UserInteraction
- Defined in:
- lib/libgems/installer.rb
Overview
The installer class processes RubyGem .gem files and installs the files contained in the .gem into the LibGems.path.
LibGems::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 fires 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 LibGems.pre_install and LibGems.post_install for details.
Defined Under Namespace
Classes: ExtensionBuildError
Constant Summary collapse
- ENV_PATHS =
Paths where env(1) might live. Some systems are broken and have it in /bin
%w[/usr/bin/env /bin/env]
Class Attribute Summary collapse
-
.exec_format ⇒ Object
Defaults to use Ruby’s program prefix and suffix.
-
.path_warning ⇒ Object
True if we’ve warned about PATH not including LibGems.bindir.
Instance Attribute Summary collapse
-
#bin_dir ⇒ Object
readonly
The directory a gem’s executables will be installed into.
-
#gem_home ⇒ Object
readonly
The gem repository the gem will be installed into.
-
#options ⇒ Object
The options passed when the LibGems::Installer was instantiated.
-
#spec ⇒ Object
readonly
The LibGems::Specification for the gem being installed.
Instance Method Summary collapse
-
#app_script_text(bin_file_name) ⇒ Object
Return the text for an application file.
-
#build_extensions ⇒ Object
Builds extensions.
- #check_that_user_bin_dir_is_in_path ⇒ Object
- #ensure_dependencies_met ⇒ Object
-
#ensure_dependency(spec, dependency) ⇒ Object
Ensure that the dependency is satisfied by the current installation of gem.
- #ensure_required_ruby_version_met ⇒ Object
- #ensure_required_rubygems_version_met ⇒ Object
-
#extract_files ⇒ Object
Reads the file index and extracts each file into the gem directory.
-
#formatted_program_filename(filename) ⇒ Object
Prefix and suffix the program filename the same as ruby.
- #generate_bin ⇒ Object
-
#generate_bin_script(filename, bindir) ⇒ Object
Creates the scripts to run the applications in the gem.
-
#generate_bin_symlink(filename, bindir) ⇒ Object
Creates the symlinks to run the applications in the gem.
-
#generate_windows_script(filename, bindir) ⇒ Object
Creates windows .bat files for easy running of commands.
-
#initialize(gem, options = {}) ⇒ Installer
constructor
Constructs an Installer instance that will install the gem located at
gem
. -
#install ⇒ Object
Installs the gem and returns a loaded LibGems::Specification for the installed gem.
-
#installation_satisfies_dependency?(dependency) ⇒ Boolean
True if the gems in the source_index satisfy
dependency
. - #load_gem_file ⇒ Object
- #process_options ⇒ Object
-
#shebang(bin_file_name) ⇒ Object
Generates a #! line for
bin_file_name
‘s wrapper copying arguments if necessary. -
#unpack(directory) ⇒ Object
Unpacks the gem into the given directory.
- #verify_gem_home(unpack = false) ⇒ Object
-
#windows_stub_script(bindir, bin_file_name) ⇒ Object
return the stub script text used to launch the true ruby script.
-
#write_spec ⇒ Object
Writes the .gemspec specification (in Ruby) to the gem home’s specifications directory.
Methods included from RequirePathsBuilder
#write_require_paths_file_if_needed
Methods included from UserInteraction
Methods included from DefaultUserInteraction
ui, #ui, ui=, #ui=, use_ui, #use_ui
Constructor Details
#initialize(gem, options = {}) ⇒ Installer
Constructs an Installer instance that will install the gem located at gem
. options
is a Hash with the following keys:
- :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.
- :ignore_dependencies
-
Don’t raise if a dependency is missing.
- :install_dir
-
The directory to install the gem into.
- :format_executable
-
Format the executable the same as the ruby executable. If your ruby is ruby18, foo_exec will be installed as foo_exec18.
- :security_policy
-
Use the specified security policy. See LibGems::Security
- :wrappers
-
Install wrappers if true, symlinks if false.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/libgems/installer.rb', line 98 def initialize(gem, ={}) @gem = gem @options = load_gem_file if [:user_install] and not [:unpack] then @gem_home = LibGems.user_dir check_that_user_bin_dir_is_in_path end verify_gem_home([:unpack]) @spec = @format.spec @gem_dir = File.join(@gem_home, "gems", @spec.full_name).untaint end |
Class Attribute Details
.exec_format ⇒ Object
Defaults to use Ruby’s program prefix and suffix.
77 78 79 |
# File 'lib/libgems/installer.rb', line 77 def exec_format @exec_format ||= LibGems.default_exec_format end |
.path_warning ⇒ Object
True if we’ve warned about PATH not including LibGems.bindir
72 73 74 |
# File 'lib/libgems/installer.rb', line 72 def path_warning @path_warning end |
Instance Attribute Details
#bin_dir ⇒ Object (readonly)
The directory a gem’s executables will be installed into
48 49 50 |
# File 'lib/libgems/installer.rb', line 48 def bin_dir @bin_dir end |
#gem_home ⇒ Object (readonly)
The gem repository the gem will be installed into
53 54 55 |
# File 'lib/libgems/installer.rb', line 53 def gem_home @gem_home end |
#options ⇒ Object
The options passed when the LibGems::Installer was instantiated.
63 64 65 |
# File 'lib/libgems/installer.rb', line 63 def @options end |
#spec ⇒ Object (readonly)
The LibGems::Specification for the gem being installed
58 59 60 |
# File 'lib/libgems/installer.rb', line 58 def spec @spec end |
Instance Method Details
#app_script_text(bin_file_name) ⇒ Object
Return the text for an application file.
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'lib/libgems/installer.rb', line 472 def app_script_text(bin_file_name) <<-TEXT #{shebang bin_file_name} # # This file was generated by LibGems. # # The application '#{@spec.name}' is installed as part of a gem, and # this file is here to facilitate running it. # require 'libgems' version = "#{LibGems::Requirement.default}" if ARGV.first =~ /^_(.*)_$/ and LibGems::Version.correct? $1 then version = $1 ARGV.shift end gem '#{@spec.name}', version load LibGems.bin_path('#{@spec.name}', '#{bin_file_name}', version) TEXT end |
#build_extensions ⇒ Object
Builds extensions. Valid types of extensions are extconf.rb files, configure scripts and rakefiles or mkrf_conf files.
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 |
# File 'lib/libgems/installer.rb', line 514 def build_extensions return if @spec.extensions.empty? unless @spec.name == LibGems::GEM_NAME say "Building native extensions. This could take a while..." end start_dir = Dir.pwd dest_path = File.join @gem_dir, @spec.require_paths.first ran_rake = false # only run rake once @spec.extensions.each do |extension| break if ran_rake results = [] builder = case extension when /extconf/ then LibGems::Ext::ExtConfBuilder when /configure/ then LibGems::Ext::ConfigureBuilder when /rakefile/i, /mkrf_conf/i then ran_rake = true LibGems::Ext::RakeBuilder else results = ["No builder for extension '#{extension}'"] nil end begin Dir.chdir File.join(@gem_dir, File.dirname(extension)) results = builder.build(extension, @gem_dir, dest_path, results) say results.join("\n") if LibGems.configuration.really_verbose rescue results = results.join "\n" File.open('gem_make.out', 'wb') { |f| f.puts results } = <<-EOF ERROR: Failed to build gem native extension. #{results} Gem files will remain installed in #{@gem_dir} for inspection. Results logged to #{File.join(Dir.pwd, 'gem_make.out')} EOF raise ExtensionBuildError, ensure Dir.chdir start_dir end end end |
#check_that_user_bin_dir_is_in_path ⇒ Object
453 454 455 456 457 458 459 460 461 |
# File 'lib/libgems/installer.rb', line 453 def check_that_user_bin_dir_is_in_path user_bin_dir = File.join(@gem_home, 'bin') unless ENV['PATH'].split(File::PATH_SEPARATOR).include? user_bin_dir then 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 |
#ensure_dependencies_met ⇒ Object
413 414 415 416 417 418 419 420 |
# File 'lib/libgems/installer.rb', line 413 def ensure_dependencies_met deps = @spec.runtime_dependencies deps |= @spec.development_dependencies if @development deps.each do |dep_gem| ensure_dependency @spec, dep_gem end end |
#ensure_dependency(spec, dependency) ⇒ Object
Ensure that the dependency is satisfied by the current installation of gem. If it is not an exception is raised.
- spec
-
LibGems::Specification
- dependency
-
LibGems::Dependency
203 204 205 206 207 208 |
# File 'lib/libgems/installer.rb', line 203 def ensure_dependency(spec, dependency) unless installation_satisfies_dependency? dependency then raise LibGems::InstallError, "#{spec.name} requires #{dependency}" end true end |
#ensure_required_ruby_version_met ⇒ Object
395 396 397 398 399 400 401 |
# File 'lib/libgems/installer.rb', line 395 def ensure_required_ruby_version_met if rrv = @spec.required_ruby_version then unless rrv.satisfied_by? LibGems.ruby_version then raise LibGems::InstallError, "#{@spec.name} requires Ruby version #{rrv}." end end end |
#ensure_required_rubygems_version_met ⇒ Object
403 404 405 406 407 408 409 410 411 |
# File 'lib/libgems/installer.rb', line 403 def ensure_required_rubygems_version_met if rrgv = @spec.required_rubygems_version then unless rrgv.satisfied_by? LibGems::Version.new(LibGems::VERSION) then raise LibGems::InstallError, "#{@spec.name} requires #{LibGems::NAME} version #{rrgv}. " + "Try 'gem update --system' to update #{LibGems::NAME} itself." end end end |
#extract_files ⇒ Object
Reads the file index and extracts each file into the gem directory.
Ensures that files can’t be installed outside the gem directory.
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
# File 'lib/libgems/installer.rb', line 572 def extract_files @gem_dir = File. @gem_dir raise ArgumentError, "format required to extract from" if @format.nil? dirs = [] @format.file_entries.each do |entry, file_data| path = entry['path'].untaint if path =~ /\A\// then # for extra sanity raise LibGems::InstallError, "attempt to install file into #{entry['path'].inspect}" end path = File. File.join(@gem_dir, path) if path !~ /\A#{Regexp.escape @gem_dir}/ then msg = "attempt to install file into %p under %p" % [entry['path'], @gem_dir] raise LibGems::InstallError, msg end FileUtils.rm_rf(path) if File.exists?(path) dir = File.dirname(path) if !dirs.include?(dir) dirs << dir FileUtils.mkdir_p dir end File.open(path, "wb") do |out| out.write file_data end FileUtils.chmod entry['mode'], path say path if LibGems.configuration.really_verbose end end |
#formatted_program_filename(filename) ⇒ Object
Prefix and suffix the program filename the same as ruby.
616 617 618 619 620 621 622 |
# File 'lib/libgems/installer.rb', line 616 def formatted_program_filename(filename) if @format_executable then self.class.exec_format % File.basename(filename) else filename end end |
#generate_bin ⇒ Object
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'lib/libgems/installer.rb', line 257 def generate_bin return if @spec.executables.nil? or @spec.executables.empty? # 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. bindir = @bin_dir ? @bin_dir : LibGems.bindir(@gem_home) Dir.mkdir bindir unless File.exist? bindir raise LibGems::FilePermissionError.new(bindir) unless File.writable? bindir @spec.executables.each do |filename| filename.untaint bin_path = File. "#{@spec.bindir}/#{filename}", @gem_dir mode = File.stat(bin_path).mode | 0111 File.chmod mode, bin_path if @wrappers then generate_bin_script filename, bindir else generate_bin_symlink filename, bindir end end end |
#generate_bin_script(filename, bindir) ⇒ Object
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 blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/193379
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 |
# File 'lib/libgems/installer.rb', line 289 def generate_bin_script(filename, bindir) bin_script_path = File.join bindir, formatted_program_filename(filename) File.join @gem_dir, @spec.bindir, filename # HACK some gems don't have #! in their executables, restore 2008/06 #if File.read(exec_path, 2) == '#!' then FileUtils.rm_f bin_script_path # prior install may have been --no-wrappers File.open bin_script_path, 'w', 0755 do |file| file.print app_script_text(filename) end say bin_script_path if LibGems.configuration.really_verbose generate_windows_script filename, bindir #else # FileUtils.rm_f bin_script_path # FileUtils.cp exec_path, bin_script_path, # :verbose => LibGems.configuration.really_verbose #end end |
#generate_bin_symlink(filename, bindir) ⇒ Object
Creates the symlinks to run the applications in the gem. Moves the symlink if the gem being installed has a newer version.
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/libgems/installer.rb', line 316 def generate_bin_symlink(filename, bindir) if LibGems.win_platform? then alert_warning "Unable to use symlinks on Windows, installing wrapper" generate_bin_script filename, bindir return end src = File.join @gem_dir, 'bin', filename dst = File.join bindir, formatted_program_filename(filename) if File.exist? dst then if File.symlink? dst then link = File.readlink(dst).split File::SEPARATOR cur_version = LibGems::Version.create(link[-3].sub(/^.*-/, '')) return if @spec.version < cur_version end File.unlink dst end FileUtils.symlink src, dst, :verbose => LibGems.configuration.really_verbose end |
#generate_windows_script(filename, bindir) ⇒ Object
Creates windows .bat files for easy running of commands
245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/libgems/installer.rb', line 245 def generate_windows_script(filename, bindir) if LibGems.win_platform? then script_name = 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 say script_path if LibGems.configuration.really_verbose end end |
#install ⇒ Object
Installs the gem and returns a loaded LibGems::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 LibGems::Specification
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/libgems/installer.rb', line 127 def install # If we're forcing the install then disable security unless the security # policy says that we only install singed gems. @security_policy = nil if @force and @security_policy and not @security_policy.only_signed unless @force ensure_required_ruby_version_met ensure_required_rubygems_version_met ensure_dependencies_met unless @ignore_dependencies end LibGems.pre_install_hooks.each do |hook| result = hook.call self if result == false then location = " at #{$1}" if hook.inspect =~ /@(.*:\d+)/ = "pre-install hook#{location} failed for #{spec.full_name}" raise LibGems::InstallError, end end LibGems.ensure_gem_subdirectories @gem_home FileUtils.mkdir_p @gem_dir extract_files build_extensions LibGems.post_build_hooks.each do |hook| result = hook.call self if result == false then FileUtils.rm_rf @gem_dir location = " at #{$1}" if hook.inspect =~ /@(.*:\d+)/ = "post-build hook#{location} failed for #{spec.full_name}" raise LibGems::InstallError, end end generate_bin write_spec write_require_paths_file_if_needed # HACK remove? Isn't this done in multiple places? cached_gem = File.join @gem_home, "cache", @gem.split(/\//).pop unless File.exist? cached_gem then FileUtils.cp @gem, File.join(@gem_home, "cache") end say @spec. unless @spec..nil? @spec.loaded_from = File.join(@gem_home, 'specifications', @spec.spec_name) @source_index.add_spec @spec LibGems.post_install_hooks.each do |hook| hook.call self end return @spec rescue Zlib::GzipFile::Error raise LibGems::InstallError, "gzip error installing #{@gem}" end |
#installation_satisfies_dependency?(dependency) ⇒ Boolean
True if the gems in the source_index satisfy dependency
.
213 214 215 |
# File 'lib/libgems/installer.rb', line 213 def installation_satisfies_dependency?(dependency) @source_index.find_name(dependency.name, dependency.requirement).size > 0 end |
#load_gem_file ⇒ Object
445 446 447 448 449 450 451 |
# File 'lib/libgems/installer.rb', line 445 def load_gem_file begin @format = LibGems::Format.from_file_by_path @gem, @security_policy rescue LibGems::Package::FormatError raise LibGems::InstallError, "invalid gem format for #{@gem}" end end |
#process_options ⇒ Object
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 |
# File 'lib/libgems/installer.rb', line 422 def self. = { :bin_dir => nil, :env_shebang => false, :exec_format => false, :force => false, :install_dir => LibGems.dir, :source_index => LibGems.source_index, }.merge @env_shebang = [:env_shebang] @force = [:force] gem_home = [:install_dir] @gem_home = File.(gem_home) @ignore_dependencies = [:ignore_dependencies] @format_executable = [:format_executable] @security_policy = [:security_policy] @wrappers = [:wrappers] @bin_dir = [:bin_dir] @development = [:development] @source_index = [:source_index] end |
#shebang(bin_file_name) ⇒ Object
Generates a #! line for bin_file_name
‘s wrapper copying arguments if necessary.
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
353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/libgems/installer.rb', line 353 def shebang(bin_file_name) ruby_name = LibGems::ConfigMap[:ruby_install_name] if @env_shebang path = File.join @gem_dir, @spec.bindir, bin_file_name first_line = File.open(path, "rb") {|file| file.gets} if /\A#!/ =~ first_line then # Preserve extra words on shebang line, like "-w". Thanks RPA. shebang = first_line.sub(/\A\#!.*?ruby\S*(?=(\s+\S+))/, "#!#{LibGems.ruby}") opts = $1 shebang.strip! # Avoid nasty ^M issues. end if which = LibGems.configuration[:custom_shebang] which = which.gsub(/\$(\w+)/) do case $1 when "env" @env_path ||= ENV_PATHS.find do |env_path| File.executable? env_path end when "ruby" "#{LibGems.ruby}#{opts}" when "exec" bin_file_name when "name" spec.name end end return "#!#{which}" end if not ruby_name then "#!#{LibGems.ruby}#{opts}" elsif opts then "#!/bin/sh\n'exec' #{ruby_name.dump} '-x' \"$0\" \"$@\"\n#{shebang}" else # Create a plain shebang line. @env_path ||= ENV_PATHS.find {|env_path| File.executable? env_path } "#!#{@env_path} #{ruby_name}" end end |
#unpack(directory) ⇒ Object
Unpacks the gem into the given directory.
220 221 222 223 224 |
# File 'lib/libgems/installer.rb', line 220 def unpack(directory) @gem_dir = directory @format = LibGems::Format.from_file_by_path @gem, @security_policy extract_files end |
#verify_gem_home(unpack = false) ⇒ Object
463 464 465 466 467 |
# File 'lib/libgems/installer.rb', line 463 def verify_gem_home(unpack = false) FileUtils.mkdir_p @gem_home raise LibGems::FilePermissionError, @gem_home unless unpack or File.writable? @gem_home end |
#windows_stub_script(bindir, bin_file_name) ⇒ Object
return the stub script text used to launch the true ruby script
499 500 501 502 503 504 505 506 507 508 |
# File 'lib/libgems/installer.rb', line 499 def windows_stub_script(bindir, bin_file_name) <<-TEXT @ECHO OFF IF NOT "%~f0" == "~f0" GOTO :WinNT @"#{File.basename(LibGems.ruby).chomp('"')}" "#{File.join(bindir, bin_file_name)}" %1 %2 %3 %4 %5 %6 %7 %8 %9 GOTO :EOF :WinNT @"#{File.basename(LibGems.ruby).chomp('"')}" "%~dpn0" %* TEXT end |
#write_spec ⇒ Object
Writes the .gemspec specification (in Ruby) to the gem home’s specifications directory.
230 231 232 233 234 235 236 237 238 239 240 |
# File 'lib/libgems/installer.rb', line 230 def write_spec rubycode = @spec.to_ruby file_name = File.join @gem_home, 'specifications', @spec.spec_name file_name.untaint File.open(file_name, "w") do |file| file.puts rubycode end end |