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:
$envthepathtotheunixenvutility$rubythepathtothecurrentlyrunningrubyinterpreter$execthepathtothegem's executable
$name the name of the gem the executable is for
# File lib/rubygems/installer.rb, line 558
def shebang(bin_file_name)
ruby_name = RbConfig::CONFIG['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+)+)/, "#!#{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}$/," #{RbConfig::CONFIG['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
"#!#{Gem.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