Method: LibGems::Installer#shebang

Defined in:
lib/libgems/installer.rb

#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