Skip to content

Commit

Permalink
Support binstubs with --enable-load-relative prolog
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez committed Jan 7, 2022
1 parent 9a1b891 commit 32a5e90
Show file tree
Hide file tree
Showing 2 changed files with 208 additions and 66 deletions.
48 changes: 40 additions & 8 deletions lib/rubygems/installer.rb
Expand Up @@ -220,7 +220,17 @@ def check_executable_overwrite(filename) # :nodoc:
existing = nil

File.open generated_bin, 'rb' do |io|
next unless io.gets =~ /^#!/ # shebang
line = io.gets
shebang = /^#!.*ruby/

if load_relative_enabled?
until line.nil? || line =~ shebang do
line = io.gets
end
end

next unless line =~ shebang

io.gets # blankline

# TODO detect a specially formatted comment instead of trying
Expand Down Expand Up @@ -585,7 +595,6 @@ def generate_bin_symlink(filename, bindir)
#

def shebang(bin_file_name)
ruby_name = 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 } || ""

Expand Down Expand Up @@ -614,14 +623,12 @@ def shebang(bin_file_name)
end

"#!#{which}"
elsif not ruby_name
"#!#{Gem.ruby}#{opts}"
elsif opts
"#!/bin/sh\n'exec' #{ruby_name.dump} '-x' \"$0\" \"$@\"\n#{shebang}"
else
elsif @env_shebang
# Create a plain shebang line.
@env_path ||= ENV_PATHS.find {|env_path| File.executable? env_path }
"#!#{@env_path} #{ruby_name}"
"#!#{@env_path} #{ruby_install_name}"
else
"#{bash_prolog_script}#!#{Gem.ruby}#{opts}"
end
end

Expand Down Expand Up @@ -980,4 +987,29 @@ def rb_config
def ruby_install_name
rb_config["ruby_install_name"]
end

def load_relative_enabled?
rb_config["LIBRUBY_RELATIVE"] == 'yes'
end

def bash_prolog_script
if load_relative_enabled?
script = +<<~EOS
bindir="${0%/*}"
EOS

script << %Q(exec "$bindir/#{ruby_install_name}" "-x" "$0" "$@"\n)

<<~EOS
#!/bin/sh
# -*- ruby -*-
_=_\\
=begin
#{script.chomp}
=end
EOS
else
""
end
end
end

0 comments on commit 32a5e90

Please sign in to comment.