Skip to content

Commit

Permalink
Add custom_shebang gemrc option for shebang alteration
Browse files Browse the repository at this point in the history
  • Loading branch information
evanphx committed Jun 7, 2011
1 parent 31a5963 commit 3e44b69
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
30 changes: 30 additions & 0 deletions lib/rubygems/installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,17 @@ def generate_bin_symlink(filename, bindir)
##
# 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
#

def shebang(bin_file_name)
ruby_name = Gem::ConfigMap[:ruby_install_name] if @env_shebang
Expand All @@ -371,6 +382,25 @@ def shebang(bin_file_name)
shebang.strip! # Avoid nasty ^M issues.
end

if which = Gem.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"
"#{Gem.ruby}#{opts}"
when "exec"
bin_file_name
when "name"
spec.name
end
end

return "#!#{which}"
end

if not ruby_name then
"#!#{Gem.ruby}#{opts}"
elsif opts then
Expand Down
45 changes: 45 additions & 0 deletions test/rubygems/test_gem_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,14 @@ def setup
@user_installer.spec = @user_spec
@user_installer.gem_home = @installer_tmp
end

@config = Gem.configuration
end

def teardown
super
Gem.configuration = @config
end

def test_app_script_text
@spec.version = 2
Expand Down Expand Up @@ -994,6 +1000,45 @@ def test_shebang_version_env_arguments
assert_equal "#!#{Gem.ruby} -ws", shebang
end

def test_shebang_custom
conf = Gem::ConfigFile.new []
conf[:custom_shebang] = 'test'

Gem.configuration = conf

util_make_exec @spec, "#!/usr/bin/ruby"

shebang = @installer.shebang 'executable'

assert_equal "#!test", shebang
end

def test_shebang_custom_with_expands
conf = Gem::ConfigFile.new []
conf[:custom_shebang] = '1 $env 2 $ruby 3 $exec 4 $name'

Gem.configuration = conf

util_make_exec @spec, "#!/usr/bin/ruby"

shebang = @installer.shebang 'executable'

assert_equal "#!1 /usr/bin/env 2 #{Gem.ruby} 3 executable 4 a", shebang
end

def test_shebang_custom_with_expands_and_arguments
conf = Gem::ConfigFile.new []
conf[:custom_shebang] = '1 $env 2 $ruby 3 $exec'

Gem.configuration = conf

util_make_exec @spec, "#!/usr/bin/ruby -ws"

shebang = @installer.shebang 'executable'

assert_equal "#!1 /usr/bin/env 2 #{Gem.ruby} -ws 3 executable", shebang
end

def test_unpack
util_setup_gem

Expand Down

0 comments on commit 3e44b69

Please sign in to comment.