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.
# File lib/rubygems/installer.rb, line 197
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(%{
^\s*(
gem \s |
load \s Gem\.bin_path\( |
load \s Gem\.activate_bin_path\(
)
(['"])(.*?)(\22)),
}, 3)
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 ".dup
if ruby_executable then
question << (existing || 'an unknown executable')
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